blob: b51adec81a96ed9aeb89703996ce11f481a50035 [file] [log] [blame]
package org.eclipse.papyrus.iotml.sna.codegen
import org.eclipse.papyrus.iotml.sensinact.ServiceProvider
import org.eclipse.uml2.uml.Class
import org.eclipse.uml2.uml.Package
import org.eclipse.uml2.uml.util.UMLUtil
import org.eclipse.papyrus.infra.tools.file.ProjectBasedFileAccess
import org.eclipse.uml2.uml.Type
import org.eclipse.uml2.uml.Enumeration
import org.eclipse.uml2.uml.Property
import org.eclipse.uml2.uml.InstanceValue
import org.eclipse.uml2.uml.EnumerationLiteral
import static extension org.eclipse.papyrus.iotml.sna.codegen.utils.IoTGenUtils.getSensorData
import static extension org.eclipse.papyrus.iotml.sna.codegen.utils.IoTGenUtils.getActions
import static extension org.eclipse.papyrus.iotml.sna.codegen.utils.IoTGenUtils.getStateVars
import static extension org.eclipse.papyrus.iotml.sna.codegen.utils.IoTGenUtils.getNonPortAttr
import org.eclipse.papyrus.iotml.sensinact.Action
import org.eclipse.papyrus.iotml.sensinact.StateVariable
import org.eclipse.papyrus.iotml.sensinact.SensorData
class CreateXML {
static def void createXML(ProjectBasedFileAccess pba, Package model) {
for (pe : model.packagedElements) {
if (pe instanceof Class) {
val sp = UMLUtil.getStereotypeApplication(pe, ServiceProvider)
if (sp !== null) {
pba.generateFile('''«pe.name»-resource.xml''', createXML(pe as Class))
}
}
else if (pe instanceof Package) {
createXML(pba, pe as Package)
}
}
}
static def String createXML(Class serviceProvider) {
createXMLResources(serviceProvider, serviceProvider.nearestPackage.getPackagedElement("Resources") as Package)
}
static def String createXMLResources(Class serviceProvider, Package resourcePkg) '''
<!--
~ Copyright (c) 2020 CEA.
~ All rights reserved. This program and the accompanying materials
~ are made available under the terms of the Eclipse Public License v1.0
~ which accompanies this distribution, and is available at
~ http://www.eclipse.org/legal/epl-v10.html
~
~ Contributors:
~ CEA - initial API and implementation
-->
<resourceInfos xmlns="http://org.eclipse.sensinact/resource"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://org.eclipse.sensinact/resource ../../../../../platform/sensinact-generic/src/main/resources/sensinact-resource.xsd">
«FOR action : resourcePkg.sensorData»
«action.createSensorData»
«ENDFOR»
«FOR action : resourcePkg.stateVars»
«action.createStateVars»
«ENDFOR»
«FOR action : resourcePkg.actions»
«action.createAction»
«ENDFOR»
<devices>
<device identifier="«serviceProvider.name»">
<service name="Control"/>
</device>
</devices>
</resourceInfos>
'''
static def String createSensorData(Class sensorData) '''
«val sensorDataSt = UMLUtil.getStereotypeApplication(sensorData, SensorData)»
<resourceInfo xsi:type="resourceInfoSensor" name="«sensorDataSt.friendlyName»" target="control">
<identifier xsi:type="stringContent">«sensorData.qualifiedName»</identifier>
«IF sensorData.nonPortAttr.size > 0»
<attributes>
«FOR attribute : sensorData.nonPortAttr»
<attribute name="«attribute.name»" type="string" modifiable="UPDATABLE">
<value>«attribute.defaultVal»</value>
«attribute.type.enumConstraints»
</attribute>
«ENDFOR»
</attributes>
«ENDIF»
</resourceInfo>
'''
static def String createStateVars(Class stateVar) '''
«val stateVarSt = UMLUtil.getStereotypeApplication(stateVar, StateVariable)»
<resourceInfo xsi:type="resourceInfoSensor" name="«stateVarSt.friendlyName»" target="control">
<identifier xsi:type="stringContent">«stateVar.qualifiedName»</identifier>
«IF stateVar.nonPortAttr.size > 0»
<attributes>
«FOR attribute : stateVar.nonPortAttr»
<attribute name="«attribute.name»" type="string" modifiable="UPDATABLE">
<value>«attribute.defaultVal»</value>
«attribute.type.enumConstraints»
</attribute>
«ENDFOR»
</attributes>
«ENDIF»
</resourceInfo>
'''
/**
* Create XML code for an action
*/
static def String createAction(Class action) '''
«val actionSt = UMLUtil.getStereotypeApplication(action, Action)»
<resourceInfo xsi:type="resourceInfoSensor" name="«actionSt.friendlyName»" target="control">
<identifier xsi:type="stringContent">«action.qualifiedName»</identifier>
«IF action.nonPortAttr.size > 0»
<attributes>
«FOR attribute : action.nonPortAttr»
<attribute name="«attribute.name»" type="string" modifiable="UPDATABLE">
<value>«attribute.defaultVal»</value>
«attribute.type.enumConstraints»
</attribute>
«ENDFOR»
</attributes>
«ENDIF»
«IF action.operations.size > 0»
<methods>
«FOR method : action.operations»
<method name="«method.name»" type="string" type="ACT">
«FOR parameter : method.ownedParameters»
<parameter name="«parameter.name»" type="«parameter.type.name»"/>
«ENDFOR»
</mehod>
«ENDFOR»
</methods>
«ENDIF»
</resourceInfo>
'''
static def String defaultVal(Property attribute) {
val defaultValue = attribute.defaultValue
if (defaultValue instanceof InstanceValue) {
val instance = (defaultValue as InstanceValue).instance
if (instance instanceof EnumerationLiteral) {
return (instance as EnumerationLiteral).label
}
}
if (defaultValue !== null) {
return defaultValue.stringValue
}
}
static def enumConstraints(Type type) '''
«IF type instanceof Enumeration»
<constraints>
<pattern value="(«FOR lit : (type as Enumeration).ownedLiterals SEPARATOR '|'»(«lit.name»)«ENDFOR»)"/>
</constraints>
«ENDIF»
'''
}