Skip to content

Commit

Permalink
updated fmi3
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed Nov 19, 2023
1 parent 9d1c0b7 commit 703c5c0
Show file tree
Hide file tree
Showing 12 changed files with 818 additions and 390 deletions.
6 changes: 6 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public boolean isNative() {
return nativeType != null;
}

static public enum Type {
public enum Type {
Void,
Int,
UInt,
Expand Down Expand Up @@ -254,7 +254,7 @@ interface Scope<AST> extends Scoping<AST> {

IntVariable<AST> store(String name, int value);

<CV> ArrayVariable<AST, CV> store(String name, CV value[]);
<CV> ArrayVariable<AST, CV> store(String name, CV[] value);

/**
* Store the given value and get a tag for it. Copy
Expand Down Expand Up @@ -399,7 +399,7 @@ interface Port<PORT_SCALAR_TYPE, AST> {
*
* @param receiver
*/
void linkTo(Port< PORT_SCALAR_TYPE,AST>... receiver) throws PortLinkException;
void linkTo(Port<PORT_SCALAR_TYPE, AST>... receiver) throws PortLinkException;

/**
* Break the source link
Expand Down Expand Up @@ -529,10 +529,10 @@ interface Fmu2Variable<AST, PORT_SCALAR_TYPE> extends Variable<AST, NamedVariabl
// }
// }
Fmi2ComponentVariable<AST, PORT_SCALAR_TYPE> instantiate(String namePrefix, TryScope<PStm> enclosingTryScope, Scope<PStm> scope,
String environmentName);
String environmentName);

Fmi2ComponentVariable<AST, PORT_SCALAR_TYPE> instantiate(String namePrefix, FmiBuilder.TryScope<PStm> enclosingTryScope,
FmiBuilder.Scope<PStm> scope, String environmentName, boolean loggingOn);
FmiBuilder.Scope<PStm> scope, String environmentName, boolean loggingOn);

Fmi2ComponentVariable<AST, PORT_SCALAR_TYPE> instantiate(String name, TryScope<AST> enclosingTryScope, Scope<AST> scope);

Expand Down Expand Up @@ -572,26 +572,6 @@ interface FmiSimulationInstance<AST, PORT_SCALAR_TYPE> extends SimulationInstanc
void setDebugLogging(List<String> categories, boolean enableLogging);


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

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

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

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


List<? extends Port<PORT_SCALAR_TYPE, AST>> getPorts();

/**
Expand Down Expand Up @@ -682,7 +662,7 @@ Map.Entry<BoolVariable<AST>, DoubleVariable<AST>> step(DoubleVariable<AST> curre
<V> void set(Scope<AST> scope, PortValueMap<V, PORT_SCALAR_TYPE, AST> value);


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

/**
* Set port values (if ports is not from this fmu then the links are used to remap)
Expand All @@ -697,7 +677,7 @@ Map.Entry<BoolVariable<AST>, DoubleVariable<AST>> step(DoubleVariable<AST> curre

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

<V> void set(PortVariableMap< V, PORT_SCALAR_TYPE, AST> value);
<V> void set(PortVariableMap<V, PORT_SCALAR_TYPE, AST> value);

/**
* Set this fmu port by name and link
Expand Down Expand Up @@ -756,7 +736,7 @@ Map.Entry<BoolVariable<AST>, DoubleVariable<AST>> step(DoubleVariable<AST> curre
*/
StateVariable<AST> getState(Scope<AST> scope) throws XPathExpressionException;

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

interface PortValueMap<V, PORT_SCALAR_TYPE, AST> extends Map<Port<PORT_SCALAR_TYPE, AST>, Value<V>> {
Expand All @@ -769,7 +749,7 @@ interface PortExpressionValueMap<PORT_SCALAR_TYPE, AST> extends Map<Port<PORT_SC
/**
* Simulation instance for FMI3
*
* @param <AST> building block
* @param <AST> building block
* @param <PORT_SCALAR_TYPE> fmi3 scalar variable type
*/
interface Fmi3InstanceVariable<AST, PORT_SCALAR_TYPE> extends FmiSimulationInstance<AST, PORT_SCALAR_TYPE> {
Expand All @@ -782,20 +762,22 @@ interface Fmi3InstanceVariable<AST, PORT_SCALAR_TYPE> extends FmiSimulationInsta
void enterInitializationMode(boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime);

void enterInitializationMode(Scope<AST> scope, FmiBuilder.BoolVariable<PStm> toleranceDefined, FmiBuilder.DoubleVariable<PStm> tolerance,
FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.BoolVariable<PStm> stopTimeDefined, FmiBuilder.DoubleVariable<PStm> stopTime);
FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.BoolVariable<PStm> stopTimeDefined,
FmiBuilder.DoubleVariable<PStm> stopTime);

void enterInitializationMode(FmiBuilder.BoolVariable<PStm> toleranceDefined, FmiBuilder.DoubleVariable<PStm> tolerance,
FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.BoolVariable<PStm> stopTimeDefined, FmiBuilder.DoubleVariable<PStm> stopTime);
FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.BoolVariable<PStm> stopTimeDefined,
FmiBuilder.DoubleVariable<PStm> stopTime);

void exitInitializationMode();

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

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

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

void exitInitializationMode(Scope<AST> scope);

Expand All @@ -811,7 +793,7 @@ void enterInitializationMode(Scope<AST> scope, boolean toleranceDefined, double
* <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.
*
* @param <AST> building block
* @param <AST> building block
* @param <PORT_SCALAR_TYPE> fmi2 scalar variable type
*/
interface Fmi2ComponentVariable<AST, PORT_SCALAR_TYPE> extends FmiSimulationInstance<AST, PORT_SCALAR_TYPE> {
Expand All @@ -827,7 +809,7 @@ interface Fmi2ComponentVariable<AST, PORT_SCALAR_TYPE> extends FmiSimulationInst
void exitInitializationMode();

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

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

Expand All @@ -838,6 +820,26 @@ void setupExperiment(Scope<AST> scope, DoubleVariable<AST> startTime, DoubleVari
void terminate(Scope<AST> scope);

void terminate();


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

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

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

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

interface Variable<AST, V> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import org.intocps.maestro.framework.fmi2.api.mabl.variables.DoubleVariableFmi2Api;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -51,23 +53,34 @@ public String getModuleIdentifier() {
}

public void unload() {
mablApiBuilder.getDynamicScope().add(newExpressionStm(newUnloadExp(Arrays.asList(getReferenceExp().clone()))));
mablApiBuilder.getDynamicScope().add(newExpressionStm(newUnloadExp(
Collections.singletonList(getReferenceExp().clone()))));
}

private PExp getReferenceExp() {
return newAIdentifierExp(moduleIdentifier);
}

// TODO: Only works for shared variables. Fixed this.
public class DataWriterInstance {
public static class DataWriterInstance {
public static class LogEntry {
final String name;
final Supplier<PExp> exp;

public LogEntry(String name, Supplier<PExp> exp) {
this.name = name;
this.exp = exp;
}
}

private final String FUNCTION_WRITEHEADER = "writeHeader";
private final String FUNCTION_WRITEDATAPOINT = "writeDataPoint";
private final String TYPE_DATAWRITERCONFIG = "DataWriterConfig";
private final DynamicActiveBuilderScope dynamicScope;
private final MablApiBuilder mablApiBuilder;
private final DataWriter dataWriter;
private boolean runtimeModuleMode;
private List<PortFmi2Api> portsToLog;
private List<LogEntry> portsToLog;
/**
* A String Array of the names of the variables to log
*/
Expand All @@ -88,49 +101,56 @@ public class DataWriterInstance {
private String dataWriterInstanceConfigurationVariableName;
private FmiBuilder.RuntimeModule<PStm> runtimeModule;

public DataWriterInstance(DynamicActiveBuilderScope dynamicScope, MablApiBuilder mablApiBuilder, DataWriter dataWriter) {
public DataWriterInstance(DynamicActiveBuilderScope dynamicScope, MablApiBuilder mablApiBuilder,
DataWriter dataWriter) {

this.dynamicScope = dynamicScope;
this.mablApiBuilder = mablApiBuilder;
this.dataWriter = dataWriter;
}

public DataWriterInstance(DynamicActiveBuilderScope dynamicScope, MablApiBuilder mablApiBuilder, DataWriter dataWriter,
FmiBuilder.RuntimeModule<PStm> runtimeModule) {
public DataWriterInstance(DynamicActiveBuilderScope dynamicScope, MablApiBuilder mablApiBuilder,
DataWriter dataWriter,
FmiBuilder.RuntimeModule<PStm> runtimeModule) {
this(dynamicScope, mablApiBuilder, dataWriter);
this.runtimeModuleMode = true;
this.runtimeModule = runtimeModule;
}

public void initialize(PortFmi2Api... portsToLog) {
public void initialize(LogEntry... portsToLog) {
this.initialize(Arrays.asList(portsToLog));
}

public void initialize(List<PortFmi2Api> portsToLog) {
public void initialize(List<LogEntry> portsToLog) {

this.portsToLog = portsToLog;

this.logHeadersVariableName = mablApiBuilder.getNameGenerator().getName("datawriter_headers");
this.dataWriterInstanceConfigurationVariableName = mablApiBuilder.getNameGenerator().getName("datawriter_configuration");
this.dataWriterInstanceConfigurationVariableName = mablApiBuilder.getNameGenerator()
.getName("datawriter_configuration");

List<AStringLiteralExp> variablesNamesToLog =
this.portsToLog.stream().map(x -> MableAstFactory.newAStringLiteralExp(x.getMultiModelScalarVariableName()))
this.portsToLog.stream().map(x -> MableAstFactory.newAStringLiteralExp(x.name))
.collect(Collectors.toList());

AVariableDeclaration datawriter_configuration =
MableAstFactory.newAVariableDeclaration(MableAstFactory.newAIdentifier(logHeadersVariableName),
MableAstFactory.newAArrayType(MableAstFactory.newAStringPrimitiveType()), variablesNamesToLog.size(),
MableAstFactory.newAArrayType(MableAstFactory.newAStringPrimitiveType()),
variablesNamesToLog.size(),
MableAstFactory.newAArrayInitializer(variablesNamesToLog));

this.logHeadersStm = MableAstFactory.newALocalVariableStm(datawriter_configuration);


this.writeHeadersStm = MableAstFactory.newALocalVariableStm(
MableAstFactory.newAVariableDeclaration(MableAstFactory.newAIdentifier(this.dataWriterInstanceConfigurationVariableName),
MableAstFactory.newAVariableDeclaration(
MableAstFactory.newAIdentifier(this.dataWriterInstanceConfigurationVariableName),
MableAstFactory.newANameType(TYPE_DATAWRITERCONFIG), MableAstFactory.newAExpInitializer(
MableAstFactory.newACallExp(MableAstFactory.newAIdentifierExp(this.dataWriter.getModuleIdentifier()),
MableAstFactory.newACallExp(
MableAstFactory.newAIdentifierExp(this.dataWriter.getModuleIdentifier()),
MableAstFactory.newAIdentifier(FUNCTION_WRITEHEADER),
Arrays.asList(MableAstFactory.newAIdentifierExp(logHeadersVariableName))))));
List.of(
newAIdentifierExp(logHeadersVariableName))))));

this.mablApiBuilder.getDynamicScope().add(logHeadersStm, writeHeadersStm);

Expand All @@ -144,16 +164,17 @@ public void log(DoubleVariableFmi2Api time) {
AExpressionStm stm = MableAstFactory.newExpressionStm(
MableAstFactory.newACallExp(MableAstFactory.newAIdentifierExp(this.dataWriter.moduleIdentifier),
MableAstFactory.newAIdentifier(this.FUNCTION_WRITEDATAPOINT), Stream.concat(
Arrays.asList(MableAstFactory.newAIdentifierExp(this.dataWriterInstanceConfigurationVariableName),
Arrays.asList(MableAstFactory.newAIdentifierExp(
this.dataWriterInstanceConfigurationVariableName),
time.getReferenceExp().clone()).stream(),
portsToLog.stream().map(x -> x.getSharedAsVariable().getReferenceExp().clone())).collect(Collectors.toList())));
portsToLog.stream().map(x -> x.exp.get().clone())).collect(Collectors.toList())));
this.dynamicScope.add(stm);
}

public void close() {
AExpressionStm stm = MableAstFactory.newExpressionStm(
MableAstFactory.newACallExp(MableAstFactory.newAIdentifierExp(this.dataWriter.moduleIdentifier),
MableAstFactory.newAIdentifier(this.dataWriter.FUNCTION_CLOSE), Arrays.asList()));
MableAstFactory.newAIdentifier(this.dataWriter.FUNCTION_CLOSE), List.of()));
this.dynamicScope.add(stm);
}

Expand Down
Loading

0 comments on commit 703c5c0

Please sign in to comment.