Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving port parameters #135

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,17 @@ public void saveSimulinkModel(String modelNameWithPath, String fileExtension) th
// Save the current directory
String currentWorkdirectory = MatlabString.getMatlabStringData(commandFactory.cd().execute());

String[] savePathSegments = modelNameWithPath.split(File.separator);
String separator = FileSystems.getDefault().getSeparator();
String[] savePathSegments;
String[] workDirSegments;
if(separator.equals("\\")){
savePathSegments = modelNameWithPath.split(separator + "\\"); // Add regex escape for windows environment
workDirSegments = currentWorkdirectory.split("\\\\"); // The original working directory location
} else {
savePathSegments = modelNameWithPath.split(separator);
workDirSegments = currentWorkdirectory.split(separator);
}

String modelName = savePathSegments[savePathSegments.length - 1];

// Navigate to the save location
Expand All @@ -215,11 +225,10 @@ public void saveSimulinkModel(String modelNameWithPath, String fileExtension) th
MatlabCommand saveSystem = commandFactory.saveSystem().addParam(modelName).addParam(modelName + "." + fileExtension);
saveSystem.execute();

// Navigate back to the original working directory
String[] workDirSegments = currentWorkdirectory.split(File.separator);

for (int i = 0; i < workDirSegments.length; i++) {
String segment = workDirSegments[i];
MatlabCommand changeToWorkDir = commandFactory.cd().addParam(segment + File.separator);
MatlabCommand changeToWorkDir = commandFactory.cd().addParam(segment + separator);
changeToWorkDir.execute();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Contributors:
* Peter Lunk - Modified API to allow custom Logger definition
* Marton Bur, Abel Hegedus, Akos Horvath - initial API and implementation
* Krisztian Gabor Mayer - additional features
*******************************************************************************/
package hu.bme.mit.massif.simulink.api;

Expand Down Expand Up @@ -71,6 +72,9 @@
import hu.bme.mit.massif.simulink.SubSystem;
import hu.bme.mit.massif.simulink.api.adapter.block.IBlockAdapter;
import hu.bme.mit.massif.simulink.api.adapter.port.IPortAdapter;
import hu.bme.mit.massif.simulink.api.dto.AbstractImporterDTO;
import hu.bme.mit.massif.simulink.api.dto.BlockDTO;
import hu.bme.mit.massif.simulink.api.dto.PortDTO;
import hu.bme.mit.massif.simulink.api.exception.SimulinkApiException;
import hu.bme.mit.massif.simulink.api.extension.IBlockImportFilter;
import hu.bme.mit.massif.simulink.api.extension.IParameterImportFilter;
Expand Down Expand Up @@ -525,7 +529,7 @@ public void saveEMFModel(String importedModelName) throws SimulinkApiException {
try {
if (ImportMode.DEEP.equals(importMode) || ImportMode.REFERENCING.equals(importMode)) {
// Save all referenced models in a separate folder
String referencesFolder = referencesFolderName + REFERENCES_FOLDER_SUFFIX + File.separator;
String referencesFolder = referencesFolderName + AbstractImporterDTO.REFERENCES_FOLDER_SUFFIX + File.separator;
saveReferences(referencedModels.values(),savePath, rs, referencesFolder);
saveReferences(referencedLibraries.values(),savePath, rs, referencesFolder);
}
Expand Down Expand Up @@ -933,22 +937,26 @@ private boolean isFiltered(Block block) {
* @throws IOException
*/
private Block createBlockInstance(String blockType, String blockName, SimulinkReference parentSimRef)
throws SimulinkApiException {

throws SimulinkApiException {
// Obtain the block provider corresponding to the block type
BlockProvider provider = new BlockProvider();
IBlockAdapter adapter = provider.adapt(blockType);

// Create the block, set the SimulinkReference
Block createdBlock = adapter.getBlock(this);
Block createdBlock = adapter.getBlock(this.getImportMode());
String blockFQN = createdBlock.getSimulinkRef().getFQN();
Handle blockHandle = getBlockHandleCache().get(blockFQN);

BlockDTO dto = new BlockDTO(this, blockHandle, parentSimRef, createdBlock);

createAndSetSimulinkRef(blockName, parentSimRef, createdBlock);

// Process the created blockk
adapter.process(this, parentSimRef, createdBlock);
// Process the created block
adapter.process(dto);
// TODO a null check + return was removed from here for createdBlock - NEEDS TESTING, then delete this message

// Create and set the SimulinkReference of the source block
String blockFQN = createdBlock.getSimulinkRef().getFQN();
MatlabCommand getReferenceBlockFQN = commandFactory.getParam().addParam(blockFQN).addParam("ReferenceBlock");
String sourceBlockFQN = MatlabString.getMatlabStringData(getReferenceBlockFQN.execute());

Expand Down Expand Up @@ -1193,6 +1201,8 @@ private void createPorts() {
*/
private void createAndAddPort(Block parent, PortProvider portProvider, Handle portHandle, String portType) {

PortDTO dto = new PortDTO(this, portHandle, inPorts, outPorts);

// Get the port adapter that contains block creation and processing logic
IPortAdapter portAdapter = portProvider.adapt(portType.toLowerCase());

Expand All @@ -1203,24 +1213,23 @@ private void createAndAddPort(Block parent, PortProvider portProvider, Handle po

// State is a special outport kind
if ("outport".equalsIgnoreCase(portType) || "state".equalsIgnoreCase(portType)) {
port = portAdapter.createPort(this, parent, portHandle, outPorts);
port = portAdapter.createPort(dto, parent);
createAndSetSimulinkRef("outport." + portNumber.toString(), parent.getSimulinkRef(), port);
cachedOutPortHandles.put((OutPort) port, Handle.getHandleData(portHandle));
} else {
// The case for Inport, Trigger, Enable, Ifact
port = portAdapter.createPort(this, parent, portHandle, inPorts);
port = portAdapter.createPort(dto, parent);
createAndSetSimulinkRef("inport." + portNumber.toString(), parent.getSimulinkRef(), port);
}

// Tells if the block contains created and registered subblocks
boolean isParent = parents.contains(parent);
if (isParent) {
// Obtaining the considerable set of OutPortBlocks and InPortBlocks
Set<InPortBlock> inPortBlockSet = inPortBlocks.get(parent.getSimulinkRef().getFQN());
Set<OutPortBlock> outPortBlockSet = outPortBlocks.get(parent.getSimulinkRef().getFQN());
dto.setInPortBlockSet(inPortBlocks.get(parent.getSimulinkRef().getFQN()));
dto.setOutPortBlockSet(outPortBlocks.get(parent.getSimulinkRef().getFQN()));

PortBlock portBlock = portAdapter.connectToBlock(port, portHandle, portNumber, inPortBlockSet,
outPortBlockSet, this);
PortBlock portBlock = portAdapter.connectToBlock(dto, port, portNumber);

// Creating the reference between the port and the portBlock
if (portBlock != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,38 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.Set;

import hu.bme.mit.massif.communication.command.MatlabCommand;
import hu.bme.mit.massif.communication.command.MatlabCommandFactory;
import hu.bme.mit.massif.communication.datatype.CellMatlabData;
import hu.bme.mit.massif.communication.datatype.Handle;
import hu.bme.mit.massif.communication.datatype.IVisitableMatlabData;
import hu.bme.mit.massif.communication.datatype.MatlabString;
import hu.bme.mit.massif.communication.datatype.StructMatlabData;
import hu.bme.mit.massif.simulink.Parameter;
import hu.bme.mit.massif.simulink.SimulinkFactory;
import hu.bme.mit.massif.simulink.api.Importer;
import hu.bme.mit.massif.simulink.api.dto.AbstractImporterDTO;
import hu.bme.mit.massif.simulink.api.dto.PortDTO;
import hu.bme.mit.massif.simulink.api.extension.IParameterImportFilter;

public class ParameterHelper {

public static List<Parameter> collectParameters(Importer traverser, MatlabCommandFactory commandFactory, Handle objectHandle) {
public static List<Parameter> collectParameters(AbstractImporterDTO dto) {
List<Parameter> blockProperties = new LinkedList<Parameter>();

MatlabCommand getAllBlockParameters = commandFactory.customCommand("massif.get_all_parameters", 1).addParam(objectHandle);
MatlabCommand getAllBlockParameters = dto.getCommandFactory().customCommand("massif.get_all_parameters", 1).addParam(dto.getHandle());
Map<String, IVisitableMatlabData> blockPropsMap = StructMatlabData.getStructMatlabDataData(getAllBlockParameters.execute());

Set<IParameterImportFilter> parameterFilters = traverser.getParameterFilters();
Set<IParameterImportFilter> parameterFilters = dto.getParameterFilters();

Set<Entry<String, IVisitableMatlabData>> entries = blockPropsMap.entrySet();
for (Entry<String, IVisitableMatlabData> entry : entries) {
String propertyName = entry.getKey();

boolean isFiltered = false;
for (IParameterImportFilter paramFilter : parameterFilters) {
isFiltered |= paramFilter.filter(commandFactory, propertyName);
isFiltered |= paramFilter.filter(dto.getCommandFactory(), propertyName);
}

if (isFiltered) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@
*
* Contributors:
* Marton Bur, Abel Hegedus, Akos Horvath - initial API and implementation
* Krisztian Gabor Mayer - additional features
*******************************************************************************/
package hu.bme.mit.massif.simulink.api.adapter.block;

import hu.bme.mit.massif.simulink.Block;
import hu.bme.mit.massif.simulink.SimulinkFactory;
import hu.bme.mit.massif.simulink.SimulinkReference;
import hu.bme.mit.massif.simulink.api.Importer;
import hu.bme.mit.massif.simulink.api.dto.BlockDTO;
import hu.bme.mit.massif.simulink.api.util.ImportMode;

/**
* Adapter class for the bus creator
*/
public class BusCreatorAdapter extends DefaultBlockAdapter {

@Override
public Block getBlock(Importer traverser) {
public Block getBlock(ImportMode importMode) {
return SimulinkFactory.eINSTANCE.createBusCreator();
}

@Override
public void process(Importer traverser, SimulinkReference parentSimRef, Block blockToProcess) {
super.process(traverser, parentSimRef, blockToProcess);
public void process(BlockDTO dto) {
super.process(dto);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marton Bur, Abel Hegedus, Akos Horvath - initial API and implementation
* Marton Bur, Abel Hegedus, Akos Horvath - initial API and implementation
* Krisztian Gabor Mayer - additional features
*******************************************************************************/
package hu.bme.mit.massif.simulink.api.adapter.block;

Expand All @@ -26,32 +27,32 @@
import hu.bme.mit.massif.simulink.BusSelector;
import hu.bme.mit.massif.simulink.Parameter;
import hu.bme.mit.massif.simulink.SimulinkFactory;
import hu.bme.mit.massif.simulink.SimulinkReference;
import hu.bme.mit.massif.simulink.api.Importer;
import hu.bme.mit.massif.simulink.api.dto.BlockDTO;
import hu.bme.mit.massif.simulink.api.util.ImportMode;

/**
* Adapter class for the bus selector
*/
public class BusSelectorAdapter extends DefaultBlockAdapter {

@Override
public Block getBlock(Importer traverser) {
public Block getBlock(ImportMode importMode) {
return SimulinkFactory.eINSTANCE.createBusSelector();
}

@Override
public void process(Importer traverser, SimulinkReference parentSimRef, Block blockToProcess) {
super.process(traverser, parentSimRef, blockToProcess);
public void process(BlockDTO dto) {
super.process(dto);

// Cast block to the correct type
BusSelector busSelector = (BusSelector) blockToProcess;
BusSelector busSelector = (BusSelector) dto.getBlockToProcess();

// Cache the FQN for the block
String busSelectorFQN = busSelector.getSimulinkRef().getFQN();
// Cache the command evaluator instance
MatlabCommandFactory commandFactory = traverser.getCommandFactory();
MatlabCommandFactory commandFactory = dto.getCommandFactory();
// Cache bus selector mappings
Map<BusSelector, List<Handle>> selectorToDestinationPorts = traverser.getBusSelectorToDestinationPorts();
Map<BusSelector, List<Handle>> selectorToDestinationPorts = dto.getBusSelectorToDestinationPorts();

// Get relevant properties - don't have to be queried again from MATLAB, super.process() already obtained all
// mask/dialog
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@
* Marton Bur, Abel Hegedus, Akos Horvath - initial API and implementation
* Marton Bur - script-based parameter querying
* Marton Bur - support for parameter filtering
* Krisztian Gabor Mayer - additional features
*******************************************************************************/
package hu.bme.mit.massif.simulink.api.adapter.block;

import java.util.List;

import hu.bme.mit.massif.communication.command.MatlabCommandFactory;
import hu.bme.mit.massif.communication.datatype.Handle;
import hu.bme.mit.massif.simulink.Block;
import hu.bme.mit.massif.simulink.Parameter;
import hu.bme.mit.massif.simulink.SimulinkFactory;
import hu.bme.mit.massif.simulink.SimulinkReference;
import hu.bme.mit.massif.simulink.api.Importer;
import hu.bme.mit.massif.simulink.api.adapter.ParameterHelper;
import hu.bme.mit.massif.simulink.api.dto.BlockDTO;
import hu.bme.mit.massif.simulink.api.util.ImportMode;

/**
* Generic adapter for non-specific blocks. This adapter is used when no adapter is registered for a block type. The
Expand All @@ -30,20 +31,15 @@
public class DefaultBlockAdapter implements IBlockAdapter {

@Override
public Block getBlock(Importer traverser) {
public Block getBlock(ImportMode importMode) {
return SimulinkFactory.eINSTANCE.createBlock();
}

@Override
public void process(Importer traverser, SimulinkReference parentSimRef, Block blockToProcess) {
public void process(BlockDTO dto) {
// The default block processing implementation goes here

MatlabCommandFactory commandFactory = traverser.getCommandFactory();
String blockFQN = blockToProcess.getSimulinkRef().getFQN();

Handle blockHandle = traverser.getBlockHandleCache().get(blockFQN);
List<Parameter> blockProperties = ParameterHelper.collectParameters(traverser, commandFactory, blockHandle);
blockToProcess.getParameters().addAll(blockProperties);
List<Parameter> blockProperties = ParameterHelper.collectParameters(dto);
dto.getBlockToProcess().getParameters().addAll(blockProperties);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,27 @@
*
* Contributors:
* Marton Bur, Abel Hegedus, Akos Horvath - initial API and implementation
* Krisztian Gabor Mayer - additional features
*******************************************************************************/
package hu.bme.mit.massif.simulink.api.adapter.block;

import hu.bme.mit.massif.simulink.Block;
import hu.bme.mit.massif.simulink.SimulinkFactory;
import hu.bme.mit.massif.simulink.SimulinkReference;
import hu.bme.mit.massif.simulink.api.Importer;
import hu.bme.mit.massif.simulink.api.dto.BlockDTO;
import hu.bme.mit.massif.simulink.api.util.ImportMode;

/**
* Adapter class for the enable port block
*/
public class EnablePortAdapter extends InportBlockAdapter {

@Override
public Block getBlock(Importer traverser) {
public Block getBlock(ImportMode importMode) {
return SimulinkFactory.eINSTANCE.createEnableBlock();
}

@Override
public void process(Importer traverser, SimulinkReference parentSimRef, Block blockToProcess) {
super.process(traverser, parentSimRef, blockToProcess);
public void process(BlockDTO dto) {
super.process(dto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,32 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Marton Bur, Abel Hegedus, Akos Horvath - initial API and implementation
* Marton Bur, Abel Hegedus, Akos Horvath - initial API and implementation
* Krisztian Gabor Mayer - additional features
*******************************************************************************/
package hu.bme.mit.massif.simulink.api.adapter.block;

import hu.bme.mit.massif.simulink.Block;
import hu.bme.mit.massif.simulink.From;
import hu.bme.mit.massif.simulink.SimulinkFactory;
import hu.bme.mit.massif.simulink.SimulinkReference;
import hu.bme.mit.massif.simulink.api.Importer;
import hu.bme.mit.massif.simulink.api.dto.BlockDTO;
import hu.bme.mit.massif.simulink.api.util.ImportMode;

/**
* Adapter class for the from block
*/
public class FromAdapter extends DefaultBlockAdapter {

@Override
public Block getBlock(Importer traverser) {
public Block getBlock(ImportMode importMode) {
return SimulinkFactory.eINSTANCE.createFrom();
}

@Override
public void process(Importer traverser, SimulinkReference parentSimRef, Block blockToProcess) {
super.process(traverser, parentSimRef, blockToProcess);
From fromBlock = (From) blockToProcess;
traverser.getFroms().put(fromBlock.getSimulinkRef().getFQN().replaceAll("\n", " "), fromBlock);
public void process(BlockDTO dto) {
super.process(dto);
From fromBlock = (From) dto.getBlockToProcess();
dto.getFroms().put(fromBlock.getSimulinkRef().getFQN().replaceAll("\n", " "), fromBlock);
// This block is processed further during the creation of connections between blocks
}

Expand Down
Loading