Learn more about graphical state machines for Java development.

Have you ever looked at your code and realized that it is actually a state machine that you are trying to implement? Usually, you would start using switch-case statements or go for the state pattern approach. For larger state machines, this approach quickly gets hard to read and maintain.

YAKINDU Statechart Tools comes with a graphical editor to model your statechart and generates the corresponding Java code automatically for you. Even better, you can use Java code directly in the graphical model. In this article, I will demonstrate what this looks like and what is possible.

You may also like: How to Implement State Machines in 5 Minutes

The deep Java integration in YAKINDU Statechart Tools allows you to directly access Java APIs in your statechart, and it hence facilitates incorporation of state machine modeling in your Java development process! The deep Java integration contains the following features:

You can import Java classes, interfaces, and enums in your statechart and use them for your statechart variables.

You can access all public and static members , like variables and methods directly from the statechart. This avoids lots of boilerplate code.

Java methods that are used in your statechart will be called during simulation. This avoids the cumbersome mocking of return values in the simulation process.

As a video shows more than a thousand words, we recorded a short screencast to give you an impression of the features described in this article. Enjoy watching, and keep on reading below if we made you curious.





Use Java Types for Statechart Variables

In YAKINDU, statecharts have a domain. In order to use the features mentioned in this article, you have to use the Java domain. You can select the Java domain in the statechart’s properties, or when you create a new statechart.

A Java domain statechart uses a Java oriented typesystem. You can use native Java types (like int) or their wrapper types (like java.lang.Integer) in variable declarations.

In addition, you can use the import statement to import Java classes, interfaces, or enums. The content assist [Ctrl]+[Space] lists all classes that are on your project’s classpath. You can import classes from your JRE as well as your own ones.

Using Java types in your statechart also means that you can use generics, just as you would use them in your Java code.

The Java-oriented typesystem is able to infer generic type information and gives errors in case types do not match. In that way, you can already avoid compilation errors before even generating any code.

Instantiation works slightly different than in Java. Instead of introducing the keyword new to our statechart language, constructors are transformed into static factory methods with the name new() .

Accessing Public Members

Once you have a variable of a Java type, you can access all its public members like methods or variables. Here again, the content assist gives you an overview of all accessible members, which contains all public instance and static class members.

Calling Java APIs During Simulation

YAKINDU Statechart Tools offers a built-in simulation for validating the behavior of your system already before generating any code. You can start the simulation in the context menu via Run -> Run As -> Statechart Simulation.

For Java domain statecharts, the simulation directly invokes the referenced Java methods. You can see the effects in the simulation view where you can also manually change values of accessible variables or raise events to see how your system reacts. You can also set breakpoints on states and transitions, just as if you were debugging your Java code.

Code Generation and Integration With Client Code

We have adapted the Java code generator that comes with the standard version of YAKINDU Statechart Tools to also generate proper Java code for Java domain statecharts. To use the generated state machine code in your Java application, you basically need to implement the following steps:

private void setup() { // 1. Initialize the state machine IPlayerStatemachine statemachine = new PlayerStatemachine(); statemachine.init(); // 2. Raise in-events on button clicks playBtn.setOnAction((e) -> { statemachine.getSCInterface().raisePlayPressed(); }); stopBtn.setOnAction((e) -> { statemachine.getSCInterface().raiseStopPressed(); }); // 3. React on out-events statemachine.getSCInterface().getListeners().add(new SCInterfaceListener() { @Override public void onPlayBtnEnabledRaised(boolean value) { playBtn.setDisable(!value); } @Override public void onStopBtnEnabledRaised(boolean value) { stopBtn.setDisable(!value); } }); // 4. Run the state machine statemachine.enter(); }





Get Your Hands-On

Are you ready to use state machines in your Java project? Here is an instruction on how to get started:

Download YAKINDU Statechart Tools Professional Edition. Unzip the downloaded archive and open the application. The first thing you will see is the welcome page. Click on Examples and select Music Player (Java). If the button Install Dependencies.. appears in the top right corner, click on it and follow the instructions. You may need to restart the application once. Click on Finish at voilá, the Music Player example is in your workspace and you can start playing around with it.

One last note about licensing. After downloading YAKINDU Statechart Tools, you can use the application for one month without any functional restrictions. After one month, you need to purchase a license. However, you can apply for an academic or non-commercial license if you don't use it for work.

Enjoy and happy statecharting!

Further Reading

How to Implement State Machines in 5 Minutes

Why Developers Never Use State Machines