Skip to content

Commit

Permalink
Update evaluator for new simple FB standard
Browse files Browse the repository at this point in the history
Add handling for EC states to simple FB evaluator and update tests.
  • Loading branch information
mx990 authored and azoitl committed Dec 12, 2024
1 parent 4de7d37 commit 8da58a8
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleFBType> {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,18 +203,36 @@ public static ElementaryVariable<AnyElementaryValue> newVariable(final AnyElemen
return new ElementaryVariable<>(name, value.getType(), value);
}

public static SimpleFBType newSimpleFBType(final String name, final Collection<Event> events,
final Collection<VarDeclaration> vars) {
public static SimpleFBType newSimpleFBType(final String name, final Collection<VarDeclaration> 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);
new ResourceImpl().getContents().add(simpleType);
return simpleType;
}

public static SimpleECState newSimpleECState(final Event event) {
final SimpleECState state = LibraryElementFactory.eINSTANCE.createSimpleECState();
state.setName(event.getName());
state.setInputEvent(event);
return state;
}

public static SimpleECAction newSimpleECAction(final String algorithm, final Event output) {
final SimpleECAction action = LibraryElementFactory.eINSTANCE.createSimpleECAction();
action.setAlgorithm(algorithm);
action.setOutput(output);
return action;
}

public static FunctionFBType newFunctionFBType(final String name, final Collection<VarDeclaration> vars,
final String text) {
final FunctionFBType functionType = LibraryElementFactory.eINSTANCE.createFunctionFBType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading

0 comments on commit 8da58a8

Please sign in to comment.