Topics
Prev Next

Example: Deferred Event Pattern

Enterprise Architect supports the Deferred Event Pattern.

To create a Deferred Event in a State:

  1. Create a self transition for the State.
  2. Change the 'kind' of the transition to 'internal'.
  3. Specify the Trigger to be the event you want to defer.
  4. In the 'Effect' field, type 'defer();'.

To Simulate:

  1. Select 'Simulate > Dynamic Simulation > Simulator > Open Simulation Window'. Also select 'Simulate > Dynamic Simulation > Triggers' to open the Simulation Events window.
  2. The Simulator Events window helps you to trigger events; double-click on a trigger in the 'Waiting Triggers' column.
  3. The Simulation window shows the execution in text. You can type 'dump' in the Simulator command line to show how many events are deferred in the queue; the output might resemble this:
            24850060]      Event Pool: [NEW,NEW,NEW,NEW,NEW,]

Deferred Event Example

This example shows a model using Deferred Events, and the Simulation Events window showing all available Events.

We firstly set up the contexts (the Class elements containing the StateMachines), simulate them in a simple context and raise the event from outside it; then simulate in a client-server context with the Send event mechanism.

Create Context and StateMachine

Create the server context

Create a Class diagram and:

  1. A Class element TransactionServer, to which you add a StateMachine ServerStateMachine.
  2. A Class element TestClient, to which you add a StateMachine ClientStateMachine.
  3. An Association from TestClient to TransactionServer, with the target role named server.

Modeling for ServerStateMachine

  1. Add an Initial Node Initial to the StateMachine diagram, and transition to a State idle.
  2. Transition (with event NEW_REQUEST as Trigger) to a State busy.
  3. Transition (with event QUIT as Trigger) to a Final State Final.
  4. Transition (with event AUTHORIZED as Trigger) to idle.
  5. Transition (with event NEW_REQUEST as Trigger and defer(); as effect) to busy

Modeling for ClientStateMachine

  1. Add an Initial Node Initial to the StateMachine diagram, and transition to a State State0.
  2. Transition (with event RUN_TEST as trigger) to a State State1.
  3. Transition (with effect: %SEND_EVENT("NEW_REQUEST", CONTEXT_REF(server))%;) to a State State2.
  4. Transition (with effect: %SEND_EVENT("NEW_REQUEST", CONTEXT_REF(server))%;) to a State State3.
  5. Transition (with effect: %BROADCAST_EVENT("NEW_REQUEST")%;) to a State State4.
  6. Transition (with effect: %SEND_EVENT("AUTHORIZED", CONTEXT_REF(server))%;) to a State State5.
  7. Transition to a Final State Final.

Simulation in a simple context

Create the Simulation Artifact

  1. Create an Executable StateMachine Artifact with the name Simulation with Deferred Event and the 'Language' field set to JavaScript.
  2. Enlarge it, then Ctrl+drag the TransactionServer element onto the Artifact and paste it as a property with the name server.

Run the Simulation

  1. Select the Artifact, then select the 'Simulate > Compiled Simulation > Build & Run' option, and specify a directory for your code (Note: all the files in the directory will be deleted before simulation starts).
  2. Click on the Generate button.
  3. Select the 'Simulate > Dynamic Simulation > Triggers' option to open the Simulation Event window.

When simulation starts, idle will be the active state.

  1. Double-click on NEW_REQUEST in the Simulation Event window to execute it as the Trigger; idle is exited and busy is activated.

  1. Double-click on NEW_REQUEST in the Simulation Event window to execute it again as the Trigger; busy remains activated, and an instance of NEW_REQUEST is appended in the Event Pool.
  2. Double-click on NEW_REQUEST in the Simulation Event window to execute it a third time as the Trigger; busy remains activated, and an instance of NEW_REQUEST is appended in the Event Pool.
  3. Type dump in the Simulation window command line; notice that the event pool has two instances of NEW_REQUEST.

  1. Double-click on AUTHORIZED in the Simulation Event window to execute it as the Trigger; these actions take place:
         -  busy is exited and idle becomes active
         -  a NEW_REQUEST event is retrieved from the pool, idle is exited and busy becomes active
  2. Type dump in the Simulation window command line; there is now only one instance of NEW_REQUEST in the Event Pool.

Interactive simulation via Send/Broadcast Event

Create the Simulation Artifact

  1. Create an Executable StateMachine Artifact with the name Interactive Simulation with Deferred Event and the 'Language' field set to JavaScript; enlarge the element.
  2. Ctrl+Drag the TransactionServer element onto the Artifact, and paste it as a property with the name server.
  3. Ctrl+Drag the TestClient element onto the Artifact, and paste it as a property with the name client.
  4. Create a connector from client to server.

Run Interactive Simulation

  1. Launch the simulation in the same way as for the simple context.

    Once the simulation has started, the client remains at State0 and the server iremains at idle.

  1. Double-click on RUN_TEST in the Simulation Event window to trigger it. The event NEW_REQUEST will be triggered three times (by SEND_EVENT and BROADCAST_EVENT) and AUTHORIZED will be triggered once by SEND_EVENT.

Type dump in the Simulation window command line, There is one instance of NEW_REQUEST left in the Event Pool. The result matches our manual triggering test.