Skip to content

Commit

Permalink
Issue #1712: improve movable obstacle behaviour, regenerate victims …
Browse files Browse the repository at this point in the history
…when scoring run starts
  • Loading branch information
bjost2s authored and NiHoffmann committed Dec 18, 2024
1 parent 8c61090 commit 9f2963d
Show file tree
Hide file tree
Showing 156 changed files with 777 additions and 1,034 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.json.JSONObject;

import de.fhg.iais.roberta.mode.general.ListElementOperations;
import de.fhg.iais.roberta.util.dbc.Assert;
import de.fhg.iais.roberta.util.dbc.DbcException;
import de.fhg.iais.roberta.util.syntax.FunctionNames;

Expand Down Expand Up @@ -137,6 +138,17 @@ public String getHelperMethodDeclarations(Set<? extends Enum<?>> usedMethods) {
* @return the helper method definitions
*/
public String getHelperMethodDefinitions(Set<? extends Enum<?>> usedMethods) {
return getHelperMethodDefinitions(usedMethods, 1);
}

/**
* Same as getHelperMethodDefinitions(Set<? extends Enum<?>> usedMethods)
* but allows specifiying number of newLines
*
*/
public String getHelperMethodDefinitions(Set<? extends Enum<?>> usedMethods, int numOfNewLines) {
Assert.isTrue((numOfNewLines > 0));

StringBuilder sb = new StringBuilder();

// guarantee order of methods for tests & consistency
Expand All @@ -149,7 +161,9 @@ public String getHelperMethodDefinitions(Set<? extends Enum<?>> usedMethods) {
for ( int sortedIndex : sortedIndices ) {
String implementation = this.helperMethods.get(usedMethodsList.get(sortedIndex));
if ( implementation != null ) { // no implementation necessary for this method
sb.append('\n');
for(int i =0; i<numOfNewLines;i++){
sb.append('\n');
}
sb.append(implementation);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.Collectors;

import de.fhg.iais.roberta.syntax.Phrase;
import de.fhg.iais.roberta.util.dbc.Assert;
import de.fhg.iais.roberta.visitor.IVisitor;

public class SourceBuilder {
Expand Down Expand Up @@ -60,6 +61,61 @@ public SourceBuilder nlI() {
return this;
}

/**
* Append N new lines if not already present and Vals to SourceBuilder
*
* @param numOfLines make sure to have atleast this many new lines before adding Vals, must >= 0
* @param vals Vals to be appended
* @return SourceBuilder
*/
public SourceBuilder addNLine(int numOfLines, Object... vals) {
Assert.isTrue(numOfLines >= 0);

//if we want 2 blank lines we need 3 new lines
numOfLines += 1;

final String sourceStringTrimmed = this.sb.toString().replace(" ", "");
int newLineCounter = 0;

//an Empty SourceBuilder does not have to go to the next line to ensure given number of new lines
if(sourceStringTrimmed.isEmpty())
newLineCounter++;

for(int i=0; (i < numOfLines) && (sourceStringTrimmed.length() - 1 - i >= 0); i++){
if(sourceStringTrimmed.charAt(sourceStringTrimmed.length() - 1 - i) == '\n') {
newLineCounter++;
}else {
break;
}
}

for(int i=0; (i < (numOfLines - newLineCounter)); i++){
nlI();
}

add(vals);
return this;
}

public SourceBuilder addLine(Object... vals) {
return addNLine(0, vals);
}

/**
* Makes sure there are a given amount of new Lines, 0 will make sure we are in the next
* Line 1 will make sure we have one Line white space, etc.,
*
* @param numOfLines to ensure are there, will add additional new Lines if needed
* @return SourceBuilder
*/
public SourceBuilder ensureBlankLines(int numOfLines) {
return addNLine(numOfLines, "");
}

public SourceBuilder ensureNextLine() {
return ensureBlankLines(0);
}

public SourceBuilder add(Object... vals) {
for ( Object val : vals ) {
this.sb.append(val);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void generateCode(boolean withWrapping) {
generateProgramSuffix(withWrapping);
}

private void generateProgramMainBody() {
protected void generateProgramMainBody() {
this.programPhrases
.stream()
.filter(phrase -> phrase.getKind().getCategory() != Category.METHOD || phrase.getKind().hasName("METHOD_CALL"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,6 @@ public Void visitIsListEmptyFunct(IsListEmptyFunct isListEmptyFunct) {

@Override
public Void visitMethodVoid(MethodVoid methodVoid) {
nlIndent();
this.src.add("def ", methodVoid.getCodeSafeMethodName(), '(');
List<String> paramList = new ArrayList<>();
for ( Expr l : methodVoid.getParameters().get() ) {
Expand All @@ -731,7 +730,6 @@ public Void visitMethodVoid(MethodVoid methodVoid) {

@Override
public Void visitMethodReturn(MethodReturn methodReturn) {
nlIndent();
this.src.add("def ", methodReturn.getCodeSafeMethodName(), '(');
List<String> paramList = new ArrayList<>();
for ( Expr l : methodReturn.getParameters().get() ) {
Expand Down Expand Up @@ -859,8 +857,7 @@ protected String getLanguageVarTypeFromBlocklyType(BlocklyType type) {

protected void addPassIfProgramIsEmpty() {
if ( this.getBean(UsedHardwareBean.class).isProgramEmpty() ) {
nlIndent();
this.src.add("pass");
this.src.addLine("pass");
}
}

Expand Down Expand Up @@ -1024,11 +1021,10 @@ public Void visitDebugAction(DebugAction debugAction) {

@Override
protected void generateProgramSuffix(boolean withWrapping) {
nlIndent();
this.src.add("if __name__ == \"__main__\":");
this.src.ensureBlankLines(1);
this.src.addLine("if __name__ == \"__main__\":");
incrIndentation();
nlIndent();
this.src.add("main()");
this.src.addLine("main()");
decrIndentation();
}

Expand All @@ -1043,15 +1039,6 @@ protected void generateProgramPrefix(boolean withWrapping) {
visitorGenerateGlobalVariables();
}

@Override
protected void generateUserDefinedMethods() {
super.generateUserDefinedMethods();
if ( this.programPhrases.stream().filter(phrase -> phrase.getKind().getCategory() == Category.METHOD && !phrase.getKind().hasName("METHOD_CALL")).count() > 0 ) {
nlIndent();
}
}


protected void collectVariablesForFunctionGlobals() {
//since we are not in a scope yet these will be out global variables
this.getBean(UsedHardwareBean.class).getInScopeVariables().forEach(s -> {
Expand All @@ -1061,7 +1048,80 @@ protected void collectVariablesForFunctionGlobals() {

@Override
protected void visitorGenerateNN() {
this.src.ensureBlankLines(1);
generateNNStuff("python");
}

//TODO Python-Code generation now always uses preceeding new line
//move these functions to abstarct language visitor once all other visiros are also changed
@Override
protected void generateProgramMainBody() {
//this is fine since MainTask[] will add a new line(this is a empty line when we add it to our code)
//we dont need an additional nlIndent() at the beginning of this
this.programPhrases
.stream()
.filter(phrase -> phrase.getKind().getCategory() != Category.METHOD || phrase.getKind().hasName("METHOD_CALL"))
.forEach(p -> {
src.ensureNextLine();
p.accept(this);
});
}

@Override
protected void generateUserDefinedMethods() {
boolean isEmpty = this.programPhrases.stream().filter(phrase -> phrase.getKind().getCategory() == Category.METHOD && !phrase.getKind().hasName("METHOD_CALL")).count() == 0;
if ( !isEmpty ) {
this.programPhrases
.stream()
.filter(phrase -> phrase.getKind().getCategory() == Category.METHOD && !phrase.getKind().hasName("METHOD_CALL"))
.forEach(e -> {
src.ensureBlankLines(1);
e.accept(this);
});
//we dont want to add training new lines anymore, but this is the easiest way to do it - python method style guide 2 new lines before and after method declaration
src.ensureBlankLines(1);
}
}

/**
* this function should not be changed for a consitant behavour, this may lead to minor changes in visitors
* right now most visitors to this in the suffix, but some in the prefix -- make sure to remove helper function logic from visitor or methods will be
* generated more than once
*
* @return returns true if helper-methods were added
*/
@Override
protected void visitorGenerateHelperMethods(){
String helperMethodImpls =
this
.getBean(CodeGeneratorSetupBean.class)
.getHelperMethodGenerator()
.getHelperMethodDefinitions(this.getBean(CodeGeneratorSetupBean.class).getUsedMethods(), 1)
.trim();

boolean hasMethods = helperMethodImpls != null && !helperMethodImpls.isEmpty();

if ( hasMethods ) {
this.src.ensureBlankLines(1);
this.src.addLine(helperMethodImpls);
this.src.ensureBlankLines(1);
}
}

/**
* this will be called from inside MaiNTask since we need the variable list, right now a lot of visitors to bascially this with some varaition
* please unify this to one function
* @return was a user-method or variable added to the code
*/
@Override
protected void visitorGenerateUserVariablesAndMethods(MainTask mainTask) {
StmtList variables = mainTask.variables;
boolean hasVariables = !(variables.get().isEmpty());
if ( hasVariables ) {
this.src.ensureNextLine();
variables.accept(this);
}
generateUserDefinedMethods();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def ____retNumber2(___x):
___x = ___x / float(2)
return ___x


def run():
global ___n1, ___b, ___n2, ___n3
# Basic Functions START
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


def _median(l):
l = sorted(l)
l_len = len(l)
Expand All @@ -24,6 +23,7 @@ def _standard_deviation(l):
for i in l:
sd += (i - mean)*(i - mean)
return math.sqrt(sd / len(l))

class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


def _median(l):
l = sorted(l)
l_len = len(l)
Expand All @@ -24,6 +23,7 @@ def _standard_deviation(l):
for i in l:
sd += (i - mean)*(i - mean)
return math.sqrt(sd / len(l))

class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


def _isPrime(number):
if(number == 0 or number == 1):
return False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def ____nnStep():
____h1n2 = ____b_h1n2 + ____n1 * ____w_n1_h1n2 + ____n3 * ____w_n3_h1n2
____n2 = ____b_n2 + ____h1n1 * ____w_h1n1_n2 + ____h1n2 * ____w_h1n2_n2
____n4 = ____b_n4 + ____h1n1 * ____w_h1n1_n4 + ____h1n2 * ____w_h1n2_n4

class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand All @@ -60,7 +61,6 @@ def ____runNN():
____nnStep()
___n = ____n2


def run():
global ___n, ____n1, ____n3, ____h1n1, ____h1n2, ____b_h1n1, ____w_n1_h1n1, ____w_n3_h1n1, ____b_h1n2, ____w_n1_h1n2, ____w_n3_h1n2, ____b_n2, ____w_h1n1_n2, ____w_h1n2_n2, ____b_n4, ____w_h1n1_n4, ____w_h1n2_n4
____runNN()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


def _randInt(min_val, max_val):
val = int.from_bytes(os.urandom(4), byteorder='big')
if min_val < max_val:
Expand Down Expand Up @@ -78,7 +77,6 @@ def ____divisionOperations(___item5):
___item5 = ( ( 6 / float(_randInt(10 - 1, 100 - 1)) ) % ( 5 ) )
___item5 = ( ( _randDouble() / float(5) ) % ( 5 ) )


def run():
global ___item
____plusOperations(___item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import os
import time


class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import random
import math


class BreakOutOfALoop(Exception): pass
class ContinueLoop(Exception): pass

Expand Down
Loading

0 comments on commit 9f2963d

Please sign in to comment.