| package org.eclipse.papyrus.iotml.sna.codegen |
| |
| import org.eclipse.uml2.uml.Package |
| import org.eclipse.uml2.uml.Class |
| import org.eclipse.uml2.uml.StateMachine |
| import static extension org.eclipse.papyrus.designer.transformation.library.xtend.StateMachineUtil.* |
| import org.eclipse.uml2.uml.CallEvent |
| import org.eclipse.uml2.uml.Transition |
| import org.eclipse.uml2.uml.ValueSpecification |
| import org.eclipse.uml2.uml.OpaqueBehavior |
| import org.eclipse.uml2.uml.OpaqueExpression |
| import org.eclipse.uml2.uml.util.UMLUtil |
| import org.eclipse.papyrus.iotml.sensinact.Resource |
| import org.eclipse.uml2.uml.Namespace |
| import org.eclipse.papyrus.iotml.sensinact.Action |
| import org.eclipse.uml2.uml.Pseudostate |
| |
| class SensiNact { |
| static def String sensinact(Package pkg) ''' |
| // resources |
| «pkg.resources» |
| |
| «pkg.statemachines» |
| ''' |
| |
| static def String resources(Package pkg) { |
| var out = "" |
| for (pe : pkg.packagedElements) { |
| // resource «resource.name» |
| if (pe instanceof Package) { |
| out += resources(pe as Package) |
| } |
| else if (pe instanceof Class) { |
| val clazz = pe as Class |
| val resource = UMLUtil.getStereotypeApplication(clazz, Resource) |
| val behavior = clazz.classifierBehavior |
| if (resource !== null || behavior instanceof StateMachine) { |
| out += ''' |
| resource «clazz.name»=/«clazz.qualifiedName.replace(Namespace.SEPARATOR, "/")» |
| ''' |
| } |
| } |
| } |
| return out; |
| } |
| |
| static def String statemachines(Package pkg) { |
| var out = "" |
| for (pe : pkg.packagedElements) { |
| // resource «resource.name» |
| if (pe instanceof Package) { |
| out += statemachines(pe as Package) |
| } |
| else if (pe instanceof Class) { |
| val clazz = pe as Class |
| val behavior = clazz.classifierBehavior |
| if (behavior instanceof StateMachine) { |
| out += createSM(behavior as StateMachine) |
| } |
| } |
| } |
| return out; |
| } |
| |
| static def createSM(StateMachine sm) ''' |
| // ------- for state-machine «sm.name» -------- |
| «FOR state : sm.region.states» |
| «ENDFOR» |
| |
| «FOR transition : sm.region.transitions SEPARATOR ""» |
| «IF !(transition.source instanceof Pseudostate)» |
| // from <«transition.source.name»> to <«transition.target.name»> |
| «IF transition.triggers.size > 0» |
| «val trigger = transition.triggers.get(0)» |
| «IF trigger !== null && trigger.event instanceof CallEvent» |
| «val ce = trigger.event as CallEvent» |
| on «ce.name» do |
| «ELSE» |
| no-call-event |
| «ENDIF» |
| «ELSE» |
| on default |
| «ENDIF» |
| «transition.conditionAction» |
| |
| «ENDIF» |
| «ENDFOR» |
| |
| ''' |
| |
| static def conditionAction(Transition transition) ''' |
| «IF transition.guard !== null» |
| if «transition.guard.specification.guardStr» do |
| «transition.effectStr» |
| end if; |
| «ELSE» |
| «transition.effectStr» |
| «ENDIF» |
| ''' |
| |
| static def guardStr(ValueSpecification vs) ''' |
| «IF vs instanceof OpaqueExpression» |
| «val oe = vs as OpaqueExpression» |
| «oe.bodies.get(0) |
| »«ENDIF»''' |
| |
| static def effectStr(Transition transition) ''' |
| «IF transition.effect instanceof OpaqueBehavior» |
| «val ob = transition.effect as OpaqueBehavior» |
| «val action = UMLUtil.getStereotypeApplication(ob, Action)» |
| «IF action !== null» |
| ««« «action.state.base_Class.name».act(); |
| «ELSE» |
| «ob.bodies.get(0)» |
| «ENDIF» |
| «ELSE» |
| default-body |
| «ENDIF» |
| ''' |
| } |