Book a Demo
Prev Next

Code Generation - StateMachines

A StateMachine illustrates how an object (represented by a Class) can change state, each change of state being a transition initiated by a trigger arising from an event, often under conditions or constraints defined as guards. As you model how the object changes state, you can generate and build (compile) code from it in the appropriate software language and execute the code, visualizing the execution via the Model Simulator.

It is also possible, in Enterprise Architect, to combine the StateMachines of separate but related objects to see how they interact (via Broadcast Events), and to quickly create and generate code from variants of the model. For example, you might model the behavior of:

  • The rear off-side wheel of a vehicle in rear-wheel drive and front-wheel drive modes (one StateMachine)
  • The steering wheel and all four drive wheels of a vehicle in 4-wheel drive mode (five StateMachines)
  • The wheels of an off-road vehicle and of a sports car (two Artifacts, instances of a combination of StateMachines)

Of critical importance in generating and testing code for all of these options is the Executable StateMachine Artifact element. This acts as the container and code generation unit for your StateMachine models.

You do not use this method to generate code for Hardware Definition Languages, but you can also generate both HDL code and software code from StateMachines using the generic Code Generation facilities in Enterprise Architect (see the Generate Source Code procedures).

Prerequisites

  • Select 'Settings > Model > Options > Source Code Engineering' and, for the appropriate software coding language (Java, C, C# or ANSI C++), set the 'Use the new Statemachine Template' option to 'True'
  • If working in C++, select 'Settings > Model > Options > Source Code Engineering > C++' and set the 'C++ Version' option to 'ANSI'

This code generation method does not apply to the Legacy StateMachine code generation templates developed prior to Enterprise Architect Release 11.0, nor to generating Hardware Definition Language code.

Access

Drag an Executable StateMachine Artifact from the 'Simulation' page of the Diagram Toolbox, onto your diagram. The 'Simulation' page of the Diagram Toolbox can be accessed using any of the methods outlined in this table.

Ribbon

Design > Diagram > Toolbox > Simulation

Keyboard Shortcuts

Ctrl+Shift+3 > Simulation

Other

You can display or hide the Diagram Toolbox by clicking on the Chevron Right or Chevron Left icons at the left-hand end of the Caption Bar at the top of the Diagram View.

Prepare your StateMachine diagram(s)

Step

Action

See also

1

For each StateMachine you want to model, create a Class diagram.

Add New Diagrams

2

From the 'Class' page of the Diagram Toolbox, drag the 'Class' icon onto your diagram and give the element an appropriate name.

Create Elements

3

Right-click on the Class element and select the 'New Child Diagram | StateMachine' context menu option.

Give the StateMachine diagram an appropriate name.

4

Create the StateMachine model to reflect the appropriate transitions between States.

StateMachines

Set up the Executable StateMachine Artifact

Step

Action

See also

1

Create a new Class diagram to contain the modeled StateMachine(s) from which you intend to generate code.

Add New Diagrams

2

From the 'Simulation' page of the Diagram Toolbox, drag the 'Executable StateMachine' icon onto the diagram to create the Artifact element. Name the element and drag its borders out to enlarge it.

Artifact

3

From the Browser window, drag the (first) Class element containing a StateMachine diagram onto the Artifact element on the diagram.

The 'Paste <element name>' dialog displays. In the 'Drop as' field, click on the drop-down arrow and select the value 'Property'.

(If the dialog does not display, press Ctrl as you drag the Class element from the Browser window.)

Drop Elements from Browser Window

4

Click on the OK button. The Class element is pasted inside the Artifact as a Part.

Part

5

Repeat steps 3 and 4 for any other Classes with StateMachines that you want to combine and generate code for. These might be:

  • Repeat 'drops' of the same Class and StateMachine, modeling parallel objects
  • Different Classes and StateMachines, modeling separate interacting objects

6

Right-click on the Artifact element and select the 'Properties > Properties' option, expand the 'Advanced' category and, in the 'Language' field, click on the drop-down arrow and set the code language to the same language as is defined for the Class elements.

You can now drag this Executable StateMachine Artifact element from the Browser window onto the diagram any number of times, and modify the Parts to model variations of the system or process, or the same system or process with different programming languages.

Generate Code From Artifact

Step

Action

See also

1

Click on the Executable StateMachine Artifact element and select the 'Simulate > Executable States > Statemachine > Generate' ribbon option.

The 'Executable Statemachine Code Generation' dialog displays.

2

In the 'Project output directory' field, type or browse for the directory path under which to create the output files.

During code generation, all existing files in this directory are deleted.

3

Select the Target System. If you are running on Windows select the 'Local' option. If you are working on Linux choose the 'Remote' option. The choice affects the scripts generated to support the Simulation.

4

In the 'Location of <compiler> installation directory' field, type or browse for the path of the compiler installation directory, to be automatically mapped to the local path (displayed to the left of the field). For each programming language, the paths might resemble these examples:

  • Java
    JAVA_HOME  C:\Program Files (x86)\Java\jdk1.7.0_17
  • C/C++
    VC_HOME  C:\Program Files (x86)\Microsoft Visual Studio 9.0
  • C#
    CS_HOME  C:\Windows\Microsoft.NET\Framework\V3.5

5

Click on the Generate button. The code files are created appropriate to the programming language.

The System Output window displays with an 'Executable StateMachine Output' tab, showing the progress and status of the generation.

During code generation, an automatic validation function is executed to check for diagram or model errors against the UML constraints. Any errors are identified by error messages on the 'Executable StateMachine Output' tab.

Double-click on an error message to display the modeling structure in which the error occurs, and correct the mistake before re-generating the code.

6

When the code generates without error, click on the Artifact element and select the 'Simulate > Executable States > Statemachine > Build' ribbon option to compile the code.

The System Output window displays with a 'Build' tab, showing the progress and status of the compilation. Notice that the compilation includes configuration of the simulation operation.

Code Generation Macros

You can also use two macros in the code generation for StateMachines.

Macro Name

Description

SEND_EVENT

Send an event to a receiver (the Part). For example:

     %SEND_EVENT("event1", "Part1")%

BROADCAST_EVENT

Broadcast an event to all receivers. For example:

     %BROADCAST_EVENT("event2")%

Execute/Simulate Code From Artifact

Step

Action

See also

1

Select the ribbon option 'Simulate > Dynamic Simulation > Simulator > Apply Workspace' to display the Simulation window and the Simulation Events window together

Dock the two windows in a convenient area of the screen.

Simulation Windows Simulation Events Window

2

On the diagram or Browser window, click on the Artifact element and select the 'Simulate > Executable States > Statemachine > Run' ribbon option.

The first StateMachine diagram in the series displays with the simulation of the process already started. In the Simulation window, the processing steps are indicated in this format:

     [03516677]  Part1[Class1].Initial_367_TO_State4_142 Effect

     [03516683]  Part1[Class1].StateMachine_State4 ENTRY

     [03516684]  Part1[Class1].StateMachine_State4 DO

     [03518375]  Blocked

3

Click on the appropriate Simulation window toolbar buttons to step through the simulation as you prefer.

When the simulation finishes at the Exit or Terminate element, click on the Stop button in the Simulation window toolbar.

Run Model Simulation

4

Where the trace shows Blocked, the simulation has reached a point where a Trigger event has to occur before processing can continue. On the Simulation Events window, in the 'Waiting Triggers' column, double-click on the appropriate Trigger.

When the Trigger is fired, the simulation continues to the next pause point, Trigger or exit.

Triggers Waiting Triggers

Notes

  • If you are making small changes to an existing StateMachine model, you can combine the code generation, build and run operations by selecting the 'Simulate > Executable States > Statemachine > Generate, build and run' ribbon option
  • You can also generate code in JavaScript

Learn more