From b0a39fc54d5a806ca8cc781fcd72a1387104c954 Mon Sep 17 00:00:00 2001 From: Nestor Date: Thu, 5 Oct 2023 11:30:48 +0200 Subject: [PATCH] getter/setters work in progress, but for merge Signed-off-by: Nestor --- .../framework/fmi2/api/Fmi2Builder.java | 13 +-- .../values/PortValueExpresssionMapImpl.java | 2 +- .../api/mabl/values/PortValueMapImpl.java | 2 +- .../variables/InstanceVariableFmi3Api.java | 91 +++++++++---------- .../mabl/variables/PortVariableMapImpl.java | 2 +- .../org/intocps/maestro/BuilderFmi3Test.java | 18 ++-- 6 files changed, 67 insertions(+), 61 deletions(-) diff --git a/frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/Fmi2Builder.java b/frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/Fmi2Builder.java index fb3270d91..2cc56ecc9 100644 --- a/frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/Fmi2Builder.java +++ b/frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/Fmi2Builder.java @@ -768,28 +768,29 @@ interface Fmi3InstanceVariable extends Variable> { void setupExperiment(double startTime, Double endTime, Double tolerance); - int enterInitializationMode(boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime); + void enterInitializationMode(boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime); - int exitInitializationMode(); + void exitInitializationMode(); void setupExperiment(Scope scope, DoubleVariable startTime, DoubleVariable endTime, BoolVariable endTimeDefined, Double tolerance); void setupExperiment(Scope scope, double startTime, Double endTime, Double tolerance); - int enterInitializationMode(Scope scope,boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, + void enterInitializationMode(Scope scope,boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime); - int exitInitializationMode(Scope scope); + void exitInitializationMode(Scope scope); - int terminate(Scope scope); + void terminate(Scope scope); - int terminate(); + void terminate(); void freeInstance(); void freeInstance(Scope scope); + /** * @param scope * @param currentCommunicationPoint diff --git a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueExpresssionMapImpl.java b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueExpresssionMapImpl.java index 24738ec65..18a3a5123 100644 --- a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueExpresssionMapImpl.java +++ b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueExpresssionMapImpl.java @@ -5,7 +5,7 @@ import java.util.HashMap; import java.util.Map; -public class PortValueExpresssionMapImpl extends HashMap implements Fmi2Builder.Fmi2ComponentVariable.PortExpressionValueMap { +public class PortValueExpresssionMapImpl extends HashMap implements Fmi2Builder.Fmi2ComponentVariable.PortExpressionValueMap, Fmi2Builder.Fmi3InstanceVariable.PortExpressionValueMap { public PortValueExpresssionMapImpl(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); } diff --git a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueMapImpl.java b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueMapImpl.java index 03852dbda..9f6c80c92 100644 --- a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueMapImpl.java +++ b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueMapImpl.java @@ -5,7 +5,7 @@ import java.util.HashMap; import java.util.Map; -public class PortValueMapImpl extends HashMap> implements Fmi2Builder.Fmi2ComponentVariable.PortValueMap { +public class PortValueMapImpl extends HashMap> implements Fmi2Builder.Fmi2ComponentVariable.PortValueMap, Fmi2Builder.Fmi3InstanceVariable.PortValueMap { public PortValueMapImpl(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); } diff --git a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/InstanceVariableFmi3Api.java b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/InstanceVariableFmi3Api.java index b0e16ee32..b93bf05f5 100644 --- a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/InstanceVariableFmi3Api.java +++ b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/InstanceVariableFmi3Api.java @@ -7,7 +7,6 @@ import org.intocps.maestro.ast.analysis.AnalysisException; import org.intocps.maestro.ast.analysis.DepthFirstAnalysisAdaptor; import org.intocps.maestro.ast.node.*; -import org.intocps.maestro.fmi.Fmi2ModelDescription; import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3Causality; import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3ModelDescription; import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3TypeEnum; @@ -22,7 +21,6 @@ import org.slf4j.LoggerFactory; import javax.xml.xpath.XPathExpressionException; -import java.lang.reflect.InvocationTargetException; import java.util.*; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Function; @@ -39,12 +37,6 @@ @SuppressWarnings("rawtypes") public class InstanceVariableFmi3Api extends VariableFmi2Api> implements Fmi2Builder.Fmi3InstanceVariable { final static Logger logger = LoggerFactory.getLogger(InstanceVariableFmi3Api.class); - private final static int FMI_OK = 0; - private final static int FMI_WARNING = 1; - private final static int FMI_DISCARD = 2; - private final static int FMI_ERROR = 3; - private final static int FMI_FATAL = 4; - private final static int FMI_PENDING = 5; private final static int FMI_STATUS_LAST_SUCCESSFUL = 2; private static final String LOGLEVELS_POSTFIX = "_log_levels"; private final static String CATEGORY_STATUS = "category_status"; @@ -95,7 +87,6 @@ public InstanceVariableFmi3Api(PStm declaration, FmuVariableFmi3Api parent, Stri this.environmentName = environmentName; } - // TODO fmi3 model description. public Fmi3ModelDescription getModelDescription() { return modelDescriptionContext.getModelDescription(); } @@ -292,13 +283,13 @@ private void setupExperiment(Fmi2Builder.Scope scope, PExp startTime, PExp } @Override - public int enterInitializationMode(boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime) { - return this.enterInitializationMode(builder.getDynamicScope(), toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime); + public void enterInitializationMode(boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime) { + this.enterInitializationMode(builder.getDynamicScope(), toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime); } @Override - public int exitInitializationMode() { - return this.exitInitializationMode(builder.getDynamicScope()); + public void exitInitializationMode() { + this.exitInitializationMode(builder.getDynamicScope()); } @Override @@ -315,29 +306,25 @@ public void setupExperiment(Fmi2Builder.Scope scope, double startTime, Dou // TODO: probably should return other things than ok also @Override - public int enterInitializationMode(Fmi2Builder.Scope scope, boolean toleranceDefined, double tolerance, double startTime, + public void enterInitializationMode(Fmi2Builder.Scope scope, boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime) { PStm stm = stateTransitionFunction(FmiFunctionType.ENTERINITIALIZATIONMODE, toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime ); - scope.add(stm); if (builder.getSettings().fmiErrorHandlingEnabled) { FmiStatusErrorHandlingBuilder.generate(builder, "enterInitializationMode", this, (IMablScope) scope, MablApiBuilder.Fmi3Status.FMI_ERROR, MablApiBuilder.Fmi3Status.FMI_FATAL); } - return Fmi3Status.OK.value; } - // TODO: Probably return other things than just ok. @Override - public int exitInitializationMode(Fmi2Builder.Scope scope) { + public void exitInitializationMode(Fmi2Builder.Scope scope) { PStm stm = stateTransitionFunction(FmiFunctionType.EXITINITIALIZATIONMODE); scope.add(stm); if (builder.getSettings().fmiErrorHandlingEnabled) { FmiStatusErrorHandlingBuilder.generate(builder, "exitInitializationMode", this, (IMablScope) scope, MablApiBuilder.Fmi3Status.FMI_ERROR, MablApiBuilder.Fmi3Status.FMI_FATAL); } - return Fmi3Status.OK.value; } @Override @@ -541,6 +528,27 @@ public Map> get(Fmi2Builder.Scope scop PType type; switch (e.getKey()) { + case Float64Type: + type = new ARealNumericPrimitiveType(); + break; + case Float32Type: + type = new AFloatNumericPrimitiveType(); + break; + case Int8Type: + type = new AByteNumericPrimitiveType(); + break; + case Int16Type: + type = new AShortNumericPrimitiveType(); + break; + case Int32Type: + type = new AIntNumericPrimitiveType(); + break; + case Int64Type: + type = new ALongNumericPrimitiveType(); + break; + + // TODO add the rest + // case Boolean: // type = new ABooleanPrimitiveType(); // break; @@ -555,10 +563,8 @@ public Map> get(Fmi2Builder.Scope scop // break; // case Enumeration: // throw new RuntimeException("Cannot assign enumeration port type."); -// default: -// throw new RuntimeException("Cannot match port types."); default: - type = new AIntNumericPrimitiveType(); // TODO For now for testing + throw new RuntimeException("Cannot match port types."); } ArrayVariableFmi2Api valBuf = getIOBuffer(type); @@ -774,14 +780,15 @@ public VariableFmi2Api getSingle(String name) { + + + public void set(Fmi2Builder.Port p, Fmi2Builder.ExpressionValue v) { - ; -// this.set(new PortValueExpresssionMapImpl(Map.of(p, v))); + this.set(new PortValueExpresssionMapImpl(Map.of(p, v))); } public void set(Fmi2Builder.Scope scope, Fmi2Builder.Port p, Fmi2Builder.ExpressionValue v) { - ; - // this.set(scope, new PortValueMapImpl(Map.of(p, v))); + this.set(scope, new PortValueMapImpl(Map.of(p, v))); } public void set(PortExpressionValueMap value) { @@ -803,8 +810,6 @@ public void set(Fmi2Builder.Scope scope, PortExpressionValueMap value) { @Override public void set(Fmi2Builder.Scope scope, PortValueMap value) { - - if (value == null || value.isEmpty()) { return; } @@ -814,6 +819,7 @@ public void set(Fmi2Builder.Scope scope, PortValueMap value) { set(scope, selectedPorts, port -> { Object val = (value.get(port)).get(); + // TODO: Add the rest if (val instanceof Double) { return Map.entry(newARealLiteralExp((Double) val), newRealType()); } @@ -849,7 +855,6 @@ public void set(Fmi2Builder.Scope scope, PortVariableMap valu } public void set(Fmi2Builder.Scope scope, List selectedPorts, Function> portToValue) { - Set selectedPortsAsStrings = selectedPorts.stream() .map(p -> p.getName() + "-" + p.aMablFmi3InstanceAPI.getName() + "-" + p.aMablFmi3InstanceAPI.getOwner().getName()) .collect(toSet()); @@ -905,7 +910,7 @@ public void set(Fmi2Builder.Scope scope, List selectedPorts, // } }); } -// + // /** // * @param ports The ports for which derivative should be set from SHARED derivative ports // * @param scope The builder scope @@ -931,7 +936,7 @@ public void set(Fmi2Builder.Scope scope, List selectedPorts, // } // // return null; -// } catch (XPathExpressionException | IllegalAccessException | InvocationTargetException e) { +// } catch (XPathExpressionException | IllegalAccessException e) { // TODO: InvocationTargetException??? // throw new RuntimeException("Exception occurred when accessing modeldescription: ", e); // } // }).filter(Objects::nonNull).collect(LinkedHashMap::new, (map, item) -> map.put(item.getKey(), item.getValue()), // Accumulator @@ -1007,22 +1012,19 @@ public void set(PortValueMap value) { @Override public void set(Fmi2Builder.Port port, Fmi2Builder.Variable value) { - ; - // this.set(new PortVariableMapImpl(Map.of(port, value))); + this.set(new PortVariableMapImpl(Map.of(port, value))); } @Override public void set(Fmi2Builder.Scope scope, Fmi2Builder.Port port, Fmi2Builder.Variable value) { - ; -// this.set(scope, new PortVariableMapImpl(Map.of(port, value))); + this.set(scope, new PortVariableMapImpl(Map.of(port, value))); } @Override public void set(Fmi2Builder.Port port, Fmi2Builder.Value value) { - ; - // PortValueMap map = new PortValueMapImpl(); - // map.put(port, value); - // set(map); + PortValueMap map = new PortValueMapImpl(); + map.put(port, value); + set(map); } @@ -1107,28 +1109,25 @@ public void setString(Map> } @Override - public int terminate() { - return this.terminate(builder.getDynamicScope()); + public void terminate() { + this.terminate(builder.getDynamicScope()); } - // TODO: Return other values but ok? @Override - public int terminate(Fmi2Builder.Scope scope) { + public void terminate(Fmi2Builder.Scope scope) { PStm stm = stateTransitionFunction(FmiFunctionType.TERMINATE); scope.add(stm); if (builder.getSettings().fmiErrorHandlingEnabled) { FmiStatusErrorHandlingBuilder.generate(builder, "terminate", this, (IMablScope) scope, MablApiBuilder.Fmi3Status.FMI_ERROR, MablApiBuilder.Fmi3Status.FMI_FATAL); } - return Fmi3Status.OK.value; } + // TODO: these are work in progress public void freeInstance() { this.freeInstance(builder.getDynamicScope()); } - public void freeInstance(Fmi2Builder.Scope scope) { - // TODO: add fault checks??? scope.add(MableAstFactory.newExpressionStm(call(getReferenceExp().clone(), "freeInstance"))); } diff --git a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/PortVariableMapImpl.java b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/PortVariableMapImpl.java index 27c0cca9a..ca4643378 100644 --- a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/PortVariableMapImpl.java +++ b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/PortVariableMapImpl.java @@ -6,7 +6,7 @@ import java.util.HashMap; import java.util.Map; -public class PortVariableMapImpl extends HashMap> implements Fmi2Builder.Fmi2ComponentVariable.PortVariableMap { +public class PortVariableMapImpl extends HashMap> implements Fmi2Builder.Fmi2ComponentVariable.PortVariableMap, Fmi2Builder.Fmi3InstanceVariable.PortVariableMap { public PortVariableMapImpl(int initialCapacity, float loadFactor) { super(initialCapacity, loadFactor); } diff --git a/maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java b/maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java index 0204077dc..7d46ff097 100644 --- a/maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java +++ b/maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java @@ -31,6 +31,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; +import java.lang.reflect.Array; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.Collections; @@ -78,7 +79,7 @@ public void wt() throws Exception { URI ballUri = new File("target/Fmi3ModuleReferenceFmusTest/cache/BouncingBall.fmu").getAbsoluteFile().toURI(); Fmu3 ball = new Fmu3(new File(ballUri)); - ArrayVariableFmi2Api varArray = builder.getDynamicScope().store("varArray", new Long[] {1L}); + ArrayVariableFmi2Api requiredIntermediateVariables = builder.getDynamicScope().store("requiredIntermediateVariables", new Long[] {1L}); Fmi3ModelDescription md3Ball = new Fmi3ModelDescription(ball.getModelDescription()); @@ -89,15 +90,20 @@ public void wt() throws Exception { boolean eventModeUsed = true; boolean earlyReturnAllowed = true; InstanceVariableFmi3Api ballInstance = ballFmu.instantiate("ballInstance", visible, loggingOn, eventModeUsed, earlyReturnAllowed, - varArray); + requiredIntermediateVariables); - int res = ballInstance.enterInitializationMode(false, 0.0, 0.0, true,10.0); - res = ballInstance.exitInitializationMode(); - res = ballInstance.terminate(); - ballInstance.freeInstance(); + ballInstance.enterInitializationMode(false, 0.0, 0.0, true,10.0); + ballInstance.exitInitializationMode(); +// ArrayVariableFmi2Api svs = builder.getDynamicScope().store("svs", new Long[] {5L, 6L}); +// ArrayVariableFmi2Api values_r = builder.getDynamicScope().store("values_r", new Double[] {-9.81, 0.7}); + ballInstance.terminate(); + + +// ballInstance.freeInstance(); +