Prev | Next |
Example: Deferred Event Pattern
Enterprise Architect supports the Deferred Event Pattern.
To create a Deferred Event in a State:
- Create a self transition for the State.
- Change the 'kind' of the transition to 'internal'.
- Specify the Trigger to be the event you want to defer.
- In the 'Effect' field, type 'defer();'.
To Simulate:
- Select 'Simulate > Dynamic Simulation > Simulator > Open Simulation Window'. Also select 'Simulate > Dynamic Simulation > Triggers' to open the Simulation Events window.
- The Simulator Events window helps you to trigger events; double-click on a trigger in the 'Waiting Triggers' column.
- 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:
- A Class element TransactionServer, to which you add a StateMachine ServerStateMachine.
- A Class element TestClient, to which you add a StateMachine ClientStateMachine.
- An Association from TestClient to TransactionServer, with the target role named server.
Modeling for ServerStateMachine
- Add an Initial Node Initial to the StateMachine diagram, and transition to a State idle.
- Transition (with event NEW_REQUEST as Trigger) to a State busy.
- Transition (with event QUIT as Trigger) to a Final State Final.
- Transition (with event AUTHORIZED as Trigger) to idle.
- Transition (with event NEW_REQUEST as Trigger and defer(); as effect) to busy
Modeling for ClientStateMachine
- Add an Initial Node Initial to the StateMachine diagram, and transition to a State State0.
- Transition (with event RUN_TEST as trigger) to a State State1.
- Transition (with effect: %SEND_EVENT("NEW_REQUEST", CONTEXT_REF(server))%;) to a State State2.
- Transition (with effect: %SEND_EVENT("NEW_REQUEST", CONTEXT_REF(server))%;) to a State State3.
- Transition (with effect: %BROADCAST_EVENT("NEW_REQUEST")%;) to a State State4.
- Transition (with effect: %SEND_EVENT("AUTHORIZED", CONTEXT_REF(server))%;) to a State State5.
- Transition to a Final State Final.
Simulation in a simple context
Create the Simulation Artifact
- Create an Executable StateMachine Artifact with the name Simulation with Deferred Event and the 'Language' field set to JavaScript.
- Enlarge it, then the TransactionServer element onto the Artifact and paste it as a property with the name server.
Run the Simulation
- 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).
- Click on the .
- Select the 'Simulate > Dynamic Simulation > Triggers' option to open the Simulation Event window.
When simulation starts, idle will be the active state.
- Double-click on NEW_REQUEST in the Simulation Event window to execute it as the Trigger; idle is exited and busy is activated.
- 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.
- 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.
- Type dump in the Simulation window command line; notice that the event pool has two instances of NEW_REQUEST.
- 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 - 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
- Create an Executable StateMachine Artifact with the name Interactive Simulation with Deferred Event and the 'Language' field set to JavaScript; enlarge the element.
- Ctrl+Drag the TransactionServer element onto the Artifact, and paste it as a property with the name server.
- Ctrl+Drag the TestClient element onto the Artifact, and paste it as a property with the name client.
- Create a connector from client to server.
- Click on the connector and press to select the association from the TestClient element to the TransactionServer element.
Run Interactive Simulation
- 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 remains at idle.
- 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.