Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 09e27650140e06faec22355d3fa040b7422d0bfe (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
h1. Tutorial Sending Data

h2. Scope

This tutorial shows how data will be sent in a eTrice model. Within the example you will create two actors (MrPing and MrPong). MrPong will simply loop back every data it received.
MrPing will send data and verify the result.   

You will perform the following steps:

# create a new model from scratch
# create a data class
# define a protocol with attached data
# create an actor structure
# create two simple state machines
# build and run the model

h2. Create a new model from scratch

Remember exercise ??HelloWorld??.
Create a new eTrice project and name it ??SendingData??
Open the ??SendingData.room?? file and copy the following code into the file or use content assist to create the model.


bc.. 
RoomModel SendingData {
		LogicalSystem SendingData_LogSystem {
			SubSystemRef SendingDataAppl:SendingData_SubSystem 
		}
		SubSystemClass SendingData_SubSystem {
			ActorRef SendigDataTopRef:SendingDataTop 
		}
		ActorClass SendingDataTop {
		}
	}
bq. 

h2. Add a data class

Position the cursor outside any class definition and right click the mouse within the editor window. From the context menu select ??Content Assist?? (or Ctrl+Space).  

!images/025-SendingData01.png!

Select ??DataClass - data class skeleton?? and name it ??DemoData??.
Remove the operations and ass the following Attributes:

bc.. 
	DataClass DemoData {
		Attribute int32Val: int32 = "4711"
		Attribute int8Array [ 10 ]: int8 = "{1,2,3,4,5,6,7,8,9,10}"
		Attribute float64Val: float64 = "0.0"
		Attribute stringVal: string = "\"empty\""
	}
bq. 

Save the model and visit the outline view.
Note that the outline view contains all data elements as defined in the model. 

h2. Create a new protocol

With the help of ??Content Assist?? create a ??ProtocolClass?? and name it ??PingPongProtocol??. Create the following messages:

bc.. 
ProtocolClass PingPongProtocol {
					incoming {
						Message ping(data: DemoData)
						Message pingSimple(data:int32)
					}
					outgoing {
						Message pong(data: DemoData)
						Message pongSimple(data:int32)
					}
				}		
bq. 

h2. Create MrPing and MrPong Actors

With the help of ??Content Assist?? create two new actor classes and name them ??MrPing?? and ??MrPong??. The resulting model should look like this:

bc.. 
RoomModel SendingData {

	LogicalSystem SendingData_LogSystem {
		SubSystemRef SendingDataAppl: SendingData_SubSystem
	}

	SubSystemClass SendingData_SubSystem {
		ActorRef SendigDataTopRef: SendingDataTop
	}

	ActorClass SendingDataTop { }

	DataClass DemoData {
		Attribute int32Val: int32 = "4711"
		Attribute int8Array [ 10 ]: int8 = "{1,2,3,4,5,6,7,8,9,10}"
		Attribute float64Val: float64 = "0.0"
		Attribute stringVal: string = "\"empty\""
	}

	ProtocolClass PingPongProtocol {
		incoming {
			Message ping(data: DemoData)
			Message pingSimple(data: int32)
		}
		outgoing {
			Message pong(data: DemoData)
			Message pongSimple(data: int32)
		}
	}

	ActorClass MrPing {
		Interface { }
		Structure { }
		Behavior { }
	}

	ActorClass MrPong {
		Interface { }
		Structure { }
		Behavior { }
	}
} 

bq.  

The outline view should look like this:

!images/025-SendingData03.png!

h2. Define Actor Structure and Behavior

Save the model and visit the outline view. Within the outline view, right click on the ??MrPong?? actor and select ??Edit Structure??. Select an ??Interface Port?? from the toolbox and add it to MrPong. Name the Port ??PingPongPort?? and select the ??PingPongProtocol??

!images/025-SendingData02.png!

Do the same with MrPing but mark the port as ??conjugated??

h3. Define MrPongs behavior

Within the outline view, right click MrPong and select ??Edit Behavior??. Create the following state machine:

!images/025-SendingData04.png!

The transition dialogues should look like this:
For ??ping??:

!images/025-SendingData05.png!

For ??pingSimple??:

!images/025-SendingData06.png!


h3. Define MrPing behavior

Within the outline view double click MrPing. Navigate the cursor to the behavior of MrPing. With the help of content assist create a new operation.

!images/025-SendingData07.png!

Name the operation ??printData?? and define the DemoData as a parameter.

Fill in the following code:

bc.. 
Operation printData(d: DemoData) : void {
			"System.out.printf(\"d.int32Val: %d\\n\",d.int32Val);"
			"System.out.printf(\"d.float64Val: %f\\n\",d.float64Val);"
			"System.out.printf(\"d.int8Array: \");"
			"for(int i = 0; i<d.int8Array.length; i++) {"
			"System.out.printf(\"%d \",d.int8Array[i]);}"
			"System.out.printf(\"\\nd.stringVal: %s\\n\",d.stringVal);"
		}
bq. 

For MrPing create the following state machine:

!images/025-SendingData08.png!

The transition dialogues should look like this:

For ??init??:

!images/025-SendingData09.png!

For ??wait1??:

!images/025-SendingData10.png!

For ??next??:

!images/025-SendingData11.png!

For ??wait2??:

!images/025-SendingData12.png!

h2. Define the top level

Open the Structure from SendingDataTop and add MrPing and MrPong as a reference. Connect the ports.

!images/025-SendingData13.png!

The model is finished now and the model file should look like this:

bc.. 
RoomModel SendingData {

	LogicalSystem SendingData_LogSystem {
		SubSystemRef SendingDataAppl: SendingData_SubSystem
	}

	SubSystemClass SendingData_SubSystem {
		ActorRef SendigDataTopRef: SendingDataTop
	}

	ActorClass SendingDataTop {
		Structure {
			ActorRef ref0: MrPing
			ActorRef ref1: MrPong
			Binding ref0.PingPongPort and ref1.PingPongPort
		}
		Behavior { }
	}

	ActorClass MrPing {
		Interface {
			conjugated Port PingPongPort: PingPongProtocol
		}
		Structure {
			external Port PingPongPort
		}
		Behavior {
						
			Operation printData(d: DemoData) : void {
						"System.out.printf(\"d.int32Val: %d\\n\",d.int32Val);"
						"System.out.printf(\"d.float64Val: %f\\n\",d.float64Val);"
						"System.out.printf(\"d.int8Array: \");"
						"for(int i = 0; i<d.int8Array.length; i++) {"
						"System.out.printf(\"%d \",d.int8Array[i]);}"
						"System.out.printf(\"\\nd.stringVal: %s\\n\",d.stringVal);"
					}
					
			StateMachine {
				Transition wait2: waitForPong -> waitForPong {
					triggers {
						<pong: PingPongPort>
					}
					action {
						"printData(data);"
					}
				}
				Transition wait1: waitForPongSimple -> waitForPongSimple {
					triggers {
						<pongSimple: PingPongPort guard {
							"data < 10"
						}>
					}
					action {
						"// keep in mind that MrPong increments"
						"PingPongPort.pingSimple(data);"
						"System.out.printf(\"data: %d\\n\",data);"
					}
				}
				Transition next: waitForPongSimple -> waitForPong {
					triggers {
						<pongSimple: PingPongPort>
					}
					action {
						"System.out.printf(\"data: %d\\n\",data);"
						""
						"DemoData d = new DemoData();"
						"// send the default values"
						"PingPongPort.ping(d);"
						"d.int32Val=815;"
						"for (int i = 0; i<d.int8Array.length;i++){"
						"\td.int8Array[i]=(byte)(i+100);"
						"\t}"
						"d.stringVal=\"some contents\";"
						"d.float64Val=3.141234;"
						"PingPongPort.ping(d);"
					}
				}
				Transition init0: initial -> waitForPongSimple {
					action {
						"PingPongPort.pingSimple(0);"
					}
				}
				State waitForPong
				State waitForPongSimple
			}
		}
	}

	ActorClass MrPong {
		Interface {
			Port PingPongPort: PingPongProtocol
		}
		Structure {
			external Port PingPongPort
		}
		Behavior {
			StateMachine {
				Transition init: initial -> looping { }
				Transition tr0: looping -> looping {
					triggers {
						<ping: PingPongPort>
					}
					action {
						"PingPongPort.pong(data);"
					}
				}
				Transition tr1: looping -> looping {
					triggers {
						<pingSimple: PingPongPort>
					}
					action {
						"PingPongPort.pongSimple(data+1);"
					}
				}
				State looping
			}
		}
	}

	ProtocolClass PingPongProtocol {
		incoming {
			Message ping(data: DemoData)
			Message pingSimple(data: int32)
		}
		outgoing {
			Message pong(data: DemoData)
			Message pongSimple(data: int32)
		}
	}

	DataClass DemoData {
		Attribute int32Val: int32 = "4711"
		Attribute int8Array [ 10 ]: int8 = "{1,2,3,4,5,6,7,8,9,10}"
		Attribute float64Val: float64 = "0.0"
		Attribute stringVal: string = "\"empty\""
	}
}
bq. 

h2. Generate and run the model

With the MWe2 workflow generate the code and run the model. 
The output should look like this:

bq.. 
type 'quit' to exit
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 1
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 2
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 3
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 4
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 5
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 6
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 7
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 8
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 9
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPongSimple
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
data: 10
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPong
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
/SendingData_SubSystem/SendigDataTopRef/ref1 -> looping
d.int32Val: 4711
d.float64Val: 0,000000
d.int8Array: 1 2 3 4 5 6 7 8 9 10 
d.stringVal: empty
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPong
d.int32Val: 815
d.float64Val: 3,141234
d.int8Array: 100 101 102 103 104 105 106 107 108 109 
d.stringVal: some contents
/SendingData_SubSystem/SendigDataTopRef/ref0 -> waitForPong
quit
echo: quit
bq. 

h2. Summary

Within the first loop an integer value will be incremented from ??MrPong?? and sent back to ??MrPing??. As long as the guard is true ??MrPing?? sends back the value.

Within the ??next?? transition, ??MrPing?? creates a data class and sends the default values. Than ??MrPing?? changes the values and sends the class again. At this point you should note that during the send operation, a copy of the data class will be created and sent. Otherwise it would not be possible to send the same object two times, even more it would not be possible to send a stack object at all. 
In future versions of eTrice an additional mechanism to send references will be implemented. However, keep in mind that sending references transfers the responsibility of the life cycle of the sent object to the user. It looks simple but is a very common source of failures.  


Back to the top