Generate code from state diagrams created with astah#
astah* formerly known as JUDE is a UML modeling tool created by Japanese company ChangeVision. It is written in Java and can therefore run on different operating systems. This text considers both astah and astah SysML. Differences are explained where necessary.
In opposite to some other tools astah provides a Java API for direct access to the model file. Therefore it is not necessary to export the model (e.g. in XMI format). The SinelaboreRT code generator can directly access the model file. This makes the development cycle very fast.
To make it possible for the java runtime to access the astah jar file there are two options:
Option a: copy the astah jar files next to the code generator#
To make this possible it is necessary to copy the astah* jar interface jar file astah-community.jar (or e.g. astah-pro.jar if you use astah* professional) from the astah* installation folder into the Java CLASSPATH. This can be done in the same way as for the jdom.jar file. The easiest way is to copy it into the folder where the codegen.jar is located. See Installation in the manual (PDF) for the different options. Since version 6.7 also additional jar files are required. For latest information check the Astah* Howto Page on the Sinelabore web site.
Example: You have copied the astah* jar file into the bin folder of sinelabore: Let’s assume you have two folders. A bin and a prj folder. In the bin folder all the jar files were located. The model file is located in the prj folder. To call the codegen from the prj folder use the following command line:
user$ ls ../../bin/
JDOM\ LICENSE.txt POI\ LICENSE.txt astah-community.jar codegen.jar jdom.jar
log4j-over-slf4j-1.6.6.jar logback-loader-1.0.9b.jar slf4j-api-1.6.6.jar
user$ java -cp "path to codegen bin folder" codegen.Main -t "oven_pkg:machine_class:oven" -l cx -p ASTAH -o oven oven.astaOption b: add the astah jar files to the class path#
This method was used in the example model for astah SysML which can be found in the examples folder. Check out the Makefile how this is done. The class path on your system might be different. On Windows make sure to use the right separator characters in the class path.
JAVA=java
JFLAGS= -cp="../../bin/":"/Applications/astah sysml/astah sysml.app/Contents/Java/" -Djava.awt.headless=true codegen.Main
...
$(JAVA) $(JFLAGS) -l cx -v -p ASTAH -o oven -t "final:oven:machine" oven_model.asmlIn case you see a Java exception like shown below the astah* jar files were not found. Carefully check your class path or the path provided in -Djava.ext.dirs=
Exception in thread "main" java.lang.NoClassDefFoundError: com/change_vision/jude/api/inf/model/INamedElement at codegen ...
A rather complex state chart designed in astah*. Most of the supported elements are used in this diagram.
Attaching action and include comments#
Remember that you can specify code (so called header code) that is simply copied to the beginning of the state machine implementation file. This mechanism allows to add own include files to the generated code. Also you can specify action code that is called every time the state machine gets executed. Put the action- and header code in a Note and place it in the class diagram / block definition diagram where it must be linked to the class / block you want to generate code from. The text must start with either action: or header: as shown in the figure below. In Adapting the Generated Code you can find more information about the used syntax.

Place header and action code in a comment linked to the correct class for astah. When using asta SysML use blocks instead of classes.
Specify the Path to a State Diagram#
With the command line flag ’-t’ you have to specify the path to the state diagram in your model file where you want to generate code from. Start from the root node and then just go down the tree until you reach the state diagram of your choice. The root node itself must be left out as it is the name of the model. Separate each level with a colon. The figure below gives an example.

To generate code from testcase set the path to “-t peters_package:testcase_class:testcase”
States#
Astah* allows you to define entry, do and exit activities as well as inner transitions for a state. Unfortunately only one line of text is possible per action. If you want to specify multiple action lines (i.e. code lines) link a comment to a state and provide the code there. An example is shown in the figure above for state S3. Take care of the required keywords to begin an action in a linked comment. The keywords are onEntry: or onExit and action:.
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. To add a region in astah* right click to a state and select Add region from the context menu. See the figure below for an example.

Example diagram with regions
Transitions#
Transitions can be drawn from and to states on all levels in the diagram. Click on a transition to specify its details like the triggering event the guard and the action. Transitions can’t cross region borders.

Definition of an event, guard an action for a transition between states
Alternatively it is possible to attach a comment to a transition and specify the event[guard]/action in the comment. Usually you don’t require to do this (e.g. #rxbuf[i]==0x0d). This type of definition makes most sense together with conditional triggers.

Definition of an event using a comment attached to a transition
History State#
Use the Shallow History symbol and put it in a state to make it a history state.
Deep History#
Use the Deep History icon and put it in a state to make it a deep history state. This has the same effect as adding a Shallow History marker to all composite child states.
Supported / Unsupported#
The code generator supports a subset of the design elements provided by astah*. 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
- Regions
The unsupported elements are:
- Constraints
- Multiple state machines 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 Visual Studio#
All files for this example are available on GitHub and as a zip archive: microwave_visual_studio_c.zip
A Microwave Oven#
In this section we create the model of a simple microwave oven using a state machine diagram. A microwave oven was chosen because it is self-explanatory and not too complex. To keep the example focused, hardware interaction routines are excluded.

Simplified microwave oven. With a wheel, cooking time can be adjusted between 0 and 60 seconds. Power can be set to high (II) or low (I).
The oven controller should support:
- Cooking time can be adjusted between 0 s and 60 s.
- Cooking starts if cooking time is larger than zero 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 adjusted to zero.
- Cooking time and power can be changed at any time.
Drawing the State Machine Diagram#
To fulfill the requirements, a hierarchical state machine was designed. Events that should be handled from every state are modeled once on the outer (parent) state.
This example includes several UML state machine elements and is sufficient to generate production-ready code. The design shown is one possible solution; other valid designs are possible.

Complete state machine design of the microwave oven. All necessary actions, guards, entry and exit code are included.
Creating Code#
It is assumed Java and Visual Studio are installed on your PC.
To verify Java installation:
C:\>java -version
java version "13.0.2" 2020-01-14
Java(TM) SE Runtime Environment (build 13.0.2+8)
Java HotSpot(TM) 64-Bit Server VM (build 13.0.2+8, mixed mode, sharing)Then switch to the example folder:
cd "your path\sinelaboreRT4.3\examples\microwave_visual_studio_c"Run the generation batch file and verify paths in the script for your environment. Example output:
Verbose mode on
Command line parsing success!
Parsing successfully finished.
Checking nodes ...
Checking for unique node names
Merging actions TestMicrowave and TestLamp
Merging actions bdc3d21b_a5c3_4ec5_b4b1_e9fd41b68d98 and TestSomethingElse
Verbose mode on
Command line parsing success!
Open project: oven_model.asta
Starting robustness tests of state machine ...
State names: ..............
Machine hierarchy: ........
Machine height = 2
Transitions: ..............
Default states: ...........
Final states: .............
Choices: ..................
No. of children in composites: ...
Connectivity of states: ...
M1054: State Error has no outgoing transitions which indicates a dead end -> check your design.
Can't find the License.txt file or invalid file. Codegen is running in demo mode
Expected license file location: /C:/sinelaboreRT4.3/bin/License.txt
Running in demo mode!Double click the .sln file to load the project in Visual Studio (C++ console project).
Generated files from UML state machine and activity diagram are stored in generated.
A timer/microwave hardware simulation is provided in helpers.
Compile the solution in Visual Studio. There should be no compile errors.

Visual Studio Console project. The batch file is used to create code from the UML models.
The oven executable should now build successfully.
Start the program and press + several times to increment the cooking timer, then press c to close the door. Observe behavior and test additional events.
It is recommended to reopen your UML editor, modify the state diagram, regenerate code, and inspect generated code to explore available options.

Executing the built oven simulation.
Some additional notes:
- The code generator can also generate code from activity diagrams. This example includes a basic activity diagram for self-test.