accessing datamodel's variables indirectly (by name)

39 views
Skip to first unread message

Andrea Galbusera

unread,
May 5, 2015, 9:26:38 AM5/5/15
to scio...@googlegroups.com
Hello,

I use scxml from npm to interpret a SCXML document with a ECMAscript datamodel which consists of a bunch of boolean variables. My transitions are mostly eventless with guard conditions which depend on the datamodel's variables.
I was wondering what would be the best way to update the datamodel by assigning a value on event triggers while keeping the document as clear and compact as possible. I currently can successfully achieve the goal with something like the this (this is just an excerpt of the specification):

state(id="automa1")
  datamodel
    data(id="Va",expr="false")
    data(id="Vb",expr="false")

  transition(event="Va.true")
    assign(location="Va", expr="true")
  transition(event="Va.false")
    assign(location="Va", expr="false")
  transition(event="Vb.true")
    assign(location="Vb", expr="true")
  transition(event="Vb.false")
    assign(location="Vb", expr="false")

  state(id="S10")
    //- A1 
    transition(cond="!Va && Vb",target="S11")

I also know that setting Va and Vb from a Javascript <script> snippet is an equivalent approach. I was wondering if it could be possible to collapse all those dedicated events into a single one, let's say "sigChange" with associated information on which variable should be updated and the value to store. This would be an acceptable simplification for external entities responsible for feeding events to the interpreter. Hence the idea to write a single targetless transition like this:

  transition(event="sigChange")
    [something here that assigns the value _event.data.sigvalue to _event.data.signame]

After some experimenting I figured out that I can't reference the "location" through a property of the _event object. Is there a way to reference a datamodel's variable (and contextually assign it) through a property in the _event objects. If so what would be a suggested way to do it? I did some tests with eva() inside a <script> snippet but I can't exactly figure out the involved variable scopes and it did not work.

Any suggestion or comment is appreciated, TIA
Regards

Jacob Beard

unread,
May 5, 2015, 9:29:06 AM5/5/15
to scio...@googlegroups.com
Hi Andrea,

I will review this and get back to you. Thank you,

Jake

--
You received this message because you are subscribed to the Google Groups "scion-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scion-dev+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jacob Beard

unread,
May 12, 2015, 1:12:59 AM5/12/15
to scio...@googlegroups.com
Hi Andrea,

I don't think it is wise to implement a statechart design that assigns to arbitrary variable names passed from externally. The reason for this is that the data model declares a finite set of variables, defined at compile-time, and does not support assigning to variable names outside of this known set.

You could collapse the transitions you mentioned into a single transition which accepts a single event, and assigns to a variable in the data model based on a property in the event data. You could use a pattern like this:

<transition event="set">
<script>
switch(_event.data.lhs){
  case "Va":
    Va = _event.data.rhs;
  case "Vb":
    Vb = _event.data.rhs;
  default:
    throw new Error("Attempted to assign to unknown variable");
}
</script>
</transition>

Where lhs and rhs are left hand side and right hand side of the assignment, respectively.

It also would be possible to build a plugin that generates this code automatically on saving the statechart. I would be happy to guide you on how to develop this.

Thank you,

Jake



On May 5, 2015, at 9:26 AM, Andrea Galbusera <giz...@gmail.com> wrote:

Hello,

I use scxml from npm to interpret a SCXML document with a ECMAscript which consists of a bunch of boolean variables. My transitions are mostly eventless with guard conditions which depend on the datamodel's variables.

--
Reply all
Reply to author
Forward
0 new messages