Generate production quality code from state diagrams created with Modelio#

SinelaboreRT generates readable and maintainable code from hierarchical UML state machines created with Modelio. With its unique features, the tool is well suited for embedded real-time and low-power application development. With its C++, Java, and other backends, it is also a good fit for server and desktop software.
When using Modelio some tool specific things must be taken care of. The following section discusses these points. First take a look how the microwave oven example from the introduction section looks like in Modelio.

The microwave oven state chart designed in Modelio
Organizing your project#
The code-generator needs to know how to find your state-based class in the exported XMI file. The path to your class in your project tree must be specified on the command line using the -t flag.
The figure below shows the project browser window for the ’oven’ example state diagram from the tutorial section. You can easily derive the path which is -t 'OvenClass' in this case. In case of multiple packages the different parts of the path must be separated with colons.

Modelio’s project browser. From the used structure the path can be directly derived.
Exporting your project to XMI#
The exported XMI file is used as input for the code-generator. Right click on the oven package in the model tree and select XMI → Export to XMI. Select ’OMG UML 2.4.1’ and deselect ’Added Modelio annotations’ in the export dialog before exporting.
States#
Modelio allows you to define entry, do and exit activities for a state. Right click to the state and select ’Create an element’ → ’Internal transition’. Activities created this way directly in the graphics are immediately visible. If created in the model tree (right click on the state in the model tree → Create an element → Internal transition) it is necessary to drop the activity into the state before they are displayed.
Internal transitions are presently not supported by Modelio. To add an internal transition to a state attach a note to a state and specify the transition following the syntax shown in States.
To add child states to a parent state just drop a new state inside its parent.
Regions#
A region is an orthogonal part of a state. It allows to express parallelism within a state. A state can have two or more regions. Region contains states and transitions. States in regions can contain regions again.
The figure below shows a state machine with a state containing two regions.

A state machine with a state containing two regions.
Rules when using regions:
- Transitions must not cross region borders. I.e. it is not allowed to add a transition from
RighttoOff. - A region might contain a history state, choices, final states and normal states
- To express that a transition should fire if the machine is in
RightandOffuse a conditional transition like follows:#machineIsInRight(…) && machineIsInOff(…) - Regions can contain states which contain regions again
Sub-Machines#
A sub-machine state specifies the insertion of the specification of a sub-machine state machine. The state machine that contains the sub-machine state is called the containing state machine. A sub-machine state is semantically equivalent to a composite state but you only see the top level state. Sub-machines are usually used to “hide” complexity in the containing state machine.
To connect a sub-machine with the containing state you have to link the state containing the sub-machine to the sub-machine diagram.
To link a state to a sub-machine specify the sub-machine in the state properties as shown in the figure below.

Specify the linked sub-machine in the state properties.
To link transitions between states of the containing state machine with states in the sub-machine diagram you have to use entry and exit points. In Modelio add connection points to the parent state of the sub-machine (e.g. S2). And add entry - and exit points to the sub-machine diagram (e.g. EX1).
Defining the connection point reference in the connection point properties finally links the connection point on the top level state to an entry - or exit point in the sub-machine as shown in the figure below.

Link the connection point to an entry – or exit point in the sub-machine.
After linking the connection point to the entry - or exit point the connection point changes its icon and shows either an exit icon or an entry icon. The figure below show the top level diagram with a state containing a sub-machine (S2) and the sub-machine in the figure below.

Diagram with state S2 referencing a sub-machine.

Sub-machine diagram.
Transitions#
To set transition properties select the transition. Now you define the trigger, guard and action statements. To make sure Modelio knows the Signal it must be defined beforehand in the model tree (add a signal and set its kind property to Signal). See the figure above how the model tree looks like with some signals defined.
To display the transition text enable the Show label property. Otherwise the transition trigger and action code is not shown.

Transition properties. Ensure that the ’received event’ was previously defined and the (from …) is shown. Otherwise the code generator generates an error later on.
History State and other Pseudo States#
Just add final states or history states from the tool bar into your diagram.
Attaching action and include comments#
By default the code generator includes the minimally needed header files into the generated code. But often it is necessary to add further includes or define local variables etc. In this case you can specify code that is just copied at the beginning of the state machine implementation file.
You can also add code that is added to the beginning of the state machine handler function. The code can be used to perform some actions each time the state machine is executed.
Add this code to your model by using comments starting with the keywords action: and include: (see Adapting the Generated Code). Link those comments to the class owning the state machine. An example is shown in the figure below. You must use plain text in the comment. It is not possible to use html comments.

Specify header and action code using comments. Make sure the format of the comment is ’plain text’.
Supported / Unsupported#
The code generator supports a subset of the design elements provided by Modelio. The supported elements are as follows:
- Hierarchical states
- Regions and “regions in regions”
- Sub-machines in a top level state
- (Signal-)Events with event name, guard and action
- Initial and final pseudo-states
- History states (deep, flat), Extended history handling (see History States in the manual (PDF))
- Choices
- Junctions
The unsupported elements are:
- Constraints
- Sync-states and junctions
- Entry and exit points (not to compare with entry/exit actions within states)
- Terminate and Fork/Join
Modelio How-To#
What options should I use for exporting an XMI file in Modelio 3?#
Select the following options:

Examples#
Watch the code generator in action#
The following example shows how the code generator works with a simple microwave oven.

Requirements:
- Cooking time can be adjusted from 0 s to 60 s.
- Cooking starts if the cooking time is > 0 s and the door is closed.
- If the door is opened during cooking, the microwave generator is switched off and cooking time stops.
- Cooking continues if cooking time is not over and the door is closed again.
- Cooking stops if cooking time is over or time is adjusted to zero.
- Cooking time and power can be changed at any time.
The state machine below shows one possible solution.

To generate C code, export your model to XMI and call the code generator:
java -jar codegen.jar -p Modelio -t "Model:Class Model:first_example_step3" -o oven first_example_step3.xmlAfter validation against well-formedness rules (for example duplicate state names), complete state machine code is generated.
Generated files include:
oven.coven.hoven_ext.h
It is also possible to generate code from activity diagrams. For example, the selftest() function can be modeled as an activity diagram.

More complex examples#

