Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Implement State Machines in 5 Minutes

DZone's Guide to

How to Implement State Machines in 5 Minutes

In just a few simple steps, you can model and implement state machines in just five minutes with YAKINDU Statechart Tools.

Free Resource

Today’s data climate is fast-paced and it’s not slowing down. Here’s why your current integration solution is not enough. Brought to you in partnership with Liaison Technologies.

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 light switch statechart simulation

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.

  1. If the Welcome page is still open, close it.Closing the "Welcome" page
  2. In the main menu, select File > New > Example.
  3. In the dialog, select YAKINDU SCT Examples > Next.
  4. The example wizard opens. If needed, follow the instructions to download the examples repository.
  5. On the left-hand side of the example wizard, select Basic > LightswitchFinish.
  6. The sample project named light_switch is created.
  7. 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. Expand project
  8. The light switch statechart is defined in the file light_switch.sct. Double-click on this file to open the model.

Simulating the Lightswitch

  1. Right-click on light_switch.sct and select Run As > Statechart Simulation in the context menu.
  2. 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.
    Light switch simulation in "off" state
  3. 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.
  4. 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.
  5. Click on the Operate link. The active state changes accordingly from "off" to "on."Light switch simulation in "on" state
  6. 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:

  1. In the project explorer, right-click on light_switch.sct and select New > Code Generator Model in the context menu. Creating a new code generator model
  2. In the dialog, replace default.sgen by light_switch.sgen, then click Next.
  3. In the next dialog, check the checkbox left from light_switch.sct, then click Finish.
  4. 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. Generated Java source code

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.

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and iPaaS+ could cost you down the road. Brought to you in partnership with Liaison Technologies.

Topics:
integration ,state machines ,statechart ,integrated modeling environment

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}