Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8aa32e78a02ccfebca89c5387834fd4ccb5a3895 (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
package org.eclipse.etrice.examples.dynamicactors4;

import org.eclipse.etrice.runtime.java.messaging.Address;
import org.eclipse.etrice.runtime.java.messaging.IRTObject;
import org.eclipse.etrice.runtime.java.messaging.IMessageReceiver;
import org.eclipse.etrice.runtime.java.modelbase.ActorClassBase;
import org.eclipse.etrice.runtime.java.modelbase.SubSystemClassBase;
import org.eclipse.etrice.runtime.java.modelbase.InterfaceItemBase;
import org.eclipse.etrice.runtime.java.debugging.DebuggingService;
import static org.eclipse.etrice.runtime.java.etunit.EtUnit.*;


import org.eclipse.etrice.examples.dynamicactors4.PC.*;
import org.eclipse.etrice.examples.dynamicactors4.PWorker.*;




public class Controller extends ActorClassBase {

	
	//--------------------- ports
	protected PWorkerConjPort worker = null;
	protected PCConjPort opt = null;
	
	//--------------------- saps
	
	//--------------------- services
	
	//--------------------- optional actors
	
	//--------------------- interface item IDs
	public static final int IFITEM_worker = 1;
	public static final int IFITEM_opt = 2;
	
	/*--------------------- attributes ---------------------*/
	/*--------------------- operations ---------------------*/

	//--------------------- construction
	public Controller(IRTObject parent, String name) {
		super(parent, name);
		setClassName("Controller");
		
		// initialize attributes

		// own ports
		worker = new PWorkerConjPort(this, "worker", IFITEM_worker);
		opt = new PCConjPort(this, "opt", IFITEM_opt);
		
		// own saps
		
		// own service implementations
		
		// sub actors
		DebuggingService.getInstance().addMessageActorCreate(this, "worker");
		new Worker(this, "worker");

	}
	
	/* --------------------- attribute setters and getters */
	
	
	//--------------------- port getters
	public PWorkerConjPort getWorker (){
		return this.worker;
	}
	public PCConjPort getOpt (){
		return this.opt;
	}

	//--------------------- lifecycle functions
	public void stop(){
		stopUser();
		super.stop();
	}
	
	public void destroy() {
		DebuggingService.getInstance().addMessageActorDestroy(this);
		super.destroy();
	}

	/* state IDs */
	public static final int STATE_createOpt1 = 2;
	public static final int STATE_SendHello = 3;
	public static final int STATE_TryCreateInvalid = 4;
	public static final int STATE_UnexpectedError = 5;
	public static final int STATE_ExpectedError = 6;
	public static final int STATE_MAX = 7;
	
	/* transition chains */
	public static final int CHAIN_TRANS_INITIAL_TO__createOpt1 = 1;
	public static final int CHAIN_TRANS_tr0_FROM_createOpt1_TO_SendHello_BY_okworker = 2;
	public static final int CHAIN_TRANS_tr1_FROM_SendHello_TO_TryCreateInvalid_BY_helloopt = 3;
	public static final int CHAIN_TRANS_tr2_FROM_createOpt1_TO_UnexpectedError_BY_errorworker = 4;
	public static final int CHAIN_TRANS_tr3_FROM_TryCreateInvalid_TO_ExpectedError_BY_errorworker = 5;
	
	/* triggers */
	public static final int POLLING = 0;
	public static final int TRIG_worker__ok = IFITEM_worker + EVT_SHIFT*PWorker.OUT_ok;
	public static final int TRIG_worker__error = IFITEM_worker + EVT_SHIFT*PWorker.OUT_error;
	public static final int TRIG_opt__hello = IFITEM_opt + EVT_SHIFT*PC.OUT_hello;
	
	// state names
	protected static final String stateStrings[] = {"<no state>","<top>","createOpt1",
	"SendHello",
	"TryCreateInvalid",
	"UnexpectedError",
	"ExpectedError"
	};
	
	// history
	protected int history[] = {NO_STATE,NO_STATE,NO_STATE,NO_STATE,NO_STATE,NO_STATE,NO_STATE};
	
	private void setState(int new_state) {
		DebuggingService.getInstance().addActorState(this,stateStrings[new_state]);
		if (stateStrings[new_state]!="Idle") {
		}	
		this.state = new_state;
	}
	
	/* Entry and Exit Codes */
	protected void entry_createOpt1() {
		worker.create("Optional1");
	}
	protected void entry_SendHello() {
		opt.sayHello();
	}
	protected void entry_TryCreateInvalid() {
		worker.create("Optional");
	}
	protected void entry_ExpectedError() {
		System.out.println("Done, enter 'quit' to exit"); 
	}
	
	/* Action Codes */
	protected void action_TRANS_tr1_FROM_SendHello_TO_TryCreateInvalid_BY_helloopt(InterfaceItemBase ifitem, String txt) {
		System.out.println("controller received "+txt);
	}
	
	/**
	 * 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
	 */
	private void exitTo(int current, int to) {
		while (current!=to) {
			switch (current) {
				case STATE_createOpt1:
					this.history[STATE_TOP] = STATE_createOpt1;
					current = STATE_TOP;
					break;
				case STATE_SendHello:
					this.history[STATE_TOP] = STATE_SendHello;
					current = STATE_TOP;
					break;
				case STATE_TryCreateInvalid:
					this.history[STATE_TOP] = STATE_TryCreateInvalid;
					current = STATE_TOP;
					break;
				case STATE_UnexpectedError:
					this.history[STATE_TOP] = STATE_UnexpectedError;
					current = STATE_TOP;
					break;
				case STATE_ExpectedError:
					this.history[STATE_TOP] = STATE_ExpectedError;
					current = STATE_TOP;
					break;
				default:
					/* should not occur */
					break;
			}
		}
	}
	
	/**
	 * 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 either with a positive sign, that indicates to execute the state's entry code, or a negative sign vice versa
	 */
	private int executeTransitionChain(int chain, InterfaceItemBase ifitem, Object generic_data) {
		switch (chain) {
			case CHAIN_TRANS_INITIAL_TO__createOpt1:
			{
				return STATE_createOpt1;
			}
			case CHAIN_TRANS_tr0_FROM_createOpt1_TO_SendHello_BY_okworker:
			{
				return STATE_SendHello;
			}
			case CHAIN_TRANS_tr1_FROM_SendHello_TO_TryCreateInvalid_BY_helloopt:
			{
				String txt = (String) generic_data;
				action_TRANS_tr1_FROM_SendHello_TO_TryCreateInvalid_BY_helloopt(ifitem, txt);
				return STATE_TryCreateInvalid;
			}
			case CHAIN_TRANS_tr2_FROM_createOpt1_TO_UnexpectedError_BY_errorworker:
			{
				return STATE_UnexpectedError;
			}
			case CHAIN_TRANS_tr3_FROM_TryCreateInvalid_TO_ExpectedError_BY_errorworker:
			{
				return STATE_ExpectedError;
			}
				default:
					/* should not occur */
					break;
		}
		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
	 * @return - the ID of the final leaf state
	 */
	private int enterHistory(int state) {
		boolean skip_entry = false;
		if (state >= STATE_MAX) {
			state =  (state - STATE_MAX);
			skip_entry = true;
		}
		while (true) {
			switch (state) {
				case STATE_createOpt1:
					if (!(skip_entry)) entry_createOpt1();
					/* in leaf state: return state id */
					return STATE_createOpt1;
				case STATE_SendHello:
					if (!(skip_entry)) entry_SendHello();
					/* in leaf state: return state id */
					return STATE_SendHello;
				case STATE_TryCreateInvalid:
					if (!(skip_entry)) entry_TryCreateInvalid();
					/* in leaf state: return state id */
					return STATE_TryCreateInvalid;
				case STATE_UnexpectedError:
					/* in leaf state: return state id */
					return STATE_UnexpectedError;
				case STATE_ExpectedError:
					if (!(skip_entry)) entry_ExpectedError();
					/* in leaf state: return state id */
					return STATE_ExpectedError;
				case STATE_TOP:
					state = this.history[STATE_TOP];
					break;
				default:
					/* should not occur */
					break;
			}
			skip_entry = false;
		}
		/* return NO_STATE; // required by CDT but detected as unreachable by JDT because of while (true) */
	}
	
	public void executeInitTransition() {
		int chain = CHAIN_TRANS_INITIAL_TO__createOpt1;
		int next = executeTransitionChain(chain, null, null);
		next = enterHistory(next);
		setState(next);
	}
	
	/* receiveEvent contains the main implementation of the FSM */
	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;
		
		if (!handleSystemEvent(ifitem, evt, generic_data)) {
			switch (getState()) {
				case STATE_createOpt1:
					switch(trigger) {
							case TRIG_worker__ok:
								{
									chain = CHAIN_TRANS_tr0_FROM_createOpt1_TO_SendHello_BY_okworker;
									catching_state = STATE_TOP;
								}
							break;
							case TRIG_worker__error:
								{
									chain = CHAIN_TRANS_tr2_FROM_createOpt1_TO_UnexpectedError_BY_errorworker;
									catching_state = STATE_TOP;
								}
							break;
							default:
								/* should not occur */
								break;
					}
					break;
				case STATE_SendHello:
					switch(trigger) {
							case TRIG_opt__hello:
								{
									chain = CHAIN_TRANS_tr1_FROM_SendHello_TO_TryCreateInvalid_BY_helloopt;
									catching_state = STATE_TOP;
								}
							break;
							default:
								/* should not occur */
								break;
					}
					break;
				case STATE_TryCreateInvalid:
					switch(trigger) {
							case TRIG_worker__error:
								{
									chain = CHAIN_TRANS_tr3_FROM_TryCreateInvalid_TO_ExpectedError_BY_errorworker;
									catching_state = STATE_TOP;
								}
							break;
							default:
								/* should not occur */
								break;
					}
					break;
				case STATE_UnexpectedError:
					break;
				case STATE_ExpectedError:
					break;
				default:
					/* should not occur */
					break;
			}
		}
		if (chain != NOT_CAUGHT) {
			exitTo(getState(), catching_state);
			{
				int next = executeTransitionChain(chain, ifitem, generic_data);
				next = enterHistory(next);
				setState(next);
			}
		}
	}
};

Back to the top