Book a Demo

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

Prev Next

Dynamic Simulation with JavaScript

The Corporate, Unified and Ultimate editions of Enterprise Architect provide 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.

You can write JavaScript that uses any variables. To enable you to display the values of such variables through the user interface, two built-in objects are defined - sim and this - whose members can be displayed in the Local Variables window (also called the Locals window). Examples of the variables that can be displayed are:

  • sim.logger
  • sim.Customer.name
  • this.count
  • this.Account.amount

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 provided here. Further examples are provided in the EAExample.eap model that comes with Enterprise Architect.

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.

Configuring simulation runtime using Analyzer Script in Enterprise Architect

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.

Specifying a Guard on a Control Flow

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");

Using Solvers

Anywhere in Enterprise Architect that has JavaScript code, such as in Dynamic Simulation, you can now use a JavaScript construct called 'Solver' (the Solver Class) to integrate with external tools and have direct use of the functionality within each tool to simply and intuitively perform complex math and charting functions. The calls help you to interchange variables between the built in JavaScript engine and each environment easily. Two Math Libraries that are supported are MATLAB and Octave.

To use the Solver Class, you need to have a knowledge of the functions available in your preferred Math Library and the parameters they use, as described in the product documentation.

Being part of the JavaScript engine, these Solver Classes are also immediately accessible to Add-In writers creating model based JavaScript Add-Ins.

See the Octave Solver, MATLAB Solver and Solvers Help topics for further details.

GNU Octave Solver MATLAB Solver Solvers

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.

The JavaScript Simulation will truncate strings that exceed the defined maximum length of the Trace message.

Learn more