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 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.
Prerequisites
This code generation method does not apply to the Legacy StateMachine code generation templates developed prior to Enterprise Architect Release 11.0.
Access
Drag an 'Executable StateMachine Artifact' from the 'Artifacts' page of the Diagram Toolbox, onto your diagram. The 'Artifacts' page of the Diagram Toolbox can be accessed using any of the methods outlined in this table.
Ribbon |
Design > Diagram > Toolbox > Artifacts |
Keyboard Shortcuts |
> Artifacts |
Other |
You can display or hide the Diagram Toolbox by clicking on the or 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 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 | StateMachine' context menu option. Give the StateMachine diagram an appropriate name. |
|
4 |
Create the StateMachine model to reflect the appropriate transitions between States. |
State Machines |
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 '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 StateMachine 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 as you drag the Class element from the Project Browser.) |
Drop from Project Browser |
4 |
Click on the . 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:
|
|
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 'Simulate > Executable > StateMachines > 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:
|
|
5 |
Click on the . 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 > StateMachines > 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 Simulator Layout' 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 Project Browser, click on the Artifact element and select the 'Simulate > Executable > StateMachines > 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 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
Learn more