Table of Contents
State Diagram Editor Introduction Part II
In the first part of the tutorial you learned to create the first state diagrams and to generate code from them. In this part you will get to know other features of the graphical editor. In particular regarding the handling of regions.
In state diagrams usually only one state is active at a time. In UML state diagrams regions allow to model concurrency – i.e. more than one state can be active at a time (AND states). A UML state may be divided into regions (one or more). Each region contains a state diagram again. Regions are executed in parallel. You can think of regions as independent state machines displayed in one diagram.
Let's add some regions to the diagram we created in part I of this tutorial. Therefore right click on state S1 and add a region. Then add another region. A default name is defined but you can change it during creation or later at any time. Then add a state to each region entering some entry and exit code. Create a transition from the init states to the just created states. Your image should look like similar to the one just below.
You have probably noticed that it was not possible to select other states as transition target than those that are within a region. That is normal. Transitions cannot cross regional boundaries.
If a state contains only one region it is basically equivalent to a hierarchical state. But to to be able to visualise the states in a separate diagram it might be still wanted to model just on region within a state.
To convert hierarchical states into a region can be done using the context menu “Convert State into new Region”. This creates a new region and moves previous children of the state into the new region.
The opposite is also possible. Select “Convert to State with Children”.
Regions can be displayed into a single big diagram as done here, or in separate diagrams. To display a region in a separate diagram right click on a region and select “Display Region in separate Tab”. Then a new tab with the region diagram is shown. This is useful if the state diagram becomes and more space is needed.
Like last time check your design, save it and generate code from it.
Compile again and execute the code. the output looks as follows:
D:\>manual Entry S1 Entry R1S1 Entry R2S1 -- Exit R1S1 Exit R2S1 Exit S1 ev1 Entry S2 -- Exit S2 ev2 Entry S2 Now we set i>0 Exit S2 ev2 i>0 D:\>
As you can see the entry and exit code of both regions inside S1 is executed when entering or leaving S1. I.e. they run “in parallel” if the machine is in state S1.
Exporting parts of your diagram
If you want to export parts of your diagram to the clipboard for later use in another diagram select right click on the state that you want to export and select the export command. The exported diagram is copied in XML format to the clipboard. It can be imported right away in another diagram. Or saved in a text file for later reuse. This allows to build a library of reusable state machine diagrams.
Regions are a powerful mechanism for modeling whether a machine is in more than one state at the same time. E.g. a microwave oven “turns the motor” AND “the light is on” AND “the door must be closed.” Such situations can be modeled well with regions.
The example folder contains the example of a microwave oven as a hierarchical UML model. And once modeled with the help of regions.