This section guides you through the whole development process from designing a state machine to integrating it into an application. This will help you to get familiar with the code generator and its features.
All files for this tutorial are available in this folder: examples/microwave_visual_studio_c
and available here as zip file.
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 model. To keep this example as simple and clear as possible the hardware interaction routines are excluded. The following figure 1 shows the hardware of our fictitious microwave oven. For this example we are using Altova UModel. In the handbook you will find hints on how to draw state diagrams for all the supported tools. It is recommended to have a quick look into that section.
Figure 1: Simplified microwave oven. With a wheel the cooking time can be adjusted between 0 – 60 seconds. The power can be set to high (II) or low (I).
The oven controller should be able do the following things:
To fulfil the requirements a hierarchical state machine was designed. Therefore events that should be handled from every state must only be modelled once on the outer (parent) state.
This example shows several UML state machine elements that make up a state machine. It is enough to generate production ready code out of it. Please note that the design presented here is not the only possible solution for the given requirements. Also some functions of a real microwave oven – e.g. control of a lamp in the oven – are still missing.
Figure 2: Complete state machine design of the microwave oven. All necessary actions, guards, entry and exit code were added. The model was created with Astah. It is running on Windows and MacOS. One of the nice new features is a free plugin that shows you a state transition table of the state machine model. It gives you a quick overview of where you have modelled state transitions from and to. And where transitions might be missing.
It is assumed you have Java and the Visual Studio installed on your PC. For a check whether everything is installed correctly open a command window and change the working directory to this folder. Then type in the following commands.
C:\>java −version 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)
Your output should look similar. Details might differ depending on your installed java versions. Now change to the ready made example in the examples folder. Depending on your installation change to the following examples folder:
cd "your path\sinelaboreRT4.3\examples\microwave_visual_studio_c"
To recreate the code open a command window and start the generation batch file. Check whether the paths inside the script are ok for your installation. Most probably you have to adjust one or the other path. Your output should be similar:
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 on the sln
file to load the project with Visual Studio. It is a C++ console project.
The files generated from the UML state machine and the activity diagram are all stored in the folder called generated
. A simulation of a timer and the microwave hardware is provided in helpers
folder.
Next compile the solution in Visual Studio. There should be no compile errors.
Figure 3: Visual Studio Console Project. The batch file is used to create the code from the UML models.
The oven
exe should be built successfully now.
Start the program and type several times +
to increment the cooking timer.
Then press c
to close the door. Watch what happens. Test the other events you can fire.
It is now recommended to start your UML editor again. And play with the state diagram. Look into the generated code to get familiar with the many possibilities the code generator offers.
Figure 4: Executing the built oven simulation.
Some additional notes: