Exercise 6 - Code generation (Java)

In this exercise we will create a generator model and generate Java code from our example model. The generated Java code is then used in a simple UI application that visualises the light bulb.

The light

Creating a new code generator model

  1. Create a new code generator model via New → File → Code Generator Model.
  2. Select the statechart you want to generate code for (Light.sct).
  3. Select the YAKINDU Java Code Generator from the drop-down list at the top.
  4. Check which features are available to tailor the code generation process (try [Ctrl]+[Space]).
  5. Create a RuntimeService and a TimerService.

Using the generated code

Open the LightControllerDemo.java class and make yourself familiar with the code. To use the generated Java code, implement the setupStatemachine() method.

  1. Create a new instance of the LightStatemachine.
  2. Add a TimerService and add the state machine to the RuntimeService.
  3. Connect the lightSwitch button to the state machine (ActionListener). When the light switch is clicked, the down event should be raised.
  4. Connect the setBrightness callback operation to the light object.