Sinelabore

Generate code from state diagrams created with the Cadifra UML editor#

Cadifra is a fast and lightweight UML diagram editor for Windows. It supports Class, Object, Sequence, State, and Use Case diagrams. Each diagram is stored in its own file. The SinelaboreRT code generator directly uses the state diagram model file. The editor is especially well suited for designs with few hierarchy levels.

The Cadifra UML editor is a very easy to use and lightweight UML modelling tool. It was not directly designed to generate code from its diagrams. Because of this it does not provide special means such as dialogs to enter events, guards, entry or exit actions and so forth. This section describes how to draw diagrams with all needed information using the available editor features.

Events#

To add an event to a transition right click to the transition line and select ’New Text’ as shown in the figure below. For the event definition you must follow the syntax as described in Transitions. Only text associated with the transition (indicated with a dashed line) is detected by the code generator. A free text element will be ignored and the generator will complain about the missing event. Even if it might look ok for you as the free text is located close to the transition. For layout reasons it is allowed to put the event and guard in different lines.

To enter events right click to the transition and use a text field to enter the event definition.

To enter events right click to the transition and use a text field to enter the event definition.

Hierarchical States#

To draw hierarchical states (aka composite states) increase the size of the parent state (e.g. S1) until there is enough space to carry the new child state (e.g. S11) . Then move the child state into the parent state’s border. The ’Large’ flag is automatically set on the parent state. With this flag set the state name is shown in the upper left corner. To add another child (e.g S111 or S12) repeat the steps from above. In total 3 levels of nesting are supported from the code-generator. For flags on the third level the ’Large’ flag shall not be set (see the figure below).

Composition of states. Move the child into the parent create a composition.

Composition of states. Move the child into the parent create a composition.

The figure below shows a fictitious example with three levels of nesting.

Three levels of states drawn with the Cadifra UML Editor. For states on level one and two the ’Large’ flag shall be set.

Three levels of states drawn with the Cadifra UML Editor. For states on level one and two the ’Large’ flag shall be set.

Adding State Details#

To add entry, exit, action or inner events a compartment must be added to the state. To do so right click to the state and select ’Add compartment’ as shown in the figure below. To edit the compartment double click on it and enter the definitions as needed. The definitions must follow the syntax as described in States. Compartments can only be used for states without children. For states with children attach a note to the state and put the state details into the note. The note must start with the text ’Compartment’. See the figure above for an example.

To enter state details right click to the state and add a compartment to it.

To enter state details right click to the state and add a compartment to it.

History States#

Append the text ’ (H)’ to the state state name if you want to make a state a history state (shallow history) (e.g. ’S1 (H)’).

Append the text ’ (H*)’ to the state name if you want to make all child states history states (deep history). This has the same effect as adding a Shallow History marker to all composite child states (e.g. ’S1 (H*)’).

Since version 6.4 transitions can also end in history states. See History States in the manual (PDF) for further explanations. Cadifra does not support history states. Therefore a special syntax for the state name must be used.

  • A shallow history pseudostate must begin with (H and end with ). Examples: (H), (Hist1)
  • A deep history pseudostate must begin with (H and end with ). Examples: (H*), (Hist1*)

An example for a diagram with history state usage is shown in the figure below.

History states can have one or more incoming transitions but must have no outgoing transition.

History states can have one or more incoming transitions but must have no outgoing transition.

Choices#

Choices are not directly supported from the UML Editor. However with the help of a naming convention it is possible to use choices. If a state name begins and ends with angle brackets it is assumed by the codegen that it is a choice state. Valid names are e.g. <>, < >, <C1> or <AnyText>. The following figure shows an example state chart with three choice states.

Choice states must have angled brackets in its name.

Choice states must have angled brackets in its name.

Junctions#

Junctions are not directly supported from the UML Editor. However with the help of a naming convention it is possible to use junctions. If a state name begins and ends with round brackets it is assumed by the codegen that it is a junction state. Valid junction names are e.g. (J), (J1) or (JAnyText). The following figure shows an example state chart with a junction.

For more background information about junctions see Junctions.

Junction states must have round brackets in its name.

Junction states must have round brackets in its name.

Connection Points#

This is a feature only available for the Cadifra editor and also not available in UML. The concept of connection points is well known from circuit diagram drawing tools. Because Cadifra has no direct support the realization is similar to Junctions. Exactly two states must have the same name, each surrounded by square brackets. Internally the code generator removes these states and creates a single transition. One of these states must have an incoming transition, and the other must have an outgoing transition. Event name, guard and action are only taken from the entering transition. An example is shown in the figure below.

Connection point states must have square brackets around their name. In this example transition S1 –evC→ S3 and transition S3 –evB→ S11 were realized with connection points.

Connection point states must have square brackets around their name. In this example transition S1 –evC→ S3 and transition S3 –evB→ S11 were realized with connection points.

Supported / Unsupported#

The codegenerator supports the following features for Cadifra UML:

  • Hierarchical states
  • (Signal-)Events with eventname, guard and action
  • Initial and final pseudostates
  • History states, 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
  • Entry and exit points (not to compare with entry/exit actions within states)
  • Terminate and Fork/Join

Video tutorial#

Part I#

This video shows how easy it is to use the SinelaboreRT code generator with the Cadifra UML editor. A small state diagram is created and then converted into C code using the code generator.

All development steps are shown.

A basic main.c is then used to send events to the state machine and execute it.

To follow the tutorial, download the demo version of SinelaboreRT and the Cadifra UML Editor from the download page. A C compiler (for example gcc) is also needed to compile the generated C code.

(5:17 min)

Part II#

In the second part of the tutorial, the first example is extended. S1 is changed into a composite state (a state with children). Two child states and several transitions are added.

(5:40 min)

Then state machine C code is generated again using the code generator. In the next step, main.c is extended to send one of the new events to the machine.

Finally, testcase generation from the state diagram is shown. The automatically generated test cases provide a good basis for testing the state diagram.