1

I want to implement this sort of a conditional transition in SCXML:

current_state = s01  
if (Math.random() < 50) go to state s02  
  else go to state s03  

Is this sort of conditional targets supported in SCXML ?
To put it into the SCXML language, is the equivalent of the following snippet possible ?

<transition event="event_1">
        <if cond="import java.util.Random; Math.abs(new Random().nextInt(100)) gt 50">
            <target="s02"/>
            <else/>
            <target="s03"/>
        </if>
</transition>

Would appreciate any pointers to their doc. for this / alternative strategies to handle it.
Thanks.

1 Answers1

1

Something like the following (untested) code will do what you want.

<scxml 
    datamodel="ecmascript"
    xmlns="http://www.w3.org/2005/07/scxml"
    version="1.0">

    <state id="s01">
      <transition event="event_1" target="s02" cond="Math.random() &gt; .5"/>
      <transition event="event_1" target="s03"/>
    </state> 

    <state id="s02"/>

    <state id="s03"/>

</scxml>
Phrogz
  • 296,393
  • 112
  • 651
  • 745
jbeard4
  • 12,664
  • 4
  • 57
  • 67
  • Thanks for the answer @jbeard4. But my main motive is to know whether the "target" block can be taken out of the top level tag, and be put into any of the inner body statements like . Does SCXML allow that ? – user1598865 Mar 11 '16 at 11:07
  • 1
    Why do you want to do this? – jbeard4 Mar 12 '16 at 15:48
  • No, SCXML (the standard) does not provide a mechanism for specifying the target anywhere other than the `` element attribute. For my company's SCXML implementation we have added a `targetexpr` attribute that evaluates code to decide the target. – Phrogz Mar 14 '16 at 05:27
  • But what we added is outside the standard, and not something supported by Apache SCXML, either. (And really, a non-deterministic state machine is a little weird.) – Phrogz Mar 14 '16 at 05:32
  • Phrogz: Having the 'target' expression within conditionals doesn't necessarily mean the State Machine will be non-deterministic. But I get your point. @jbeard4: Imagine a scenario in experimentation which involves more than 5 paths from a node, each selected conditionally. While it is possible to solve it through data model / variables, it would be more intuitive to write conditionals inside the ; mentioning targets against each condition, and a 'base' transition, if nothing matches. This would still make sure the SM is deterministic, but increase the flexibility in modelling. – user1598865 Mar 14 '16 at 06:46
  • @user1598865 I hope you see that what you describe is what jbeard4 shows in his answer. You can have the same event matched by many transitions, each with its own condition. The first transition (in document order) whose condition evaluates to true will be taken. This is thus similar to a `switch` statement. – Phrogz Mar 14 '16 at 23:03