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
  • Loading branch information
lausdahl committed Sep 7, 2023
2 parents 2debd67 + 9fe0c2c commit 3237670
Show file tree
Hide file tree
Showing 20 changed files with 1,974 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ public static AIntLiteralExp newAIntLiteralExp(Integer value) {
return exp;
}


public static AStringLiteralExp newAStringLiteralExp(String value) {
AStringLiteralExp exp = new AStringLiteralExp();
exp.setValue(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import java.lang.reflect.InvocationTargetException
import javax.xml.transform.stream.StreamSource
import javax.xml.xpath.XPathExpressionException


class Fmi3ModelDescription : ModelDescription {
private var variables: Collection<Fmi3Variable>? = null
private var typeDefinitions: Collection<IFmi3TypeDefinition>? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ abstract class Fmi3Variable protected constructor(
val previous: UInt? = null,
val clocks: List<UInt>? = null,
val typeIdentifier: Fmi3TypeEnum // This is for easier type identification and is not part of the official spec
)
) {
fun getValueReferenceAsLong():Long { return valueReference.toLong() }
}

data class Dimension(val valueReference: UInt?, val start: List<Long>?)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,13 @@ public void defaultInPStm(PStm node) throws AnalysisException {
config.setName(newAIdentifier("FMI2"));
//config.setConfig(StringEscapeUtils.escapeJava(simulationEnvironment.));
// unit.setFrameworkConfigs(Arrays.asList(config));
unit.setImports(Stream.concat(Stream.of(newAIdentifier("FMI2")), importedModules.stream().map(MableAstFactory::newAIdentifier))

// TODO: added "import FMI3" after "import FMI2". Should probably figure out a smarter way to do this
unit.setImports(Stream.concat(Stream.of(newAIdentifier("FMI2")),
Stream.concat(Stream.of(newAIdentifier("FMI3")), importedModules.stream().map(MableAstFactory::newAIdentifier)))
.collect(Collectors.toList()));


return unit;

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.intocps.maestro.framework.fmi2.api.mabl;

import org.intocps.maestro.fmi.Fmi2ModelDescription;
import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3ModelDescription;

import javax.xml.xpath.XPathExpressionException;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -20,8 +21,9 @@ public ModelDescriptionContext(
this.valRefToSv.put(sv.valueReference, sv);
});
}

public Fmi2ModelDescription getModelDescription() {
return modelDescription;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.intocps.maestro.framework.fmi2.api.mabl;

import kotlin.UInt;
import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3ModelDescription;

import javax.xml.xpath.XPathExpressionException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class ModelDescriptionContext3 {

private final Fmi3ModelDescription modelDescription;
public Map<String, Fmi3ModelDescription.Fmi3ScalarVariable> nameToSv = new HashMap<>();
public Map<Long, Fmi3ModelDescription.Fmi3ScalarVariable> valRefToSv = new HashMap<>();
public ModelDescriptionContext3(
Fmi3ModelDescription modelDescription) throws IllegalAccessException, XPathExpressionException, InvocationTargetException {
this.modelDescription = modelDescription;
modelDescription.getScalarVariables().forEach((sv) -> {
this.nameToSv.put(sv.getVariable().getName(), sv);
this.valRefToSv.put((long) sv.getVariable().getValueReferenceAsLong(), sv);
});
}
public Fmi3ModelDescription getModelDescription() {
return modelDescription;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package org.intocps.maestro.framework.fmi2.api.mabl;

import org.intocps.maestro.ast.node.PType;
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.framework.fmi2.api.Fmi2Builder;
import org.intocps.maestro.framework.fmi2.api.mabl.variables.ComponentVariableFmi2Api;
import org.intocps.maestro.framework.fmi2.api.mabl.variables.InstanceVariableFmi3Api;
import org.intocps.maestro.framework.fmi2.api.mabl.variables.VariableFmi2Api;

import java.util.ArrayList;
import java.util.List;

import static org.intocps.maestro.ast.MableAstFactory.*;

public class PortFmi3Api implements Fmi2Builder.Port {

public final InstanceVariableFmi3Api aMablFmi3InstanceAPI;
public final Fmi3ModelDescription.Fmi3ScalarVariable scalarVariable;
private final List<PortFmi3Api> targetPorts = new ArrayList<>();
private VariableFmi2Api sharedAsVariable;
private PortFmi3Api sourcePort;


// TODO model description fmi3
public PortFmi3Api(InstanceVariableFmi3Api aMablFmi3InstanceAPI, Fmi3ModelDescription.Fmi3ScalarVariable scalarVariable) {

this.aMablFmi3InstanceAPI = aMablFmi3InstanceAPI;
this.scalarVariable = scalarVariable;
}

@Override
public String toString() {
return "Port( '" + aMablFmi3InstanceAPI.getName() + "." + scalarVariable.getVariable().getName() + "' , '" + scalarVariable.getVariable().getTypeIdentifier().name() + "')";
}

public VariableFmi2Api getSharedAsVariable() {
return sharedAsVariable;
}

public void setSharedAsVariable(VariableFmi2Api sharedAsVariable) {
this.sharedAsVariable = sharedAsVariable;
}

public PType getType() {
switch (scalarVariable.getVariable().getTypeIdentifier()) {
// case Boolean:
// return newBoleanType();
// case Real:
// return newRealType();
// case Integer:
// return newIntType();
// case String:
// return newStringType();
// case Enumeration:
default:
return null;
}
}

@Override
public String getName() {
return this.scalarVariable.getVariable().getName();
}

@Override
public Long getPortReferenceValue() {
return this.scalarVariable.getVariable().getValueReferenceAsLong();
}


@Override
public void linkTo(Fmi2Builder.Port... receivers) throws PortLinkException {

if (receivers == null || receivers.length == 0) {
return;
}

if (this.scalarVariable.getVariable().getCausality() != Fmi3Causality.Output) {
throw new PortLinkException("Can only link output ports. This port is: " + this.scalarVariable.getVariable().getCausality(), this);
}

for (Fmi2Builder.Port receiver : receivers) {
PortFmi3Api receiverPort = (PortFmi3Api) receiver;

if (receiverPort.scalarVariable.getVariable().getCausality() != Fmi3Causality.Input) {
throw new PortLinkException("Receivers must be input ports. This receiver is: " + receiverPort.scalarVariable.getVariable().getCausality(),
receiverPort);
}

// HEJ: TBD - This check fails with "already linked" in expansion since both rbmq fmus connect to single actuation
if (receiverPort.getSourcePort() != null) {
throw new PortLinkException("Cannot port already linked please break link first", receiver);
}
receiverPort.sourcePort = this;
if (!this.targetPorts.contains(receiverPort)) {
this.targetPorts.add(receiverPort);
}
}
}

public PortFmi3Api getSourcePort() {
return this.sourcePort;
}

@Override
public void breakLink() {
if (sourcePort != null) {
//delete this from the source port
sourcePort.targetPorts.remove(this);
}
sourcePort = null;
}

@Override
public boolean isLinked() {
return isLinkedAsInputConsumer() || isLinkedAsOutputProvider();
}

@Override
public boolean isLinkedAsOutputProvider() {
return targetPorts.isEmpty();
}

@Override
public boolean isLinkedAsInputConsumer() {
return this.sourcePort != null;
}

public String toLexName() {
return this.aMablFmi3InstanceAPI.getOwner().getName() + "_" + this.aMablFmi3InstanceAPI.getName() + "_" + this.getName();
}

public String getMultiModelScalarVariableName() {
return this.aMablFmi3InstanceAPI.getOwner().getFmuIdentifier() + "." + this.aMablFmi3InstanceAPI.getEnvironmentName() + "." +
this.getName();
}

public String getMultiModelScalarVariableNameWithoutFmu() {
return this.aMablFmi3InstanceAPI.getEnvironmentName() + "." + this.getName();
}

public List<PortFmi3Api> getTargetPorts() {
return this.targetPorts;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ public void registerComponentVariableFmi2Api(ComponentVariableFmi2Api componentV

}


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

@Override
public <S> S findParentScope(Class<S> type) {
return this.activeScope.findParentScope(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,8 @@ public interface IMablScope extends Fmi2Builder.Scope<PStm> {
*/
void registerComponentVariableFmi2Api(ComponentVariableFmi2Api componentVariableFmi2Api);

void registerInstanceVariableFmi3Api(InstanceVariableFmi3Api instanceVariableFmi3Api);


<S> S findParentScope(Class<S> type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ protected <V> ArrayVariableFmi2Api<V> store(Supplier<String> nameProvider, V[] v
if (length > 1 && value[0] != null) {
initializer = newAArrayInitializer(Arrays.stream(value).map(v -> newAIntLiteralExp((Integer) v)).collect(Collectors.toList()));
}
} else if (value instanceof Long[]) {
type = new AUIntNumericPrimitiveType();
if (length > 1 && value[0] != null) {
initializer = newAArrayInitializer(Arrays.stream(value).map(v -> newAUIntLiteralExp((Long) v)).collect(Collectors.toList()));
}
} else if (value instanceof Boolean[]) {
type = new ABooleanPrimitiveType();
if (length > 1 && value[0] != null) {
Expand Down Expand Up @@ -472,7 +477,6 @@ private <V> Fmi2Builder.Variable<PStm, V> storePrivate(String name, Fmi2Builder.
if (v.get() != null) {
initial = newAIntLiteralExp((Integer) v.get());
}

} else if (v.getType() instanceof ABooleanPrimitiveType) {
if (v.get() != null) {
initial = newABoolLiteralExp((Boolean) v.get());
Expand All @@ -498,14 +502,22 @@ public FmuVariableFmi2Api createFMU(String name, String loaderName, String... ar

@Override
public FmuVariableFmi3Api createFMU(String name, Fmi3ModelDescription modelDescription, URI path) throws Exception {
return null;
return VariableCreatorFmi3Api.createFMU(builder, builder.getNameGenerator(), builder.getDynamicScope(), name, modelDescription, path, this);
}

@Override
public FmuVariableFmi3Api createFMU3(String name, String loaderName, String... args) throws Exception {
return null;
}


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

}


@Override
public void markTransferPoint(String... names) {

Expand Down Expand Up @@ -601,6 +613,8 @@ public void registerComponentVariableFmi2Api(ComponentVariableFmi2Api componentV
this.fmi2ComponentVariables.add(componentVariableFmi2Api);
}



@Override
public <S> S findParentScope(Class<S> type) {
Fmi2Builder.ScopeElement<PStm> p = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1243,9 +1243,7 @@ private ArrayVariableFmi2Api<Object> growBuffer(ArrayVariableFmi2Api<Object> buf

@Override
public Fmi2Builder.StateVariable<PStm> getState() throws XPathExpressionException {

return getState(builder.getDynamicScope());

}

@Override
Expand Down
Loading

0 comments on commit 3237670

Please sign in to comment.