Generate production quality code from state diagrams created with Magic Draw#
SinelaboreRT generates readable and maintainable code from hierarchical UML state machines. With its C backend it is well suited for embedded real-time and low-power applications. With C++, Python and Java backends it is also useful for server and desktop software.
When using Magic Draw some tool specific things must be taken care of. The following section discusses these points. First take a look how a rather complex state chart looks like in Magic Draw.

A rather complex state chart designed in Magic Draw
Organizing your project#
The code-generator needs to know how to find your state-based class in the XMI file. The path to your class in your project tree must be specified on the command line using the -t flag.
The following figure shows the project browser window for the state diagram from above. From the figure below you can directly derive the path which is -t 'MyModel:Class Model:complex_class'. The different parts of the path must be separated with colons.

Magic Draw’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 that is just copied at the beginning of the state machine c-file. Also you can add action code that is called every time the state machine is called. This can be done by using comments starting with the keywords action: and include: (see Adapting the Generated Code). It is important that you use a comment and not a note! In Magic Draw these two comments must be linked to the class owning the state machine.

Action and include comments must be linked to the class which owns the state machine.
Saving your project to XMI#
Magic Draw allows you to save a project directly in XMI format. All the project information is stored in the XMI file. No other project file is needed. This is very handy as no export of the XMI file is required. Please remember to tick the XMI 2.1 (rich XMI) check box in the save dialog. If not checked the generated XML export can’t be processed from the code generator.
State Details#
Magic Draw allows you to define entry, do and exit actions as well as inner transitions for a state. Actions are usually operations (i.e. functions) which are triggered in case of the event. This means that for each of these actions a C-function (operation) must be defined. Sometimes this is not what you want especially if the action code is very short and a function would add a lot of overhead. There are two options to overcome this.
Option A: Just misuse the operation name as field where you can type in the C-code you want to execute. It is important that the Behavior Type is defined as Opaque Behavior as shown in the figure below.
Option B: You can attach a comment (not a note!) to a state (either a child state or a composite state) and specify entry/exit or action code in there. To edit the comment click on it and enter the definitions as needed. The definitions must follow the syntax as described in States. Make sure you use plain text. The code-generator is not able to handle HTML text.

A state with entry/exit/do code plus an inner event following option A

Actions must have the behaviour type set to Opaque Behaviour.
To define an inner transition double click on a state and select Internal Transitions. The definition of an internal transition is done the same way as a normal transition is defined. See the next section for more info.
Transitions#
Double clicking on a transition brings up the transition properties dialog (see the figure below). This dialog allows you to define the event that triggers the state transition. As usual you can specify a guard (a valid C-statement evaluating to true or false), the effect and the trigger itself. The trigger type must be set to type SignalEvent. The effect behaviour type must be set to Opaque Behaviour.
To add a guard click on the guard field and then on the tree appearing dots. Now select Constraint as guard type. Now type in the guard in the Specification field (not in the name field!). The guard should appear in bracket braces after the event name if you click close.

Magic Draw’s transition properties dialog.
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.
Sub-Machines#
Sub-machine states allow to “hide” the child states of a state with hierarchy. You only see the child states etc. if you double click on the state which opens the internal view. From the code generator point of view a sub-machine is a normal state with children. There is absolutely no difference compared to a normal hierarchical state.
But using a sub-machine state instead of a state has some other consequences:
-
MD does not allow to provide entry/do/exit actions for a sub-state. You have to attach a comment field to specify state actions to overcome this MD limitation.
-
It is not possible to connect a transition starting from a state outside the sub-machine state to a state in the sub-machine. And vice versa. Use entry and exit pseudo states for this purpose (see next section).
-
MD does not allow to “transform” a sub-machine state into a state with children. You have to do this manually by moving states etc. in the Model Browser.
Entry and Exit Points#
Note: Entry and exit points are only useful together with a sub-machine. Read the previous section before if you are not familiar with the usage of sub-machines.
When entering a sub-machine usually the initial state is entered. I.e. the transition ends at the border of the sub-machine state If this should not be the case for a specific transition it is possible to place an entry point inside the sub-machine state. This entry point serves as glue between the sub-machine state and the internal of the sub-machine.
Exit states provide a similar function. By default only transition can be modelled starting from the sub-machine. If a transition should start from a specific state inside the sub-machine and enter a state outside the sub-machine an exit state can be used. Again this exit state serves as glue between the transition starting inside the sub-machine and ending one level up at another state.
To use entry and exit points in your model use the following recipe:
- Double click on the sub-machine state to open the sub-machine diagram
- Drag and drop an entry and/or exit point inside your sub-machine diagram
- Give the points a meaningful name.
- Go up to the outside diagram
- Place references of the entry / exit points on the border of the sub-machine state.
- Connect transitions
- The sub-machine must be placed below the main state machine and its name must start with four underlines (’
____’). This is needed that the code-generator can identify the machine as sub-machine. See the figure below.

Place your sub-machine below the main state as shown in this figure.
Limitations when using entry and exit points:
- An exit point can have more than one incoming transitions inside the sub-machine. But only one outgoing transition from the sub-machine state.
- An entry state can have more than one incoming transitions on the sub-machine state. But only one outgoing transition inside the sub-machine.
- The transition leaving the exit or entry point must end in a normal state. Chaining of entry / exit states or connecting these transitions to choices or junctions is not allowed.
The figure below shows an example for a diagram with a sub-machine and the internals of this sub-machine.

(a) Top level diagram with the sub-machine state S3

(b) Internals of the sub-machine state S3
This figure shows the top level diagram with a sub-machine state and the internals of the sub-machine state. The transitions between these two diagrams are glued together using entry and exit points.
Supported / Unsupported#
The code generator supports a subset of the design elements provided by Magic Draw. The supported elements are:
- Hierarchical states
- (Signal-)Events with eventname, guard and action
- Initial and final pseudostates
- Sub-machines and entry / exit points
- History states (deep, flat), Extended history handling (see History States in the manual (PDF))
- Choices
- Junctions
- Constraints
The unsupported elements are:
- Multiple state machines/regions in a diagram
- Syncstates
- Terminate and Fork/Join
Magic Draw How-To#
I don’t see the on-entry/on-exit/do code in my state diagram#
Since Magic Draw 19, it is necessary to copy your code to the body text.

Is it necessary to specify an action name for on-entry/on-exit/do code?#
If there is text in the name field, that text is used for code generation. If the name field is empty, the body part is used for code generation. The body text can be longer than one line.

Example: a microwave oven#
Watch the code generator in action#
The following example shows how the code generator works using 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 the cooking time stops.
- Cooking continues if the cooking time is not over and the door is closed again.
- 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 and class diagram below show a possible solution.


To generate C code, export your model to XMI and call the code generator as follows:
java -cp "../../bin/*" codegen.Main -l cx -p md -t "Model:oven_package:oven" -o oven oven.xmi.xmlAfter model validation (for example duplicate-state checks), full state machine code is generated.
Generated output files (example):
oven.coven.hoven_ext.h
In the examples folder you will find complete sample code ready to test and expand.