This five-minute tutorial gives you a brief introduction into modeling state machines with YAKINDU Statechart Tools with a simple example. The free to use, open-source toolkit YAKINDU Statechart Tools (SCT) provides an integrated modeling environment for the specification and development of reactive, event-driven systems based on the concept of state machines.
The example model is a light switch that can be flipped on and off. Operating the switch repeatedly turns the light on, off, back on again, and so forth. You can simulate this behavior in a dynamic statechart model (state machine model). Here’s how this looks:
Interactive lightswitch statechart simulation.
In the simulation screenshot above, the light switch is currently in the "on" state. To toggle the light switch, the user clicks on Operate on the right-hand side of the window.
Importing the Lightswitch Example
Now try this for yourself. We are assuming that you have already installed and started YAKINDU Statechart Tools. If you don’t, please see the Installation Guide for details.
- If the Welcome page is still open, close it.
- In the main menu, select File > New > Example.
- In the dialog, select YAKINDU SCT Examples > Next.
- The example wizard opens. If needed, follow the instructions to download the examples repository.
- On the left-hand side of the example wizard, select Basic > Lightswitch > Finish.
- The sample project named light_switch is created.
- In the project explorer on the left-hand side of the window, open the light_switch project by clicking on the small triangle left from the project name (light_switch). Then ,open the Model folder.
- The light switch statechart is defined in the file light_switch.sct. Double-click on this file to open the model.
Simulating the Lightswitch
- Right-click on light_switch.sct and select Run As > Statechart Simulation in the context menu.
- The simulation starts and shows the light switch model in its "off" state. The rectangle named Off is highlighted in red, meaning it is the active state.
- At the right-bottom side of the window, the simulation view is located. In this view, you can change variable values and raise events during a simulation run.
- Click on the small triangle left from Internal to unfold the Operate link. This link represents the Operateevent to switch the light on or off.
- Click on the Operate link. The active state changes accordingly from "off" to "on."
- Click on Operate again to switch off the light.
Generating Statechart Code
You have developed and tested your statechart model, and now you need it as a C, C++, or Java implementation? YAKINDU Statechart Tools can create it for you.
As an example, we will take the light switch statechart and generate Java source code from it. Please proceed as follows:
- In the project explorer, right-click on light_switch.sct and select New > Code Generator Model in the context menu.
- In the dialog, replace
light_switch.sgen, then click Next.
- In the next dialog, check the checkbox left from light_switch.sct, then click Finish.
- Java classes implementing the statechart model are generated. You can find the generated source code in the folders src and src-gen within the light_switch project.
That’s it. If you want to know more, take the time and work through the Comprehensive Tutorial.
If you want to try it out, download YAKINDU Statechart Tools.