Prev | Next |
Dynamic Simulation with JavaScript
The Corporate and suite editions of Enterprise Architect have the capability of using JavaScript to evaluate guards, effects and other aspects of behavior within the Simulation context. This provides for a fully automated, intelligent execution of your State or Activity model, with fine control over breakpoints, execution speed and simulation variables.
JavaScript can be written that uses any variables. To enable you to display the variable values to the user interface, two objects are defined that have their members shown in the Local Variables window. These are sim and this; for example:
- sim.logger
- sim.Customer.name
- this.count
- this.Account.amount
All these variables will be shown in the Locals window.
The recommended convention is to add any global or control variables not declared in the owning Class to the sim object. In contrast it would be normal to add attributes of the owning classifier to the this object.
Some examples of where and how you can set Simulation behavior using JavaScript are shown here. See the EAExample.eap model that comes with Enterprise Architect for further examples. Also see the Learning Center for further information on setting up and working with JavaScript in Simulations.
Using JavaScript
Setting |
Action |
See also |
---|---|---|
Analyzer Script Input |
If you enter JavaScript code into the Execution Analyzer window 'Input' field, this code will be injected into the Simulation and executed before the Simulation starts. This is useful for establishing COM variables, global counters, functions and other initialization.
|
|
Transition and Control Flow Guards |
This is the workhorse of the Simulation functionality. As Enterprise Architect evaluates possible paths forward at each node in a Simulation, it tests the Guards on outgoing transitions and control flows and will only move forward if there is a single true path to follow - otherwise the Simulation is considered 'blocked' and manual intervention is required. You must use the '==' operator to test for equality.
|
|
Element Behavior |
Entry and Exit behavior can be defined for States. Such code will execute at the appropriate time and is useful for updating Simulation variables and making other assignments.
You can also simulate the behavior of Classes via their Object Instances, and Activities in your model. |
Call Behaviors |
Using COM |
One very important feature of the implementation of JavaScript in Enterprise Architect's simulator is that it supports the creation of COM objects. This provides the ability to connect the running Simulation with almost any other local or remote process and either influence the Simulation based on external data, or potentially change data or behavior in the external world based on the current Simulation state (for example, update a mechanical model or software simulation external to Enterprise Architect). The syntax for creating COM objects is shown here: this.name="Odd Even"; var logger = new COMObject("MySim.Logger"); logger.Show(); logger.Log("Simulation started"); |
|
Signalled Actions |
It is possible to raise a signalled event (trigger) directly using JavaScript. The BroadcastSignal() command is used to raise a named trigger that could influence the current state of a simulation. For example, this fragment raises the signal (trigger) named "CancelPressed". BroadcastSignal("CancelPressed"); Note that a trigger named CancelPressed must exist within the current simulation environment and must uniquely have that name. You can also call the signal using its GUID. For example: BroadcastSignal("{996EAF52-6843-41f7-8966-BCAA0ABEC41F}"); |
|
IS_IN() |
The IS_IN(state) operator returns True if the current simulation has an active state in any thread matching the passed in name. For example, to conditionally control execution it is possible to write code such as this: if (IS_IN("WaitingForInput")) BroadcastSignal("CancelPressed") Note that the name must be unique within all contexts. |
|
Trace() |
The Trace(statement) method allows you to print out trace statements at any arbitrary point in your simulation. This is an excellent means of supplementing the Simulation log with additional output information during execution. |
Learn more