Generate production quality code from state diagrams created with UModel#
SinelaboreRT generates readable and maintainable code from hierarchical UML state machines created with UModel. With its C/C++ backend it is well suited for embedded real-time and low-power applications. With support for code generation in additional widely used programming languages, it is also well suited for backend and IoT software.
When using UModel some tool specific things must be taken care of. The following section discusses these points. First take a look on the figure below that shows how a rather complex state chart looks like in UModel.

A rather complex state chart designed in UModel. Most of the supported elements are used in this diagram.
Organizing your project#
The code-generator needs to know how to find your state-based class in the XMI file. The path to the class containing the state machine 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 state diagram from above. From there you can directly derive the path which is -t 'Model:class' for the shown example. The different parts of the path must be separated with colons.

UModel’s project browser. The package structure defines the path to the class you want to generate code from.
Attaching action and include comments#
Remember that you can specify code (so called header code) that is simply copied at the beginning of the state machine c-file. Also you can specify action code that is called every time the state machine gets executed. Add the action- and header code in the documentation window of the class containing the state machine. The text must start with either action: or header: (see Adapting the Generated Code). It is not possible to use a Note which is attached to the class as UModel does unfortunately not export notes in the XMI file. The documentation window can contain action - and/or include code. The order does not matter.
UModel does not export line end information correctly from the documentation window. Therefore it was necessary to define a specific line break character. Use the paragraph character § for that purpose. It is usually not used in normal C-code. An example documentation window with action - and include code is shown in the figure below.

Action and include comments can be specified in the documentation window of the class which owns the state machine.
Saving your project to XMI#
UModel allows you to export your project in XMI format. All the project information is stored in the XMI file. Check the tick boxes as shown in the figure below to export a XMI file which can be processed from the code generator.

Exporting the project to XMI.
States#
UModel allows you to define entry, do and exit activities as well as inner transitions for a state. Do not use the interactions for that purpose but the activities. Only one single line can be specified for the entry, do and exit activities. If you want to execute more code define a function which contains the code.
There are different type of states in the toolbar. Simple states can’t contain children. A composite state can contain further children. If you are unsure if a state will have substates later on draw a composite state right from the beginning. There seems to be no way to change a simple state into a composite one later on. The use of orthogonal states is not supported from the code generator so far. See Regions in the manual (PDF) for a possible solution.
Transitions#
To create a transition click the transition handle of the source state (on the right of the element). Then drag-and-drop the transition arrow onto the target state. A text field is shown. Type in the event name and optionally the guard and the action. This is quite convenient as it allows fast editing. The event, guard and actions can also be specified later on by adding the element in the model tree below the transition.
History State#
Use the Shallow History if you want to make a state a history state.
Deep History#
Use the Deep History if you want to make all child states history states. This has the same effect as adding a Shallow History marker to all composite child states.
Adding Operations and Attributes to Classes#
It is possible to add attributes and operations to the class containing the state machine. Attributes are added to the instance data and always require a default value. Operations are mapped to C-functions and added to the state machine header file. Hint: Directly use data types that you want to use in your code later on.

Class with user defined attributes and operations. The code generator includes them into the generated code.
Supported / Unsupported#
The code generator supports a subset of the design elements provided by UModel. The supported elements are:
- Hierarchical states
- 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
- Multiple state machines/regions in a diagram
- Sync states
- Entry and exit points (not to compare with entry/exit actions within states)
- Terminate and Fork/Join
Example: a microwave oven on Windows with MinGW#
Getting started on Windows and MinGW by creating a microwave oven#
The following example shows how the code generator workflow works with UModel using a simplified microwave oven. It walks you through building a microwave oven example using UModel, MinGW, and SinelaboreRT code generation.
Requirements#
- Cooking time can be adjusted.
- Cooking starts if the cooking time is > 0s and the door is closed.
- If the door is opened during cooking, the microwave generator is switched off and the cooking time stops.
- Cooking continues if the cooking time is not over and the door is closed again (history).
- Cooking stops if the cooking time is over or the time is adjusted to zero.
- Cooking time and power can be changed at any time.
- The state machine below shows a possible solution.
The basis is a class model that contains the oven class with the state machine. The class can contain attributes and methods which are added to the generated code.

The State Machine Diagram#

Generate Code#
To generate C code, export your model to XMI and call the code generator as follows. With -t you can specify which class to generate code from. The path must match the path to the class in your model.
java -cp "../../bin/*" codegen.Main -p UModel -l cx -t "MicrowaveOven:MachineClass" -o oven oven.xmiAfter model validation (for example duplicate-state checks), full state machine code is generated.
Build and Run Generated Code#
After code generation, run the build:
mingw32-make clean
mingw32-makeAfter build, run the oven executable. You can send events to the oven with the keys as shown on the console help text. E.g. fire events such as + and c.
