Skip to content

Commit

Permalink
getter/setters work in progress, but for merge
Browse files Browse the repository at this point in the history
Signed-off-by: Nestor <[email protected]>
  • Loading branch information
TheRealNestor committed Oct 5, 2023
1 parent e070a2e commit b0a39fc
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -768,28 +768,29 @@ interface Fmi3InstanceVariable<T> extends Variable<T, NamedVariable<T>> {

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

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

int exitInitializationMode();
void 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,
void enterInitializationMode(Scope<T> scope,boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined,
double stopTime);

int exitInitializationMode(Scope<T> scope);
void exitInitializationMode(Scope<T> scope);

int terminate(Scope<T> scope);
void terminate(Scope<T> scope);

int terminate();
void terminate();

void freeInstance();

void freeInstance(Scope<T> scope);


/**
* @param scope
* @param currentCommunicationPoint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.HashMap;
import java.util.Map;

public class PortValueExpresssionMapImpl extends HashMap<Fmi2Builder.Port, Fmi2Builder.ExpressionValue> implements Fmi2Builder.Fmi2ComponentVariable.PortExpressionValueMap {
public class PortValueExpresssionMapImpl extends HashMap<Fmi2Builder.Port, Fmi2Builder.ExpressionValue> implements Fmi2Builder.Fmi2ComponentVariable.PortExpressionValueMap, Fmi2Builder.Fmi3InstanceVariable.PortExpressionValueMap {
public PortValueExpresssionMapImpl(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.util.HashMap;
import java.util.Map;

public class PortValueMapImpl<V> extends HashMap<Fmi2Builder.Port, Fmi2Builder.Value<V>> implements Fmi2Builder.Fmi2ComponentVariable.PortValueMap<V> {
public class PortValueMapImpl<V> extends HashMap<Fmi2Builder.Port, Fmi2Builder.Value<V>> implements Fmi2Builder.Fmi2ComponentVariable.PortValueMap<V>, Fmi2Builder.Fmi3InstanceVariable.PortValueMap<V> {
public PortValueMapImpl(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.intocps.maestro.ast.analysis.AnalysisException;
import org.intocps.maestro.ast.analysis.DepthFirstAnalysisAdaptor;
import org.intocps.maestro.ast.node.*;
import org.intocps.maestro.fmi.Fmi2ModelDescription;
import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3Causality;
import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3ModelDescription;
import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3TypeEnum;
Expand All @@ -22,7 +21,6 @@
import org.slf4j.LoggerFactory;

import javax.xml.xpath.XPathExpressionException;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
Expand All @@ -39,12 +37,6 @@
@SuppressWarnings("rawtypes")
public class InstanceVariableFmi3Api extends VariableFmi2Api<Fmi2Builder.NamedVariable<PStm>> implements Fmi2Builder.Fmi3InstanceVariable<PStm> {
final static Logger logger = LoggerFactory.getLogger(InstanceVariableFmi3Api.class);
private final static int FMI_OK = 0;
private final static int FMI_WARNING = 1;
private final static int FMI_DISCARD = 2;
private final static int FMI_ERROR = 3;
private final static int FMI_FATAL = 4;
private final static int FMI_PENDING = 5;
private final static int FMI_STATUS_LAST_SUCCESSFUL = 2;
private static final String LOGLEVELS_POSTFIX = "_log_levels";
private final static String CATEGORY_STATUS = "category_status";
Expand Down Expand Up @@ -95,7 +87,6 @@ public InstanceVariableFmi3Api(PStm declaration, FmuVariableFmi3Api parent, Stri
this.environmentName = environmentName;
}

// TODO fmi3 model description.
public Fmi3ModelDescription getModelDescription() {
return modelDescriptionContext.getModelDescription();
}
Expand Down Expand Up @@ -292,13 +283,13 @@ private void setupExperiment(Fmi2Builder.Scope<PStm> scope, PExp startTime, PExp
}

@Override
public int enterInitializationMode(boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime) {
return this.enterInitializationMode(builder.getDynamicScope(), toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime);
public void enterInitializationMode(boolean toleranceDefined, double tolerance, double startTime, boolean stopTimeDefined, double stopTime) {
this.enterInitializationMode(builder.getDynamicScope(), toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime);
}

@Override
public int exitInitializationMode() {
return this.exitInitializationMode(builder.getDynamicScope());
public void exitInitializationMode() {
this.exitInitializationMode(builder.getDynamicScope());
}

@Override
Expand All @@ -315,29 +306,25 @@ public void setupExperiment(Fmi2Builder.Scope<PStm> scope, double startTime, Dou

// TODO: probably should return other things than ok also
@Override
public int enterInitializationMode(Fmi2Builder.Scope<PStm> scope, boolean toleranceDefined, double tolerance, double startTime,
public void enterInitializationMode(Fmi2Builder.Scope<PStm> scope, boolean toleranceDefined, double tolerance, double startTime,
boolean stopTimeDefined, double stopTime) {
PStm stm = stateTransitionFunction(FmiFunctionType.ENTERINITIALIZATIONMODE, toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime );


scope.add(stm);
if (builder.getSettings().fmiErrorHandlingEnabled) {
FmiStatusErrorHandlingBuilder.generate(builder, "enterInitializationMode", this, (IMablScope) scope, MablApiBuilder.Fmi3Status.FMI_ERROR,
MablApiBuilder.Fmi3Status.FMI_FATAL);
}
return Fmi3Status.OK.value;
}

// TODO: Probably return other things than just ok.
@Override
public int exitInitializationMode(Fmi2Builder.Scope<PStm> scope) {
public void exitInitializationMode(Fmi2Builder.Scope<PStm> scope) {
PStm stm = stateTransitionFunction(FmiFunctionType.EXITINITIALIZATIONMODE);
scope.add(stm);
if (builder.getSettings().fmiErrorHandlingEnabled) {
FmiStatusErrorHandlingBuilder.generate(builder, "exitInitializationMode", this, (IMablScope) scope, MablApiBuilder.Fmi3Status.FMI_ERROR,
MablApiBuilder.Fmi3Status.FMI_FATAL);
}
return Fmi3Status.OK.value;
}

@Override
Expand Down Expand Up @@ -541,6 +528,27 @@ public <V> Map<PortFmi3Api, VariableFmi2Api<V>> get(Fmi2Builder.Scope<PStm> scop

PType type;
switch (e.getKey()) {
case Float64Type:
type = new ARealNumericPrimitiveType();
break;
case Float32Type:
type = new AFloatNumericPrimitiveType();
break;
case Int8Type:
type = new AByteNumericPrimitiveType();
break;
case Int16Type:
type = new AShortNumericPrimitiveType();
break;
case Int32Type:
type = new AIntNumericPrimitiveType();
break;
case Int64Type:
type = new ALongNumericPrimitiveType();
break;

// TODO add the rest

// case Boolean:
// type = new ABooleanPrimitiveType();
// break;
Expand All @@ -555,10 +563,8 @@ public <V> Map<PortFmi3Api, VariableFmi2Api<V>> get(Fmi2Builder.Scope<PStm> scop
// break;
// case Enumeration:
// throw new RuntimeException("Cannot assign enumeration port type.");
// default:
// throw new RuntimeException("Cannot match port types.");
default:
type = new AIntNumericPrimitiveType(); // TODO For now for testing
throw new RuntimeException("Cannot match port types.");
}

ArrayVariableFmi2Api<Object> valBuf = getIOBuffer(type);
Expand Down Expand Up @@ -774,14 +780,15 @@ public VariableFmi2Api getSingle(String name) {






public void set(Fmi2Builder.Port p, Fmi2Builder.ExpressionValue v) {
;
// this.set(new PortValueExpresssionMapImpl(Map.of(p, v)));
this.set(new PortValueExpresssionMapImpl(Map.of(p, v)));
}

public void set(Fmi2Builder.Scope<PStm> scope, Fmi2Builder.Port p, Fmi2Builder.ExpressionValue v) {
;
// this.set(scope, new PortValueMapImpl(Map.of(p, v)));
this.set(scope, new PortValueMapImpl(Map.of(p, v)));
}

public void set(PortExpressionValueMap value) {
Expand All @@ -803,8 +810,6 @@ public void set(Fmi2Builder.Scope<PStm> scope, PortExpressionValueMap value) {

@Override
public <V> void set(Fmi2Builder.Scope<PStm> scope, PortValueMap<V> value) {


if (value == null || value.isEmpty()) {
return;
}
Expand All @@ -814,6 +819,7 @@ public <V> void set(Fmi2Builder.Scope<PStm> scope, PortValueMap<V> value) {

set(scope, selectedPorts, port -> {
Object val = (value.get(port)).get();
// TODO: Add the rest
if (val instanceof Double) {
return Map.entry(newARealLiteralExp((Double) val), newRealType());
}
Expand Down Expand Up @@ -849,7 +855,6 @@ public <V> void set(Fmi2Builder.Scope<PStm> scope, PortVariableMap<PStm, V> valu
}

public void set(Fmi2Builder.Scope<PStm> scope, List<PortFmi3Api> selectedPorts, Function<PortFmi3Api, Map.Entry<PExp, PType>> portToValue) {

Set<String> selectedPortsAsStrings = selectedPorts.stream()
.map(p -> p.getName() + "-" + p.aMablFmi3InstanceAPI.getName() + "-" + p.aMablFmi3InstanceAPI.getOwner().getName())
.collect(toSet());
Expand Down Expand Up @@ -905,7 +910,7 @@ public void set(Fmi2Builder.Scope<PStm> scope, List<PortFmi3Api> selectedPorts,
// }
});
}
//

// /**
// * @param ports The ports for which derivative should be set from SHARED derivative ports
// * @param scope The builder scope
Expand All @@ -931,7 +936,7 @@ public void set(Fmi2Builder.Scope<PStm> scope, List<PortFmi3Api> selectedPorts,
// }
//
// return null;
// } catch (XPathExpressionException | IllegalAccessException | InvocationTargetException e) {
// } catch (XPathExpressionException | IllegalAccessException e) { // TODO: InvocationTargetException???
// throw new RuntimeException("Exception occurred when accessing modeldescription: ", e);
// }
// }).filter(Objects::nonNull).collect(LinkedHashMap::new, (map, item) -> map.put(item.getKey(), item.getValue()), // Accumulator
Expand Down Expand Up @@ -1007,22 +1012,19 @@ public <V> void set(PortValueMap<V> value) {

@Override
public <V> void set(Fmi2Builder.Port port, Fmi2Builder.Variable<PStm, V> value) {
;
// this.set(new PortVariableMapImpl(Map.of(port, value)));
this.set(new PortVariableMapImpl(Map.of(port, value)));
}

@Override
public <V> void set(Fmi2Builder.Scope<PStm> scope, Fmi2Builder.Port port, Fmi2Builder.Variable<PStm, V> value) {
;
// this.set(scope, new PortVariableMapImpl(Map.of(port, value)));
this.set(scope, new PortVariableMapImpl(Map.of(port, value)));
}

@Override
public void set(Fmi2Builder.Port port, Fmi2Builder.Value value) {
;
// PortValueMap map = new PortValueMapImpl();
// map.put(port, value);
// set(map);
PortValueMap map = new PortValueMapImpl();
map.put(port, value);
set(map);
}


Expand Down Expand Up @@ -1107,28 +1109,25 @@ public void setString(Map<? extends String, ? extends Fmi2Builder.Value<String>>
}

@Override
public int terminate() {
return this.terminate(builder.getDynamicScope());
public void terminate() {
this.terminate(builder.getDynamicScope());
}

// TODO: Return other values but ok?
@Override
public int terminate(Fmi2Builder.Scope<PStm> scope) {
public void terminate(Fmi2Builder.Scope<PStm> scope) {
PStm stm = stateTransitionFunction(FmiFunctionType.TERMINATE);
scope.add(stm);
if (builder.getSettings().fmiErrorHandlingEnabled) {
FmiStatusErrorHandlingBuilder.generate(builder, "terminate", this, (IMablScope) scope, MablApiBuilder.Fmi3Status.FMI_ERROR,
MablApiBuilder.Fmi3Status.FMI_FATAL);
}
return Fmi3Status.OK.value;
}

// TODO: these are work in progress
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")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.HashMap;
import java.util.Map;

public class PortVariableMapImpl<V> extends HashMap<Fmi2Builder.Port, Fmi2Builder.Variable<PStm, V>> implements Fmi2Builder.Fmi2ComponentVariable.PortVariableMap<PStm, V> {
public class PortVariableMapImpl<V> extends HashMap<Fmi2Builder.Port, Fmi2Builder.Variable<PStm, V>> implements Fmi2Builder.Fmi2ComponentVariable.PortVariableMap<PStm, V>, Fmi2Builder.Fmi3InstanceVariable.PortVariableMap<PStm, V> {
public PortVariableMapImpl(int initialCapacity, float loadFactor) {
super(initialCapacity, loadFactor);
}
Expand Down
18 changes: 12 additions & 6 deletions maestro/src/test/java/org/intocps/maestro/BuilderFmi3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void wt() throws Exception {

URI ballUri = new File("target/Fmi3ModuleReferenceFmusTest/cache/BouncingBall.fmu").getAbsoluteFile().toURI();
Fmu3 ball = new Fmu3(new File(ballUri));
ArrayVariableFmi2Api varArray = builder.getDynamicScope().store("varArray", new Long[] {1L});
ArrayVariableFmi2Api requiredIntermediateVariables = builder.getDynamicScope().store("requiredIntermediateVariables", new Long[] {1L});
Fmi3ModelDescription md3Ball = new Fmi3ModelDescription(ball.getModelDescription());


Expand All @@ -89,15 +90,20 @@ public void wt() throws Exception {
boolean eventModeUsed = true;
boolean earlyReturnAllowed = true;
InstanceVariableFmi3Api ballInstance = ballFmu.instantiate("ballInstance", visible, loggingOn, eventModeUsed, earlyReturnAllowed,
varArray);
requiredIntermediateVariables);

int res = ballInstance.enterInitializationMode(false, 0.0, 0.0, true,10.0);
res = ballInstance.exitInitializationMode();
res = ballInstance.terminate();
ballInstance.freeInstance();
ballInstance.enterInitializationMode(false, 0.0, 0.0, true,10.0);
ballInstance.exitInitializationMode();

// ArrayVariableFmi2Api svs = builder.getDynamicScope().store("svs", new Long[] {5L, 6L});
// ArrayVariableFmi2Api values_r = builder.getDynamicScope().store("values_r", new Double[] {-9.81, 0.7});


ballInstance.terminate();


// ballInstance.freeInstance();




Expand Down

0 comments on commit b0a39fc

Please sign in to comment.