Search in sources :

Example 1 with StateVar

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);
}
Also used : ArrayList(java.util.ArrayList) StateVar(org.lflang.lf.StateVar)

Example 2 with StateVar

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));
        }
    });
}
Also used : Model(org.lflang.lf.Model) StateVar(org.lflang.lf.StateVar) Test(org.junit.jupiter.api.Test)

Example 3 with StateVar

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));
        }
    });
}
Also used : Model(org.lflang.lf.Model) StateVar(org.lflang.lf.StateVar) Test(org.junit.jupiter.api.Test)

Aggregations

StateVar (org.lflang.lf.StateVar)3 Test (org.junit.jupiter.api.Test)2 Model (org.lflang.lf.Model)2 ArrayList (java.util.ArrayList)1