Skip to content

Commit

Permalink
Updated references to scenario verifier
Browse files Browse the repository at this point in the history
Updated references to Scen-verifier
  • Loading branch information
SimplisticCode committed Dec 18, 2023
1 parent 2523491 commit c2defc3
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.spencerwi.either.Either;
import org.intocps.verification.scenarioverifier.core.MasterModel;
import org.intocps.verification.scenarioverifier.core.masterModel.MasterModel;
import org.intocps.verification.scenarioverifier.core.ScenarioLoader;
import org.apache.commons.lang3.tuple.Pair;
import org.intocps.maestro.Mabl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.intocps.verification.scenarioverifier.core.*;
import org.intocps.verification.scenarioverifier.core.masterModel.*;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.intocps.maestro.Mabl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.intocps.maestro.template;

import org.intocps.verification.scenarioverifier.core.MasterModel;
import org.intocps.verification.scenarioverifier.core.masterModel.MasterModel;
import org.apache.commons.lang3.tuple.Pair;
import org.intocps.maestro.core.Framework;
import org.intocps.maestro.framework.fmi2.Fmi2SimulationEnvironment;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.intocps.maestro.template;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.intocps.verification.scenarioverifier.core.MasterModel;
import org.intocps.verification.scenarioverifier.core.masterModel.MasterModel;
import org.apache.commons.text.StringEscapeUtils;
import org.intocps.maestro.ast.LexIdentifier;
import org.intocps.maestro.ast.MableAstFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.intocps.verification.scenarioverifier.core.MasterModel;
import org.intocps.verification.scenarioverifier.core.masterModel.MasterModel;
import org.intocps.verification.scenarioverifier.core.ScenarioLoader;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import scala.jdk.javaapi.CollectionConverters;
import org.intocps.verification.scenarioverifier.core.masterModel.*;
import org.intocps.verification.scenarioverifier.core.*;
import org.intocps.verification.scenarioverifier.core.FMI3.AdaptiveModel;
import org.intocps.verification.scenarioverifier.synthesizer.LoopStrategy;
import org.intocps.verification.scenarioverifier.synthesizer.SynthesizerSimple;

Expand Down Expand Up @@ -106,8 +108,8 @@ public <R> RuntimeConfigAddition<R> expandWithRuntimeAddition(AFunctionDeclarati

// GENERATE MaBL
try {
MasterModel masterModel = ScenarioLoader.load(new ByteArrayInputStream(configuration.masterModel.getBytes()));
ScenarioModel scenarioModel = masterModel.scenario();
MasterModelFMI2 masterModel = ScenarioLoader.load(new ByteArrayInputStream(configuration.masterModel.getBytes()));
FMI2ScenarioModel scenarioModel = masterModel.scenario();
adaptiveModel = scenarioModel.config();

MablApiBuilder builder = (MablApiBuilder) providedBuilder;
Expand All @@ -134,7 +136,7 @@ public <R> RuntimeConfigAddition<R> expandWithRuntimeAddition(AFunctionDeclarati
});

// If the initialization section is missing use the SynthesizerSimple to generate it from the scenario model.
if (masterModel.initialization().length() == 0) {
if (masterModel.initialization().isEmpty()) {
SynthesizerSimple synthesizer = new SynthesizerSimple(scenarioModel, LoopStrategy.maximum());
masterModel.initialization().concat(synthesizer.synthesizeInitialization());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import org.intocps.maestro.framework.fmi2.Fmi2SimulationEnvironment
import org.intocps.maestro.framework.fmi2.Fmi2SimulationEnvironmentConfiguration
import scala.jdk.javaapi.CollectionConverters
import org.intocps.verification.scenarioverifier.api.GenerationAPI
import org.intocps.verification.scenarioverifier.core.masterModel.*
import org.intocps.verification.scenarioverifier.core.*
import org.intocps.verification.scenarioverifier.core.FMI3.AdaptiveModel
import org.intocps.verification.scenarioverifier.core.FMI3.ConfigurationModel
import org.intocps.verification.scenarioverifier.core.FMI3.ScenarioLoaderFMI3
import scala.collection.immutable.Map

class MasterModelMapper {
companion object {

private fun getFmuNameFromFmuInstanceName(name: String): String {
return name.split(Sigver.MASTER_MODEL_FMU_INSTANCE_DELIMITER)[0]
}
Expand All @@ -29,12 +33,18 @@ class MasterModelMapper {
return name.split(Sigver.MULTI_MODEL_FMU_INSTANCE_DELIMITER).let { it.subList(2, it.size).joinToString(Sigver.MULTI_MODEL_FMU_INSTANCE_DELIMITER) }
}

fun scenarioToMasterModel(scenario: String): MasterModel {
fun scenarioToFMI2MasterModel(scenario: String): MasterModel {
// Load master model without algorithm
val masterModel = ScenarioLoader.load(scenario.byteInputStream())
return GenerationAPI.synthesizeAlgorithm(masterModel.name(), masterModel.scenario())
}

fun scenarioToFMI3MasterModel(scenario: String): MasterModel {
// Load master model without algorithm
val masterModel = ScenarioLoaderFMI3.load(scenario.byteInputStream())
return GenerationAPI.synthesizeAlgorithm(masterModel.name(), masterModel.scenario())
}

fun masterModelConnectionsToMultiModelConnections(masterModel: MasterModel): HashMap<String, MutableList<String>> {
// Setup connections as defined in the scenario (These should be identical to the multi-model)
return CollectionConverters.asJava(masterModel.scenario().connections())
Expand Down Expand Up @@ -82,6 +92,104 @@ class MasterModelMapper {
// Instantiate a simulationConfiguration to be able to access fmu model descriptions
val simulationConfiguration = Fmi2SimulationEnvironmentConfiguration(extendedMultiModel.connections, extendedMultiModel.fmus)

// Map fmus to fmu models
val simulationEnvironment = Fmi2SimulationEnvironment.of(simulationConfiguration, null)
val fmusWithModelDescriptions = simulationEnvironment.fmusWithModelDescriptions
val fmuInstanceNames = connectionModelList.map { connectionModel ->
listOf(
connectionModel.srcPort().fmu(),
connectionModel.trgPort().fmu()
)
}.flatten().distinct()
val fmuNameToFmuModel = fmuInstanceNames.mapNotNull { fmuInstanceName ->
fmusWithModelDescriptions.find { it.key.contains(getFmuNameFromFmuInstanceName(fmuInstanceName)) }
?.let { fmuWithMD ->
val scalarVariables = fmuWithMD.value.scalarVariables
val inputs =
scalarVariables.filter { port -> port.causality.equals(Fmi2ModelDescription.Causality.Input) }
.associate { inputPort ->
inputPort.getName() to FMI2InputPortModel(if (extendedMultiModel.sigver.reactivity.any { portReactivity ->
portReactivity.key.contains(inputPort.getName()) &&
portReactivity.key.contains(
getFmuNameFromFmuInstanceName(
fmuInstanceName
)
) &&
portReactivity.key.contains(
getInstanceNameFromFmuInstanceName(
fmuInstanceName
)
) &&
portReactivity.value == ExtendedMultiModel.Sigver.Reactivity.Reactive
}) Reactivity.reactive() else Reactivity.delayed())
}

val outputs =
scalarVariables.filter { port ->
port.causality.equals(Fmi2ModelDescription.Causality.Output) && connectionModelList.find { connectionModel ->
connectionModel.srcPort().fmu().equals(fmuInstanceName) && connectionModel.srcPort()
.port().equals(port.getName())
} != null
}.associate { outputPort ->
val dependencies =
outputPort.outputDependencies.map { entry -> entry.key.getName() }
// This might need to change later
outputPort.getName() to FMI2OutputPortModel(
CollectionConverters.asScala(dependencies).toList(),
CollectionConverters.asScala(dependencies).toList()
)
}

fmuInstanceName to Fmu2Model(
Map.from(
scala.jdk.CollectionConverters.MapHasAsScala(inputs).asScala()
),
Map.from(
scala.jdk.CollectionConverters.MapHasAsScala(outputs).asScala()
),
fmuWithMD.value.getCanGetAndSetFmustate(),
simulationEnvironment.getUriFromFMUName(fmuWithMD.key).path
)
}
}.toMap()

val scenarioModel = FMI2ScenarioModel(
Map.from(
scala.jdk.CollectionConverters.MapHasAsScala(fmuNameToFmuModel).asScala()
),
AdaptiveModel(
CollectionConverters.asScala(listOf<PortRef>()).toList(), Map.from(
scala.jdk.CollectionConverters.MapHasAsScala(mapOf<String, ConfigurationModel>()).asScala()
)
),
CollectionConverters.asScala(connectionModelList).toList(),
maxPossibleStepSize
)

// Generate the master model from the scenario
return GenerationAPI.synthesizeAlgorithm("generatedFromMultiModel", scenarioModel)
}

fun multiModelToMasterModel(extendedMultiModel: ExtendedMultiModel, maxPossibleStepSize: Int): MasterModel3 {
// Map multi model connections type to scenario connections type
val connectionModelList = extendedMultiModel.connections.entries.map { (key, value) ->
value.map { targetPortName ->
ConnectionModel(
PortRef(
multiModelConnectionNameToMasterModelInstanceName(key), // Fully qualify the fmu instance with <fmu-name>_<instance-name>
multiModelConnectionNameToPortName(key)
),
PortRef(
multiModelConnectionNameToMasterModelInstanceName(targetPortName), // Fully qualify the fmu instance with <fmu-name>_<instance-name>
multiModelConnectionNameToPortName(targetPortName)
)
)
}
}.flatten()

// Instantiate a simulationConfiguration to be able to access fmu model descriptions
val simulationConfiguration = Fmi2SimulationEnvironmentConfiguration(extendedMultiModel.connections, extendedMultiModel.fmus)

// Map fmus to fmu models
val simulationEnvironment = Fmi2SimulationEnvironment.of(simulationConfiguration, null)
val fmusWithModelDescriptions = simulationEnvironment.fmusWithModelDescriptions
Expand Down Expand Up @@ -131,10 +239,10 @@ class MasterModelMapper {
}

fmuInstanceName to FmuModel(
scala.collection.immutable.Map.from(
Map.from(
scala.jdk.CollectionConverters.MapHasAsScala(inputs).asScala()
),
scala.collection.immutable.Map.from(
Map.from(
scala.jdk.CollectionConverters.MapHasAsScala(outputs).asScala()
),
fmuWithMD.value.getCanGetAndSetFmustate(),
Expand All @@ -144,11 +252,11 @@ class MasterModelMapper {
}.toMap()

val scenarioModel = ScenarioModel(
scala.collection.immutable.Map.from(
Map.from(
scala.jdk.CollectionConverters.MapHasAsScala(fmuNameToFmuModel).asScala()
),
AdaptiveModel(
CollectionConverters.asScala(listOf<PortRef>()).toList(), scala.collection.immutable.Map.from(
CollectionConverters.asScala(listOf<PortRef>()).toList(), Map.from(
scala.jdk.CollectionConverters.MapHasAsScala(mapOf<String, ConfigurationModel>()).asScala()
)
),
Expand Down

0 comments on commit c2defc3

Please sign in to comment.