| package org.eclipse.papyrus.iotml.sna.codegen; |
| |
| import java.util.ArrayList; |
| import org.eclipse.emf.common.util.EList; |
| import org.eclipse.papyrus.infra.tools.file.ProjectBasedFileAccess; |
| import org.eclipse.papyrus.iotml.sensinact.Action; |
| import org.eclipse.papyrus.iotml.sensinact.SensorData; |
| import org.eclipse.papyrus.iotml.sensinact.ServiceProvider; |
| import org.eclipse.papyrus.iotml.sensinact.StateVariable; |
| import org.eclipse.papyrus.iotml.sna.codegen.utils.IoTGenUtils; |
| import org.eclipse.uml2.uml.Enumeration; |
| import org.eclipse.uml2.uml.EnumerationLiteral; |
| import org.eclipse.uml2.uml.InstanceSpecification; |
| import org.eclipse.uml2.uml.InstanceValue; |
| import org.eclipse.uml2.uml.Operation; |
| import org.eclipse.uml2.uml.PackageableElement; |
| import org.eclipse.uml2.uml.Parameter; |
| import org.eclipse.uml2.uml.Property; |
| import org.eclipse.uml2.uml.Type; |
| import org.eclipse.uml2.uml.ValueSpecification; |
| import org.eclipse.uml2.uml.util.UMLUtil; |
| import org.eclipse.xtend2.lib.StringConcatenation; |
| |
| @SuppressWarnings("all") |
| public class CreateXML { |
| public static void createXML(final ProjectBasedFileAccess pba, final org.eclipse.uml2.uml.Package model) { |
| EList<PackageableElement> _packagedElements = model.getPackagedElements(); |
| for (final PackageableElement pe : _packagedElements) { |
| if ((pe instanceof org.eclipse.uml2.uml.Class)) { |
| final ServiceProvider sp = UMLUtil.<ServiceProvider>getStereotypeApplication(pe, ServiceProvider.class); |
| if ((sp != null)) { |
| StringConcatenation _builder = new StringConcatenation(); |
| String _name = ((org.eclipse.uml2.uml.Class)pe).getName(); |
| _builder.append(_name); |
| _builder.append("-resource.xml"); |
| pba.generateFile(_builder.toString(), CreateXML.createXML(((org.eclipse.uml2.uml.Class) pe))); |
| } |
| } else { |
| if ((pe instanceof org.eclipse.uml2.uml.Package)) { |
| CreateXML.createXML(pba, ((org.eclipse.uml2.uml.Package) pe)); |
| } |
| } |
| } |
| } |
| |
| public static String createXML(final org.eclipse.uml2.uml.Class serviceProvider) { |
| PackageableElement _packagedElement = serviceProvider.getNearestPackage().getPackagedElement("Resources"); |
| return CreateXML.createXMLResources(serviceProvider, ((org.eclipse.uml2.uml.Package) _packagedElement)); |
| } |
| |
| public static String createXMLResources(final org.eclipse.uml2.uml.Class serviceProvider, final org.eclipse.uml2.uml.Package resourcePkg) { |
| StringConcatenation _builder = new StringConcatenation(); |
| _builder.append("<!--"); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("~ Copyright (c) 2020 CEA."); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("~ All rights reserved. This program and the accompanying materials"); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("~ are made available under the terms of the Eclipse Public License v1.0"); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("~ which accompanies this distribution, and is available at"); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("~ http://www.eclipse.org/legal/epl-v10.html"); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("~"); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("~ Contributors:"); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("~ CEA - initial API and implementation"); |
| _builder.newLine(); |
| _builder.append(" "); |
| _builder.append("-->"); |
| _builder.newLine(); |
| _builder.newLine(); |
| _builder.append("<resourceInfos xmlns=\"http://org.eclipse.sensinact/resource\""); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("xsi:schemaLocation=\"http://org.eclipse.sensinact/resource ../../../../../platform/sensinact-generic/src/main/resources/sensinact-resource.xsd\">"); |
| _builder.newLine(); |
| { |
| ArrayList<org.eclipse.uml2.uml.Class> _sensorData = IoTGenUtils.getSensorData(resourcePkg); |
| for(final org.eclipse.uml2.uml.Class action : _sensorData) { |
| _builder.append("\t"); |
| String _createSensorData = CreateXML.createSensorData(action); |
| _builder.append(_createSensorData, "\t"); |
| _builder.newLineIfNotEmpty(); |
| } |
| } |
| { |
| ArrayList<org.eclipse.uml2.uml.Class> _stateVars = IoTGenUtils.getStateVars(resourcePkg); |
| for(final org.eclipse.uml2.uml.Class action_1 : _stateVars) { |
| _builder.append("\t"); |
| String _createStateVars = CreateXML.createStateVars(action_1); |
| _builder.append(_createStateVars, "\t"); |
| _builder.newLineIfNotEmpty(); |
| } |
| } |
| { |
| ArrayList<org.eclipse.uml2.uml.Class> _actions = IoTGenUtils.getActions(resourcePkg); |
| for(final org.eclipse.uml2.uml.Class action_2 : _actions) { |
| _builder.append("\t"); |
| String _createAction = CreateXML.createAction(action_2); |
| _builder.append(_createAction, "\t"); |
| _builder.newLineIfNotEmpty(); |
| } |
| } |
| _builder.append("\t"); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("<devices>"); |
| _builder.newLine(); |
| _builder.append("\t\t"); |
| _builder.append("<device identifier=\""); |
| String _name = serviceProvider.getName(); |
| _builder.append(_name, "\t\t"); |
| _builder.append("\">"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t\t\t"); |
| _builder.append("<service name=\"Control\"/>"); |
| _builder.newLine(); |
| _builder.append("\t\t"); |
| _builder.append("</device>"); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("</devices>"); |
| _builder.newLine(); |
| _builder.append("</resourceInfos>"); |
| _builder.newLine(); |
| return _builder.toString(); |
| } |
| |
| public static String createSensorData(final org.eclipse.uml2.uml.Class sensorData) { |
| StringConcatenation _builder = new StringConcatenation(); |
| final SensorData sensorDataSt = UMLUtil.<SensorData>getStereotypeApplication(sensorData, SensorData.class); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("<resourceInfo xsi:type=\"resourceInfoSensor\" name=\""); |
| String _friendlyName = sensorDataSt.getFriendlyName(); |
| _builder.append(_friendlyName); |
| _builder.append("\" target=\"control\">"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("<identifier xsi:type=\"stringContent\">"); |
| String _qualifiedName = sensorData.getQualifiedName(); |
| _builder.append(_qualifiedName, "\t"); |
| _builder.append("</identifier>"); |
| _builder.newLineIfNotEmpty(); |
| { |
| int _size = IoTGenUtils.getNonPortAttr(sensorData).size(); |
| boolean _greaterThan = (_size > 0); |
| if (_greaterThan) { |
| _builder.append("\t"); |
| _builder.append("<attributes>"); |
| _builder.newLine(); |
| { |
| ArrayList<Property> _nonPortAttr = IoTGenUtils.getNonPortAttr(sensorData); |
| for(final Property attribute : _nonPortAttr) { |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("<attribute name=\""); |
| String _name = attribute.getName(); |
| _builder.append(_name, "\t\t"); |
| _builder.append("\" type=\"string\" modifiable=\"UPDATABLE\">"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("<value>"); |
| String _defaultVal = CreateXML.defaultVal(attribute); |
| _builder.append(_defaultVal, "\t\t\t"); |
| _builder.append("</value>"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| CharSequence _enumConstraints = CreateXML.enumConstraints(attribute.getType()); |
| _builder.append(_enumConstraints, "\t\t\t"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("</attribute>"); |
| _builder.newLine(); |
| } |
| } |
| _builder.append("\t"); |
| _builder.append("</attributes>"); |
| _builder.newLine(); |
| } |
| } |
| _builder.append("</resourceInfo>"); |
| _builder.newLine(); |
| return _builder.toString(); |
| } |
| |
| public static String createStateVars(final org.eclipse.uml2.uml.Class stateVar) { |
| StringConcatenation _builder = new StringConcatenation(); |
| final StateVariable stateVarSt = UMLUtil.<StateVariable>getStereotypeApplication(stateVar, StateVariable.class); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("<resourceInfo xsi:type=\"resourceInfoSensor\" name=\""); |
| String _friendlyName = stateVarSt.getFriendlyName(); |
| _builder.append(_friendlyName); |
| _builder.append("\" target=\"control\">"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("<identifier xsi:type=\"stringContent\">"); |
| String _qualifiedName = stateVar.getQualifiedName(); |
| _builder.append(_qualifiedName, "\t"); |
| _builder.append("</identifier>"); |
| _builder.newLineIfNotEmpty(); |
| { |
| int _size = IoTGenUtils.getNonPortAttr(stateVar).size(); |
| boolean _greaterThan = (_size > 0); |
| if (_greaterThan) { |
| _builder.append("\t"); |
| _builder.append("<attributes>"); |
| _builder.newLine(); |
| { |
| ArrayList<Property> _nonPortAttr = IoTGenUtils.getNonPortAttr(stateVar); |
| for(final Property attribute : _nonPortAttr) { |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("<attribute name=\""); |
| String _name = attribute.getName(); |
| _builder.append(_name, "\t\t"); |
| _builder.append("\" type=\"string\" modifiable=\"UPDATABLE\">"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("<value>"); |
| String _defaultVal = CreateXML.defaultVal(attribute); |
| _builder.append(_defaultVal, "\t\t\t"); |
| _builder.append("</value>"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| CharSequence _enumConstraints = CreateXML.enumConstraints(attribute.getType()); |
| _builder.append(_enumConstraints, "\t\t\t"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("</attribute>"); |
| _builder.newLine(); |
| } |
| } |
| _builder.append("\t"); |
| _builder.append("</attributes>"); |
| _builder.newLine(); |
| } |
| } |
| _builder.append("</resourceInfo>"); |
| _builder.newLine(); |
| return _builder.toString(); |
| } |
| |
| /** |
| * Create XML code for an action |
| */ |
| public static String createAction(final org.eclipse.uml2.uml.Class action) { |
| StringConcatenation _builder = new StringConcatenation(); |
| final Action actionSt = UMLUtil.<Action>getStereotypeApplication(action, Action.class); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("<resourceInfo xsi:type=\"resourceInfoSensor\" name=\""); |
| String _friendlyName = actionSt.getFriendlyName(); |
| _builder.append(_friendlyName); |
| _builder.append("\" target=\"control\">"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("<identifier xsi:type=\"stringContent\">"); |
| String _qualifiedName = action.getQualifiedName(); |
| _builder.append(_qualifiedName, "\t"); |
| _builder.append("</identifier>"); |
| _builder.newLineIfNotEmpty(); |
| { |
| int _size = IoTGenUtils.getNonPortAttr(action).size(); |
| boolean _greaterThan = (_size > 0); |
| if (_greaterThan) { |
| _builder.append("\t"); |
| _builder.append("<attributes>"); |
| _builder.newLine(); |
| { |
| ArrayList<Property> _nonPortAttr = IoTGenUtils.getNonPortAttr(action); |
| for(final Property attribute : _nonPortAttr) { |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("<attribute name=\""); |
| String _name = attribute.getName(); |
| _builder.append(_name, "\t\t"); |
| _builder.append("\" type=\"string\" modifiable=\"UPDATABLE\">"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("<value>"); |
| String _defaultVal = CreateXML.defaultVal(attribute); |
| _builder.append(_defaultVal, "\t\t\t"); |
| _builder.append("</value>"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| CharSequence _enumConstraints = CreateXML.enumConstraints(attribute.getType()); |
| _builder.append(_enumConstraints, "\t\t\t"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("</attribute>"); |
| _builder.newLine(); |
| } |
| } |
| _builder.append("\t"); |
| _builder.append("</attributes>"); |
| _builder.newLine(); |
| } |
| } |
| { |
| int _size_1 = action.getOperations().size(); |
| boolean _greaterThan_1 = (_size_1 > 0); |
| if (_greaterThan_1) { |
| _builder.append("\t"); |
| _builder.append("<methods>"); |
| _builder.newLine(); |
| { |
| EList<Operation> _operations = action.getOperations(); |
| for(final Operation method : _operations) { |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("<method name=\""); |
| String _name_1 = method.getName(); |
| _builder.append(_name_1, "\t\t"); |
| _builder.append("\" type=\"string\" type=\"ACT\">"); |
| _builder.newLineIfNotEmpty(); |
| { |
| EList<Parameter> _ownedParameters = method.getOwnedParameters(); |
| for(final Parameter parameter : _ownedParameters) { |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("<parameter name=\""); |
| String _name_2 = parameter.getName(); |
| _builder.append(_name_2, "\t\t\t"); |
| _builder.append("\" type=\""); |
| String _name_3 = parameter.getType().getName(); |
| _builder.append(_name_3, "\t\t\t"); |
| _builder.append("\"/>"); |
| _builder.newLineIfNotEmpty(); |
| } |
| } |
| _builder.append("\t"); |
| _builder.append("\t"); |
| _builder.append("</mehod>"); |
| _builder.newLine(); |
| } |
| } |
| _builder.append("\t"); |
| _builder.append("</methods>"); |
| _builder.newLine(); |
| } |
| } |
| _builder.append("</resourceInfo>"); |
| _builder.newLine(); |
| return _builder.toString(); |
| } |
| |
| public static String defaultVal(final Property attribute) { |
| final ValueSpecification defaultValue = attribute.getDefaultValue(); |
| if ((defaultValue instanceof InstanceValue)) { |
| final InstanceSpecification instance = ((InstanceValue) defaultValue).getInstance(); |
| if ((instance instanceof EnumerationLiteral)) { |
| return ((EnumerationLiteral) instance).getLabel(); |
| } |
| } |
| if ((defaultValue != null)) { |
| return defaultValue.stringValue(); |
| } |
| return null; |
| } |
| |
| public static CharSequence enumConstraints(final Type type) { |
| StringConcatenation _builder = new StringConcatenation(); |
| { |
| if ((type instanceof Enumeration)) { |
| _builder.append("<constraints>"); |
| _builder.newLine(); |
| _builder.append("\t"); |
| _builder.append("<pattern value=\"("); |
| { |
| EList<EnumerationLiteral> _ownedLiterals = ((Enumeration) type).getOwnedLiterals(); |
| boolean _hasElements = false; |
| for(final EnumerationLiteral lit : _ownedLiterals) { |
| if (!_hasElements) { |
| _hasElements = true; |
| } else { |
| _builder.appendImmediate("|", "\t"); |
| } |
| _builder.append("("); |
| String _name = lit.getName(); |
| _builder.append(_name, "\t"); |
| _builder.append(")"); |
| } |
| } |
| _builder.append(")\"/>"); |
| _builder.newLineIfNotEmpty(); |
| _builder.append("</constraints>"); |
| _builder.newLine(); |
| } |
| } |
| return _builder; |
| } |
| } |