Commit 005e023e authored by Hafiz Muhammad Wahaj Aziz's avatar Hafiz Muhammad Wahaj Aziz
Browse files

latest cosimulation developments

refs 3055
parent db03e153
CosimulationContextMenuContributor.java fd1cb7b9a1aa140d730397b76e642083cd949dbe GREEN
CosimulationExecutionTarget.java 0dd304aaab233aa88998995d39d3f37acbcdb80d GREEN
CosimulationSpecificationEditor.java 6bb527d9c0d62f870d0edccccf674971735ca1a0 GREEN
CosimulationSpecificationEditorBinding.java 80b711cdbb84426981b81825ae7693803ce8964e GREEN
FMUSpecificationEditor.java 92da0a286866fe1d434f17342e191c9dec8fadaf GREEN
FMUSpecificationEditorBinding.java a07e7bc6eb6065a1d8336112bafbbefead15d4e9 GREEN
/*--------------------------------------------------------------------------+
$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
| |
| Copyright 2017 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.cosimulation.ui.editor;
import static org.apache.commons.io.FileUtils.deleteDirectory;
import static org.eclipse.ui.PlatformUI.getWorkbench;
import static org.fortiss.af3.component.utils.ComponentModelElementFactory.createInputPort;
import static org.fortiss.af3.component.utils.ComponentModelElementFactory.createOutputPort;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.boolType;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.createNoVal;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.doubleType;
import static org.fortiss.af3.expression.utils.ExpressionModelElementFactory.intType;
import static org.fortiss.tooling.kernel.ui.util.DataBindingUtils.bind;
import static org.fortiss.tooling.kernel.ui.util.WidgetsFactory.createTextWithUndo;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.fortiss.af3.component.model.Component;
import org.fortiss.af3.component.model.InputPort;
import org.fortiss.af3.component.model.Port;
import org.fortiss.af3.cosimulation.model.AF3CosimulationPackage;
import org.fortiss.af3.cosimulation.model.FMUSpecification;
import org.fortiss.af3.project.model.typesystem.IType;
import org.fortiss.tooling.base.ui.editor.FormsEditorBase;
import org.fortiss.tooling.kernel.extension.data.ITopLevelElement;
import org.fortiss.tooling.kernel.service.IPersistencyService;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
/**
* Editor for {@link FMUSpecification}
*
* @author aziz
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating GREEN Hash: C0D10DFC53D72DBDF0FF18542E1F7801
*/
public class FMUSpecificationEditor extends FormsEditorBase<FMUSpecification> {
/** Cached form object. */
private ScrolledForm specForm;
/** To store the file path from the user */
private String filePath;
/** {@inheritDoc} */
@Override
protected void createSections() {
pathSection();
specForm.reflow(true);
}
/** Section to define the parameter FMUPath of the FMU. */
private void pathSection() {
GridDataFactory layout = GridDataFactory.fillDefaults().grab(true, false);
Composite composeFMUSection =
createNewSection("FMU Specification", "Provide the path for FMU:");
composeFMUSection.setLayout(new GridLayout(2, false));
toolkit.createLabel(composeFMUSection, "FMU Path");
Text hostText = createTextWithUndo(toolkit, composeFMUSection, "");
hostText.setEditable(false);
layout.applyTo(hostText);
bind(dbc, hostText, getEditedObject(),
AF3CosimulationPackage.Literals.FMU_SPECIFICATION__FMU_PATH);
toolkit.createLabel(composeFMUSection, "Please provide the path for FMU (.fmu):");
Button browse = new Button(composeFMUSection, SWT.NONE);
browse.setText("Browse");
browse.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
switch(e.type) {
case SWT.Selection:
try {
executeFileExtractionAndCreateIOPorts(editedObject, true);
} catch(IOException ioex) {
throw new RuntimeException("[FMU import] I/O error: " +
ioex.getMessage());
}
break;
}
}
});
toolkit.createLabel(composeFMUSection, "Press to update the component:");
Button update = new Button(composeFMUSection, SWT.NONE);
update.setText("Update");
update.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
switch(e.type) {
case SWT.Selection:
try {
executeFileExtractionAndCreateIOPorts(editedObject, false);
} catch(IOException ioex) {
MessageDialog.openInformation(getEditorSite().getShell(),
"FMU not found", "FMU does not exist in the provided path(" +
filePath + ")!");
throw new RuntimeException("[FMU Update] FMU not found error: " +
ioex.getMessage());
}
break;
}
}
});
}
/** {@inheritDoc} */
@Override
protected void setupScrolledForm(final ScrolledForm form) {
this.specForm = form;
}
/**
* Unzip the FMU and extract the XML file in the temporary build directory. Then, read the
* extracted file to generate the I/O ports for component.
*/
private void executeFileExtractionAndCreateIOPorts(FMUSpecification elt, boolean takeInput)
throws IOException {
ITopLevelElement modelContext =
IPersistencyService.getInstance().getTopLevelElementFor(elt);
String tempBuildDirectoryPath =
Files.createTempDirectory("temporaryXMLFileDirectory").toString();
filePath = takeInput ? fileDialog() : elt.getFMUPath();
modelContext.runAsCommand(() -> elt.setFMUPath(filePath));
extractXMLFile(tempBuildDirectoryPath);
readXMLFileAndCreatePorts(tempBuildDirectoryPath + File.separator + "modelDescription.xml",
elt.getComponent());
deleteDirectory(new File(tempBuildDirectoryPath));
}
/** Unzip the FMU and extract modelDescription.xml file. */
private void extractXMLFile(String xmlDestinationDirectory) throws IOException {
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(filePath));
ZipEntry entry = zipIn.getNextEntry();
while(entry != null) {
String path = xmlDestinationDirectory + File.separator + entry.getName();
if(!entry.isDirectory() && entry.getName().equals("modelDescription.xml")) {
extractFile(zipIn, path);
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
/** Extract XML file in the temporary build directory. */
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[4096];
int read = 0;
while((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
/** Read XML file to generate the I/O ports for component. */
private void readXMLFileAndCreatePorts(String xmlPath, Component fmuContainer) {
ITopLevelElement modelContext =
IPersistencyService.getInstance().getTopLevelElementFor(fmuContainer);
modelContext
.runAsCommand(() -> {
ArrayList<Port> newOutputPorts = new ArrayList<>();
ArrayList<Port> newInputPorts = new ArrayList<>();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler =
parseXMLAndCreateOrModifyPorts(fmuContainer, newOutputPorts,
newInputPorts);
saxParser.parse(xmlPath, handler);
} catch(Exception e) {
e.printStackTrace();
}
deleteExtraPorts(newInputPorts, newOutputPorts, fmuContainer);
});
}
/** Parse XML and create or modify ports. */
private DefaultHandler parseXMLAndCreateOrModifyPorts(Component fmuContainer,
ArrayList<Port> newOutputPorts, ArrayList<Port> newInputPorts) {
DefaultHandler handler = new DefaultHandler() {
String portName;
boolean isType = false;
boolean isInput = false;
IType type = null;
@Override
public void startElement(String uri, String localName, String startElementName,
Attributes attributes) {
if(startElementName.equals("ScalarVariable")) {
portName = attributes.getValue("name");
isInput = attributes.getValue("causality").equals("input");
}
if(startElementName.equals("Real")) {
type = doubleType();
isType = true;
}
if(startElementName.equals("Integer")) {
type = intType();
isType = true;
}
if(startElementName.equals("Boolean")) {
type = boolType();
isType = true;
}
}
@Override
public void characters(char ch[], int start, int length) {
if(isType) {
Port existingPort = fmuContainer.findInputPort(portName);
if(existingPort != null) {
existingPort.getPortSpecification().setType(type);
newInputPorts.add(isInput ? existingPort : null);
newOutputPorts.add(isInput ? null : existingPort);
} else {
Port port;
if(isInput) {
port = createInputPort(portName, "", type, createNoVal());
} else {
port = createOutputPort(portName, "", type, createNoVal());
}
fmuContainer.getConnectors().add(port);
newInputPorts.add(isInput ? port : null);
newOutputPorts.add(isInput ? null : port);
}
isType = false;
}
}
};
return handler;
}
/** Delete extra input and output ports. */
private void deleteExtraPorts(ArrayList<Port> newInputPorts, ArrayList<Port> newOutputPorts,
Component fmuContainer) {
ArrayList<Port> ports = new ArrayList<>(fmuContainer.getInputPorts());
ports.addAll(fmuContainer.getOutputPorts());
for(Port p : ports) {
if(p instanceof InputPort ? !newInputPorts.contains(p) : !newOutputPorts.contains(p)) {
fmuContainer.getConnectors().remove(p);
}
}
}
/** Open the file dialog and ask to provide the path for the file (.fmu). */
private String fileDialog() {
Shell shell = getWorkbench().getActiveWorkbenchWindow().getShell();
FileDialog fd = new FileDialog(shell, SWT.OPEN);
fd.setText("Provide FMU file");
fd.setFilterPath("C:/");
String[] filterExt = {"*.fmu"};
fd.setFilterExtensions(filterExt);
return fd.open();
}
}
/*--------------------------------------------------------------------------+
$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
| |
| Copyright 2017 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.cosimulation.ui.editor;
import org.eclipse.ui.IEditorPart;
import org.fortiss.af3.cosimulation.model.FMUSpecification;
import org.fortiss.tooling.kernel.ui.extension.base.ModelEditorBindingBase;
/**
* Editor binding for {@link FMUSpecificationEditor}.
*
* @author aziz
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating RED Hash:
*/
public class FMUSpecificationEditorBinding extends ModelEditorBindingBase<FMUSpecification> {
/** {@inheritDoc} */
@Override
public Class<? extends IEditorPart> getEditorClass() {
return FMUSpecificationEditor.class;
}
/** {@inheritDoc} */
@Override
public String getLabel() {
return "FMU Specification Editor";
}
/** {@inheritDoc} */
@Override
public int getPriority() {
return 0;
}
}
CosimulationSpecificationHandler.java cebe7b9bcd49e844a4288f7b612136cf01fe964c GREEN
FMUSpecificationHandler.java d54ddee0066f20dd969e68db4f643a71cac7595f GREEN
/*--------------------------------------------------------------------------+
$Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
| |
| Copyright 2017 fortiss GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.af3.cosimulation.ui.handler;
import static org.conqat.lib.commons.string.StringUtils.isEmpty;
import org.eclipse.jface.resource.ImageDescriptor;
import org.fortiss.af3.component.ui.handler.IComponentBehaviorDefinitionHandler;
import org.fortiss.af3.cosimulation.model.FMUSpecification;
import org.fortiss.af3.cosimulation.ui.AF3CosimulationUIActivator;
import org.fortiss.tooling.kernel.ui.extension.base.ModelElementHandlerBase;
/**
* Handler for {@link FMUSpecification}s.
*
* @author aziz
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating RED Hash: 38C0193B339E49C06F8ADA57F04BD5EB
*/
public class FMUSpecificationHandler extends ModelElementHandlerBase<FMUSpecification> implements
IComponentBehaviorDefinitionHandler {
/** {@inheritDoc} */
@Override
public ImageDescriptor getIconImageDescriptor() {
return AF3CosimulationUIActivator.getImageDescriptor("icons/FMU_16x16.png");
}
/** {@inheritDoc} */
@Override
public String getName(FMUSpecification element) {
if(isEmpty(element.getName())) {
return "FMU Specification";
}
return element.getName() + " (FMU Specification)";
}
}
CosimulationServiceImpl.java 6dc5d73d991a645e701288534dd416f950acc6fb GREEN
CosimulationServiceManager.java 8f6b63546e917938217bc8be5bb5e199e9b6de5e GREEN
ICosimulationService.java be82fa6e0df4b6a3a500bc5160b540c7d5645381 GREEN
CosimulationServiceUtils.java 1402302d0c0342bd6b7962f2a3269f7467ad8266 GREEN
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment