Graphical State Machines for Java Development With YAKINDU Statechart Tools
Tags: java, opensource, modeling, statemachine
Published: 2019-11-27 13:18:44

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](https://www.itemis.com/en/yakindu/state-machine/) 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.
{% youtube iSG2s_eUZUs %}

# 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](https://thepracticaldev.s3.amazonaws.com/i/sq5f5l5nfkxdvhsbeu49.png)

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](https://thepracticaldev.s3.amazonaws.com/i/ragxjp68163c03h7ac02.png)

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](https://thepracticaldev.s3.amazonaws.com/i/we8p9fjjblyou8uliuf7.png)

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](https://thepracticaldev.s3.amazonaws.com/i/5idfxq52nly6kku6bzp7.png)

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](https://thepracticaldev.s3.amazonaws.com/i/x9ab3dbdq90taecnxwh0.png)

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](https://thepracticaldev.s3.amazonaws.com/i/c40fcub7lywsm1crosnv.png)

# Calling Java APIs During Simulation

YAKINDU Statechart Tools offers a [built-in simulation](https://www.itemis.com/en/yakindu/state-machine/documentation/user-guide/simu_simulating_statecharts) 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](https://thepracticaldev.s3.amazonaws.com/i/5xdsbszl3rgdk9uzbmjw.png)

# 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:

{% gist https://gist.github.com/tkutz/96958acf3bf392c5d21e00915b4187c5.js %}

# 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](https://www.itemis.com/en/yakindu/state-machine/) 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](https://www.itemis.com/en/yakindu/state-machine/licenses) free of charge, in case you don't use it for commercial purposes.

Enjoy and happy statecharting!