From e070a2eb841e0a048e99f7c240c756b45b63d48a Mon Sep 17 00:00:00 2001 From: Nestor Date: Thu, 14 Sep 2023 14:08:21 +0200 Subject: [PATCH] Can now pass booleans to instantiateCoSimulation properly, small tweaks Signed-off-by: Nestor --- .../framework/fmi2/api/Fmi2Builder.java | 6 +- .../mabl/variables/FmuVariableFmi3Api.java | 59 ++++++++++--------- .../variables/InstanceVariableFmi3Api.java | 20 +++---- .../org/intocps/maestro/BuilderFmi3Test.java | 13 +++- .../fmi3/reference/bounchingball/fmi3_bb.mabl | 2 +- 5 files changed, 56 insertions(+), 44 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 f6d78fcd..fb3270d9 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 @@ -786,9 +786,9 @@ int enterInitializationMode(Scope scope,boolean toleranceDefined, double tole int terminate(); -// void freeInstance(); -// -// void freeInstance(Scope scope); + void freeInstance(); + + void freeInstance(Scope scope); /** * @param scope diff --git a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/FmuVariableFmi3Api.java b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/FmuVariableFmi3Api.java index ddb6d3c4..26743d3c 100644 --- a/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/FmuVariableFmi3Api.java +++ b/frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/FmuVariableFmi3Api.java @@ -30,7 +30,6 @@ public class FmuVariableFmi3Api extends VariableFmi2Api dynamicScope, PStateDesignator designator, PExp referenceExp) { this(builder, modelDescriptionContext, declaration, type, declaredScope, dynamicScope, designator, referenceExp); @@ -49,33 +48,40 @@ public ModelDescriptionContext3 getModelDescriptionContext() { return modelDescriptionContext; } -// @Override - public InstanceVariableFmi3Api instantiate(String name, String environmentName, ArrayVariableFmi2Api variables) { + public InstanceVariableFmi3Api instantiate(String name, boolean visible, boolean loggingOn, boolean eventModeUsed, + boolean earlyReturnAllowed, ArrayVariableFmi2Api requiredIntermediateVariables) { IMablScope scope = builder.getDynamicScope().getActiveScope(); - return instantiate(name, scope.findParentScope(TryMaBlScope.class), scope, environmentName, variables); + return instantiate(name, scope.findParentScope(TryMaBlScope.class), scope, visible, loggingOn, + eventModeUsed, earlyReturnAllowed, requiredIntermediateVariables); } - public InstanceVariableFmi3Api instantiate(String name, ArrayVariableFmi2Api variables) { + public InstanceVariableFmi3Api instantiate(String name, Fmi2Builder.TryScope enclosingTryScope, Fmi2Builder.Scope scope, + boolean visible, boolean loggingOn, boolean eventModeUsed, boolean earlyReturnAllowed, + ArrayVariableFmi2Api requiredIntermediateVariables) { + return instantiate(name, enclosingTryScope, scope, null, visible, loggingOn, eventModeUsed, + earlyReturnAllowed, requiredIntermediateVariables); + + } + + public InstanceVariableFmi3Api instantiate(String name, String environmentName, boolean visible, boolean loggingOn, boolean eventModeUsed, + boolean earlyReturnAllowed, ArrayVariableFmi2Api requiredIntermediateVariables) { IMablScope scope = builder.getDynamicScope().getActiveScope(); - return instantiate(name, scope.findParentScope(TryMaBlScope.class), scope, variables); + return instantiate(name, scope.findParentScope(TryMaBlScope.class), builder.getDynamicScope(), environmentName, + visible, loggingOn, eventModeUsed, earlyReturnAllowed, requiredIntermediateVariables); } public InstanceVariableFmi3Api instantiate(String namePrefix, Fmi2Builder.TryScope enclosingTryScope, Fmi2Builder.Scope scope, - String environmentName, ArrayVariableFmi2Api variables) { - return instantiate(namePrefix, enclosingTryScope, scope, environmentName, true, variables ); - } - public InstanceVariableFmi3Api instantiate(String namePrefix, Fmi2Builder.TryScope enclosingTryScope, Fmi2Builder.Scope scope, - String environmentName, boolean loggingOn, ArrayVariableFmi2Api variables) { + String environmentName, boolean visible, boolean loggingOn, boolean eventModeUsed, boolean earlyReturnAllowed, + ArrayVariableFmi2Api requiredIntermediateVariables) { String name = builder.getNameGenerator().getName(namePrefix); - //TODO: Extract bool visible and bool loggingOn from configuration var var = newVariable(name, newANameType("FMI3Instance"), newNullExp()); PStm instantiateAssign = newAAssignmentStm(newAIdentifierStateDesignator(name), - call(getReferenceExp().clone(), "instantiateCoSimulation", newAStringLiteralExp(name), newABoolLiteralExp(true), - newABoolLiteralExp(loggingOn), newABoolLiteralExp(true), newABoolLiteralExp(true), - variables.getReferenceExp().clone())); + call(getReferenceExp().clone(), "instantiateCoSimulation", newAStringLiteralExp(name), newABoolLiteralExp(visible), + newABoolLiteralExp(loggingOn), newABoolLiteralExp(eventModeUsed), newABoolLiteralExp(earlyReturnAllowed), + requiredIntermediateVariables.getReferenceExp().clone())); if (enclosingTryScope == null) { throw new IllegalArgumentException("Call to instantiate is not allowed with a null enclosing try scope"); @@ -85,29 +91,29 @@ public InstanceVariableFmi3Api instantiate(String namePrefix, Fmi2Builder.TrySco TryMaBlScope mTryScope = (TryMaBlScope) enclosingTryScope; mTryScope.parent().addBefore(mTryScope.getDeclaration(), var); - InstanceVariableFmi3Api compVar; + InstanceVariableFmi3Api instanceVar; if (environmentName == null) { - compVar = new InstanceVariableFmi3Api(var, this, name, this.modelDescriptionContext, builder, mTryScope.parent(), + instanceVar = new InstanceVariableFmi3Api(var, this, name, this.modelDescriptionContext, builder, mTryScope.parent(), newAIdentifierStateDesignator(newAIdentifier(name)), newAIdentifierExp(name)); } else { AInstanceMappingStm mapping = newAInstanceMappingStm(newAIdentifier(name), environmentName); - compVar = new InstanceVariableFmi3Api(var, this, name, this.modelDescriptionContext, builder, mTryScope.parent(), + instanceVar = new InstanceVariableFmi3Api(var, this, name, this.modelDescriptionContext, builder, mTryScope.parent(), newAIdentifierStateDesignator(newAIdentifier(name)), newAIdentifierExp(name), environmentName); scope.add(mapping); } scope.add(instantiateAssign); - mTryScope.getFinallyBody().addAfterOrTop(null, newIf(newNotEqual(compVar.getReferenceExp().clone(), newNullExp()), - newABlockStm(MableAstFactory.newExpressionStm(call(getReferenceExp().clone(), "freeInstance", compVar.getReferenceExp().clone())), - newAAssignmentStm(compVar.getDesignatorClone(), newNullExp())), null)); + mTryScope.getFinallyBody().addAfterOrTop(null, newIf(newNotEqual(instanceVar.getReferenceExp().clone(), newNullExp()), + newABlockStm(MableAstFactory.newExpressionStm(call(getReferenceExp().clone(), "freeInstance", instanceVar.getReferenceExp().clone())), + newAAssignmentStm(instanceVar.getDesignatorClone(), newNullExp())), null)); scope.activate(); if (builder.getSettings().fmiErrorHandlingEnabled) { ScopeFmi2Api thenScope = - (ScopeFmi2Api) scope.enterIf(new PredicateFmi2Api(newEqual(compVar.getReferenceExp().clone(), newNullExp()))).enterThen(); + (ScopeFmi2Api) scope.enterIf(new PredicateFmi2Api(newEqual(instanceVar.getReferenceExp().clone(), newNullExp()))).enterThen(); builder.getLogger().error(thenScope, "Instantiate failed on fmu: '%s' for instance: '%s'", this.getFmuIdentifier(), namePrefix); thenScope.add(new AErrorStm( @@ -115,14 +121,13 @@ public InstanceVariableFmi3Api instantiate(String namePrefix, Fmi2Builder.TrySco thenScope.leave(); } - ((IMablScope) scope).registerInstanceVariableFmi3Api(compVar); + ((IMablScope) scope).registerInstanceVariableFmi3Api(instanceVar); - return compVar; - } - public InstanceVariableFmi3Api instantiate(String namePrefix, Fmi2Builder.TryScope enclosingTryScope, Fmi2Builder.Scope scope, ArrayVariableFmi2Api variables) { - return instantiate(namePrefix, enclosingTryScope, scope, null, variables); + return instanceVar; } + + public String getFmuIdentifier() { return fmuIdentifier; } 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 8fe16bc1..b0e16ee3 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 @@ -1123,17 +1123,15 @@ public int terminate(Fmi2Builder.Scope scope) { return Fmi3Status.OK.value; } -// public void freeInstance() { -// this.terminate(builder.getDynamicScope()); -// } -// -// public void freeInstance(Fmi2Builder.Scope scope) { -// scope.add(newABlockStm(MableAstFactory.newExpressionStm(call(getReferenceExp().clone(), "fmi3FreeInstance")))) -// scope.add(newIf(newNotEqual(((InstanceVariableFmi3Api) inst).getReferenceExp().clone(), newNullExp()), newABlockStm( -// MableAstFactory.newExpressionStm( -// call(getReferenceExp().clone(), "freeInstance", ((InstanceVariableFmi3Api) inst).getReferenceExp().clone())), -// newAAssignmentStm(((InstanceVariableFmi3Api) inst).getDesignatorClone(), newNullExp())), null)); -// } + 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"))); + } + // TODO: implement this from the other share function below. This for now to build. diff --git a/maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java b/maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java index 66ea38c5..0204077d 100644 --- a/maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java +++ b/maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java @@ -84,12 +84,21 @@ public void wt() throws Exception { FmuVariableFmi3Api ballFmu = builder.getDynamicScope().createFMU("ball", md3Ball, ballUri); - InstanceVariableFmi3Api ballInstance = ballFmu.instantiate("ballInstance", varArray); + boolean visible = true; + boolean loggingOn = true; + boolean eventModeUsed = true; + boolean earlyReturnAllowed = true; + InstanceVariableFmi3Api ballInstance = ballFmu.instantiate("ballInstance", visible, loggingOn, eventModeUsed, earlyReturnAllowed, + varArray); int res = ballInstance.enterInitializationMode(false, 0.0, 0.0, true,10.0); res = ballInstance.exitInitializationMode(); - res = ballInstance.terminate(); + ballInstance.freeInstance(); + + + + diff --git a/maestro/src/test/resources/fmi3/reference/bounchingball/fmi3_bb.mabl b/maestro/src/test/resources/fmi3/reference/bounchingball/fmi3_bb.mabl index 0a97e116..46601503 100644 --- a/maestro/src/test/resources/fmi3/reference/bounchingball/fmi3_bb.mabl +++ b/maestro/src/test/resources/fmi3/reference/bounchingball/fmi3_bb.mabl @@ -106,4 +106,4 @@ import Logger; { unload(x1); } -} \ No newline at end of file +}