Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 746f12a8ced49cad14a10e62daac2fdfa8a6a8e8 (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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package org.eclipse.etrice.integration.ChoicePointTest;

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 org.eclipse.etrice.integration.ChoicePointTest.TestProtocol.*;

/*--------------------- begin user code ---------------------*/
	import org.eclipse.etrice.integration.tests.helpers.*;
	import org.eclipse.etrice.runtime.java.messaging.RTServices;
/*--------------------- end user code ---------------------*/


public class CPUser extends ActorClassBase {

	/*--------------------- begin user code ---------------------*/
		private CRC16Generator crcGen = new CRC16Generator();
	/*--------------------- end user code ---------------------*/
	
	//--------------------- ports
	protected TestProtocolPort TestPort1 = null;
	
	//--------------------- saps
	
	//--------------------- services

	//--------------------- interface item IDs
	public static final int IFITEM_TestPort1 = 1;
		
	/*--------------------- attributes ---------------------*/
	int counter;
	/*--------------------- operations ---------------------*/

	//--------------------- construction
	public CPUser(IRTObject parent, String name, Address[][] port_addr, Address[][] peer_addr){
		super(parent, name, port_addr[0][0], peer_addr[0][0]);
		setClassName("CPUser");
		
		// initialize attributes
		counter = 0;

		// own ports
		TestPort1 = new TestProtocolPort(this, "TestPort1", IFITEM_TestPort1, 0, port_addr[IFITEM_TestPort1][0], peer_addr[IFITEM_TestPort1][0]); 
		
		// own saps
		
		// own service implementations
	}
	//--------------------- attributes getter and setter
	//--------------------- attribute setters and getters
	public void setCounter (int counter) {
		 this.counter = counter;
	}
	public int getCounter () {
		return this.counter;
	}

	//--------------------- lifecycle functions
	public void init(){
		initUser();
	}

	public void start(){
		startUser();
	}

	public void stop(){
		stopUser();
	}
	
	public void destroy(){
	}

	
	/* state IDs */
	public static final int STATE_state1 = 2;
	public static final int STATE_state0 = 3;
	public static final int STATE_state1_state0 = 4;
	public static final int STATE_testOk = 5;
	public static final int STATE_testFalse = 6;
	
	/* transition chains */
	public static final int CHAIN_TRANS_INITIAL_TO__cp0 = 1;
	public static final int CHAIN_TRANS_tr1_FROM_state0_TO_cp1_BY_msg1TestPort1 = 2;
	public static final int CHAIN_TRANS_tr5_FROM_state1_TO_cp2_BY_msg1TestPort1 = 3;
	public static final int CHAIN_TRANS_tr7_FROM_state1_TO_state0_BY_msg1TestPort1 = 4;
	public static final int CHAIN_TRANS_tr8_FROM_tp0_TO_tp0_BY_msg1TestPort1_tr8 = 5;
	public static final int CHAIN_state1_TRANS_INITIAL_TO__state0 = 6;
	
	/* triggers */
	public static final int POLLING = 0;
	public static final int TRIG_TestPort1__msg1 = IFITEM_TestPort1 + EVT_SHIFT*TestProtocol.IN_msg1;
	
	// state names
	protected static final String stateStrings[] = {"<no state>","<top>","state1",
	"state0",
	"state1_state0",
	"testOk",
	"testFalse"
	};
	
	// 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") {
			System.out.println(getInstancePath() + " -> " + stateStrings[new_state]);
		}	
		this.state = new_state;
	}
	
	/* Entry and Exit Codes */
	protected void entry_state0() {
		System.out.println("state0: entry code");
		crcGen.update(12);
	}
	protected void exit_state0() {
		System.out.println("state0: exit code");
		crcGen.update(13);
	}
	protected void entry_state1() {
		System.out.println("state1: entry code");
		crcGen.update(20);
	}
	protected void exit_state1() {
		System.out.println("state1: exit code");
		crcGen.update(21);
	}
	protected void entry_state1_state0() {
		System.out.println("state1\\0: entry code");
		System.out.printf("counter => %d\n",counter);
		crcGen.update(22);
	}
	protected void exit_state1_state0() {
		System.out.println("state1\\0: exit code");
		crcGen.update(23);
	}
	
	/* Action Codes */
	protected void action_TRANS_INITIAL_TO__cp0() {
		System.out.println("Top Level init transition");
		crcGen.update(10);
		counter = 0;
	}
	protected void action_TRANS_tr4_FROM_cp0_TO_state0() {
		System.out.println("choice0: else code");
		crcGen.update(17);
	}
	protected void action_TRANS_LastCheck_FROM_cp0_TO_state0_COND_LastCheck() {
		System.out.println("choice0: true code");
		crcGen.update(15);
	}
	protected void action_TRANS_tr1_FROM_state0_TO_cp1_BY_msg1TestPort1(InterfaceItemBase ifitem) {
		System.out.println("state0: msg1: transition code");
		crcGen.update(18);
	}
	protected void action_TRANS_tr2_FROM_cp1_TO_state0(InterfaceItemBase ifitem) {
		System.out.println("choice1: else code");
		crcGen.update(10);
		counter=3;
	}
	protected void action_TRANS_tr3_FROM_cp1_TO_state1_tp0_COND_tr3(InterfaceItemBase ifitem) {
		System.out.println("choice1: to entry state1 code");
		crcGen.update(30);
	}
	protected void action_TRANS_tr5_FROM_state1_TO_cp2_BY_msg1TestPort1(InterfaceItemBase ifitem) {
		System.out.println("state1: msg1: transition code");
		crcGen.update(30);
		System.out.printf("CRC: %d \n", crcGen.getCrc());
	}
	protected void action_TRANS_tr0_FROM_cp2_TO_testFalse(InterfaceItemBase ifitem) {
		System.out.println("Test false");
		RTServices.getInstance().getSubSystem().testFinished(1);
	}
	protected void action_TRANS_tr6_FROM_cp2_TO_testOk_COND_tr6(InterfaceItemBase ifitem) {
		System.out.println("Test ok");
		RTServices.getInstance().getSubSystem().testFinished(0);
	}
	protected void action_TRANS_tr11_FROM_cp1_TO_state1_COND_tr11(InterfaceItemBase ifitem) {
		System.out.println("choice1: history to State1 transition code");
		crcGen.update(25);
		counter = 1;
	}
	protected void action_TRANS_tr7_FROM_state1_TO_state0_BY_msg1TestPort1(InterfaceItemBase ifitem) {
		System.out.println("state1: msg1: transition code to state0");
		crcGen.update(51);
		counter=4;
	}
	protected void action_TRANS_tr8_FROM_tp0_TO_tp0_BY_msg1TestPort1_tr8(InterfaceItemBase ifitem) {
		System.out.println("Handler: msg1 received ");
		crcGen.update(115);
		counter = 2;
	}
	protected void action_state1_TRANS_tr1_FROM_tp0_TO_cp0(InterfaceItemBase ifitem) {
		System.out.println("state1: tp0: transition code");
		crcGen.update(25);
	}
	protected void action_state1_TRANS_tr2_FROM_cp0_TO_state0(InterfaceItemBase ifitem) {
		System.out.println("state1: choice false code");
		crcGen.update(57);
	}
	protected void action_state1_TRANS_tr0_FROM_cp0_TO_state0_COND_tr0(InterfaceItemBase ifitem) {
		System.out.println("state1 choice: true code");
		crcGen.update(56);
	}
	protected void action_state1_TRANS_INITIAL_TO__state0() {
		System.out.println("state1 initial transition code");
		crcGen.update(53);
	}
	
	/**
	 * 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) {
				case STATE_state0:
					if (!handler) exit_state0();
					this.history[STATE_TOP] = STATE_state0;
					current = STATE_TOP;
					break;
				case STATE_state1:
					if (!handler) exit_state1();
					this.history[STATE_TOP] = STATE_state1;
					current = STATE_TOP;
					break;
				case STATE_state1_state0:
					if (!handler) exit_state1_state0();
					this.history[STATE_state1] = STATE_state1_state0;
					current = STATE_state1;
					break;
				case STATE_testOk:
					this.history[STATE_TOP] = STATE_testOk;
					current = STATE_TOP;
					break;
				case STATE_testFalse:
					this.history[STATE_TOP] = STATE_testFalse;
					current = STATE_TOP;
					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
	 */
	private int executeTransitionChain(int chain, InterfaceItemBase ifitem, Object generic_data) {
		switch (chain) {
			case CHAIN_TRANS_INITIAL_TO__cp0:
			{
				action_TRANS_INITIAL_TO__cp0();
				if (counter == 0) {
				action_TRANS_LastCheck_FROM_cp0_TO_state0_COND_LastCheck();
				return STATE_state0;}
				else {
				action_TRANS_tr4_FROM_cp0_TO_state0();
				return STATE_state0;}
			}
			case CHAIN_TRANS_tr1_FROM_state0_TO_cp1_BY_msg1TestPort1:
			{
				action_TRANS_tr1_FROM_state0_TO_cp1_BY_msg1TestPort1(ifitem);
				if (counter == 4) {
				action_TRANS_tr3_FROM_cp1_TO_state1_tp0_COND_tr3(ifitem);
				entry_state1();
				action_state1_TRANS_tr1_FROM_tp0_TO_cp0(ifitem);
				if (counter == 4) {
				action_state1_TRANS_tr0_FROM_cp0_TO_state0_COND_tr0(ifitem);
				return STATE_state1_state0;}
				else {
				action_state1_TRANS_tr2_FROM_cp0_TO_state0(ifitem);
				return STATE_state1_state0;}
				}
				else if (counter == 3) {
				action_TRANS_tr11_FROM_cp1_TO_state1_COND_tr11(ifitem);
				return STATE_state1;}
				else {
				action_TRANS_tr2_FROM_cp1_TO_state0(ifitem);
				return STATE_state0;}
			}
			case CHAIN_TRANS_tr5_FROM_state1_TO_cp2_BY_msg1TestPort1:
			{
				action_TRANS_tr5_FROM_state1_TO_cp2_BY_msg1TestPort1(ifitem);
				if (crcGen.getCrc()==26639) {
				action_TRANS_tr6_FROM_cp2_TO_testOk_COND_tr6(ifitem);
				return STATE_testOk;}
				else {
				action_TRANS_tr0_FROM_cp2_TO_testFalse(ifitem);
				return STATE_testFalse;}
			}
			case CHAIN_TRANS_tr7_FROM_state1_TO_state0_BY_msg1TestPort1:
			{
				action_TRANS_tr7_FROM_state1_TO_state0_BY_msg1TestPort1(ifitem);
				return STATE_state0;
			}
			case CHAIN_TRANS_tr8_FROM_tp0_TO_tp0_BY_msg1TestPort1_tr8:
			{
				action_TRANS_tr8_FROM_tp0_TO_tp0_BY_msg1TestPort1_tr8(ifitem);
				return STATE_TOP;
			}
			case CHAIN_state1_TRANS_INITIAL_TO__state0:
			{
				action_state1_TRANS_INITIAL_TO__state0();
				return STATE_state1_state0;
			}
		}
		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) {
				case STATE_state0:
					if (!(skip_entry || handler)) entry_state0();
					// in leaf state: return state id
					return STATE_state0;
				case STATE_state1:
					if (!(skip_entry || handler)) entry_state1();
					// state has a sub graph
					// with init transition
					if (this.history[STATE_state1]==NO_STATE) {
						state = executeTransitionChain(CHAIN_state1_TRANS_INITIAL_TO__state0, null, null);
					}
					else {
						state = this.history[STATE_state1];
					}
					break;
				case STATE_state1_state0:
					if (!(skip_entry || handler)) entry_state1_state0();
					// in leaf state: return state id
					return STATE_state1_state0;
				case STATE_testOk:
					// in leaf state: return state id
					return STATE_testOk;
				case STATE_testFalse:
					// in leaf state: return state id
					return STATE_testFalse;
				case STATE_TOP:
					state = this.history[STATE_TOP];
					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__cp0;
		int next = executeTransitionChain(chain, null, null);
		next = enterHistory(next, false, false);
		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;
		boolean is_handler = false;
		boolean skip_entry = false;
		
		if (!handleSystemEvent(ifitem, evt, generic_data)) {
			switch (this.state) {
				case STATE_state0:
					switch(trigger) {
						case TRIG_TestPort1__msg1:
							{
								chain = CHAIN_TRANS_tr1_FROM_state0_TO_cp1_BY_msg1TestPort1;
								catching_state = STATE_TOP;
							}
						break;
					}
					break;
				case STATE_state1_state0:
					switch(trigger) {
						case TRIG_TestPort1__msg1:
							{ 
							if (counter==2)
							{
								chain = CHAIN_TRANS_tr5_FROM_state1_TO_cp2_BY_msg1TestPort1;
								catching_state = STATE_TOP;
							} else 
							if (counter==1)
							{
								chain = CHAIN_TRANS_tr7_FROM_state1_TO_state0_BY_msg1TestPort1;
								catching_state = STATE_TOP;
							} else 
							{
								chain = CHAIN_TRANS_tr8_FROM_tp0_TO_tp0_BY_msg1TestPort1_tr8;
								catching_state = STATE_TOP;
								is_handler = true;
								skip_entry = true;
							}
							}
						break;
					}
					break;
				case STATE_testOk:
					switch(trigger) {
						case TRIG_TestPort1__msg1:
							{
								chain = CHAIN_TRANS_tr8_FROM_tp0_TO_tp0_BY_msg1TestPort1_tr8;
								catching_state = STATE_TOP;
								is_handler = true;
								skip_entry = true;
							}
						break;
					}
					break;
				case STATE_testFalse:
					switch(trigger) {
						case TRIG_TestPort1__msg1:
							{
								chain = CHAIN_TRANS_tr8_FROM_tp0_TO_tp0_BY_msg1TestPort1_tr8;
								catching_state = STATE_TOP;
								is_handler = true;
								skip_entry = true;
							}
						break;
					}
					break;
			}
		}
		if (chain != NOT_CAUGHT) {
			exitTo(this.state, catching_state, is_handler);
			int next = executeTransitionChain(chain, ifitem, generic_data);
			next = enterHistory(next, is_handler, skip_entry);
			setState(next);
		}
	}
		 
	//******************************************
	// END of generated code for FSM
	//******************************************
};

Back to the top