Skip to main content

Java Code Generation

This example demonstrates how to generate Java code from a statechart. We will use a simple light switch example and focus on the generator model.

Generator Model

In order to generate code we need to specify which code generator to use and into which folder to generate. For this, we need to create a so-called generator model. To do so,

  • Select the models folder in the Project Explorer
  • Right-click and choose New -> Code generator model
  • Follow the wizard to select the Java code generator and the state machine for which you want to generate Java code

The new generator model will look similar to this one:

/* Defines which code generator to use */
GeneratorModel for create::java {
/* Refers to the statechart model 'LightSwitch' */
statechart LightSwitch {
/* Specify the target location for the generated artifacts. */
feature Outlet {
targetProject = "itemis.create.examples.codegen.java"
targetFolder = "src-gen"
}
/* Specify package names */
feature Naming {
basePackage = "itemis.create.examples.codegen.java"
}
/* Generate a default timer service implementation */
feature GeneralFeatures {
TimerService = true
}
}
}

The generator model defines where the code should be generated into, and enables the generation of a default timer service. Besides setting up the target location and the package names, the generator model allows you to adjust several aspects of the generated code like naming, license comments or function inlining. Press [CTRL]+[SPACE] to get a list of available features. For more information, please refer to our documentation.

Content assist in generator model

Invoking Code Generation

Code generation is usually invoked each time the statechart model is saved. This behavior can be disabled by unchecking the option Project -> Build Automatically. You can always manually invoke the code generation with Generate Code Artifacts in the context menu of the generator model.

The Example Application

As an example application we will use the light switch example with brightness adjustment from the Basic Tutorial.

Light switch model

Our application is a simple interactive console with which the user can switch the light on or off. The complete application code is implemented in Main.java:

package itemis.create.examples.codegen.java;

import java.util.Scanner;

import com.yakindu.core.TimerService;
import com.yakindu.core.rx.Observer;

public class Main {

/* Observer with callback for the light.on event */
static class LightOnObserver implements Observer<Void>{
@Override
public void next(Void value) {
System.out.println("Light is on.");
}
}

/* Observer with callback for the light.off event */
static class LightOffObserver implements Observer<Void>{
@Override
public void next(Void value) {
System.out.println("Light is off.");
}
}

public static void main(String[] args) {
/* Instantiates the state machine */
LightSwitch lightSwitch = new LightSwitch();

/* Set the timer service */
lightSwitch.setTimerService(new TimerService());

/* Subscribes observers to the state machine's observables */
lightSwitch.light().getOn().subscribe(new LightOnObserver());
lightSwitch.light().getOff().subscribe(new LightOffObserver());

/* Enters the state machine; from this point on the state machine is ready to react on incoming event */
lightSwitch.enter();

userInteraction(lightSwitch);
}

private static void userInteraction(LightSwitch lightSwitch) {
System.out.println("Type 'On' or 'Off' to switch the light on or off.");
Scanner sc = new Scanner(System.in);
sc.useDelimiter("\\s");
while (!lightSwitch.isFinal()) {
String action = sc.next();
if ("On".equals(action)) {
/* Raises the On event in the state machine which causes the corresponding transition to be taken */
lightSwitch.user().raiseOn_button();
printStatus(lightSwitch);

} else if ("Off".equals(action)) {
/* Raises the Off event in the state machine */
lightSwitch.user().raiseOff_button();
printStatus(lightSwitch);
}
}
sc.close();
}

private static void printStatus(LightSwitch lightSwitch) {
/* Gets the value of the brightness variable */
long brightness = lightSwitch.light().getBrightness();
System.out.println("Brightness: " + brightness + ".");
}

}

The most important parts are commented in the Main.java file. These are the bullet points:

  • Instantiate the state machine and the observers
  • Set the timer service
  • Subscribe observers to react on outgoing events
  • Enter the state machine; from this point on the state machine is ready to react on incoming event
  • Raise the input events user.on_button and user.off_button

You can run the application with Run As -> Java Application on the Main.java file. A console should open and ask you for input like in the screenshot below.

Light switch console application

Get this example

The complete project — statechart models, sources and build files — lives in the itemis CREATE examples repository. Inside itemis CREATE for Eclipse you can import it directly with the example wizard.