Skip to content

Traffic Light (Java)

This example application demonstrates how to generate Java source code from a statechart and how to integrate the generated code in a simple GUI application.

The example shows the following features:

  • Statechart with history states, composite and orthogonal states
  • Java code generator model with timer service
  • Application that wires input and output events with a simple GUI

The statechart

The State Machine

In summary, the traffic light can be in one of two different modes, a normal mode and an interrupt mode which is enabled by the police.interrupt event. The interrupt mode turns the yellow lights into a blinking mode. In an orthogonal region a counter is decremented for visual feedback on the duration of the traffic light's red and green phases. The duration of the corresponding traffic light phases is specified by the variables redPeriod, greenPeriod and yellowPeriod.

The Application

The traffic light example is based on Java and contains a small GUI that demonstrate the state machine behaviour in /model/TrafficLightCtrl.sct. This example combines generated Java code with manual Java implementations (the GUI). To generate the state machine code, right-click on /model/TrafficLightCtrl.sgen and select 'Generate Code Artifacts'. To start the application run src/traffic/light/TrafficlightDemo.java. For this, just right-click on the project and select 'Run As > Java Application'.

The GUI

For interaction with the GUI, the traffic light controller raises outgoing events whenever a state is entered. The application code in TrafficlightDemo.java implements a corresponding listener to get notified whenever a state changes in order to paint the graphics accordingly.

Wiring of out-events in application code

The listener interface is not generated out of the box. It needs to be enabled in the generator model. The corresponding property is InterfaceObserverSupport under GeneralFeatures.

The generator model

Download examples

Drag to install

Drag to your running itemis CREATE (min. version 2.8.0) workspace.

Back to overview