To demonstrate the programmatic use of the Massif API, an introductory example project is placed under the
massif/examples/hu.bme.mit.massif.examples.api
project.
In the following sections code fragments are from the corresponding example classes.
Prerequisites
Headless usage (Massif in non-eclipse environment)
If you wish to use Massif in a non-eclipse environment, you have to run the following
setup, found in the hu.bme.mit.massif.simulink.cli
package:
CLIInitializationUtil.setupEnvironment()
Also, the default VIATRA backends must be provided:
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngineOptions;
import org.eclipse.viatra.query.runtime.localsearch.matcher.integration.LocalSearchEMFBackendFactory;
import org.eclipse.viatra.query.runtime.rete.matcher.ReteBackendFactory;
ViatraQueryEngineOptions.setSystemDefaultBackends(ReteBackendFactory.INSTANCE, ReteBackendFactory.INSTANCE, LocalSearchEMFBackendFactory.INSTANCE);
General prerequisites
In order to successfully use the Importer (ImporterExample
class) or the Exporter (ExporterExample
class),
some other components has to be created first in both classes, namely:
-
command evaluator
-
command factory
In the followings an example shows their creation, and there is also an explained example for using both the importer and exporter.
Command evaluator
In order to be able to execute commands in Matlab, a command evaluator has to be created first. In the example we use helper method to do this work, but the main logic can be written in one line:
ICommandEvaluator commandEvaluator = new CommandEvaluatorImpl(new MatlabClient(hostAddress, hostPort, serviceName));
This provides low level command evaluation,
like sending commands to Matlab in form of strings and receiving the results in array of objects.
It is recommended to use the CommandFactory
instead for executing commands.
Command factory
To wrap low level Matlab commands a command factory is provided, which can be used to create objects that represent common commands in Matlab, and have methods like parameter addition, execution initiation. The factory is to be instantiated in the following way using the previously provided command evaluator:
MatlabCommandFactory factory = new MatlabCommandFactory(commandEvaluator);
Importer API example
The creation of the importer is shown below:
ModelObject model = new ModelObject(modelName, commandEvaluator);
model.setLoadPath(modelPath);
model.registerApplicableFilters("famfilter");
Importer importer = new Importer(model);
First a ModelObject
is created that represents a model in Simulink.
It is parameterized with the model name string and an assigned command evaluator,
that is used throughout the import process of the model.
Then the path to the containing folder of the model and the chosen filter is set on the model object.
In the example, famfilter
refers to a filter that skips importing the subblocks of subsystems with Tag value of "FAM Leaf".
And after initializing the model object an Importer
object is created.
importer.traverseAndCreateEMFModel(ImportMode.FLATTENING);
The import process is then initiated with the traverseAndCreateEMFModel
method of the importer.
The model is not persisted until an explicit save was done like in the following:
importer.saveEMFModel(/*Name of the imported model as string comes here*/);
Exporter API example
The exporter - compared to the importer - has much less to configure. Its usage is demonstrated below:
Exporter exporter = new Exporter();
SimulinkModel loadedModel = exporter.loadSimulinkModel(/*Model path with name as string comes here*/);
exporter.export(loadedModel, commandFactory);
It simply loads the EMF representation from the specified location, then exports it using a specified command factory.
To save the model in Matlab, an explicit save has to be done:
exporter.saveSimulinkModel(loadedModel.getSimulinkRef().getFQN(), /*extension as string comes here*/);
The extension of the model to save can be slx
or mdl
.
(The default by MATLAB is slx
).
Please note, that the save path and save name is already set in the EMF representation,
so it can be obtained by the loadedModel.getSimulinkRef().getFQN()
call.
Caveats
If a model is already loaded by Simulink, you can’t re-import or re-export them.
Use the bdclose('all')
simulink command to close all opened systems.
It can be run via Massif programmatically:
ICommandEvaluator commandEvaluator = ... // The commandEvaluator of your choice
MatlabCommandFactory commandFactory = new MatlabCommandFactory(commandEvaluator);
MatlabCommand closeAllCommand = commandFactory.customCommand("bdclose", 0);
closeAllCommand.addParam("all");
closeAllCommand.execute();