Please note : This help page is not for the latest version of Enterprise Architect. The latest help can be found here.

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.


  • Select 'Tools | Options | Objects' and select the 'Port and Part type visible by default' checkbox
  • Select 'Project | Settings | Project 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 'Project | Settings | Project Options | Source Code Engineering' 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.0.

Access Diagram | Toolbox > Artifacts

Prepare your State Machine diagram(s)



See also


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

Add New Diagrams


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


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

Give the State Machine diagram an appropriate name.


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

State Machines

Set up the Executable Statemachine Artifact



See also


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

Add New Diagrams


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.



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.)

Drop from Project Browser


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



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


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



See also


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.


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.


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


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.


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

Macro Name



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

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


Broadcst an event to all receivers. For example:


Execute/Simulate Code From Artifact



See also


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 Events Window Simulation Windows


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


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


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.

Waiting Triggers Triggers


  • 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

Learn more