One Class Executable State Machine

The One Class Executable State Machine pattern creates elements and diagrams that model the important states in the lifetime of a Class allowing code to be automatically generated that can be included in the implementation of a system in a range of programming languages. 

Figure 1. Shows a State Machine diagram during an executable simulation. An Event has just been fired and the trigger resulted in a transition into the highlighted State

Figure 2. Shows the Internal Source Code Editor and a fragment of one of the files that was automatically generated when the Generate, Build and Run option was selected.
Discussion
The purpose of the pattern is to model the important States in a class and to generate programming code in a variety of languages that can be incorporated into a system.  The State Machine can be simulated allowing a variety of stakeholders to visualize how, why and when the machine transitions from one state to another.

The pattern is typically created during the analysis or implementation phase of an initiative to model the states in the lifetime of one or more important Classes in the system. It may however also be used during the support phase to try and understand a complex problem that exists in a running system in test or production.
The ability to visualize the transitions in an executable simulation allows a modeler or designer to reason about complex and often intractable problems.

The following is a list of some things you may want to do when working with this pattern.
·	Change the names of the Class and the Instance of the Class (Property) to suit the initiative.
Change the name of the States and Events and re-direct the transitions as required to suit the initiative.

Running the Simulation
1.	Using the Project Browser find and open the State Machine Diagram located under ClassA.
From the Simulate Ribbon | Dynamic Simulation Panel Open the following windows:  Simulator Window, Trigger Window.
Select the Configuration Artifact in the Project Browser. 
From the Simulate Ribbon |Compiled Simulation | Build and Run Select Generate, Build and Run. Wait for the code to be generated and the simulation to be started.
Double Click on Event_A in the Waiting Events in the right Panel of the Simulation Events Window to trigger an event the State Machine.
Double Click on other events to continue the simulation.

Figure 3. Shows the Events listed in the Waiting Triggers section in the panel on the right. The left panel list the Events that have already been fired including details such as time and parameters.

The following is a list of some of the next steps available when applying the pattern.
·	Automatically generate one or more Timing Diagrams that describe the timing and sequence the triggers are fired in. (Use the Generate Timing Diagram option from the Tools menu on the Simulation window.)

Figure 4. Shows a timing diagram that was automatically generated from the simulation.
·	Use the Execution Analyzer and the built in Debuggers to verify the code is executing correctly and modify the State Machine if necessary and regenerate the code as required.
Add additional Instances or entirely new Classes

Useful Workspace Layouts Core | Core Modeling
References
The following help topics will assist you learn about how to work with this pattern.
Executable State Machines
Example Simulation Commands
Modeling Executable State Machines
Executable State Machine Artifact
Example Executable State Machine
Using the Compiled Simulation Panel

The following are some of the tools that will be helpful when working with this pattern.
Compiled Simulator
The Compiled Simulator provides a number of facilities for working with Executable State Machines. This includes generating the state models to a variety of programming language code, building and running a simulation. Analyzer Scripts can be configured that determine how code is generated, compiled and executed for the Executable StateMachine. The generated code can be viewed and edited using the built-in code editor and the execution analyzer can be used to debug running code.
Compiled Simulator
The Compiled Simulator provides a number of facilities for working with Executable State Machines. This includes generating the state models to a variety of programming language code, building and running a simulation. Analyzer Scripts can be configured that determine how code is generated, compiled and executed for the Executable State Machine. The generated code can be viewed and edited using the built-in code editor and the execution analyzer can be used to debug running code. For more details see the Using the Compiled Simulation Panel help topic.
Source Code Editor
The Source Code Editor is a fully featured programming source code editor. It has a structure tree for easy navigation of attributes, properties and methods. Line numbers can be displayed and syntax highlight options can be configured. Many of the features that software engineers are familiar with in their favorite IDE, such as Intelli-sense and code completion are included in the editor. Viewing the source code juxtaposed with the Models from which it is generated brings a great clarity to the design effort and its implementation. For more details see the Editing Source Code help topic.

© 2000 - 2018  Sparx Systems Pty Ltd.  All rights Reserved.