Skip to content

Commit

Permalink
added fmi3 support to the template generator aka import
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed Sep 7, 2023
1 parent ea12c23 commit 2debd67
Show file tree
Hide file tree
Showing 26 changed files with 542 additions and 233 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.intocps.maestro.framework.fmi2;
package org.intocps.maestro.framework.core;

public class FaultInject {
public String constraintId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
package org.intocps.maestro.framework.core;

import java.util.Optional;

public interface FrameworkUnitInfo {

String getOwnerIdentifier();
Optional<FaultInject> getFaultInject();
void setFaultInject(String constraintId);


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
public interface RelationVariable {
LexIdentifier getInstance();

Fmi2ModelDescription.ScalarVariable getScalarVariable();
String getName();

<T> T getScalarVariable(Class<T> clz);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package org.intocps.maestro.framework.fmi2;

import org.intocps.maestro.fmi.Fmi2ModelDescription;
import org.intocps.maestro.framework.core.FaultInject;
import org.intocps.maestro.framework.core.FrameworkUnitInfo;

import java.util.List;
import java.util.Map;
import java.util.Optional;

public class ComponentInfo implements FrameworkUnitInfo {
Expand Down Expand Up @@ -32,4 +31,9 @@ public Fmi2ModelDescription getModelDescription() {
public String getFmuIdentifier() {
return fmuIdentifier;
}

@Override
public String getOwnerIdentifier() {
return getFmuIdentifier();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.intocps.maestro.framework.fmi2;

import org.intocps.maestro.framework.core.FaultInject;

public class FaultInjectWithLexName extends FaultInject {
public String lexName;

Expand Down

Large diffs are not rendered by default.

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

import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3ModelDescription;
import org.intocps.maestro.framework.core.FaultInject;
import org.intocps.maestro.framework.core.FrameworkUnitInfo;

import java.util.Optional;

public class InstanceInfo implements FrameworkUnitInfo {
public final Fmi3ModelDescription modelDescription;
public final String fmuIdentifier;
public Optional<FaultInject> faultInject = Optional.empty();

public InstanceInfo(Fmi3ModelDescription modelDescription, String fmuIdentifier) {
this.modelDescription = modelDescription;
this.fmuIdentifier = fmuIdentifier;
}

public Optional<FaultInject> getFaultInject() {
return this.faultInject;
}

public void setFaultInject(String constraintId) {
this.faultInject = Optional.of(new FaultInject(constraintId));
}

public Fmi3ModelDescription getModelDescription() {
return modelDescription;
}

public String getFmuIdentifier() {
return fmuIdentifier;
}

@Override
public String getOwnerIdentifier() {
return getFmuIdentifier();
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.intocps.maestro.framework.fmi2;

import org.intocps.fmi.IFmu;
import org.intocps.maestro.core.messages.IErrorReporter;
import org.intocps.maestro.modeldefinitionchecker.VdmSvChecker;
import org.intocps.maestro.fmi.Fmi2ModelDescription;
import org.intocps.maestro.fmi.ModelDescription;
import org.intocps.maestro.modeldefinitionchecker.VdmSvChecker;

import javax.xml.xpath.XPathExpressionException;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -16,11 +16,14 @@ public class MaestroV1FmuValidation implements IFmuValidator {
@Override
public boolean validate(String id, URI path, IErrorReporter reporter) {
try {
IFmu fmu = FmuFactory.create(null, path);
Fmi2SimulationEnvironment.FileModelDescriptionResolver resolver = new Fmi2SimulationEnvironment.FileModelDescriptionResolver();
ModelDescription md = resolver.apply(null, path);

Fmi2ModelDescription md = new Fmi2ModelDescription(fmu.getModelDescription());
validateModelDescription(md);
VdmSvChecker.validateModelVariables(md.getScalarVariables());
if (md instanceof Fmi2ModelDescription) {
validateModelDescription((Fmi2ModelDescription) md);
VdmSvChecker.validateModelVariables(((Fmi2ModelDescription) md).getScalarVariables());

}
return true;
} catch (Exception e) {
reporter.report(0, e.getMessage(), null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ public LexIdentifier getInstance() {
}

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

@Override
public <T> T getScalarVariable(Class<T> clz) {
if (clz.isAssignableFrom(scalarVariable.getClass())) {
return clz.cast(scalarVariable);
}
return null;
}

public Fmi2ModelDescription.ScalarVariable getScalarVariable() {
return scalarVariable;
return getScalarVariable(this.scalarVariable.getClass());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.intocps.maestro.framework.fmi2;

import org.intocps.maestro.ast.LexIdentifier;
import org.intocps.maestro.fmi.org.intocps.maestro.fmi.fmi3.Fmi3ModelDescription;

public class RelationVariable3 implements org.intocps.maestro.framework.core.RelationVariable {
public final Fmi3ModelDescription.Fmi3ScalarVariable scalarVariable;
public final LexIdentifier instance;

public RelationVariable3(Fmi3ModelDescription.Fmi3ScalarVariable scalarVariable, LexIdentifier instance) {
this.scalarVariable = scalarVariable;
this.instance = instance;
}

@Override
public LexIdentifier getInstance() {
return this.instance;
}

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

@Override
public <T> T getScalarVariable(Class<T> clz) {
if (clz.isAssignableFrom(scalarVariable.getClass())) {
return clz.cast(scalarVariable);
}
return null;
}

public Fmi3ModelDescription.Fmi3ScalarVariable getScalarVariable() {
return getScalarVariable(this.scalarVariable.getClass());
}

@Override
public String toString() {
return instance + "." + scalarVariable;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof RelationVariable)) {
return false;
}

RelationVariable rv = (RelationVariable) o;
return rv.toString().equals(this.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void beforeEachTestMethod() throws Exception {
modelDescriptionValidator = new ModelDescriptionValidator();
var iterator = env.getFmusWithModelDescriptions().iterator();
iterator.next();
md = iterator.next().getValue();
md = (Fmi2ModelDescription) iterator.next().getValue();
variables = md.getScalarVariables();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
import org.intocps.maestro.ast.AVariableDeclaration;
import org.intocps.maestro.ast.LexIdentifier;
import org.intocps.maestro.ast.node.*;
import org.intocps.maestro.framework.core.IRelation;
import org.intocps.maestro.framework.core.ISimulationEnvironment;
import org.intocps.maestro.framework.core.IVariable;
import org.intocps.maestro.framework.core.*;
import org.intocps.maestro.framework.fmi2.ComponentInfo;
import org.intocps.maestro.framework.fmi2.Fmi2SimulationEnvironment;
import org.intocps.maestro.framework.fmi2.RelationVariable;
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.FmuVariableFmi2Api;
Expand Down Expand Up @@ -40,34 +37,43 @@ public static Map.Entry<String, ComponentVariableFmi2Api> getComponentVariableFr
if (exp instanceof AIdentifierExp) {
String componentName = ((AIdentifierExp) exp).getName().getText();

ComponentInfo instance = env.getInstanceByLexName(environmentComponentName);
ModelDescriptionContext modelDescriptionContext = new ModelDescriptionContext(instance.modelDescription);
FrameworkUnitInfo inst = env.getInstanceByLexName(environmentComponentName);
if (inst instanceof ComponentInfo) {

//This dummy statement is removed later. It ensures that the share variables are added to the root scope.
PStm dummyStm = newABlockStm();
builder.getDynamicScope().add(dummyStm);

FmuVariableFmi2Api fmu = new FmuVariableFmi2Api(instance.fmuIdentifier, builder, modelDescriptionContext, dummyStm, newANameType("FMI2"),
builder.getDynamicScope().getActiveScope(), builder.getDynamicScope(), null,
new AIdentifierExp(new LexIdentifier(instance.fmuIdentifier.replace("{", "").replace("}", ""), null)));
ComponentInfo instance = (ComponentInfo) inst;
ModelDescriptionContext modelDescriptionContext = new ModelDescriptionContext(instance.modelDescription);

ComponentVariableFmi2Api a;
if (environmentComponentName == null) {
a = new ComponentVariableFmi2Api(dummyStm, fmu, componentName, modelDescriptionContext, builder,
builder.getDynamicScope().getActiveScope(), null, newAIdentifierExp(componentName));
} else {
a = new ComponentVariableFmi2Api(dummyStm, fmu, componentName, modelDescriptionContext, builder,
builder.getDynamicScope().getActiveScope(), null, newAIdentifierExp(componentName), environmentComponentName);
}
List<RelationVariable> variablesToLog = null;
if (environmentComponentName == null) {
variablesToLog = env.getVariablesToLog(componentName);
//This dummy statement is removed later. It ensures that the share variables are added to the root scope.
PStm dummyStm = newABlockStm();
builder.getDynamicScope().add(dummyStm);

FmuVariableFmi2Api fmu =
new FmuVariableFmi2Api(instance.fmuIdentifier, builder, modelDescriptionContext, dummyStm, newANameType("FMI2"),
builder.getDynamicScope().getActiveScope(), builder.getDynamicScope(), null,
new AIdentifierExp(new LexIdentifier(instance.fmuIdentifier.replace("{", "").replace("}", ""), null)));

ComponentVariableFmi2Api a;
if (environmentComponentName == null) {
a = new ComponentVariableFmi2Api(dummyStm, fmu, componentName, modelDescriptionContext, builder,
builder.getDynamicScope().getActiveScope(), null, newAIdentifierExp(componentName));
} else {
a = new ComponentVariableFmi2Api(dummyStm, fmu, componentName, modelDescriptionContext, builder,
builder.getDynamicScope().getActiveScope(), null, newAIdentifierExp(componentName), environmentComponentName);
}
List<RelationVariable> variablesToLog = null;
if (environmentComponentName == null) {
variablesToLog = env.getVariablesToLog(componentName);
} else {
variablesToLog = env.getVariablesToLog(environmentComponentName);
}
a.setVariablesToLog(variablesToLog.stream().filter(org.intocps.maestro.framework.fmi2.RelationVariable.class::isInstance)
.map(org.intocps.maestro.framework.fmi2.RelationVariable.class::cast).collect(Collectors.toList()));

return Map.entry(componentName, a);
} else {
variablesToLog = env.getVariablesToLog(environmentComponentName);
throw new RuntimeException("instance is not an fmi2 component: " + componentName);
}
a.setVariablesToLog(variablesToLog);

return Map.entry(componentName, a);
} else {
throw new RuntimeException("exp is not of type AIdentifierExp, but of type: " + exp.getClass());
}
Expand All @@ -93,9 +99,9 @@ public static void createBindings(Map<String, ComponentVariableFmi2Api> instance
}
}

PortFmi2Api targetPort = instance.getPort(targetVar.getScalarVariable().getScalarVariable().getName());
PortFmi2Api targetPort = instance.getPort(targetVar.getScalarVariable().getName());

String sourcePortName = relation.getSource().getScalarVariable().getScalarVariable().getName();
String sourcePortName = relation.getSource().getScalarVariable().getName();
if (targetPort != null) {
entry.getValue().getPort(sourcePortName).linkTo(targetPort);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import org.intocps.maestro.ast.analysis.DepthFirstAnalysisAdaptor;
import org.intocps.maestro.ast.node.*;
import org.intocps.maestro.fmi.Fmi2ModelDescription;
import org.intocps.maestro.framework.fmi2.RelationVariable;

import org.intocps.maestro.framework.core.RelationVariable;
import org.intocps.maestro.framework.fmi2.api.Fmi2Builder;
import org.intocps.maestro.framework.fmi2.api.mabl.*;
import org.intocps.maestro.framework.fmi2.api.mabl.scoping.IMablScope;
Expand Down Expand Up @@ -99,7 +100,7 @@ public List<PortFmi2Api> getVariablesToLog() {
}

public void setVariablesToLog(List<RelationVariable> variablesToLog) {
this.variabesToLog = variablesToLog.stream().map(x -> x.scalarVariable.getName()).collect(Collectors.toList());
this.variabesToLog = variablesToLog.stream().map(RelationVariable::getName).collect(Collectors.toList());
}

@Override
Expand Down
37 changes: 21 additions & 16 deletions maestro/src/main/java/org/intocps/maestro/BuilderHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.intocps.maestro.ast.analysis.AnalysisException;
import org.intocps.maestro.ast.analysis.DepthFirstAnalysisAdaptorQuestion;
import org.intocps.maestro.ast.node.*;
import org.intocps.maestro.framework.core.FrameworkUnitInfo;
import org.intocps.maestro.framework.core.ISimulationEnvironment;
import org.intocps.maestro.framework.fmi2.ComponentInfo;
import org.intocps.maestro.framework.fmi2.Fmi2SimulationEnvironment;
Expand Down Expand Up @@ -113,23 +114,27 @@ public void caseAInstanceMappingStm(AInstanceMappingStm node,
if (exp instanceof AIdentifierExp) {
String componentName = ((AIdentifierExp) exp).getName().getText();

ComponentInfo instance = env.getInstanceByLexName(componentName);
ModelDescriptionContext modelDescriptionContext = null;
try {
modelDescriptionContext = new ModelDescriptionContext(instance.modelDescription);
} catch (IllegalAccessException | XPathExpressionException | InvocationTargetException e) {
throw new RuntimeException(e);
}

//This dummy statement is removed later. It ensures that the share variables are added to the root scope.
PStm dummyStm = newABlockStm();
builder.getDynamicScope().add(dummyStm);
FrameworkUnitInfo instance = env.getInstanceByLexName(componentName);
if (instance instanceof ComponentInfo) {
ModelDescriptionContext modelDescriptionContext = null;
try {
modelDescriptionContext = new ModelDescriptionContext(((ComponentInfo) instance).modelDescription);
} catch (IllegalAccessException | XPathExpressionException | InvocationTargetException e) {
throw new RuntimeException(e);
}

//This dummy statement is removed later. It ensures that the share variables are added to the root scope.
PStm dummyStm = newABlockStm();
builder.getDynamicScope().add(dummyStm);

FmuVariableFmi2Api fmu =
new FmuVariableFmi2Api(instance.getOwnerIdentifier(), builder, modelDescriptionContext, dummyStm, newANameType("FMI2"),
builder.getDynamicScope().getActiveScope(), builder.getDynamicScope(), null,
new AIdentifierExp(new LexIdentifier(instance.getOwnerIdentifier().replace("{", "").replace("}", ""), null)));
return fmu;

FmuVariableFmi2Api fmu =
new FmuVariableFmi2Api(instance.fmuIdentifier, builder, modelDescriptionContext, dummyStm, newANameType("FMI2"),
builder.getDynamicScope().getActiveScope(), builder.getDynamicScope(), null,
new AIdentifierExp(new LexIdentifier(instance.fmuIdentifier.replace("{", "").replace("}", ""), null)));
return fmu;
}
throw new RuntimeException("exp is not identifying as a component: " + instance);
} else {
throw new RuntimeException("exp is not of type AIdentifierExp, but of type: " + exp.getClass());
}
Expand Down
Loading

0 comments on commit 2debd67

Please sign in to comment.