Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ff117363562a1ccbd773976a1f8f49e432c6b50e (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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
h1. eTrice Overview

h2. What is eTrice?

eTrice provides an implementation of the ROOM modeling language (Real Time Object Oriented Modeling) together with editors, code generators for Java, C++ and C code and exemplary target middleware.

The model is defined in textual form (Xtext) with graphical editors (Graphiti) for the structural and behavioral (i.e. state machine) parts.  

h2. Who should use eTrice?

Basically everyone who develops eventdriven realtime or embedded systems. 

If you have other ideas how to use it, tell us!

h2. How Does It Work?

TODO

h2. Who is Behind eTrice?

TODO

h1. Introduction to the ROOM Language


h1. Tutorial HelloWorld

h2. Scope

In this tutorial you will build your first very simple eTrice model. The goal is to learn the work flow of eTrice and to understand a few basic features of ROOM. You will perform the following steps:

# create a new model from scratch
# add a very simple state machine to an actor
# generate the source code
# run the model
# open the message sequence chart

h2. Create a new model from scratch

The easiest way to create a new eTrice Project is to use the eclipse project wizard. From the eclipse file menu select ??File->New->Project?? and create a new eTrice project and name it ??HelloWorld??

!images/015-HelloWorld10.PNG!

The wizard creates everything that is needed to create, build and run a eTrice model. The resulting project should look like this:

!images/015-HelloWorld11.PNG!

Within the model directory the model file ??HelloWorld.room?? was created. Open the ??HelloWorld.room?? file and position the cursor at the very beginning of the file. Open the content assist with Ctrl+Space and select ??model skeleton??.

!images/015-HelloWorld12.PNG!   

Edit the template variables and remove the artefacts from the wizard. 

The resulting model code should look like this:

bc.. 
RoomModel HelloWorld {

	LogicalSystem System_HelloWorld {
		SubSystemRef subsystem : SubSystem_HelloWorld
	}

	SubSystemClass SubSystem_HelloWorld {
		ActorRef application : HelloWorldTop
	}

	ActorClass HelloWorldTop {
	}
} 
bq. 

The goal of eTrice is to describe distributed systems on a logical level. In the current version not all elements will be supported. But as prerequisite for further versions the following elements are mandatory for an eTrice model:
* the ??LogicalSystem?? 
* at least one ??SubSystemClass??
* at least one ??ActorClass??

The ??LogicalSystem?? represents the complete distributed system and contains at least one ??SubSystemRef??. The ??SubSystemClass?? represents an address space and contains at least one ??ActorRef??. The ??ActorClass?? is the building block of which an application will be built of. It is in general a good idea to define a top level actor that can be used as reference within the subsystem.

The outline view of the textual ROOM editor shows the main modeling elements in an easy to navigate tree.


!images/015-HelloWorld02.PNG!


h2. Create a state machine

We will implement the Hello World code on the initial transition of the ??HelloWorldTop?? actor. Therefore open the state machine editor by right clicking the ??HelloWorldTop?? actor in the outline view and select ??Edit Behavior??.

!images/015-HelloWorld03.PNG!

The state machine editor will be opened. Drag and drop an ??Initial Point?? from the tool box to the diagram into the top level state. Drag and drop a ??State?? from the tool box to the diagram. Confirm the dialogue with ??ok??. Select the ??Transition?? in the tool box and draw the transition from the ??Initial Point?? to the State. Open the transition dialogue by double clicking the caption of the transition and fill in the action code.

bc. System.out.println("Hello World !");
 
The result should look like this:

!images/015-HelloWorld04.PNG!

Save the diagram and inspect the model file. Note that the textual representation was created after saving the diagram.

!images/015-HelloWorld05.PNG!


h2. Build and run the model

Now the model is finished and source code can be generated. The project wizard has created a workflow that is responsible to generate the source code. From ??HelloWorld/src/workflow?? right click ??HelloWorld.mwe2?? and run it as MWE2Workflow. All model files in the model directory will be generated.

!images/015-HelloWorld06.PNG!

The code will be generated to the src-gen directory. The main function will be contained in ??SubSystem_HelloWorldRunner.java??. Select this file and run it as Java application.

!images/015-HelloWorld07.PNG!


The Hello World application starts and the string will be printed on the console window. To stop the application the user must type ??quit?? in the console window.

!images/015-HelloWorld08.PNG!

h2. Open the Message Sequence Chart

During runtime the application produced a MSC and wrote it to a file. Open /org.eclipse.etrice.doc.tutorials/tmp/log/SubSystem_HelloWorld_Async.seq using Trace2UML (it is open source and can be obtained from  http://trace2uml.tigris.org/). You should see something like this:

!images/015-HelloWorld09.PNG!


h2. Summary

Now you have generated your first eTrice model from scratch. You can switch between diagram editor and model (.room file) and you can see what will be generated during editing and saving the diagram files. 
You should take a look at the generated source files to understand how the state machine is generated and the life cycle of the application. The next tutorials will deal with more complex hierarchies in structure and behavior.
 

h1. Tutorial Blinky

h2. Scope

This tutorial describes how to use the ??TimingService??, how to combine a generated model with manual code and how to model a hierarchical state machine. The idea of the tutorial is to switch a LED on and off. The behavior of the LED should be: blinking in a one second interval for 5 seconds, stop blinking for 5 seconds, blinking, stop,...  
For this exercise we will use a little GUI class that will be used in more sophisticated tutorials too. The GUI simulates a pedestrian traffic crossing. For now, just a simple LED simulation will be used from the GUI. 

To use the GUI please copy the package ??de.protos.PedLightGUI?? to your ??src?? directory. The package contains four java classes which implement a small window with a 3-light traffic light which simulates the signals for the car traffic and a 2-light traffic light which simulates the pedestrian signals.

The GUI looks like this:

!images/020-Blinky08.PNG!

Within this tutorial we just will toggle the yellow light.

You will perform the following steps:

# create a new model from scratch
# define a protocol
# create an actor structure
# create a hierarchical state machine
# use the predefined ??TimingService??
# combine manual code with generated code
# build and run the model
# open the message sequence chart

h2. Create a new model from scratch

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

bc.. 
RoomModel Blinky {

	LogicalSystem System_Blinky {
		SubSystemRef subsystem : SubSystem_Blinky
	}

	SubSystemClass SubSystem_Blinky {
		ActorRef application : BlinkyTop
	}

	ActorClass BlinkyTop {
	}
}
bq. 

h2. Add two additional actor classes

Position the cursor outside any class definition and right click the mouse within the editor window. From the context menu select ??Content Assist??  

!images/020-Blinky02.png!

Select ??ActorClass - actor class skeleton?? and name it ??Blinky??.

!images/020-Blinky01.PNG! 

Repeat the described procedure and name the new actor ??BlinkyController??.

Save the model and visit the outline view.

h2. Create a new protocol

With the help of ??Content Assist?? create a ??ProtocolClass?? and name it ??BlinkyControlProtocol??.
Inside the brackets use the ??Content Assist?? (CTRL+Space) to create two incoming messages called ??start?? and ??stop??.

The resulting code should look like this:

!images/020-Blinky03.PNG! 

With Ctrl-Shift+F or selecting ??Format?? from the context menu you can format the text. Note that all elements are displayed in the outline view.

h2. Import the Timing Service

Switching on and off the LED is timing controlled. Therefore a timing service is needed. To import the timing service in the outline view right click to ??SubSystem_Blinky??. Select ??Edit Structure??. Drag and Drop an ??ActorRef?? to the ??SubSystem_Blinky?? and name it ??application??. From the actor class drop down list select ??BlinkyTop??. Do the same clicks for the timing service. Name it ??timingService?? and from the drop down list select ??room.basic.service.timing.ATimingService??. Draw a ??LayerConnection?? from ??application?? to each service provision point (SPP) of the ??timingService??. The resulting structure should look like this:

!images/020-Blinky06.PNG! 

The current version of eTrice does not provide a graphical element for a service access point (SAP). Therefore the SAPs to access the timing service must be added in the .room file. Open the ??Blinky.room?? file and navigate to the ??Blinky?? actor. Add the following line to the structure of the actor:

bc. SAP timer: room.basic.service.timing.PTimeout

Do the same thing for ??BlinkyController??.

The resulting code should look like this:

!images/020-Blinky07.PNG!


h2. Finish the model structure

From the outline view right click to ??Blinky?? and select ??Edit Structure??. Drag and Drop an ??Interface Port?? to the boarder of the ??Blinky?? actor. Note that an interface port is not possible inside the the actor. Name the port ??ControlPort?? and select ??BlinkyControlProtocol?? from the drop down list. Uncheck ??Conjugated?? and ??Is Relay Port??. Klick ??ok??. The resulting structure should look like this:


!images/020-Blinky04.PNG!

Repeat the above steps for the ??BlinkyController??. Make the port ??Conjugated??

Keep in mind that the protocol defines ??start?? and ??stop?? as incoming messages. ??Blinky?? receives this messages and therefore ??Blinky??'s ??ControlPort?? must be a regular port and ??BlinkyController??'s ??ControlPort?? must be a conjugated port.


From the outline view right click ??BlinkyTop?? and select ??Edit Structure??.

Drag and Drop an ??ActorRef?? inside the ??BlinkyTop?? actor. Name it ??blinky??. From the actor class drop down list select ??Blinky??. Do the same for ??controller??. Connect the ports via the binding tool. The resulting structure should look like this:

!images/020-Blinky05.PNG!

h2. Implement the Behavior

The application should switch on and off the LED for 5 seconds in a 1 second interval, than stop blinking for 5 seconds and start again. To implement this behavior we will implement two FSMs. One for the 1 second interval and one for the 5 second interval. The 1 second blinking should be implemented in ??Blinky??. The 5 second interval should be implemented in ??BlinkyController??. First implement the Controller.

Right click to ??BlinkyController?? and select ??Edit Behavior??.
Drag and Drop the ??Initial Point?? and two ??States?? into the top state. Name the states ??on?? and ??off??. 
Use the ??Transition?? tool to draw transitions from ??init?? to ??off?? from ??on?? to ??off?? and from ??off?? to ??on??.

Open the transition dialog by double click the arrow to specify the trigger event and the action code of each transition. Note that the initial transition does not have a trigger event.

The dialog should look like this:

!{width=500px}images/020-Blinky09.PNG! 

The defined ports will be generated as a member attribute of the actor class from type of the attached protocol. So, to send e message you must state ??port.message(param);??. In this example ??ControlPort.start()?? sends the ??start?? message via the ??ControlPort?? to the outside world. Assuming that ??Blinky?? is connected to this port, the message will start the one second blinking FSM. It is the same thing with the ??timer??. The SAP is also a port and follows the same rules. So it is clear that ??timer.Start(5000);?? will send the ??Start?? message to the timing service. The timing service will send a ??timeoutTick?? message back after 5000ms.

Within each transition the timer will be restarted and the appropriate message will be sent via the ??ControlPort??. 

The resulting state machine should look like this:

!images/020-Blinky10.PNG!

Save the diagram and inspect the ??Blinky.room?? file. The ??BlinkyController?? should look like this:

!images/020-Blinky11.PNG! 
 
Now we will implement ??Blinky??. Due to the fact that ??Blinky?? interacts with the GUI class a view things must to be done in the model file.

Double click ??Blinky?? in the outline view to navigate to ??Blinky?? within the model file.
Add the following code:

!images/020-Blinky12.PNG! 

??usercode1?? will be generated at the beginning of the file, outside the class definition. ??usercode2?? will be generated within the class definition. The code imports the GUI class and instantiates the window class. Attributes for the carLights and pedLights will be declared to easily access the lights in the state machine.
The Operation ??destroyUser()?? is a predefined operation that will be called during shutdown of the application. Within this operation, cleanup of manual coded classes can be done.
 
 
 
Now design the FSM of ??Blinky??. Open the behavior diagram of ??Blinky?? by right clicking the ??Blinky?? actor in the outline view. Create two states named ??blinking?? and ??off??. Right click to ??blinking?? and create a subgraph.

!images/020-Blinky13.PNG!

Create the following state machine. The trigger events between ??on?? and ??off?? are the ??timeoutTick?? from the ??timer?? port. 

!images/020-Blinky14.PNG!

Create entry code for both states by right clicking the state and select ??Edit State...??

Entry code of ??on?? is:

bc..  
timer.Start(1000);
carLights.setState(TrafficLight3.YELLOW); 
bq. 

 
Entry code  of ??off?? is:

bc.. 
timer.Start(1000);
carLights.setState(TrafficLight3.OFF);
bq. 

Navigate to the Top level state by double clicking the ??/blinking?? state. Create the following state machine:

!images/020-Blinky15.PNG!

The trigger event from ??off?? to ??blinking?? is the ??start?? event from the ??ControlPort??.The trigger event from ??blinking?? to ??off?? is the ??stop?? event from the ??ControlPort??.

Action code of the init transition is:

bc.. 
carLights = light.getCarLights();
pedLights = light.getPedLights();
carLights.setState(TrafficLight3.OFF);
pedLights.setState(TrafficLight2.OFF);
bq. 

Action code from ??blinking?? to ??off?? is:

bc.. 
timer.Kill();
carLights.setState(TrafficLight3.OFF); 
bq. 

The complete resulting model looks like this:

bc.. 
RoomModel Blinky {

	LogicalSystem System_Blinky {
		SubSystemRef subsystem: SubSystem_Blinky
	}

	SubSystemClass SubSystem_Blinky {
		ActorRef application: BlinkyTop
		ActorRef timingService: room.basic.service.timing.ATimingService
		LayerConnection ref application satisfied_by timingService.timer
		LayerConnection ref application satisfied_by timingService.timeout
	}

	ActorClass BlinkyTop {
		Structure {
			ActorRef blinky: Blinky
			ActorRef controller: BlinkyController
			Binding blinky.ControlPort and controller.ControlPort
		}
		Behavior { }
	}

	ActorClass Blinky {
		Interface {
			Port ControlPort: BlinkyControlProtocoll
		}
		Structure {
			usercode1{
				"import de.protos.PedLightGUI.*;"
			}
			usercode2 {
				"private PedestrianLightWndNoTcp light = new PedestrianLightWndNoTcp();"
				"private TrafficLight3 carLights;"
				"private TrafficLight2 pedLights;"
				
			}
			external Port ControlPort
			SAP timer: room.basic.service.timing.PTimeout
		}
		Behavior {
			Operation destroyUser(){
				"light.closeWindow();"
			}
			StateMachine {
				Transition init: initial -> off {
					action {
						"carLights = light.getCarLights();"
						"pedLights = light.getPedLights();"
						"carLights.setState(TrafficLight3.OFF);"
						"pedLights.setState(TrafficLight2.OFF);"
					}
				}
				Transition tr0: off -> tp0 of blinking {
					triggers {
						<start: ControlPort>
					}
				}
				Transition tr1: blinking -> off {
					triggers {
						<stop: ControlPort>
					}
					action {
						"timer.Kill();"
						"carLights.setState(TrafficLight3.OFF);"
					}
				}
				State off
				State blinking {
					subgraph {
						Transition tr0: my tp0 -> on
						Transition tr1: on -> off {
							triggers {
								<timeoutTick: timer>
							}
						}
						Transition tr2: off -> on {
							triggers {
								<timeoutTick: timer>
							}
						}
						Transition init: initial -> on { }
						EntryPoint tp0
						State on {
							entry {
								"timer.Start(1000);"
								"carLights.setState(TrafficLight3.YELLOW);"
							}
						}
						State off {
							entry {
								"timer.Start(1000);"
								"carLights.setState(TrafficLight3.OFF);"
							}
						}
					}
				}
			}
		}
	}

	ActorClass BlinkyController {
		Interface {
			conjugated Port ControlPort: BlinkyControlProtocoll
		}
		Structure {
			external Port ControlPort
			SAP timer: room.basic.service.timing.PTimeout
		}
		Behavior {
			StateMachine {
				Transition init: initial -> on {
					action {
						"timer.Start(5000);"
						"ControlPort.start();"
					}
				}
				Transition goOff: on -> off {
					triggers {
						<timeoutTick: timer>
					}
					action {
						"ControlPort.stop();"
						"timer.Start(5000);"
					}
				}
				Transition goOn: off -> on {
					triggers {
						<timeoutTick: timer|timeoutTick: timer>
					}
					action {
						"ControlPort.start();"
						"timer.Start(5000);"
					}
				}
				State on
				State off
			}
		}
	}

	ProtocolClass BlinkyControlProtocoll {
		incoming {
			Message start()
			Message stop()
		}
		outgoing { }
	}

}
bq. 

The model is complete now. You can run and debug the model as described in getting started. Have fun.

h2. Summary

Run the model and take a look at the generated MSCs. Inspect the generated code to understand the runtime model of eTrice. Within this tutorial you have learned how to create a hierarchical FSM with group transitions and history transitions and you have used entry code. You are now familiar with the basic features of eTrice. The further tutorials will take this knowledge as a precondition.


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.  



h1. Tutorial Pedestrian Lights

h2. Scope

The scope of this tutorial is to demonstrate how to receive model messages from outside the model. Calling methods which are not part of the model is simple and you have already done this within the blinky tutorial (this is the other way round: model => external code). Receiving events from outside the model is a very common problem and a very frequently asked question. Therefore this tutorial shows how an external event (outside the model) can be received from the model.

This tutorial is not like hello world or blinky. Being familiar with the basic tool features is mandatory for this tutorial. The goal is to understand the mechanism not to learn the tool features.

The idea behind the exercise is, to control a Pedestrian crossing light. We will use the same GUI as for the blinky tutorial but now we will use the ??REQUEST?? button to start a FSM, which controls the traffic lights.

!images/020-Blinky08.PNG!

The ??REQUEST?? must lead to a model message which starts the activity of the lights.

There are several possibilities to receive external events (e.g. TCP/UDP Socket, using OS messaging mechanism), but the easiest way is, to make a port usable from outside the model. To do that a few steps are necessary:
# specify the messages (within a protocol) which should be sent into the model
# model an actor with a port (which uses the specified protocol) and connect the port to the receiver 
# the external code should know the port (import of the port class)
# the external code should provide a registration method, so that the actor is able to allow access to this port
# the port can be used from the external code

h2. Setup the model

* Use the ??New Model Wizzard?? to create a new eTrice project and name it ??PedLightsController??.
* Copy the package ??de.protos.PedLightGUI?? to your ??src?? directory (see blinky tutorial).
* In PedestrianLightWndNoTcp.jav uncomment line 15 (import), 36, 122 (usage) and 132-134 (registration).
* Copy the following model to your model file:

bc.. 
RoomModel PedLightsController {

	LogicalSystem LogSys_PedLights {
		SubSystemRef application: SubSys_PedLights
	}

	SubSystemClass SubSys_PedLights {
		ActorRef PedLightsTopRef: PedLightsTop
		ActorRef timingService: room.basic.service.timing.ATimingService
		LayerConnection ref PedLightsTopRef satisfied_by timingService.timer
		LayerConnection ref PedLightsTopRef satisfied_by timingService.timeout
	}

	ActorClass PedLightsTop {
		Structure {
			ActorRef adapter: GuiAdapter
			ActorRef controller: Controller
			Binding adapter.ControlPort and controller.ControlPort
		}
		Behavior { }
	}

	ActorClass GuiAdapter {
		Interface {
			conjugated Port ControlPort: PedControlProtocol
		}
		Structure {
			usercode1 {
				"import PedLightGUI.*;"
			}
			usercode2 {
				"private PedestrianLightWndNoTcp lights = new PedestrianLightWndNoTcp(\"Pedestrian Lights\",\"  external port connection \");"
				"private TrafficLight3 carLights;"
				"private TrafficLight2 pedLights;"
			}
			external Port ControlPort
		}
		Behavior {
			Operation destroyUser() {
				"lights.closeWindow();"
			}
			StateMachine {
				Transition init: initial -> running {
					action {
						"carLights=lights.getCarLights();"
						"pedLights=lights.getPedLights();"
						"carLights.setState(TrafficLight3.OFF);"
						"pedLights.setState(TrafficLight2.OFF);"
						"lights.setPort(ControlPort);"
					}
				}
				Transition tr0: running -> running {
					triggers {
						<setCarLights: ControlPort>
					}
					action {
						"carLights.setState(state);"
					}
				}
				Transition tr1: running -> running {
					triggers {
						<setPedLights: ControlPort>
					}
					action {
						"pedLights.setState(state);"
					}
				}
				State running
			}
		}
	}

	ActorClass Controller {
		Interface {
			Port ControlPort: PedControlProtocol
		}
		Structure {
			usercode1 {
				"import PedLightGUI.*;"
			}
			external Port ControlPort
			SAP timer: room.basic.service.timing.PTimeout
		}
		Behavior {
			StateMachine {
				Transition init: initial -> off { }
				Transition tr0: off -> carsGreen {
					triggers {
						<start: ControlPort>
					}
					action {
						"timer.Start(700);"
						"ControlPort.setCarLights(TrafficLight3.GREEN);"
						"ControlPort.setPedLights(TrafficLight2.RED);"
					}
				}
				Transition tr1: carsGreen -> carsYellow {
					triggers {
						<timeoutTick: timer>
					}
					action {
						"timer.Start(700);"
						"ControlPort.setCarLights(TrafficLight3.YELLOW);"
						"ControlPort.setPedLights(TrafficLight2.RED);"
					}
				}
				Transition tr2: carsYellow -> carsRed {
					triggers {
						<timeoutTick: timer>
					}
					action {
						"timer.Start(1500);"
						"ControlPort.setCarLights(TrafficLight3.RED);"
						"ControlPort.setPedLights(TrafficLight2.GREEN);"
					}
				}
				Transition tr3: carsRed -> carsYellowRed {
					triggers {
						<timeoutTick: timer>
					}
					action {
						"timer.Start(700);"
						"ControlPort.setCarLights(TrafficLight3.YELLOW_RED);"
						"ControlPort.setPedLights(TrafficLight2.RED);"
					}
				}
				Transition tr4: carsYellowRed -> carsGreen2 {
					triggers {
						<timeoutTick: timer>
					}
					action {
						"timer.Start(700);"
						"ControlPort.setCarLights(TrafficLight3.GREEN);"
						"ControlPort.setPedLights(TrafficLight2.RED);"
					}
				}
				Transition tr5: carsGreen2 -> off {
					triggers {
						<timeoutTick: timer>
					}
					action {
						"ControlPort.setCarLights(TrafficLight3.OFF);"
						"ControlPort.setPedLights(TrafficLight2.OFF);"
					}
				}
				State off
				State carsGreen
				State carsYellow
				State carsRed
				State carsYellowRed
				State carsGreen2
			}
		}
	}

	ProtocolClass PedControlProtocol {
		incoming {
			Message start()
		}
		outgoing {
			Message setCarLights(state: int32)
			Message setPedLights(state: int32)
		}
	}
}
bq. 

* Arrange the Structure and the Statemachines to understand the model 

!images/030-PedLights01.PNG!
The ??GuiAdapter?? represents the interface to the external code. It registers its ??ControlPort?? by the external code.

!images/030-PedLights02.PNG!
Visit the initial transition to understand the registration. The actor handles the incoming messages as usual and controls the traffic lights as known from blinky. 

!images/030-PedLights03.PNG!
The ??Controller?? receives the ??start?? message and controls the timing of the lights. Note that the ??start?? message will be sent from the external code whenever the ??REQUEST?? button is pressed.

*  Visit the model and take a closer look to the following elements:
# PedControlProtocol => notice that the start message is defined as usual
# Initial transition of the ??GuiAdapter?? => see the registration
# The ??Controller?? => notice that the ??Controller?? receives the external message (not the ??GuiAdapter??). The ??GuiAdapter?? just provides its port and handles the incoming messages.
# Visit the hand written code => see the import statement of the protocol class and the usage of the port.

* Generate and test the model
* Take a look at the generated MSC => notice that the start message will shown as if the ??GuiAdapter?? had sent it.

!images/030-PedLights04.PNG!

h2. Why does it work and why is it safe?

The tutorial shows that it is generally possible to use every port from outside the model as long as the port knows its peer. This is guaranteed by describing protocol and the complete structure (especially the bindings) within the model. 
The only remaining question is: Why is it safe and does not violate the "run to completion" semantic. To answer this question, take a look at the ??MessageService.java?? from the runtime environment. There you will find the receive method which puts each message into the queue. 

bc.. 
	@Override
	public synchronized void receive(Message msg) {
		if (msg!=null) {
			messageQueue.push(msg);
			notifyAll(); // wake up thread to compute message
		}
	}
bq. 

This method is synchronized. That means, regardless who sends the message, the queue is secured. If we later on (e.g. for performance reasons in C/C++) distinguish between internal and external senders (same thread or not), care must be taken to use the external (secure) queue.




h1. ROOM Concepts

h2. Main Concepts

Back to the top