Skip to content

Commit

Permalink
terminate, enter/exit initializationMode
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 3237670 commit fe86ff0
Show file tree
Hide file tree
Showing 9 changed files with 377 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,235 @@ interface PortExpressionValueMap extends Map<Port, ExpressionValue> {
}
}

/**
* Interface for an fmi instance.
* <p>
* Note that all methods that do not take a scope uses the builders dynamic scope and adds the underlying instructions int he active scope.
*/
interface Fmi3InstanceVariable<T> extends Variable<T, NamedVariable<T>> {

void setDebugLogging(List<String> categories, boolean enableLogging);

void setupExperiment(DoubleVariable<T> startTime, DoubleVariable<T> endTime, BoolVariable<T> endTimeDefined, Double tolerance);

void setupExperiment(double startTime, Double endTime, Double tolerance);

int enterInitializationMode(boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime);

int exitInitializationMode();

void setupExperiment(Scope<T> scope, DoubleVariable<T> startTime, DoubleVariable<T> endTime, BoolVariable<T> endTimeDefined,
Double tolerance);

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

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

int exitInitializationMode(Scope<T> scope);

int terminate(Scope<T> scope);

int terminate();

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

/**
* @param scope
* @param currentCommunicationPoint
* @param communicationStepSize
* @param noSetFMUStatePriorToCurrentPoint a pair representing (full step completed, current time after step)
* @return
*/
Map.Entry<BoolVariable<T>, DoubleVariable<T>> step(Scope<T> scope, DoubleVariable<T> currentCommunicationPoint,
DoubleVariable<T> communicationStepSize, BoolVariable<T> noSetFMUStatePriorToCurrentPoint);

Map.Entry<BoolVariable<T>, DoubleVariable<T>> step(Scope<T> scope, DoubleVariable<T> currentCommunicationPoint,
DoubleVariable<T> communicationStepSize);

Map.Entry<BoolVariable<T>, DoubleVariable<T>> step(DoubleVariable<T> currentCommunicationPoint, DoubleVariable<T> communicationStepSize,
BoolVariable<T> noSetFMUStatePriorToCurrentPoint);

Map.Entry<BoolVariable<T>, DoubleVariable<T>> step(DoubleVariable<T> currentCommunicationPoint, DoubleVariable<T> communicationStepSize);


List<? extends Port> getPorts();

/**
* Get ports by name
*
* @param names
* @return
*/
List<? extends Port> getPorts(String... names);

/**
* Get ports by ref val
*
* @param valueReferences
* @return
*/
List<? extends Port> getPorts(int... valueReferences);

/**
* Get port by name
*
* @param name
* @return
*/
Port getPort(String name);

/**
* Get port by ref val
*
* @param valueReference
* @return
*/
Port getPort(int valueReference);

/**
* Get port values aka fmiGet
*
* @param ports
* @return
*/
<V> Map<? extends Port, ? extends Variable<T, V>> get(Port... ports);

<V> Map<? extends Port, ? extends Variable<T, V>> get(Scope<T> scope, Port... ports);

/**
* Get all (linked) port values
*
* @return
*/
<V> Map<? extends Port, ? extends Variable<T, V>> get();

/**
* get filter by value reference
*
* @param valueReferences
* @return
*/
<V> Map<? extends Port, ? extends Variable<T, V>> get(int... valueReferences);

/**
* Get filter by names
*
* @param names
* @return
*/
<V> Map<? extends Port, ? extends Variable<T, V>> get(String... names);

<V> Map<? extends Port, ? extends Variable<T, V>> getAndShare(String... names);

<V> Map<? extends Port, ? extends Variable<T, V>> getAndShare(Port... ports);

<V> Map<? extends Port, ? extends Variable<T, V>> getAndShare();

<V> Variable<T, V> getShared(String name);

<V> Variable<T, V> getShared(Port port);

/**
* Get the value of a single port
*
* @param name
* @return
*/
<V> Variable<T, V> getSingle(String name);

<V> Variable<T, V> getSingle(Port port);

<V> void set(Scope<T> scope, PortValueMap<V> value);


<V> void set(Scope<T> scope, PortVariableMap<T, V> value);

/**
* Set port values (if ports is not from this fmu then the links are used to remap)
*
* @param value
*/
<V> void set(PortValueMap<V> value);

<V> void set(Port port, Value<V> value);

<V> void set(Port port, Variable<T, V> value);

<V> void set(Scope<T> scope, Port port, Variable<T, V> value);

<V> void set(PortVariableMap<T, V> value);

/**
* Set this fmu port by name and link
*/
void setLinked(Scope<T> scope, Port... filterPorts);

void setLinked();

void setLinked(Port... filterPorts);

void setLinked(String... filterNames);

void setLinked(long... filterValueReferences);

/**
* Set this fmu ports by val ref
*
* @param values
*/
void setInt(Map<? extends Integer, ? extends Value<Integer>> values);

/**
* Set this fmy ports by name
*
* @param value
*/
void setString(Map<? extends String, ? extends Value<String>> value);

/**
* Makes the values publicly available to all linked connections. On next set these ports will be resolved to the values given for
* other fmu
*
* @param values
*/
<V> void share(Map<? extends Port, ? extends Variable<T, V>> values);

/**
* Makes the value publicly available to all linked connections. On next set these ports will be resolved to the values given for
* other fmu
*
* @param value
*/
<V> void share(Port port, Variable<T, V> value);

/**
* Get the current state
*
* @return
*/
StateVariable<T> getState() throws XPathExpressionException;

/**
* Get the current state
*
* @return
*/
StateVariable<T> getState(Scope<T> scope) throws XPathExpressionException;


interface PortVariableMap<S, V> extends Map<Port, Variable<S, V>> {
}

interface PortValueMap<V> extends Map<Port, Value<V>> {
}

interface PortExpressionValueMap extends Map<Port, ExpressionValue> {
}
}

interface Variable<T, V> {
String getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MablApiBuilder implements Fmi2Builder<PStm, ASimulationSpecificatio
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<Fmi2Builder.RuntimeModule<PStm>, T> creat
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
Loading

0 comments on commit fe86ff0

Please sign in to comment.