Executable State Machine Simulation and Code Generation with Enterprise Architect

Webinar August 2017
In this Webinar, you will learn how to:
  • Model and design a State Machine representing an Audio Player
  • Simulate the Player's functionality using an Executable State Machine.
  • Generate code and implement a web-based prototype of the Audio Player.

How can you generate and implement code from an Executable State Machine to build a web based prototype?

Enterprise Architect allows you to model, build and simulate an Executable State Machine. We'll use an Audio Player example to show how you can model system behavior and generate code from an Executable State Machine.

Not only does the simulation help us to better understand system behavior, but it can also produce JavaScript code that allows us to develop a convenient web application for a virtual Audio Player.

Scott Hebbard
Scott Hebbard

Questions from the Audience

Click on a question to see the answer.

General

What Version of Enterprise Architect was used during this webinar?

Enterprise Architect 13.5, Build 1351 was used throughout the webinar.

How can I learn more about Model Simulation?

The Enterprise Architect User Guide provides a comprehensive overview of model Simulation. For more information, please see the Help topic Model Simulation.

What should be the minimum Enterprise Architect software version to execute state machine simulation?

Code Generation for Executable State Machines is supported in the Ultimate and Systems Engineering editions of Enterprise Architect. For reference, see our Compare Editions page.

In addition to a recording of the webinar for later viewing, will you be able to share the model being demonstrated for hands-on use?

Yes. We have used the EAExample model for your convenience. A copy of the EAExample model is shipped with every copy of Enterprise Architect.

Where is the Enterprise Architect Example Model located?

The file is EAExample.eap, which is typically found in the installation directory. On Windows, a typical file path would be:

C:\Program Files (x86)\Sparx Systems\EA

Where is the CD player Example found in the Project Browser of the EA Example Model?

The path of the CD Player Model in the Project Browser of the EAExample model is:
Example Model.Model Simulation.Executable State Machine.Example: CD Player.CD Player Simulation.

Which version of Enterprise Architect has this function to generate JavaScript Code?

The code generation capabilities are available in Enterprise Architect 13 and above. Enterprise Architect has been capable of simulating a State Machine for some time.

Are you going to send an email to get access to the recorded webinar and download the sample code?

Yes. We email all registered attendees to inform them about the publication of the webinar. The back catalog of all previous webinars is available via our Webinar Library.

So, I have the current systems engineering edition and am looking at the Sparx Systems\EA directory...which subdirectory is the model in?

The file is EAExample.eap, which is typically found in the installation Directory:
C:\Program Files (x86)\Sparx Systems\EA
The path in the Project Browser is:
Example Model.Model Simulation.Executable State Machine.Example: CD Player.CD Player Simulation.

Simulation and Code Generation

I generated JavaScript code, but what other alternatives are available?

Five languages are currently supported. These languages include C, C++, C#, Java and JavaScript.

Do you have more examples of Executable State Machines?

Yes. A Regular Expression and CD example is available from the Help topic Example: Simulation in HTML with JavaScript.

Could you have shown a similar simulation but by automatically generating the code in another language like ASP.net?

ASP.net is not currently supported for automatic code generation from an Executable State Machine. However, other languages including C, C++, C#, Java and JavaScript are supported.

Does Enterprise Architect support C# for State Machine Simulation Code Generation?

Yes.

Are the code generation templates for this simulation code editable?

Yes. There are two ways:

  1. You can modify the templates so all further code generation will use the customized template. Use the ribbon Code | Configure | Code Template Editor. In the Language field choose one of: STM_C, STM_C#_Structured, STM_C++_Structured, STM_JAVA_Structured, STM_JavaScript
  2. You can modify the generated code directly. When you want to simulate with the modified code, do NOT 'Generate', which will overwrite the customized code. Just 'Compile' (not needed for JavaScript) and 'Run'.

Can an event be issued from one class to another class? For example a gas peddle simulation sending events to an engine simulation?

Yes. You can broadcast an event to all instances or send an event to a specific instance.
Refer to the example in the Help topic Example: Deferred Event Pattern.

In order to support generating code for multiple languages with the same model, we introduced two macros:
%SEND_EVENT(Event Name In Quotes, CONTEXT_REF(instance name))%
%BROADCAST_EVENT(Event Name In Quotes)%

Example usage:
%SEND_EVENT("NEW_REQUEST", CONTEXT_REF(server))%
%BROADCAST_EVENT("NEW_REQUEST")%

I understand that UML state machine allows for operations with parameters. How do you model that?

You can define operations with parameters in the Class (owning the StateMachine), then call the operations in the State's behavior or transition's effect by specifying arguments.

For example, MyClass defines an operation int AddTwoNumbers(int MyParam) and three attributes: int nOne; int nTwo; int nThree
Now in an InitState's entry behavior, we specify the initial code as:
nOne = 5;
nTwo = 6;

In MyState's entry behavior, we specify the initial code as:
nThree = AddTwoNumbers(nOne, nTwo);

When this state is active, the runtime value of nThree will be 11.

Other

Will the simulation work with the CD Player as a SysML Block instead of a class?

A Block is of type Class, so yes, if you set up the Block with the necessary Attributes and Operations it will use these during code generation.

Can it interface to external devices such as PLCs?

The Simulation generates code based on the StateMachine model. That model can have triggers. These triggers can be set to be from external devices like PLCs, but you do need to alter the code generated to set your interface to them.

See the section Leveraging existing code, in the Help topic Code Generation for Executable StateMachines.

It seems that the C, C++ and other compiled languages would require compilation. Right? So it would require some setup to connect Enterprise Architect to the compiler and runtime?

Yes. This is explained in the Help topic Code Generation for Executable StateMachines.

You need to ensure that you have your code generation configured to interface with your IDE and you need to change the Element.Language from Javascript to C++ in the <<Executable StateMachine>> Artifact.

Also for C++, note that statements use a different notation:

Javascript: this.currentTrack=0;
C++: this->currentTrack=0;

So you do need to edit these statements in the Operations and Transitions.

We are considering providing a new feature to enhance this changeover.

If I changed code generated by Enterprise Architect outside of Enterprise Architect to add detailed functions, could I import that code to Enterprise Architect so it wouldn't be overwritten on Enterprise Architect enhancements after the import?

Enterprise Architect supports forward engineering of behavioral models. It does not support reverse engineering them for obvious reasons.
If you do want to modify the code, then it is best to do it in the model and then generate this out.

Can sequence diagrams be generated from simulation trace output?

Not directly as part of the Simulation process.
However, Sequence diagrams can be generated from the code using the Enterprise Architects Visual Execution Analyzer. See the Help topic Recording.

How to avoid that the next version of the generated code does overwrite the detailed implementation code that we wrote?

If you are updating the generated code outside of the model then, as there is no reverse engineering of behavioral model code, this will not be updated in the model. So, if you are modifying code, it is best you either do this in the model or assume the modeling is complete and do this separately to the modeling.

How does Enterprise Architect support statistical analysis to validate models?

Sparx Systems BPSim MDG supports statistical analysis of simulations. As StateMachines are more about timing, logic and debugging these statistics are not recorded for an Executable State Machine simulation.

Can we simulate and produce base code for more than one language?

Yes.

Please send info on the coupling with OpenModelica?

Enterprise Architect supports interfacing with OpenModelica for SysML Parametric simulation. It does not provide OpenModelica support for Executable StateMachines.

Can I add activity diagram simulation to state transition?

For a standard Behavioral model simulation in Enterprise Architect, you can call an Activity diagram from a State in a StateMachine diagram, as part of the simulation. For the simulation via Executable StateMachines – this only supports code generation of StateMachines, not other Behavioral models such as Activities.

Enterprise Architect's Behavioral model code generation does support generating Activity, Sequence and StateMachines to code. It does not support simulation.

What is the idea of the Executable StateMachine Artifact and what is its purpose for the code generation? Can we customize the template for generating code?

The Executable StateMachine Artifact may appear redundant when only executing a single instance of a Class containing a StateMachine. The purpose of the Artifact is to ensure you can simulate multiple instances of your Class/StateMachine by creating multiple Artifacts to cover them.

For examples of using multiple instances see the Help topic Example Executable StateMachine.

Where can we get a copy of the model you are using?

In the EAExample.eap repository supplied with Enterprise Architect.

See EAExample.eap file in the directory ...\Program Files (x86)\Sparx Systems\EA.

See the package Example Model.Model Simulation.Executable State Machine.Example: CD Player.

Is it possible to define automated test cases for the Executable State Machine in order to verify it?

Automated Test Cases can be generated in Enterprise Architect from Use Case Scenarios. If, in your modeling, you have defined a set of Use Case Scenarios prior to and covering the creation of your StateMachines, then these Test Cases can be generated from the Scenarios for use with the StateMachines.

If we generate the code, is it possible to synchronize code changes back to the model?

Enterprise Architect supports forward engineering of behavioral models. It does not support reverse engineering of them for obvious reasons.
If you do want to modify the code, then it is best to do it in the model and generate this out.
However, you can import the code, but it is not recommended.

Is it also possible to simulate models with multiple instances within Enterprise Architect?

Yes using multiple Artifacts representing the same StateMachine. For details, refer to the Help topic Executable StateMachine Artifact.

How can an external function be called from inside the state machine or: How to integrate the state machine into other software?

After generating the code you can modify it using Enterprise Architect's code editor, and add new functions or call external functions as you would modify any code in an IDE. You can also modify the code in any IDE or editor that supports your target language.

Is it possible to perform simulation on composite diagrams?

Yes.

Which version of Enterprise Architect supports Simulation of a State Machine?

Code Generation for Executable State Machines is supported in the Ultimate and Systems Engineering editions of Enterprise Architect. For a detailed comparison of Enterprise Architect editions, see our Compare Editions page.

Does code generation from an Executable State Machine require a special kind of License?

Code Generation for Executable State Machines is supported in the Ultimate and Systems Engineering editions of Enterprise Architect. . For a detailed comparison of Enterprise Architect editions, see our Compare Editions page.

Is it possible to export the state machine model to a JSON formatted file?

Enterprise Architect supports exporting the model to XMI format, but not JSON format.

Can the generated C code be easily or quickly customized to programmers' needs? (Example: add a specific function header to every function or change the indentation)

Yes.

Is the simulation still an interpretation of the model or an execution of the generated code?

It is the execution of the generated code.