Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/nkj/fmi3' into fmi3
Browse files Browse the repository at this point in the history
# Conflicts:
#	frameworks/builder/src/main/java/org/intocps/maestro/framework/fmi2/api/FmiBuilder.java
#	frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueExpresssionMapImpl.java
#	frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/values/PortValueMapImpl.java
#	frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/FmuVariableFmi3Api.java
#	frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/InstanceVariableFmi3Api.java
#	frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/PortVariableMapImpl.java
#	frameworks/fmi2api/src/main/java/org/intocps/maestro/framework/fmi2/api/mabl/variables/StateMablVariableFmi3Api.java
#	maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java
  • Loading branch information
lausdahl committed Oct 5, 2023
2 parents c85f4ee + b0a39fc commit 80bd6bb
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ void setupExperiment(Scope<T> scope, DoubleVariable<T> startTime, DoubleVariable

void setupExperiment(Scope<T> scope, double startTime, Double endTime, Double tolerance);

void enterInitializationMode(Scope<T> scope, boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined,
void enterInitializationMode(Scope<T> scope,boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined,
double stopTime);

void exitInitializationMode(Scope<T> scope);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MablApiBuilder implements FmiBuilder<PStm, ASimulationSpecification
private final IntVariableFmi2Api globalFmiStatus;
private final MablToMablAPI mablToMablAPI;
private final MablSettings settings;
private final Map<FmiStatus, IntVariableFmi2Api> fmiStatusVariables;
private final Map<FmiStatusInterface, IntVariableFmi2Api> fmiStatusVariables;
private final Set<String> externalLoadedModuleIdentifier = new HashSet<>();
int dynamicScopeInitialSize;
List<String> importedModules = new Vector<>();
Expand Down Expand Up @@ -140,76 +140,32 @@ public MablSettings getSettings() {
return this.settings;
}

public IntVariableFmi2Api getFmiStatusConstant(FmiStatus status) {
//if (!settings.fmiErrorHandlingEnabled) {
// throw new IllegalStateException("Fmi error handling feature not enabled");
// }
interface FmiStatusInterface {
int getValue();

if (!this.fmiStatusVariables.containsKey(status)) {
switch (status) {
case FMI_OK: {
IntVariableFmi2Api var = rootScope.store(status.name(), FmiStatus.FMI_OK.getValue());
//relocate to top of scope
rootScope.addAfterOrTop(null, var.getDeclaringStm());
fmiStatusVariables.put(FmiStatus.FMI_OK, var);
}
break;
case FMI_WARNING: {
IntVariableFmi2Api var = rootScope.store(status.name(), FmiStatus.FMI_WARNING.getValue());
//relocate to top of scope
rootScope.addAfterOrTop(null, var.getDeclaringStm());
fmiStatusVariables.put(FmiStatus.FMI_WARNING, var);
break;
}
case FMI_DISCARD: {
IntVariableFmi2Api var = rootScope.store(status.name(), FmiStatus.FMI_DISCARD.getValue());
//relocate to top of scope
rootScope.addAfterOrTop(null, var.getDeclaringStm());
fmiStatusVariables.put(FmiStatus.FMI_DISCARD, var);
}
break;
case FMI_ERROR: {
IntVariableFmi2Api var = storeStatusVariable(rootScope, status.name(), FmiStatus.FMI_ERROR.getValue());
//IntVariableFmi2Api var = rootScope.store(status.name(), FmiStatus.FMI_ERROR.getValue());
//relocate to top of scope
rootScope.addAfterOrTop(null, var.getDeclaringStm());
fmiStatusVariables.put(FmiStatus.FMI_ERROR, var);
break;
}
case FMI_FATAL: {
IntVariableFmi2Api var = storeStatusVariable(rootScope, status.name(), FmiStatus.FMI_FATAL.getValue());
//relocate to top of scope
rootScope.addAfterOrTop(null, var.getDeclaringStm());
fmiStatusVariables.put(FmiStatus.FMI_FATAL, var);
break;
}
case FMI_PENDING: {
IntVariableFmi2Api var = rootScope.store(status.name(), FmiStatus.FMI_PENDING.getValue());
//relocate to top of scope
rootScope.addAfterOrTop(null, var.getDeclaringStm());
fmiStatusVariables.put(FmiStatus.FMI_PENDING, var);
break;
}
}
String getName();
}

private IntVariableFmi2Api getFmiStatusConstant_aux(FmiStatusInterface status) {
if(!this.fmiStatusVariables.containsKey(status)) {
IntVariableFmi2Api var = rootScope.store(status.getName(), status.getValue());
rootScope.addAfterOrTop(null, var.getDeclaringStm());
fmiStatusVariables.put(status, var);
}

return this.fmiStatusVariables.get(status);
}

public IntVariableFmi2Api getFmiStatusConstant(FmiStatus status) {return getFmiStatusConstant_aux(status);}
public IntVariableFmi2Api getFmiStatusConstant(Fmi3Status status) {return getFmiStatusConstant_aux(status);}

public MablToMablAPI getMablToMablAPI() {
return this.mablToMablAPI;
}


public IntVariableFmi2Api getGlobalFmiStatus() {
return globalFmiStatus;
}

private IntVariableFmi2Api storeStatusVariable(ScopeFmi2Api rootScope, String name, int errorCode) {
return rootScope.store(() -> this.getNameGenerator().getNameIgnoreCase(name), errorCode);
}

@SuppressWarnings("rawtypes")
private Variable createVariable(IMablScope scope, PType type, PExp initialValue, String... prefixes) {
String name = nameGenerator.getName(prefixes);
Expand Down Expand Up @@ -632,7 +588,7 @@ <T> T load(String moduleType, Function<FmiBuilder.RuntimeModule<PStm>, T> creato
return m;
}

public enum FmiStatus {
public enum FmiStatus implements FmiStatusInterface {
FMI_OK(0),
FMI_WARNING(1),
FMI_DISCARD(2),
Expand All @@ -645,11 +601,41 @@ public enum FmiStatus {
private FmiStatus(final int value) {
this.value = value;
}
public int getValue() { return this.value; }

// Interface method, not sure how to best preserve enum name() method.
public String getName() {
for (FmiStatus status : FmiStatus.values()) {
if (status.value == this.value) {
return status.name();
}
}
return null;
}
}

public enum Fmi3Status implements FmiStatusInterface {
FMI_OK(0),
FMI_WARNING(1),
FMI_DISCARD(2),
FMI_ERROR(3),
FMI_FATAL(4);
private final int value;
private Fmi3Status(final int value) {
this.value = value;
}
public int getValue() {
return this.value;
}

public String getName() {
for (FmiStatus status : FmiStatus.values()) {
if (status.value == this.value) {
return status.name();
}
}
return null;
}
}

public static class MablSettings {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void registerComponentVariableFmi2Api(ComponentVariableFmi2Api componentV

@Override
public void registerInstanceVariableFmi3Api(InstanceVariableFmi3Api instanceVariableFmi3Api) {
// TODO stuff goes here.
activeScope.registerInstanceVariableFmi3Api((instanceVariableFmi3Api));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ public IntVariableFmi2Api store(String name, int value) {
return store(() -> builder.getNameGenerator().getName(name), value);
}


@Override
public <V> ArrayVariableFmi2Api<V> store(String name, V[] value) {
return store(() -> builder.getNameGenerator().getName(name), value);
}


public DoubleVariableFmi2Api store(Supplier<String> nameProvider, double value) {
String name = nameProvider.get();
ARealLiteralExp initial = newARealLiteralExp(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class FmuVariableFmi3Api extends VariableFmi2Api<FmiBuilder.NamedVariable
private final MablApiBuilder builder;
private String fmuIdentifier;

//
public FmuVariableFmi3Api(String fmuIdentifier, MablApiBuilder builder, ModelDescriptionContext3 modelDescriptionContext, PStm declaration,
PType type, IMablScope declaredScope, FmiBuilder.DynamicActiveScope<PStm> dynamicScope, PStateDesignator designator, PExp referenceExp) {
this(builder, modelDescriptionContext, declaration, type, declaredScope, dynamicScope, designator, referenceExp);
Expand All @@ -44,34 +43,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) {
IMablScope scope = builder.getDynamicScope().getActiveScope();
return instantiate(name, scope.findParentScope(TryMaBlScope.class), scope, variables);
}
public InstanceVariableFmi3Api instantiate(String name, FmiBuilder.TryScope<PStm> enclosingTryScope, FmiBuilder.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 namePrefix, FmiBuilder.TryScope<PStm> enclosingTryScope, FmiBuilder.Scope<PStm> scope,
String environmentName, ArrayVariableFmi2Api variables) {
return instantiate(namePrefix, enclosingTryScope, scope, environmentName, true, variables);
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), builder.getDynamicScope(), environmentName, visible, loggingOn,
eventModeUsed, earlyReturnAllowed, requiredIntermediateVariables);
}


public InstanceVariableFmi3Api instantiate(String namePrefix, FmiBuilder.TryScope<PStm> enclosingTryScope, FmiBuilder.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 @@ -81,45 +86,41 @@ public InstanceVariableFmi3Api instantiate(String namePrefix, FmiBuilder.TryScop
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;
return instanceVar;
}

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

public String getFmuIdentifier() {
return fmuIdentifier;
Expand Down
Loading

0 comments on commit 80bd6bb

Please sign in to comment.