1
General Board / Re: Communication between concurrent states
« on: July 01, 2016, 02:38:39 pm »
You can use Executable Statemachine.
Create a class element MyClass, from Project Browser | Context menu | Add | Statemachine
Then create an Executable Statemachine artifact, enlarge it, Ctrl + Drag MyClass and drop as a Property, name it Part1.
Set the artifact's language, JavaScript (you can also set to Java, C++, C#, C if you have a compiler).
Context menu on Artifact | Code Engineering | Executable Statemachine | Generate, Build and Run | Specify a path for the source code | Generate
The simulation will start and State2 and State4 are the two active states in the concurrent regions. Open Simulate Events (Trigger) window, double click "AAA", which will trigger event "AAA" and transition from State4 to Final3 takes effect, during which, an event "BBB" will be broadcasted and cause State2 exit and transition to State3. The full trace are available in the Debug window.
[21383023] Part1[MyClass].Initial1_280__TO__State1_127 Effect
[21383024] Part1[MyClass].StateMachine_State1 ENTRY
[21383025] Part1[MyClass].StateMachine_State1 DO
[21383026] Part1[MyClass].Initial3_285__TO__State4_130 Effect
[21383027] Part1[MyClass].StateMachine_State1_State4 ENTRY
[21383028] Part1[MyClass].StateMachine_State1_State4 DO
[21383029] Part1[MyClass].Initial2_282__TO__State2_128 Effect
[21383030] Part1[MyClass].StateMachine_State1_State2 ENTRY
[21383030] Part1[MyClass].StateMachine_State1_State2 DO
[21384117] Blocked
[21386083] Part1[MyClass].StateMachine_State1_State4 EXIT
[21386085] Part1[MyClass].State4__TO__Final3_287_131 Effect
[21386640] Part1[MyClass].StateMachine_State1_State2 EXIT
[21386647] Part1[MyClass].State2__TO__State3_129 Effect
[21386649] Part1[MyClass].StateMachine_State1_State3 ENTRY
[21386650] Part1[MyClass].StateMachine_State1_State3 DO
[21387185] Part1[MyClass].StateMachine_State1_State3 EXIT
[21387192] Part1[MyClass].State3__TO__Final2_284_133 Effect
[21387700] Part1[MyClass].StateMachine_State1 EXIT
[21387707] Part1[MyClass].State1__TO__Final1_288_132 Effect
The key point is the effect expression:
%BROADCAST_EVENT("EventName")%
If you want to send the event to a specific context (E.g. Your artifact has 2 contexts, Part1, Par2; you want to send a event to Part2 from a transition in Part1), you can use:
%SEND_EVENT("EventName", CONTEXT_REF(context instance name))%
like:
%SEND_EVENT("CCC", CONTEXT_REF(Part2))%
Create a class element MyClass, from Project Browser | Context menu | Add | Statemachine
- Initial1, transition to State1, transition to Final1
- Enlarge State1 | context menu | advanced | Define Concurrent Substates, create region1, region2
- Inside region1: Initial2, transition to Stete2, transition(triggered by BBB) to State3, transition to Final2
- Inside region2: Initial3, transition to Stete4, transition (triggered by AAA, Effect: %BROADCAST_EVENT("BBB")%) to Final3
Then create an Executable Statemachine artifact, enlarge it, Ctrl + Drag MyClass and drop as a Property, name it Part1.
Set the artifact's language, JavaScript (you can also set to Java, C++, C#, C if you have a compiler).
Context menu on Artifact | Code Engineering | Executable Statemachine | Generate, Build and Run | Specify a path for the source code | Generate
The simulation will start and State2 and State4 are the two active states in the concurrent regions. Open Simulate Events (Trigger) window, double click "AAA", which will trigger event "AAA" and transition from State4 to Final3 takes effect, during which, an event "BBB" will be broadcasted and cause State2 exit and transition to State3. The full trace are available in the Debug window.
[21383023] Part1[MyClass].Initial1_280__TO__State1_127 Effect
[21383024] Part1[MyClass].StateMachine_State1 ENTRY
[21383025] Part1[MyClass].StateMachine_State1 DO
[21383026] Part1[MyClass].Initial3_285__TO__State4_130 Effect
[21383027] Part1[MyClass].StateMachine_State1_State4 ENTRY
[21383028] Part1[MyClass].StateMachine_State1_State4 DO
[21383029] Part1[MyClass].Initial2_282__TO__State2_128 Effect
[21383030] Part1[MyClass].StateMachine_State1_State2 ENTRY
[21383030] Part1[MyClass].StateMachine_State1_State2 DO
[21384117] Blocked
[21386083] Part1[MyClass].StateMachine_State1_State4 EXIT
[21386085] Part1[MyClass].State4__TO__Final3_287_131 Effect
[21386640] Part1[MyClass].StateMachine_State1_State2 EXIT
[21386647] Part1[MyClass].State2__TO__State3_129 Effect
[21386649] Part1[MyClass].StateMachine_State1_State3 ENTRY
[21386650] Part1[MyClass].StateMachine_State1_State3 DO
[21387185] Part1[MyClass].StateMachine_State1_State3 EXIT
[21387192] Part1[MyClass].State3__TO__Final2_284_133 Effect
[21387700] Part1[MyClass].StateMachine_State1 EXIT
[21387707] Part1[MyClass].State1__TO__Final1_288_132 Effect
The key point is the effect expression:
%BROADCAST_EVENT("EventName")%
If you want to send the event to a specific context (E.g. Your artifact has 2 contexts, Part1, Par2; you want to send a event to Part2 from a transition in Part1), you can use:
%SEND_EVENT("EventName", CONTEXT_REF(context instance name))%
like:
%SEND_EVENT("CCC", CONTEXT_REF(Part2))%