Dynamic target in a state machine

1.8k views Asked by At

In a state machine made with SCXML, is there any way to set a dynamic target value for a transition?

I mean, suppose I have an object called "obj" which has been set as the datamodel for a scxml. So there can be set conditions (if there were a property called checkCondition in the object) on it like:

cond="obj.checkCondition"

<state id="state1">
    <transition cond="obj.checkCondition" target="state2"/>
</state>
<state id="state2">
    ...
</state>

I have another property in obj called nextTarget. I want to set the target in this transition reading its value from the object (as it is done in the conditions).

<state id="state1">
    <transition cond="obj.checkCondition" target="eval(obj.nextTarget)"/>
</state>
<!-- Where in obj.nextTarget there it has been set as value "state1", "state2" or any state name -->

Is there any syntax to do this?

Thanks.

4

There are 4 answers

0
Stephane Rolland On

Although the answer of @Charles Goodwin is pretty acurate... I may add my words. To simplify:

  • What you have in your design: you have one transition my_transition
  • What you woud like: that this transition may lead to several state_targets

You are sheerly hiding the logic of the problem.

What you should have: several transitions

  • my_transition_A targetting state_A_target
  • my_transition_B targetting state_B_target
  • my_transition_C targetting state_C_target
  • my_transition_D targetting state_D_target...

and the process which would have dynamically set the target in your design, will dynamically process the event for my_transition_A or my_transition_C... just like it would have chosen the target_state in your design).

0
Michael Thomas Wells On

You can specify the cond attribute in a transition element

<transition cond="data.value > 10" target="state2"/>
0
Charles Goodwin On

SCXML is a fairly simple description of states and the possible transitions between those states. There's no such thing as conditional transitions.

However, you can have more than one transition from each state. There's no limit on the number of transitions you have from a state.

So the answer to your question is you have as many transitions as are required to describe the conditional directions you wish to go, and you evaluate the conditions elsewhere (i.e. in Java).

Example source is the SCXML Wikipedia entry.

<state id="ready">
    <transition event="watch.start" target="running"/>
</state>
<state id="running">
    <transition event="watch.split" target="paused"/>
    <transition event="watch.stop" target="stopped"/>
</state>
<state id="paused">
    <transition event="watch.unsplit" target="running"/>
    <transition event="watch.stop" target="stopped"/>
</state>
<state id="stopped">
    <transition event="watch.reset" target="ready"/>
</state>

enter image description here

0
Janusz Dobrowolski On

You are trying to build very application oriented, complicated State Machine framework. This is a bit like building own spreadsheet framework for every spreadsheet application instead of using EXCEL. A better approach may be to use existing framework and define your events and actions in SCXML. You can see examples of SM API framework in www.StateSoft.org -> State Machine Gallery.

-Janusz