loading...

Graphical State Machines for Java Development With YAKINDU Statechart Tools

tkutz profile image Thomas Kutz ・4 min read

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.

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 the 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.

Alt Text

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.

Alt Text

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.

Alt Text

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

Alt Text

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.

Alt Text

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.

Alt Text

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.

Alt Text

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:

Get Your Hands-On

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

  1. Download YAKINDU Statechart Tools Professional Edition.

  2. Unzip the downloaded archive and open the application.

  3. The first thing you will see is the welcome page. Click on Examples and select Music Player (Java).

  4. 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.

  5. 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 free of charge, in case you don't use it for commercial purposes.

Enjoy and happy statecharting!

Posted on Nov 27 '19 by:

tkutz profile

Thomas Kutz

@tkutz

Interest in language engineering, smart IDEs, clever algorithms and everything that makes development faster.

Discussion

markdown guide