Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 552e34952194f27603e31c061c4b48d4d145f987 (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
«REM»
/*******************************************************************************
 * Copyright (c) 2010 protos software gmbh (http://www.protos.de).
 * 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:
 * 		Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
 * 
 *******************************************************************************/
«ENDREM»
«IMPORT room»;
«IMPORT etricegen»;

«EXTENSION org::eclipse::etrice::generator::extensions::Extensions»
«EXTENSION extensions::Extensions»
«EXTENSION org::eclipse::xtend::util::stdlib::io»


«DEFINE stateMachine FOR ExpandedActorClass»
«LET this.actorClass AS ac»
//******************************************
// START of generated code for FSM
//******************************************

// State IDs for FSM
«LET ac.getNumberOfInheritedBaseStates() AS offset»
«FOREACH ac.stateMachine.getBaseStateList() AS state ITERATOR iter»protected static final int «state.getStateId()» = «iter.counter1+1+offset»;
«ENDFOREACH»«ENDLET»
protected static final String stateStrings[] = {"<no state>","<top>",«FOREACH ac.getAllBaseStates() AS state ITERATOR iter SEPARATOR ","»"«state.getStatePathName()»"
«ENDFOREACH»};

// history
// TODOHRR: history defined in ActorClassBase, init in constructor
//		history = new int[5];
//		for (int i = 0; i < history.length; i++) {
//			history[i] = NO_STATE;
//		}
protected int history[] = {NO_STATE,NO_STATE«FOREACH ac.getAllBaseStates() AS state»,NO_STATE«ENDFOREACH»};

«LET this.getOwnTransitionChains() AS chains»
«LET this.getTransitionChains().size-chains.size AS offset»
// transition chains
«FOREACH chains AS tc ITERATOR i»protected static final int «tc.getChainId()» = «i.counter1+offset»;
«ENDFOREACH»«ENDLET»«ENDLET»

«LET this.getOwnTriggers() AS triggers»
// triggers for FSM
«FOREACH triggers AS mif»protected static final int «this.getTriggerCodeName(mif)» = IFITEM_«mif.from.name» + EVT_SHIFT*«this.getMessageID(mif)»;
«ENDFOREACH»«ENDLET»

// receiveEvent contains the main implementation of the FSM
@Override
public void receiveEvent(InterfaceItemBase ifitem, int evt, Object generic_data) {
	int trigger = ifitem.getLocalId() + EVT_SHIFT*evt;
	int chain = NOT_CAUGHT;
	int catching_state = NO_STATE;
	boolean is_handler = false;
	boolean skip_entry = false;
	
	if (!handleSystemEvent(ifitem, evt, generic_data)) {
		switch (state) {
			«FOREACH this.stateMachine.getLeafStateList() AS state-»
			case «state.getStateId()»:
				«LET this.getActiveTriggers(state) AS atlist-»
				«IF !atlist.isEmpty-»
					switch(trigger) {
					«FOREACH atlist AS at-»
						case «this.getTriggerCodeName(at.trigger)»:
							«LET this.hasGuard(at) AS needData-»
							«IF needData»{ «at.msg.getTypedDataDefinition()»«ENDIF-»
							«FOREACH at.transitions AS tt SEPARATOR " else "-»
								«LET this.getChain(tt) AS chain-»
								«EXPAND guard(at.trigger, this) FOR chain.transition-»
								{
									chain = «chain.getChainId()»;
									catching_state = «chain.getContextId()»;
									«IF chain.isHandler()»is_handler = true;«ENDIF-»
									«IF chain.skipEntry»skip_entry = true;«ENDIF-»
								}
								«ENDLET-»
							«ENDFOREACH-»
							«IF needData»}«ENDIF-»
							«ENDLET-»
						break;
					«ENDFOREACH-»
					}
				«ENDIF»«ENDLET-»
				break;
			«ENDFOREACH-»
		}
	}
	if (chain != NOT_CAUGHT) {
		exitTo(state, catching_state, is_handler);
		int next = executeTransitionChain(chain, ifitem, generic_data);
		next = enterHistory(next, is_handler, skip_entry);
		setState(next);
	}
}

private void setState(int new_state) {
	DebuggingService.getInstance().addActorState(this,stateStrings[new_state]);
	if (stateStrings[new_state]!="Idle") {
		// TODOTS: model switch for activation
		System.out.println(getInstancePath() + " -> " + stateStrings[new_state]);
	}	
	this.state = new_state;
}

@Override
public void executeInitTransition() {
	«LET this.stateMachine.getInitTransition() AS initt-»
		int chain = «this.getChain(initt).getChainId()»;
		int next = executeTransitionChain(chain, null, null);
		next = enterHistory(next, false, false);
		setState(next);
	«ENDLET-»
}

/**
 * calls exit codes while exiting from the current state to one of its
 * parent states while remembering the history
 * @param current - the current state
 * @param to - the final parent state
 * @param handler - entry and exit codes are called only if not handler (for handler TransitionPoints)
 */
private void exitTo(int current, int to, boolean handler) {
	while (current!=to) {
		switch (current) {
			«FOREACH this.stateMachine.getBaseStateList() AS state-»
				case «state.getStateId()»:
					«IF state.hasExitCode()»if (!handler) «state.getExitCodeOperationName()»();«ENDIF-»
					history[«state.getParentStateId()»] = «state.getStateId()»;
					current = «state.getParentStateId()»;
					break;
			«ENDFOREACH-»
		}
	}
}
/**
 * calls action, entry and exit codes along a transition chain. The generic data are cast to typed data
 * matching the trigger of this chain. The ID of the final state is returned
 * @param chain - the chain ID
 * @param generic_data - the generic data pointer
 * @return the ID of the final state
 */
private int executeTransitionChain(int chain, InterfaceItemBase ifitem, Object generic_data) {
	switch (chain) {
		«LET this.getTransitionChains() AS chains-»
		«FOREACH chains AS tc-»
			case «tc.getChainId()»:
			{
				«this.getExecuteChainCode(tc)-»
			}
		«ENDFOREACH»«ENDLET-»
	}
	return NO_STATE;
}
/**
 * calls entry codes while entering a state's history. The ID of the final leaf state is returned
 * @param state - the state which is entered
 * @param handler - entry code is executed if not handler
 * @return - the ID of the final leaf state
 */
private int enterHistory(int state, boolean handler, boolean skip_entry) {
	while (true) {
		switch (state) {
			«FOREACH this.stateMachine.getBaseStateList() AS state-»
			case «state.getStateId()»:
				«IF state.hasEntryCode()»if (!(skip_entry || handler)) «state.getEntryCodeOperationName()»();«ENDIF»
				«IF state.isLeaf()-»
					// in leaf state: return state id
					return «state.getStateId()»;
				«ELSE-»
					// state has a sub graph
					«IF state.subgraph.hasInitTransition()-»
						// with init transition
						if (history[«state.getStateId()»]==NO_STATE) {
							«LET state.subgraph.getInitTransition() AS initt-»
								state = executeTransitionChain(«this.getChain(initt).getChainId()-», null, null);
							«ENDLET-»
						}
						else {
							state = history[«state.getStateId()»];
						}
					«ELSE-»
						// without init transition
						state = history[«state.getStateId()»];
					«ENDIF-»
					break;
				«ENDIF-»
			«ENDFOREACH-»
			case STATE_TOP:
				state = history[STATE_TOP];
				break;
		}
		skip_entry = false;
	}
	«REM»return NO_STATE; // required by CDT but detected as unreachable by JDT because of while (true)«ENDREM»
}

//*** Entry and Exit Codes
«FOREACH this.stateMachine.getStateList() AS state»
	«IF this.isOwnObject(state)»
		«IF state.hasEntryCode()»
			protected void «state.getEntryCodeOperationName()»() {
				«this.getEntryCode(state)»}
		«ENDIF»
		«IF state.hasExitCode()»
			protected void «state.getExitCodeOperationName()»() {
				«this.getExitCode(state)»}
		«ENDIF»
	«ENDIF»
«ENDFOREACH»

//*** Action Codes
«FOREACH this.stateMachine.getTransitionList() AS tr»
	«IF this.isOwnObject(tr) && tr.hasActionCode()»
		protected void «tr.getActionCodeOperationName()»(«IF NonInitialTransition.isInstance(tr)»InterfaceItemBase ifitem«this.getArgumentList(tr)»«ENDIF») {
			«this.getActionCode(tr)»}
	«ENDIF»
«ENDFOREACH»
	 
//******************************************
// END of generated code for FSM
//******************************************
«ENDLET»
«ENDDEFINE»

«DEFINE guard(String trigger, ExpandedActorClass ac) FOR TriggeredTransition-»
	«LET this.triggers.selectFirst(e|ac.isMatching(e, trigger)) AS tr-»
		«IF tr.hasGuard()-»
			if («ac.getCode(tr.guard.guard)»)
		«ENDIF-»
	«ENDLET-»
«ENDDEFINE»

«DEFINE guard(String trigger, ExpandedActorClass ac) FOR Transition»
/* error */
«ENDDEFINE»

Back to the top