2.4 HelloWorld for Java

2.4.1 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:

  1. create a new model from scratch
  2. add a very simple state machine to an actor
  3. generate the source code
  4. run the model
  5. open the message sequence chart

Make sure that you have set up the workspace as described in Setting up the Workspace for Java.

2.4.2 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 Empty eTrice Java Project and name it HelloWorld.

PIC

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

PIC

Within the model directory the model file HelloWorld.room was created. Open the HelloWorld.room file and delete the contents of the file. Open the content assist with Ctrl+Space and select RoomModel - model skeleton.

PIC

Edit the template variables by typing the new names and jumping with Tab from name to name.

The resulting model code should look like this:

1RoomModel HelloWorld_Model { 
2 
3 LogicalSystem LogSys1 { 
4   SubSystemRef subSysRef1: SubSysClass1 
5 } 
6 
7 SubSystemClass SubSysClass1 { 
8   ActorRef actorRef1: HelloWorldTop 
9   LogicalThread defaultThread 
10 } 
11 
12 ActorClass HelloWorldTop { } 
13 
14}

The goal of eTrice is to describe distributed systems on a logical level. In the current version not all elements will be used. But as prerequisite for further versions the following elements can be defined:

The LogicalSystem represents the complete distributed system and contains at least one SubSystemRef. The SubSystemClass represents an address space (e.g. a linux process or an image for a microcontroller) and contains at least one ActorRef. The ActorClass is the building block for building the hierachical structure of an application. A good point to start is to define a top level actor that can be used as structural root within the subsystem.

The outline view of the textual ROOM editor shows the main modeling elements in a navigation tree. You can jump to an element in the textual editor by double clicking the element in the outline view.

PIC

2.4.3 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.

PIC

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 transition arrow and fill in the action code. Be aware of the different action code in Java and C.


_________________________________________ _________________________________________         

action code for Java

System.out.println("Hello World !");

 
_________________________________________________________________         

action code for C

printf("Hello World\n");


The result should look like this:

PIC

Save the diagram and inspect the model (HelloWorld.room) file. Note that the textual representation was changed after saving the diagram.


______________________________________________ ______________________________________________         

room model for Java

1RoomModel HelloWorld_Model { 
2 LogicalSystem LogSys1 { 
3   SubSystemRef subSysRef1:SubSysClass1 
4 } 
5 SubSystemClass SubSysClass1 { 
6   ActorRef actorRef1:HelloWorldTop 
7   LogicalThread defaultThread 
8 } 
9 ActorClass HelloWorldTop { 
10   Structure { } 
11   Behavior { 
12    StateMachine { 
13      Transition init: initial -> state0 { 
14       action { 
15         "System.out.println(\"Hello World\");" 
16       } 
17      } 
18      State state0 
19    } 
20   } 
21 } 
22}
___________________________________________________         

room model for C

1RoomModel HelloWorld_Model { 
2 LogicalSystem LogSys1 { 
3   SubSystemRef subSysRef1: SubSysClass1 
4 } 
5 SubSystemClass SubSysClass1 { 
6   ActorRef actorRef1: HelloWorldTop 
7   LogicalThread defaultThread 
8 } 
9 ActorClass HelloWorldTop { 
10   Structure { } 
11   Behavior { 
12    StateMachine { 
13      Transition init: initial -> state0 { 
14       action { 
15         "printf(\"Hello World\\n\");" 
16       } 
17      } 
18      State state0 
19    } 
20   } 
21 } 
22}

2.4.4 Build and run the model

Now the model is finished and the source code can be generated. The project wizard has created a launch configuration that is responsible for generating the source code. In the project HelloWorld right click _HelloWorld.launch and run it as gen_HelloWorld.

PIC

The source code for the model will be generated into the folder src-gen. The main function will be contained in SubSystem_HelloWorldRunner.java. Select this file and run it as Java application.

PIC

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

PIC

2.4.5 Open the Message Sequence Chart

For debugging and learning purposes, the application produced a Message Sequence Chart and wrote it to a file. Open the file subSysRef1_Async.seq or msc.seq in the folder HelloWorld/tmp/log/ using the tool Trace2UML. Create the path if not already there.

Trace2UML is an open source MSC viewer and can be obtained here:

After opening the file, you should see something like this:

PIC

The Actor with the instance path /LogSys1/subSysRef1/actorRef1 is in the state state0. This is the simplest possible MSC. The MSCs for further tutorials will contain more information.

2.4.6 Summary

Now you have generated your first eTrice model from scratch. You can switch between diagram editor and textual model representation (.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 works. The next tutorials will deal with more complex hierarchies in structure and behavior.