Code Generation - State Machines

A State Machine 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 language and execute the code, visualizing the execution via the model Simulator.

It is also possible, in Enterprise Architect, to combine the State Machines 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 State Machine)
The steering wheel and all four drive wheels of a vehicle in 4-wheel drive mode (five State Machines)
The wheels of an off-road vehicle and of a sports car (two Artifacts, instances of a combination of State Machines)
 

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

Prerequisites

Select Tools | Options | Objects and select the Port and Part type visible by default checkbox
Select Tools | Options | Source Code Engineering and, for the appropriate coding language (Java, C, C# or ANSI C++), set the Use the new Statemachine Template option to True
If working in C++, select Tools | Options | Source Code Engineering | C++ and set the C++ Version option to ANSI
 

This code generation method does not apply to the Legacy State Machine code generation templates developed prior to Enterprise Architect Release 11.

Access     Diagram | Diagram Toolbox > Artifacts

Prepare your State Machine diagram(s)

Step

Action

See also

1

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

Add New Diagrams

 

2

From the Class Elements 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 | State Machine menu option.

Give the State Machine diagram an appropriate name.

 

 

4

Create the State Machine model to reflect the appropriate transitions between States.

 

State Machines

Set up the Executable State Machine Artifact

Step

Action

See also

1

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

Add New Diagrams

 

2

From the Artifacts 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 Project Browser, drag the (first) Class element containing a State Machine diagram onto the Artifact element on the diagram.

The <element name>: Paste As dialog displays, with the Paste as field defaulted to Property.

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

 

 

 

Paste from Project Browser

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 State Machines that you want to combine and generate code for. These might be:

Repeat 'drops' of the same Class and State Machine, modeling parallel objects
Different Classes and State Machines, modeling separate interacting objects
 

 

6

Right-click on the Artifact element and select the Properties option, 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 Project Browser 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

Right-click on the Executable StateMachine Artifact element and select the Code Engineering | Executable Statemachine | Generate menu option.

The Executable StateMachine Code Generation dialog displays.

 

 

2

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

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

 

 

3

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, these might resemble the following:
 

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
 

 

4

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

The System Output window displays with an Executable State Machine 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 State Machine 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.

 

 

5

When the code generates without error, right-click on the Artifact element and select the Code Engineering | Executable Statemachine | Build menu 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 (below).

 

 

Execute/Simulate Code From Artifact

Step

Action

See also

1

Select the two menu options:

Analyzer | Simulator to display the Simulation window
Analyzer | Simulation Events to display the Simulation Events window
 

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

 

 

Simulation Windows

Simulation Events Window

2

On the diagram or Project Browser, right-click on the Artifact element and select the Code Engineering | Executable Statemachine | Run menu option.

The first State Machine 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.

http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/domain_based_models/spem_toolbox_pages.html

 

Triggers

Waiting Triggers

 

Notes

If you are making small changes to an existing State Machine model, you can combine the code generation, build and run operations by selecting the Code Engineering | Executable Statemachine | Generate, Build and Run option
You can also generate code in JavaScript and System C
 

Learn more