use of org.lflang.lf.StateVar in project lingua-franca by lf-lang.
the class PythonStateGenerator method generatePythonInstantiations.
/**
* Generate state variable instantiations for reactor "decl"
* @param decl The reactor declaration to generate state variables.
*/
public static String generatePythonInstantiations(ReactorDecl decl) {
List<String> lines = new ArrayList<>();
lines.add("# Define state variables");
// Next, handle state variables
for (StateVar state : ASTUtils.allStateVars(ASTUtils.toDefinition(decl))) {
lines.add("self." + state.getName() + " = " + generatePythonInitializer(state));
}
lines.add("");
return String.join("\n", lines);
}
use of org.lflang.lf.StateVar in project lingua-franca by lf-lang.
the class LinguaFrancaASTUtilsTest method initializedState.
/**
* Test that isInititialized returns true for inititialized state variables
*/
@Test
public void initializedState() throws Exception {
// Java 17:
// Model model = parser.parse("""
// target Cpp;
// main reactor Foo {
// state a();
// state b:int(3);
// state c:int[](1,2,3);
// state d(1 sec);
// state e(1 sec, 2 sec, 3 sec);
// }
// """);
// Java 11:
Model model = parser.parse(String.join(System.getProperty("line.separator"), "target Cpp;", "main reactor {", " state a();", " state b:int(3);", " state c:int[](1,2,3);", " state d(1 sec);", " state e(1 sec, 2 sec, 3 sec);", "}"));
Assertions.assertNotNull(model);
Assertions.assertTrue(model.eResource().getErrors().isEmpty(), "Encountered unexpected error while parsing: " + model.eResource().getErrors());
model.eAllContents().forEachRemaining((obj) -> {
if (obj instanceof StateVar) {
Assertions.assertTrue(isInitialized((StateVar) obj));
}
});
}
use of org.lflang.lf.StateVar in project lingua-franca by lf-lang.
the class LinguaFrancaASTUtilsTest method uninitializedState.
/**
* Test that isInititialized returns false for uninititialized state variables
*/
@Test
public void uninitializedState() throws Exception {
// Java 17:
// Model model = parser.parse("""
// target Cpp;
// main reactor Foo {
// state a;
// state b:int;
// state c:int[];
// state d:time;
// state e:time[];
// }
// '''
// Java 11:
Model model = parser.parse(String.join(System.getProperty("line.separator"), "target Cpp;", "main reactor {", " state a;", " state b:int;", " state c:int[];", " state d:time;", " state e:time;", "}"));
Assertions.assertNotNull(model);
Assertions.assertTrue(model.eResource().getErrors().isEmpty(), "Encountered unexpected error while parsing: " + model.eResource().getErrors());
model.eAllContents().forEachRemaining((obj) -> {
if (obj instanceof StateVar) {
Assertions.assertFalse(isInitialized((StateVar) obj));
}
});
}