| package org.eclipse.papyrus.ease.test.fmi; |
| |
| import java.util.ArrayList; |
| import java.util.Collection; |
| import java.util.List; |
| |
| import org.eclipse.papyrus.moka.fmi.fmiprofile.CS_FMU; |
| import org.eclipse.uml2.uml.AggregationKind; |
| import org.eclipse.uml2.uml.Class; |
| import org.eclipse.uml2.uml.Connector; |
| import org.eclipse.uml2.uml.ConnectorEnd; |
| import org.eclipse.uml2.uml.NamedElement; |
| import org.eclipse.uml2.uml.Property; |
| import org.eclipse.uml2.uml.util.UMLUtil; |
| |
| public class FMISimulatorHandler { |
| |
| private Class simClass; |
| |
| |
| public FMISimulatorHandler(Class simulatorClass) { |
| simClass = simulatorClass; |
| |
| } |
| |
| public List<FMUInstanceHandler> getInstances() { |
| List<FMUInstanceHandler> result = new ArrayList<>(); |
| for (Property prop : simClass.getAllAttributes()) { |
| CS_FMU fmu = UMLUtil.getStereotypeApplication(prop.getType(), CS_FMU.class); |
| if (fmu != null) { |
| result.add(new FMUInstanceHandler(prop)); |
| } |
| } |
| |
| return result; |
| |
| } |
| |
| public List<ConnectorHandler> getConnectors() { |
| List<ConnectorHandler> result = new ArrayList<>(); |
| for (Connector connector : simClass.getOwnedConnectors()) { |
| result.add(new ConnectorHandler(connector)); |
| } |
| |
| return result; |
| } |
| |
| public String getName() { |
| return simClass.getName(); |
| } |
| |
| public FMUInstanceHandler instantiateFMU(String fmuQN, String instanceName) { |
| Collection<NamedElement> fmuTypes = UMLUtil.findNamedElements(simClass.eResource().getResourceSet(), fmuQN); |
| Class fmuType = null; |
| FMUInstanceHandler result = null; |
| for (NamedElement namedElem : fmuTypes) { |
| if (namedElem instanceof Class && UMLUtil.getStereotypeApplication(namedElem, CS_FMU.class) != null) { |
| fmuType = (Class) namedElem; |
| } |
| } |
| |
| if (fmuType != null) { |
| Property fmuProp = simClass.createOwnedAttribute(instanceName, fmuType); |
| fmuProp.setAggregation(AggregationKind.COMPOSITE_LITERAL); |
| result = new FMUInstanceHandler(fmuProp); |
| |
| } else { |
| throw new RuntimeException("Failed to find FMU type " + fmuQN); |
| |
| } |
| |
| return result; |
| |
| } |
| |
| public ConnectorHandler addConnection(String sourcePort, String targetPort) { |
| ConnectorHandler result = null; |
| FMIPortHandler sourcePortHandler = getInstancePort(sourcePort); |
| FMIPortHandler targetPortHandler = getInstancePort(targetPort); |
| |
| if (sourcePortHandler!= null && targetPortHandler !=null) { |
| Connector connector = simClass.createOwnedConnector(sourcePort+"_"+targetPort); |
| ConnectorEnd sourceEnd = connector.createEnd(); |
| sourceEnd.setPartWithPort(sourcePortHandler.getUMLPart()); |
| sourceEnd.setRole(sourcePortHandler.getUMLPort()); |
| |
| ConnectorEnd targetEnd = connector.createEnd(); |
| targetEnd.setPartWithPort(targetPortHandler.getUMLPart()); |
| targetEnd.setRole(targetPortHandler.getUMLPort()); |
| |
| return new ConnectorHandler(connector); |
| |
| |
| |
| } |
| System.out.println("ERROR: Failed to create connector between " + sourcePort +" and "+ targetPort); |
| throw new RuntimeException("Failed to create connector between " + sourcePort +" and "+ targetPort); |
| |
| } |
| |
| public FMIPortHandler getInstancePort(String portName) { |
| |
| int dotIndex= portName.indexOf(':'); |
| if (dotIndex==-1) { |
| throw new RuntimeException("Invalid instance portName " + portName); |
| } |
| |
| String instanceName = portName.substring(0, dotIndex); |
| |
| |
| FMUInstanceHandler instance = getInstance(instanceName); |
| if (instance != null) { |
| |
| FMIPortHandler result = instance.getPort(portName.substring(dotIndex+1)); |
| if (result != null) { |
| return result; |
| } |
| |
| } |
| return null; |
| |
| } |
| |
| private FMUInstanceHandler getInstance(String instanceName) { |
| for (Property prop : simClass.getAllAttributes()) { |
| if (prop.getName() != null && prop.getName().equals(instanceName)) { |
| return new FMUInstanceHandler(prop); |
| } |
| } |
| System.out.println("Could not find instance " + instanceName); |
| throw new RuntimeException("Could not find instance " + instanceName); |
| |
| |
| } |
| |
| } |