From 8da58a8e3748c1924c99d966f3451e812fba67a8 Mon Sep 17 00:00:00 2001 From: Martin Jobst Date: Thu, 12 Dec 2024 19:35:51 +0100 Subject: [PATCH] Update evaluator for new simple FB standard Add handling for EC states to simple FB evaluator and update tests. --- .../ide/model/eval/fb/SimpleFBEvaluator.java | 29 +- .../model/eval/AbstractEvaluatorTest.java | 26 +- .../eval/fb/SamplingFBEvaluatorTest.java | 1 - .../model/eval/fb/SimpleFBEvaluatorTest.java | 357 +++++++++--------- .../eval/variable/VariableOperationTest.java | 3 +- 5 files changed, 223 insertions(+), 193 deletions(-) diff --git a/plugins/org.eclipse.fordiac.ide.model.eval/src/org/eclipse/fordiac/ide/model/eval/fb/SimpleFBEvaluator.java b/plugins/org.eclipse.fordiac.ide.model.eval/src/org/eclipse/fordiac/ide/model/eval/fb/SimpleFBEvaluator.java index e788e92aa9..11ca563ff8 100644 --- a/plugins/org.eclipse.fordiac.ide.model.eval/src/org/eclipse/fordiac/ide/model/eval/fb/SimpleFBEvaluator.java +++ b/plugins/org.eclipse.fordiac.ide.model.eval/src/org/eclipse/fordiac/ide/model/eval/fb/SimpleFBEvaluator.java @@ -17,6 +17,8 @@ import org.eclipse.fordiac.ide.model.eval.variable.Variable; import org.eclipse.fordiac.ide.model.libraryElement.Algorithm; import org.eclipse.fordiac.ide.model.libraryElement.Event; +import org.eclipse.fordiac.ide.model.libraryElement.SimpleECAction; +import org.eclipse.fordiac.ide.model.libraryElement.SimpleECState; import org.eclipse.fordiac.ide.model.libraryElement.SimpleFBType; public class SimpleFBEvaluator extends BaseFBEvaluator { @@ -27,18 +29,21 @@ public SimpleFBEvaluator(final SimpleFBType type, final Variable context, fin @Override public void evaluate(final Event event) throws EvaluatorException, InterruptedException { - final Algorithm algorithm; - if (event != null) { - algorithm = getType().getAlgorithmNamed(event.getName()); - } else if (!getType().getAlgorithm().isEmpty()) { - algorithm = getType().getAlgorithm().getFirst(); - } else { - algorithm = null; - } - if (algorithm != null) { - getAlgorithmEvaluators().get(algorithm).evaluate(); + evaluateState(getType().getSimpleECStates().stream().filter(state -> state.getInputEvent().equals(event)) + .findAny().orElseThrow()); + } + + private void evaluateState(final SimpleECState state) throws EvaluatorException, InterruptedException { + for (final SimpleECAction action : state.getSimpleECActions()) { + final Algorithm algorithm = getType().getAlgorithmNamed(action.getAlgorithm()); + if (algorithm != null) { + getAlgorithmEvaluators().get(algorithm).evaluate(); + } + final Event output = action.getOutput(); + if (output != null) { + sendOutputEvent(output); + } + update(getVariables().values()); } - sendOutputEvents(getType().getInterfaceList().getEventOutputs()); - update(getVariables().values()); } } diff --git a/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/AbstractEvaluatorTest.java b/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/AbstractEvaluatorTest.java index 4d2a91a19b..97ff8f3e70 100644 --- a/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/AbstractEvaluatorTest.java +++ b/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/AbstractEvaluatorTest.java @@ -39,6 +39,8 @@ import org.eclipse.fordiac.ide.model.libraryElement.STAlgorithm; import org.eclipse.fordiac.ide.model.libraryElement.STFunctionBody; import org.eclipse.fordiac.ide.model.libraryElement.STMethod; +import org.eclipse.fordiac.ide.model.libraryElement.SimpleECAction; +import org.eclipse.fordiac.ide.model.libraryElement.SimpleECState; import org.eclipse.fordiac.ide.model.libraryElement.SimpleFBType; import org.eclipse.fordiac.ide.model.libraryElement.Value; import org.eclipse.fordiac.ide.model.libraryElement.VarDeclaration; @@ -201,11 +203,15 @@ public static ElementaryVariable newVariable(final AnyElemen return new ElementaryVariable<>(name, value.getType(), value); } - public static SimpleFBType newSimpleFBType(final String name, final Collection events, - final Collection vars) { + public static SimpleFBType newSimpleFBType(final String name, final Collection vars) { final SimpleFBType simpleType = LibraryElementFactory.eINSTANCE.createSimpleFBType(); simpleType.setName(name); - simpleType.setInterfaceList(newInterfaceList(events, vars)); + final Event inputEvent = newEvent("REQ", true); + final Event outputEvent = newEvent("CNF", false); + simpleType.setInterfaceList(newInterfaceList(List.of(inputEvent, outputEvent), vars)); + final SimpleECState state = newSimpleECState(inputEvent); + state.getSimpleECActions().add(newSimpleECAction("REQ", outputEvent)); + simpleType.getSimpleECStates().add(state); final FBTypeEntryMock typeEntry = new FBTypeEntryMock(simpleType, typeLib, null); simpleType.setTypeEntry(typeEntry); typeLib.addTypeEntry(typeEntry); @@ -213,6 +219,20 @@ public static SimpleFBType newSimpleFBType(final String name, final Collection vars, final String text) { final FunctionFBType functionType = LibraryElementFactory.eINSTANCE.createFunctionFBType(); diff --git a/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/fb/SamplingFBEvaluatorTest.java b/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/fb/SamplingFBEvaluatorTest.java index 12aaf5829d..072fc999fd 100644 --- a/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/fb/SamplingFBEvaluatorTest.java +++ b/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/fb/SamplingFBEvaluatorTest.java @@ -146,7 +146,6 @@ void testSimpleFBReset() throws EvaluatorException, InterruptedException { SimpleFBType newTestSimpleFBType() { final SimpleFBType simpleType = newSimpleFBType("TestSimple", - List.of(newEvent("REQ", true), newEvent("CNF", false)), List.of(newVarDeclaration("DI1", ElementaryTypes.DINT, true, "17"), newVarDeclaration("DI2", ElementaryTypes.DINT, true, "4"), newVarDeclaration("DO1", ElementaryTypes.DINT, false), diff --git a/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/fb/SimpleFBEvaluatorTest.java b/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/fb/SimpleFBEvaluatorTest.java index dde626d315..3421e090bc 100644 --- a/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/fb/SimpleFBEvaluatorTest.java +++ b/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/fb/SimpleFBEvaluatorTest.java @@ -37,7 +37,7 @@ import org.eclipse.fordiac.ide.model.libraryElement.FB; import org.eclipse.fordiac.ide.model.libraryElement.FBType; import org.eclipse.fordiac.ide.model.libraryElement.ICallable; -import org.eclipse.fordiac.ide.model.libraryElement.LibraryElementFactory; +import org.eclipse.fordiac.ide.model.libraryElement.SimpleECState; import org.eclipse.fordiac.ide.model.libraryElement.SimpleFBType; import org.eclipse.fordiac.ide.model.libraryElement.VarDeclaration; import org.eclipse.xtext.xbase.lib.CollectionLiterals; @@ -50,7 +50,7 @@ class SimpleFBEvaluatorTest extends AbstractFBEvaluatorTest { @Test void testSimpleFB() throws EvaluatorException, InterruptedException { assertEquals(toDIntValue(21), - evaluateSimpleFB(List.of(newSTAlgorithm("DO1 := DI1 + DI2;", "REQ")), "REQ", + evaluateSimpleFB(List.of(newSTAlgorithm("DO1 := DI1 + DI2;", "REQ")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -59,15 +59,38 @@ void testSimpleFB() throws EvaluatorException, InterruptedException { void testSimpleFBMultiAlgorithm() throws EvaluatorException, InterruptedException { assertEquals(toDIntValue(21), evaluateSimpleFB( - List.of(newSTAlgorithm("DO1 := DI1 - DI2;", "REQ1"), - newSTAlgorithm("DO1 := DI1 + DI2;", "REQ2")), - "REQ2", List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + List.of(newSTAlgorithm("DO1 := DI1 - DI2;", "INIT"), + newSTAlgorithm("DO1 := DI1 + DI2;", "REQ")), + List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } + @Test + void testSimpleFBMultiState() throws EvaluatorException, InterruptedException { + final SimpleFBType fbType = newSimpleFBType("Test", + List.of(newVarDeclaration("DI1", ElementaryTypes.DINT, true, "17"), + newVarDeclaration("DI2", ElementaryTypes.DINT, true, "4"), + newVarDeclaration("DO1", ElementaryTypes.DINT, false))); + final Event initInputEvent = newEvent("INIT", true); + final Event initOutputEvent = newEvent("INITO", true); + fbType.getInterfaceList().getEventInputs().add(initInputEvent); + fbType.getInterfaceList().getEventOutputs().add(initOutputEvent); + final SimpleECState initState = newSimpleECState(initInputEvent); + initState.getSimpleECActions().add(newSimpleECAction("INIT", initOutputEvent)); + fbType.getSimpleECStates().add(initState); + fbType.getCallables().add(newSTAlgorithm("DO1 := DI1 - DI2;", "REQ")); + fbType.getCallables().add(newSTAlgorithm("DO1 := DI1 + DI2;", "INIT")); + final TracingFBEvaluatorEventQueue queue = new TracingFBEvaluatorEventQueue(List.of(initInputEvent)); + final SimpleFBEvaluator eval = new SimpleFBEvaluator(fbType, null, List.of(), null); + eval.setEventQueue(queue); + eval.evaluate(); + assertIterableEquals(List.of(initOutputEvent), queue.getOutputEvents()); + assertEquals(toDIntValue(21), eval.getVariables().get("DO1").getValue()); + } + @Test void testSimpleFBReset() throws EvaluatorException, InterruptedException { - final SimpleFBEvaluator evaluator = evaluateSimpleFB(List.of(newSTAlgorithm("DO1 := DI1 + DI2;", "REQ")), "REQ", + final SimpleFBEvaluator evaluator = evaluateSimpleFB(List.of(newSTAlgorithm("DO1 := DI1 + DI2;", "REQ")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)); assertEquals(toDIntValue(21), evaluator.getVariables().get("DO1").getValue()); @@ -78,7 +101,7 @@ void testSimpleFBReset() throws EvaluatorException, InterruptedException { @Test void testSimpleFBInternalVar() throws EvaluatorException, InterruptedException { assertEquals(toDIntValue(42), - evaluateSimpleFB(List.of(newSTAlgorithm("DO1 := (DI1 + DI2) * INTERNALVAR1;", "REQ")), "REQ", + evaluateSimpleFB(List.of(newSTAlgorithm("DO1 := (DI1 + DI2) * INTERNALVAR1;", "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), @@ -89,7 +112,7 @@ void testSimpleFBInternalVar() throws EvaluatorException, InterruptedException { @Test void testSimpleFBInternalConst() throws EvaluatorException, InterruptedException { assertEquals(toDIntValue(42), - evaluateSimpleFB(List.of(newSTAlgorithm("DO1 := (DI1 + DI2) * INTERNALCONST1;", "REQ")), "REQ", + evaluateSimpleFB(List.of(newSTAlgorithm("DO1 := (DI1 + DI2) * INTERNALCONST1;", "REQ")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(newVarDeclaration("INTERNALCONST1", ElementaryTypes.DINT, false, "2")), List.of()) @@ -113,7 +136,7 @@ void testMethodCall() throws EvaluatorException, InterruptedException { C := A + B; END_METHOD """, "TEST_METHOD")), - "REQ", List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -131,7 +154,7 @@ void testMethodCallWithNotBool() throws EvaluatorException, InterruptedException END_VAR C := A AND B; END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(BoolValue.toBoolValue(false), "DI1"), newVariable(BoolValue.toBoolValue(true), "DI2")), newVarDeclaration("DO1", ElementaryTypes.BOOL, false)).getVariables().get("DO1").getValue()); @@ -151,7 +174,7 @@ void testMethodCallWithNotWord() throws EvaluatorException, InterruptedException END_VAR C := A AND B; END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(WordValue.toWordValue(Integer.valueOf(21)), "DI1"), newVariable(WordValue.toWordValue(Integer.valueOf(4)), "DI2")), newVarDeclaration("DO1", ElementaryTypes.WORD, false)).getVariables().get("DO1").getValue()); @@ -168,7 +191,7 @@ void testMethodCallVariableAccess() throws EvaluatorException, InterruptedExcept END_VAR DO1 := A + B; END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -184,7 +207,7 @@ void testMethodCallThisVariableAccess() throws EvaluatorException, InterruptedEx END_VAR THIS.DO1 := A + B; END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -203,7 +226,7 @@ void testMethodCallWithDefaultInput() throws EvaluatorException, InterruptedExce END_VAR C := A + B; END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -221,7 +244,7 @@ void testMethodCallWithDefaultOutput() throws EvaluatorException, InterruptedExc C: DINT := 21; END_VAR END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -240,7 +263,7 @@ void testMethodCallNonFormal() throws EvaluatorException, InterruptedException { END_VAR C := A + B; END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -256,7 +279,7 @@ void testMethodCallWithReturnValue() throws EvaluatorException, InterruptedExcep END_VAR TEST_METHOD := A + B; END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -272,63 +295,59 @@ void testMethodCallWithReturnValueAndDefaults() throws EvaluatorException, Inter END_VAR TEST_METHOD := A + B; END_METHOD - """, "TEST_METHOD")), "REQ", + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @Test void testMethodCallWithInOut() throws EvaluatorException, InterruptedException { - assertEquals(toDIntValue(42), - evaluateSimpleFB(List.of(newSTAlgorithm(""" - VAR_TEMP - tempVar : DINT; - END_VAR - tempVar := DI1; - THIS.TEST_METHOD(X := tempVar, A := DI2, O => DO1); DO1 := DO1 + tempVar; - """, "REQ"), newSTMethod(""" - METHOD TEST_METHOD - VAR_INPUT - A: DINT; - END_VAR - VAR_OUTPUT - O: DINT := 21; - END_VAR - VAR_IN_OUT - X: DINT; - END_VAR - X := X + A; - END_METHOD - """, "TEST_METHOD")), "REQ", - List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), - newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); + assertEquals(toDIntValue(42), evaluateSimpleFB(List.of(newSTAlgorithm(""" + VAR_TEMP + tempVar : DINT; + END_VAR + tempVar := DI1; + THIS.TEST_METHOD(X := tempVar, A := DI2, O => DO1); DO1 := DO1 + tempVar; + """, "REQ"), newSTMethod(""" + METHOD TEST_METHOD + VAR_INPUT + A: DINT; + END_VAR + VAR_OUTPUT + O: DINT := 21; + END_VAR + VAR_IN_OUT + X: DINT; + END_VAR + X := X + A; + END_METHOD + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @Test void testMethodCallNonFormalWithInOut() throws EvaluatorException, InterruptedException { - assertEquals(toDIntValue(42), - evaluateSimpleFB(List.of(newSTAlgorithm(""" - VAR_TEMP - tempVar : DINT; - END_VAR - tempVar := DI1; - THIS.TEST_METHOD(DI2, tempVar, DO1); DO1 := DO1 + tempVar; - """, "REQ"), newSTMethod(""" - METHOD TEST_METHOD - VAR_INPUT - A: DINT; - END_VAR - VAR_OUTPUT - O: DINT := 21; - END_VAR - VAR_IN_OUT - X: DINT; - END_VAR - X := X + A; - END_METHOD - """, "TEST_METHOD")), "REQ", - List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), - newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); + assertEquals(toDIntValue(42), evaluateSimpleFB(List.of(newSTAlgorithm(""" + VAR_TEMP + tempVar : DINT; + END_VAR + tempVar := DI1; + THIS.TEST_METHOD(DI2, tempVar, DO1); DO1 := DO1 + tempVar; + """, "REQ"), newSTMethod(""" + METHOD TEST_METHOD + VAR_INPUT + A: DINT; + END_VAR + VAR_OUTPUT + O: DINT := 21; + END_VAR + VAR_IN_OUT + X: DINT; + END_VAR + X := X + A; + END_METHOD + """, "TEST_METHOD")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @Test @@ -359,7 +378,7 @@ void testMethod2MethodCall() throws EvaluatorException, InterruptedException { C := A + B; END_METHOD """, "TEST_METHOD2")), - "REQ", List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -382,7 +401,7 @@ void testMethod2MethodCallVariableAccess() throws EvaluatorException, Interrupte END_VAR DO1 := A + B; END_METHOD - """, "TEST_METHOD2")), "REQ", + """, "TEST_METHOD2")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -409,7 +428,7 @@ void testMethod2MethodCallForwardReturn() throws EvaluatorException, Interrupted END_VAR C := A + B; END_METHOD - """, "TEST_METHOD2")), "REQ", + """, "TEST_METHOD2")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -442,7 +461,7 @@ void testMethod2MethodCallWithDefaultInput() throws EvaluatorException, Interrup C := A + B; END_METHOD """, "TEST_METHOD2")), - "REQ", List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -473,7 +492,7 @@ void testMethod2MethodCallWithDefaultOutput() throws EvaluatorException, Interru END_VAR END_METHOD """, "TEST_METHOD2")), - "REQ", List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -505,7 +524,7 @@ void testMethod2MethodCallNonFormal() throws EvaluatorException, InterruptedExce C := A + B; END_METHOD """, "TEST_METHOD2")), - "REQ", List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -534,7 +553,7 @@ void testMethod2MethodCallWithReturnValue() throws EvaluatorException, Interrupt TEST_METHOD2 := A + B; END_METHOD """, "TEST_METHOD2")), - "REQ", List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @@ -563,84 +582,80 @@ void testMethod2MethodCallWithReturnValueAndDefaults() throws EvaluatorException TEST_METHOD2 := A + B; END_METHOD """, "TEST_METHOD2")), - "REQ", List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @Test void testMethod2MethodCallWithInOut() throws EvaluatorException, InterruptedException { - assertEquals(toDIntValue(42), - evaluateSimpleFB(List.of(newSTAlgorithm(""" - ALGORITHM REQ - VAR_TEMP - tempVar : DINT; - END_VAR - tempVar := DI1; - THIS.TEST_METHOD(X := tempVar, A := DI2, O => DO1); DO1 := DO1 + tempVar; - END_ALGORITHM - """, "REQ"), newSTMethod(""" - METHOD TEST_METHOD - VAR_INPUT - A: DINT; - END_VAR - VAR_OUTPUT - O: DINT; - END_VAR - VAR_IN_OUT - X: DINT; - END_VAR - THIS.TEST_METHOD2(A := A, X := X, O => O); - END_METHOD - """, "TEST_METHOD"), newSTMethod(""" - METHOD TEST_METHOD2 - VAR_INPUT - A: DINT; - END_VAR - VAR_OUTPUT - O: DINT := 21; - END_VAR - VAR_IN_OUT - X: DINT; - END_VAR - X := X + A; - END_METHOD - """, "TEST_METHOD2")), "REQ", - List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), - newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); + assertEquals(toDIntValue(42), evaluateSimpleFB(List.of(newSTAlgorithm(""" + ALGORITHM REQ + VAR_TEMP + tempVar : DINT; + END_VAR + tempVar := DI1; + THIS.TEST_METHOD(X := tempVar, A := DI2, O => DO1); DO1 := DO1 + tempVar; + END_ALGORITHM + """, "REQ"), newSTMethod(""" + METHOD TEST_METHOD + VAR_INPUT + A: DINT; + END_VAR + VAR_OUTPUT + O: DINT; + END_VAR + VAR_IN_OUT + X: DINT; + END_VAR + THIS.TEST_METHOD2(A := A, X := X, O => O); + END_METHOD + """, "TEST_METHOD"), newSTMethod(""" + METHOD TEST_METHOD2 + VAR_INPUT + A: DINT; + END_VAR + VAR_OUTPUT + O: DINT := 21; + END_VAR + VAR_IN_OUT + X: DINT; + END_VAR + X := X + A; + END_METHOD + """, "TEST_METHOD2")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @Test void testMethod2MethodCallNonFormalWithInOut() throws EvaluatorException, InterruptedException { - assertEquals(toDIntValue(21), - evaluateSimpleFB(List.of(newSTAlgorithm(""" - VAR_TEMP - tempVar : DINT; - END_VAR - tempVar := DI1; - THIS.TEST_METHOD(DI2, tempVar); DO1 := tempVar; - """, "REQ"), newSTMethod(""" - METHOD TEST_METHOD - VAR_INPUT - A: DINT; - END_VAR - VAR_IN_OUT - X: DINT; - END_VAR - THIS.TEST_METHOD2(A, X); - END_METHOD - """, "TEST_METHOD"), newSTMethod(""" - METHOD TEST_METHOD2 - VAR_INPUT - A: DINT; - END_VAR - VAR_IN_OUT - X: DINT; - END_VAR - X := X + A; - END_METHOD - """, "TEST_METHOD2")), "REQ", - List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), - newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); + assertEquals(toDIntValue(21), evaluateSimpleFB(List.of(newSTAlgorithm(""" + VAR_TEMP + tempVar : DINT; + END_VAR + tempVar := DI1; + THIS.TEST_METHOD(DI2, tempVar); DO1 := tempVar; + """, "REQ"), newSTMethod(""" + METHOD TEST_METHOD + VAR_INPUT + A: DINT; + END_VAR + VAR_IN_OUT + X: DINT; + END_VAR + THIS.TEST_METHOD2(A, X); + END_METHOD + """, "TEST_METHOD"), newSTMethod(""" + METHOD TEST_METHOD2 + VAR_INPUT + A: DINT; + END_VAR + VAR_IN_OUT + X: DINT; + END_VAR + X := X + A; + END_METHOD + """, "TEST_METHOD2")), List.of(newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2")), + newVarDeclaration("DO1", ElementaryTypes.DINT, false)).getVariables().get("DO1").getValue()); } @ParameterizedTest @@ -653,8 +668,8 @@ void testSimpleWithArray(final String arraySize) throws EvaluatorException, Inte assertEquals(toDIntValue(21), evaluateSimpleFB( List.of(newSTAlgorithm("DO1 := DI1[0] + DI1[1] + DI1[2] + DI1[3] + DI1[4] + DI1[5];", "REQ")), - "REQ", List.of(inputVarDecl, outputVarDecl), List.of(), List.of(internalConstVarDecl), - List.of()).getVariables().get("DO1").getValue()); + List.of(inputVarDecl, outputVarDecl), List.of(), List.of(internalConstVarDecl), List.of()) + .getVariables().get("DO1").getValue()); } @Test @@ -671,7 +686,7 @@ void testSimpleWithStruct() throws EvaluatorException, InterruptedException { ((StructValue) evaluateSimpleFB(List.of(newSTAlgorithm(""" DO1.a := DI1.a + DI1.b; DO1.b := 42; - """, "REQ")), "REQ", List.of(inputVar), outputVarDecl).getVariables().get("DO1").getValue())); + """, "REQ")), List.of(inputVar), outputVarDecl).getVariables().get("DO1").getValue())); } @Test @@ -682,7 +697,7 @@ void testSimpleFBCall() throws EvaluatorException, InterruptedException { FB1(DI1 := DI1); FB1(DI1 := DI2); FB1.REQ(DI1 := FB1.DO1, DO1 => DO1); - """, "REQ")), "REQ", + """, "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(), @@ -708,7 +723,7 @@ void testFunctionFBCall() throws EvaluatorException, InterruptedException { assertEquals(toDIntValue(21), evaluateSimpleFB(List.of(newSTAlgorithm(""" DO1 := FB1(DI1, DI2); - """, "REQ")), "REQ", + """, "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(), @@ -734,7 +749,7 @@ void testFunctionFBCallFormal() throws EvaluatorException, InterruptedException assertEquals(toDIntValue(21), evaluateSimpleFB(List.of(newSTAlgorithm(""" DO1 := FB1(DI1 := 17, DI2 := 4); - """, "REQ")), "REQ", + """, "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(), @@ -760,7 +775,7 @@ void testFunctionFBCallEvent() throws EvaluatorException, InterruptedException { assertEquals(toDIntValue(21), evaluateSimpleFB(List.of(newSTAlgorithm(""" DO1 := FB1.REQ(DI1, DI2); - """, "REQ")), "REQ", + """, "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(), @@ -786,7 +801,7 @@ void testFunctionFBCallEventFormal() throws EvaluatorException, InterruptedExcep assertEquals(toDIntValue(21), evaluateSimpleFB(List.of(newSTAlgorithm(""" DO1 := FB1.REQ(DI1 := DI1, DI2 := DI2); - """, "REQ")), "REQ", + """, "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(), @@ -812,7 +827,7 @@ void testFunctionFBCallNestedExpression() throws EvaluatorException, Interrupted assertEquals(toDIntValue(42), evaluateSimpleFB(List.of(newSTAlgorithm(""" DO1 := FB1(DI1, DI2) * 2; - """, "REQ")), "REQ", + """, "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(), @@ -838,7 +853,7 @@ void testFunctionFBCallNestedCall() throws EvaluatorException, InterruptedExcept assertEquals(toDIntValue(42), evaluateSimpleFB(List.of(newSTAlgorithm(""" DO1 := MUL(FB1(DI1, DI2), 2); - """, "REQ")), "REQ", + """, "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(), @@ -866,7 +881,7 @@ void testFunctionFBCallVariables() throws EvaluatorException, InterruptedExcepti FB1.DI1 := DI1; FB1.DI2 := DI2; DO1 := MUL(FB1(), 2); - """, "REQ")), "REQ", + """, "REQ")), Collections.>unmodifiableList(CollectionLiterals.>newArrayList( newVariable(toDIntValue(17), "DI1"), newVariable(toDIntValue(4), "DI2"))), newVarDeclaration("DO1", ElementaryTypes.DINT, false), List.of(), List.of(), @@ -874,64 +889,56 @@ void testFunctionFBCallVariables() throws EvaluatorException, InterruptedExcepti } FBType newTestSimpleFBType() { - final Event inputEvent = newEvent("REQ", true); - final Event outputEvent = newEvent("CNF", false); - final SimpleFBType simpleType = newSimpleFBType("TestSimple", List.of(inputEvent, outputEvent), + final SimpleFBType simpleType = newSimpleFBType("TestSimple", List.of(newVarDeclaration("DI1", ElementaryTypes.DINT, true), newVarDeclaration("DO1", ElementaryTypes.DINT, false))); simpleType.getCallables().add(newSTAlgorithm("DO1 := DO1 + DI1;", "REQ")); return simpleType; } - static SimpleFBEvaluator evaluateSimpleFB(final List callables, final String inputEventName, + static SimpleFBEvaluator evaluateSimpleFB(final List callables, final List> variables, final VarDeclaration output) throws EvaluatorException, InterruptedException { - return evaluateSimpleFB(callables, inputEventName, variables, output, List.of(), List.of(), List.of()); + return evaluateSimpleFB(callables, variables, output, List.of(), List.of(), List.of()); } - static SimpleFBEvaluator evaluateSimpleFB(final List callables, final String inputEventName, + static SimpleFBEvaluator evaluateSimpleFB(final List callables, final List> variables, final VarDeclaration output, final List internalVars, final List internalConstVars, final List internalFBs) throws EvaluatorException, InterruptedException { - final Event inputEvent = newEvent(inputEventName, true); - final Event outputEvent = newEvent("CNF", false); - final SimpleFBType fbType = LibraryElementFactory.eINSTANCE.createSimpleFBType(); - fbType.setName("Test"); - fbType.setInterfaceList( - newInterfaceList(List.of(inputEvent, outputEvent), - Stream.concat(variables.stream().map( + final SimpleFBType fbType = newSimpleFBType("Test", + Stream.concat( + variables.stream().map( variable -> newVarDeclaration(variable.getName(), (DataType) variable.getType(), true)), - Stream.of(output)).toList())); + Stream.of(output)).toList()); ECollections.setEList(fbType.getCallables(), callables); ECollections.setEList(fbType.getInternalVars(), internalVars); ECollections.setEList(fbType.getInternalConstVars(), internalConstVars); ECollections.setEList(fbType.getInternalFbs(), internalFBs); - final TracingFBEvaluatorEventQueue queue = new TracingFBEvaluatorEventQueue(List.of(inputEvent)); + final TracingFBEvaluatorEventQueue queue = new TracingFBEvaluatorEventQueue( + fbType.getInterfaceList().getEventInputs()); final SimpleFBEvaluator eval = new SimpleFBEvaluator(fbType, null, variables, null); eval.setEventQueue(queue); eval.evaluate(); - assertIterableEquals(List.of(outputEvent), queue.getOutputEvents()); + assertIterableEquals(fbType.getInterfaceList().getEventOutputs(), queue.getOutputEvents()); return eval; } - static SimpleFBEvaluator evaluateSimpleFB(final List callables, final String inputEventName, + static SimpleFBEvaluator evaluateSimpleFB(final List callables, final List variables, final List internalVars, final List internalConstVars, final List internalFBs) throws EvaluatorException, InterruptedException { - final Event inputEvent = newEvent(inputEventName, true); - final Event outputEvent = newEvent("CNF", false); - final SimpleFBType fbType = LibraryElementFactory.eINSTANCE.createSimpleFBType(); - fbType.setName("Test"); - fbType.setInterfaceList(newInterfaceList(List.of(inputEvent, outputEvent), variables)); + final SimpleFBType fbType = newSimpleFBType("Test", variables); ECollections.setEList(fbType.getCallables(), callables); ECollections.setEList(fbType.getInternalVars(), internalVars); ECollections.setEList(fbType.getInternalConstVars(), internalConstVars); ECollections.setEList(fbType.getInternalFbs(), internalFBs); - final TracingFBEvaluatorEventQueue queue = new TracingFBEvaluatorEventQueue(List.of(inputEvent)); + final TracingFBEvaluatorEventQueue queue = new TracingFBEvaluatorEventQueue( + fbType.getInterfaceList().getEventInputs()); final SimpleFBEvaluator eval = new SimpleFBEvaluator(fbType, null, List.of(), null); eval.setEventQueue(queue); eval.evaluate(); - assertIterableEquals(List.of(outputEvent), queue.getOutputEvents()); + assertIterableEquals(fbType.getInterfaceList().getEventOutputs(), queue.getOutputEvents()); return eval; } } diff --git a/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/variable/VariableOperationTest.java b/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/variable/VariableOperationTest.java index 8e6f2513fd..539c627283 100644 --- a/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/variable/VariableOperationTest.java +++ b/tests/org.eclipse.fordiac.ide.test.model.eval/src/org/eclipse/fordiac/ide/test/model/eval/variable/VariableOperationTest.java @@ -101,8 +101,7 @@ void setup() { typeVarDeclaration1 = newVarDeclaration("DI1", ElementaryTypes.DINT, true); typeVarDeclaration2 = newVarDeclaration("DI2", ElementaryTypes.DINT, true, "17"); typeVarDeclaration3 = newVarDeclaration("DI3", ElementaryTypes.DINT, true, "4"); - type = newSimpleFBType("TestSimple", List.of(), - List.of(typeVarDeclaration1, typeVarDeclaration2, typeVarDeclaration3)); + type = newSimpleFBType("TestSimple", List.of(typeVarDeclaration1, typeVarDeclaration2, typeVarDeclaration3)); instance = newFB("TestSimpleInstance", type); instanceVarDeclaration1 = instance.getInterface().getVariable("DI1");