Skip to content

Commit

Permalink
Can now pass booleans to instantiateCoSimulation properly, small tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: Nestor <[email protected]>
  • Loading branch information
TheRealNestor committed Sep 14, 2023
1 parent fe86ff0 commit e070a2e
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -786,9 +786,9 @@ int enterInitializationMode(Scope<T> scope,boolean toleranceDefined, double tole

int terminate();

// void freeInstance();
//
// void freeInstance(Scope<T> scope);
void freeInstance();

void freeInstance(Scope<T> scope);

/**
* @param scope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class FmuVariableFmi3Api extends VariableFmi2Api<Fmi2Builder.NamedVariabl
private final ModelDescriptionContext3 modelDescriptionContext;
private final MablApiBuilder builder;
private String fmuIdentifier;
//
public FmuVariableFmi3Api(String fmuIdentifier, MablApiBuilder builder, ModelDescriptionContext3 modelDescriptionContext, PStm declaration,
PType type, IMablScope declaredScope, Fmi2Builder.DynamicActiveScope<PStm> dynamicScope, PStateDesignator designator, PExp referenceExp) {
this(builder, modelDescriptionContext, declaration, type, declaredScope, dynamicScope, designator, referenceExp);
Expand All @@ -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<PStm> enclosingTryScope, Fmi2Builder.Scope<PStm> 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<PStm> enclosingTryScope, Fmi2Builder.Scope<PStm> scope,
String environmentName, ArrayVariableFmi2Api variables) {
return instantiate(namePrefix, enclosingTryScope, scope, environmentName, true, variables );
}
public InstanceVariableFmi3Api instantiate(String namePrefix, Fmi2Builder.TryScope<PStm> enclosingTryScope, Fmi2Builder.Scope<PStm> 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");
Expand All @@ -85,44 +91,43 @@ 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(
newAStringLiteralExp(String.format("Instantiate failed on fmu: '%s' for instance: '%s'", this.getFmuIdentifier(), namePrefix))));
thenScope.leave();
}

((IMablScope) scope).registerInstanceVariableFmi3Api(compVar);
((IMablScope) scope).registerInstanceVariableFmi3Api(instanceVar);

return compVar;
}
public InstanceVariableFmi3Api instantiate(String namePrefix, Fmi2Builder.TryScope<PStm> enclosingTryScope, Fmi2Builder.Scope<PStm> scope, ArrayVariableFmi2Api variables) {
return instantiate(namePrefix, enclosingTryScope, scope, null, variables);
return instanceVar;
}



public String getFmuIdentifier() {
return fmuIdentifier;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1123,17 +1123,15 @@ public int terminate(Fmi2Builder.Scope<PStm> scope) {
return Fmi3Status.OK.value;
}

// public void freeInstance() {
// this.terminate(builder.getDynamicScope());
// }
//
// public void freeInstance(Fmi2Builder.Scope<PStm> 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<PStm> 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.
Expand Down
13 changes: 11 additions & 2 deletions maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();







Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ import Logger;
{
unload(x1);
}
}
}

0 comments on commit e070a2e

Please sign in to comment.