Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 310526b1998ff3b0ee27e6ecae277915fefaaa70 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package org.eclipse.papyrus.qompass.modellibs.core.xtend

import org.eclipse.emf.common.util.BasicEList
import org.eclipse.emf.common.util.EList
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.papyrus.C_Cpp.Ptr
import org.eclipse.papyrus.FCM.DerivedElement
import org.eclipse.papyrus.qompass.designer.core.StUtils
import org.eclipse.papyrus.qompass.designer.core.Utils
import org.eclipse.papyrus.qompass.designer.core.UMLTool
import org.eclipse.papyrus.qompass.designer.core.extensions.IXtend
import org.eclipse.papyrus.qompass.designer.core.sync.InterfaceSync
import org.eclipse.papyrus.qompass.designer.core.transformations.TransformationContext
import org.eclipse.papyrus.qompass.designer.core.transformations.TransformationException
import org.eclipse.papyrus.uml.tools.utils.StereotypeUtil
import org.eclipse.uml2.uml.Behavior
import org.eclipse.uml2.uml.BehavioredClassifier
import org.eclipse.uml2.uml.CallEvent
import org.eclipse.uml2.uml.Class
import org.eclipse.uml2.uml.OpaqueExpression
import org.eclipse.uml2.uml.Operation
import org.eclipse.uml2.uml.Pseudostate
import org.eclipse.uml2.uml.PseudostateKind
import org.eclipse.uml2.uml.Reception
import org.eclipse.uml2.uml.Signal
import org.eclipse.uml2.uml.SignalEvent
import org.eclipse.uml2.uml.State
import org.eclipse.uml2.uml.StateMachine
import org.eclipse.uml2.uml.TimeEvent
import org.eclipse.uml2.uml.Transition
import org.eclipse.uml2.uml.Trigger
import org.eclipse.uml2.uml.Type
import org.eclipse.uml2.uml.ValueSpecification
import org.eclipse.uml2.uml.Vertex
import org.eclipse.uml2.uml.util.UMLUtil

import static org.eclipse.papyrus.qompass.designer.core.EnumService.*
import static org.eclipse.papyrus.qompass.designer.vsl.ParseVSL.*

import static extension org.eclipse.papyrus.qompass.designer.core.UMLTool.*
import static extension org.eclipse.papyrus.qompass.modellibs.core.xtend.BehaviorUtil.*
import static extension org.eclipse.papyrus.qompass.modellibs.core.xtend.CppUtils.cppCall
import static extension org.eclipse.papyrus.qompass.modellibs.core.xtend.StateMachineUtil.*
import org.eclipse.papyrus.uml.tools.utils.PackageUtil
import org.eclipse.uml2.uml.UMLPackage
import org.eclipse.uml2.uml.Enumeration

class StateMachineGen implements IXtend {

	Class clazz
	
	boolean ooPattern = true;
	
	def getStateMachine(Class clazz) {
		// organized in a loop. But effectively supports single state machine
		for (smBehavior : (clazz as BehavioredClassifier).ownedBehaviors.filter[it instanceof StateMachine]) {
			return smBehavior as StateMachine
		}
		return null
	}
	
	def activate(Class clazz) {
		val sm = clazz.stateMachine
		val flattener = new UMLFlattener
		flattener.transform(sm)
		activate(clazz, sm)
	}
	
	def activate(Class clazz, StateMachine sm) '''
		m_currentState = STATE_«sm.region.initialState.name»;
#ifdef SM_VERBOSE
		cout << "SM «clazz.name»: in state «sm.region.initialState.name»" << endl;
#endif
		for (;;) {
			processEvents();
		}
	'''
	
	def processEvents(Class clazz) {
		val sm = clazz.stateMachine
		// - Option to put processElements into original class (but, would need to copy dependencies & attributes)
		// create new operation in class owning the state machine.
		// val operation = clazz.createOperation("processEvents", null)
		// val ob = clazz.createOpaqueBehavior(operation)
		// ob.set(clazz.processEventsSM(sm).toString)
		// return "executor->processEvents();"
		this.clazz = clazz
		return clazz.processEventsSM(sm)
	}

	def eventInterceptor(Operation operation) '''
		«/*TODO: need better way to detect signal*/»
		«operation.eventInterceptorCommon» 
 		event.portID = portID;
		out->writeEvent(event);

		«IF (operation.type != null)»return «ENDIF»rconn->«operation.cppCall»;
	'''

	/**
	 * OO variant of event interceptor. Adds call event to pool and
	 * then calls the original method (handled by LW container)
	 */
	def eventInterceptorOO(Operation operation) '''
		«operation.eventInterceptorCommon» 
	'''

	def eventInterceptorCommon(Operation operation) '''
		«val derivedElement = UMLUtil.getStereotypeApplication(operation, DerivedElement)»
		«IF (derivedElement != null) && (derivedElement.source instanceof Reception)»
			«val signal = (derivedElement.source as Reception).signal»
			// create event with global signal ID
			core::ContainerServices::CallEvent_ event;
			event.operationID = «literal(SIGNAL_ENUM, operation.name)»;
			// map signal into value-buffer and copy attributes
			::«signal.qualifiedName» * signal = (::«signal.qualifiedName» *) &event.params;
			«FOR attribute : signal.ownedAttributes»
				signal->«attribute.name» = «attribute.name»;
			«ENDFOR»
			«PackageUtil.getRootPackage(operation).declareDependencyToSignalIDs»
			«UMLTool.declareDependency(TransformationContext.classifier, signal)»
		«ELSE»
			// create event with operationID/portID and pass call
			core::ContainerServices::CallEvent_ event;
			«IF ooPattern»
				event.operationID = OP_ID_«operation.name»;
			«ELSE»
				event.operationID = ID_«operation.name»;
			«ENDIF»
		«ENDIF»
	'''
	
	def processEventsSM(BehavioredClassifier clazz, StateMachine sm) '''
		// processEvents body - generated by Qompass 
		//
		// supports ports «FOR port : (clazz as Class).ownedPorts» «literal('PortEnum_'+clazz.name, 'port_'+clazz.name+'_'+port.name)»«ENDFOR»

		core::ContainerServices::CallEvent_ event;
		int timeout;
		int newState;
		bool needsTrigger;

		switch(m_currentState) {
			«FOR state : sm.region.subvertices»
				case «literal('LStateIDs_'+clazz.name, 'STATE_'+state.name)»:
					// -------- treatment of accepted events
					«state.acceptableEvents»
					break;
			«ENDFOR»
			default:
				OSAL_ERROR ("Inconsistent state");
			break;
		}
		if (animOut != 0) {
			animOut->enterState(newState, «clazz.fragment»);
		}
	'''

	/*
	 * Pass the actual to which the port is bound. In case of a CallEvent, pass the implemented interface
	 * (Not the class to which the state machine is bound). This is useful in the context of components
	 * whose ports implement an interface
	 */
	def cetrigger(Operation operation) {
		val intf = operation.implementsInterface
		if (intf != null) {
			val packageRef = intf.boundPackageRef
			// declare dependency to OperationIDs enumeration
			packageRef.declareDependencyToOperationIDs
			'''«packageRef.qualifiedName»::ID_«operation.name»'''
		}
		else {
			// create operationIDs literal
			literal("LOperationIDs", '''OP_ID_«operation.name»''')
		}
	}

	// Use service for global enumerations
	def setrigger(Trigger trigger) {
		val se = trigger.event as SignalEvent
		literal(SIGNAL_ENUM, InterfaceSync.SIG_PREFIX + se.signal.name)
	}

	/**
	 * create code for acceptable events
	 * 
	 * big restriction: will only analyse first of possibly multiple triggers
	 */
	def acceptableEvents(Vertex state) '''
		// loop on state
		// execute action ...
		timeout = -1;	// no timeout by default
		«FOR transition : state.outgoings»
			«IF transition.triggers.size > 0»
				«val trigger = transition.triggers.get(0)»
				«IF (trigger.event instanceof TimeEvent)»
					«val timeEvent = trigger.event as TimeEvent»
					// transition «transition.name» - trigger: TimeEvent, expression «(timeEvent.when.expr as OpaqueExpression).bodies.get(0)».
					timeout = «getDurationFromVSL((timeEvent.when.expr as OpaqueExpression).bodies.get(0))»/1000;
				«ENDIF»
			«ENDIF»
		«ENDFOR»

		«IF ((state instanceof State) && (state as State).entry != null)»
			// execute entry action
			executor->«(state as State).entry.name»();
		«ENDIF»

		needsTrigger = true;

		«IF hasTransitionWithoutTrigger(state)»
			«FOR transition : state.outgoings»
				«IF transition.triggers.size == 0»
					«IF transition.guard != null»
						«transition.guard.specification.createGuardFct(null)»
						if (executor->«transition.guard.specification.name»()) {
					«ENDIF»
					newState = STATE_«transition.target.name»;
#ifdef SM_VERBOSE
						cout << "SM «clazz.name»: transition to state «transition.target.name»" << endl;
#endif
					«IF (transition.effect != null)»
						executor->«effectName(transition)»();
					«ENDIF»
						needsTrigger = false;
					«IF transition.guard != null»
						}
					«ENDIF»
				«ENDIF»
			«ENDFOR»
		«ENDIF»

		if (needsTrigger) {
			// get an event from the pool.
			«IF ooPattern»
				event = eventPool.readEvent(timeout);
			«ELSE»
				event = ep->readEvent(timeout);
			«ENDIF»
		}
		else {
			event.operationID = -1;
		}
		
		«val allOutgoings = state.outgoings»
		«FOR transition : allOutgoings»
			«IF transition.triggers.size > 0»
				// has «transition.triggers.size» outgoing transitions
				«val trigger = transition.triggers.get(0)»
				«IF (trigger.event instanceof TimeEvent)»
					// transition «transition.name» - trigger: TimeEvent (there should be at most one outgoing timed transition per state).
					if (event.operationID == core::ContainerServices::EventPool::ID_TIMEOUT) {
						«IF transition.guard != null»
							«transition.guard.specification.createGuardFct(null)»
							if (executor->«transition.guard.specification.name»()) {
						«ENDIF»
						newState = STATE_«transition.target.name»;
#ifdef SM_VERBOSE
						cout << "SM «clazz.name»: transition to state «transition.target.name»" << endl;
#endif
						«IF (transition.effect != null)»
							executor->«effectName(transition)»();
						«ENDIF»
						«IF transition.guard != null»
							}
						«ENDIF»
					}
				«ENDIF»
				«IF (trigger.event instanceof CallEvent)» 
					// transition «trigger.name» - trigger: CallEvent («trigger.event.name»), operation «(trigger.event as CallEvent).operation.name»
					if (event.operationID == «cetrigger((trigger.event as CallEvent).operation)») {
						newState = STATE_«transition.target.name»;
#ifdef SM_VERBOSE
						cout << "SM «clazz.name»: transition to state «transition.target.name»" << endl;
#endif
						«IF (transition.effect != null)»
							executor->«effectName(transition)»();
						«ENDIF»
					} 
				«ENDIF»
				«IF (trigger.event instanceof SignalEvent)» 
					«val signalEvent = trigger.event as SignalEvent»
					// transition «trigger.name» - trigger: SignalEvent («signalEvent.name»), signal «signalEvent.signal.name»
					if (event.operationID == «setrigger(trigger)») {
						«IF (transition.effect != null) || (transition.guard != null)»
							// map signal to parameter section
							::«signalEvent.signal.qualifiedName» * signal = (::«signalEvent.signal.qualifiedName» *) &event.params;
						«ENDIF»
						«IF transition.guard != null»
							«transition.guard.specification.createGuardFct(signalEvent)»
							if (executor->«transition.guard.specification.name»(signal)) {
						«ENDIF»
						newState = STATE_«transition.target.name»;
#ifdef SM_VERBOSE
						cout << "SM «clazz.name»: transition to state «transition.target.name» (due to signal «signalEvent.signal.name»)" << endl;
#endif
						«IF (transition.effect != null)»
							«transition.effect.addSignalParameter(signalEvent.signal)»
							executor->«effectName(transition)»(«IF signalEvent.signal.attributes.size > 0»signal«ENDIF»);
						«ENDIF»
						// ok = EvQUEUE ;
						«IF transition.guard != null»
							}
						«ENDIF»
					}
				«ENDIF»
			«ENDIF»
		«ENDFOR»
	
		if (newState != m_currentState) {
			m_currentState = newState;
			«IF ((state instanceof State) && (state as State).exit != null)»
			// execute exit action
			executor->«(state as State).exit.name»();
		«ENDIF»

			
		}
	'''
	
	def boolean hasTransitionWithoutTrigger(Vertex state) {
		for (transition : state.outgoings) {
			if (transition.triggers.size == 0) {
				return true;
			}
		}
		return false;		
	}
	
	def EList<Transition> calculateTransitions(Vertex state) {
		val allOutgoings = new BasicEList<Transition>
		allOutgoings.addAll(state.outgoings)
		for (transition : state.outgoings) {
			if (transition.target instanceof Pseudostate) {
				val ps = transition.target as Pseudostate
				if (ps.kind == PseudostateKind.JUNCTION_LITERAL) {
					allOutgoings.addAll(ps.outgoings.clone)
				}
			}
		}
		return allOutgoings
	}
	
	/**
	 * Effects are moved from behaviors embedded into transitions towards behaviors of the class. Calculate the name of these
	 * effects. The class FilterStateMachines moves the effects (TODO: single name calculation)
	 */
	def effectName(Transition transition) {
		// transition.containingStateMachine.name + "_" + transition.effect.name
		if (transition.effect.name == null) {
			throw new TransformationException(
				String.format("effect of transition has no name (in SM %s)", transition.containingStateMachine.name))
		}
		transition.effect.name
	}
	
	def void addSignalParameter(Behavior behavior, Signal signal) {
		if (behavior.ownedParameters.size == 0) {
			val parameter = behavior.createOwnedParameter("signal", signal)
			StereotypeUtil.apply(parameter, Ptr)
		}
	}

	def void moveBehavior(String newName, Class tmClass, Behavior effect) {
		val copiedEffect = EcoreUtil.copy(effect)
		if (tmClass.getOwnedOperation(newName, null, null) != null) {
			// has already been added
			return;
		}
		val operation = tmClass.createOwnedOperation(newName, null, null);
		for (parameter : effect.getOwnedParameters()) {
			val newParameter = EcoreUtil.copy(parameter);
			operation.getOwnedParameters().add(newParameter);
			StUtils.copyStereotypes(parameter, newParameter);
		}
		copiedEffect.setSpecification(operation);
		copiedEffect.setName(newName);
		tmClass.getOwnedBehaviors().add(copiedEffect);
	}

	static final String CLIB_BOOL = "AnsiCLibrary::bool"

	def void createGuardFct(ValueSpecification specification, SignalEvent event) {
		val name = specification.getName()
		
		if (clazz.getOwnedBehavior(name) != null) {
			return
		}
		var booleanNamedElement = Utils.getQualifiedElement(TransformationContext.sourceRoot, CLIB_BOOL);
		
		if (booleanNamedElement instanceof Type) {
			val booleanType = TransformationContext.copier.getCopy(booleanNamedElement) as Type
			val operation = clazz.createOperation(name, booleanType as Type)
			if (event != null) {
				val sigParam = operation.createOwnedParameter("signal", event.signal)
				StereotypeUtil.apply(sigParam, Ptr)
			}
			val ob = clazz.createOpaqueBehavior(operation)
			if (specification instanceof OpaqueExpression) {
				ob.getLanguages().addAll(specification.getLanguages());
				for (String body : specification.getBodies()) {
					ob.getBodies().add("return " + body + ";");
				}
			}
		}
		else {
			System.out.println("was");
		}
			// if (tmClass.getOwnedOperation(newName, null, null) != null) {
		// copiedEffect.setSpecification(operation);
	}
}

Back to the top