MetaCase Homepage
Forum Home Forum Home > > MetaEdit+
  New Posts New Posts RSS Feed - State Machine Translator
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

State Machine Translator

 Post Reply Post Reply
Author
Message
mert View Drop Down
Contributor
Contributor


Joined: 16.Mar.2016
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote mert Quote  Post ReplyReply Direct Link To This Post Topic: State Machine Translator
    Posted: 07.Apr.2018 at 05:30
Dear All,

I am trying to write a generator for the simplified version of the UML state machine diagram. In my notation, there are simply the start/end state notations, the state notation, and the transition arrow. I am trying to figure out how to traverse an entire diagram starting from its start state and ending with its end state. 

The following code visits the start state and the next state. However, I cannot figure out how I can keep moving to each next state until I reach the end state.

foreach .Start {
do ~()>()~() {
do .State {
:Name
}
}
}
Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 556
Post Options Post Options   Thanks (1) Thanks(1)   Quote stevek Quote  Post ReplyReply Direct Link To This Post Posted: 07.Apr.2018 at 08:16
Have you tried looking in the manual? The MERL Primer shows how to do that (5.2.6): I'd suggest you do the whole Primer, it's the quickest way to get yourself up to speed.

Note also that in your second line, you navigate from the Start, to any role, to the relationship, to any role in that relationship (including the one you just came from). That will make you loop back to where you started from (see Loop-Backs in the manual). You'll be saved in this case by the fact that your next line will only take you to a State, not a Start, but as you navigate further from State to State you need to take account of which direction you're going in: 
do ~From~To.State {...}
If you need to look at the Transition relationship, e.g. some property or other role attached to it, you can do that after ~From: 
do ~From
{  do >Transition { :Event }
   do ~To.State {...}
}
Back to Top
mert View Drop Down
Contributor
Contributor


Joined: 16.Mar.2016
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote mert Quote  Post ReplyReply Direct Link To This Post Posted: 23.Jun.2018 at 10:46
Hi Stevek,

I have tried to generate some LTL code from a state machine model. The state machine model starts with a start state and ends with an end state. I want to basically generate the :Service name '->' for each state  between the start and end states. However, for the last state, I do not want to print '->'. 

I am using the following code snippet; how can I modify it in a way that the last '->' will not be printed ?

.................

do :Graph link type{
   foreach .() { 
 _handleStateAndRecurse()
  }
}
...........

_handleStateAndRecurse()

if id <> 'Stop' then
:Service Name '->'
    endif

 do ~From>()~event.State
 {  
 _handleStateAndRecurse() }

Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 556
Post Options Post Options   Thanks (1) Thanks(1)   Quote stevek Quote  Post ReplyReply Direct Link To This Post Posted: 23.Jun.2018 at 11:43
You have both foreach .() iterating over all objects, and _handleStateAndRecurse recursing through all objects. In general you can't just recurse through a state model, because it may contain cycles. If I understand your message correctly, you want to print out a simple chain of states from Start to Stop - but what do you want if the path diverges to two states, or two paths join back together to the same state?

If all you have is a single chain of states, with the first called Start and last called Stop:

foreach .() where id = 'Start' 
{ _handleStateAndRecurse()
}

_handleStateAndRecurse()
id
do ~From>~To.State
{ ' -> ' _handleStateAndRecurse()
}


You'll want to check the types to match your metamodel, e.g. :Service Name; instead of id, and maybe ~event instead of ~To, but hopefully you get the idea. We use iteration to find just the first object, and from there we start the recursion. That outputs what we want from an object, then navigates, outputting what we want from the transition, and recurses.

Other common ways to iterate to find the first object are:
foreach .Start
foreach .() where not ~To

As I mentioned at the start, for a generic state model you can't usually use recursion, because there isn't just a single path to follow. It's more common to see an iteration to list all States, then another iteration to list all transitions:

foreach .State { id newline }
newline
foreach >Transition
{ do ~From.State { id }
' -> '
do ~To.State { id }
newline
}


Or then iterate over all States, and for each iterate over the transitions that leave it -nested iteration, e.g. to generate a nested switch-case statement:

'switch (state) {' newline
foreach .State
{ ' case ' id ':' newline
   '    switch (event) {' newline
   do ~From>()
   { '      case ' id ':' newline
      '        state = ' do ~To.State { id } newline
      '        break;' newline
   }
   '    }' newline
   '    break;' newline
}
'}'

Back to Top
mert View Drop Down
Contributor
Contributor


Joined: 16.Mar.2016
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote mert Quote  Post ReplyReply Direct Link To This Post Posted: 23.Jun.2018 at 14:15
Thanks for the detailed response. However, I have not still managed to resolve the issue. The question is so simple, I want to generate a text from a state machine in the following form : state1->state2->state3.. However, now what I am getting basically is the following state1->state2->state3-> . So, I want to find a way to get rid of the last ->. 
Following is my recursive code


_handleStateAndRecurse()
if id <> 'Start' then
:Service Name
endif
do ~From>()~event.State{ '->'
   _handleStateAndRecurse() 
endif
}
Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 556
Post Options Post Options   Thanks (1) Thanks(1)   Quote stevek Quote  Post ReplyReply Direct Link To This Post Posted: 23.Jun.2018 at 15:37
Sure, see my first piece of code above.
Back to Top
mert View Drop Down
Contributor
Contributor


Joined: 16.Mar.2016
Points: 12
Post Options Post Options   Thanks (0) Thanks(0)   Quote mert Quote  Post ReplyReply Direct Link To This Post Posted: 23.Jun.2018 at 16:14
Thanks again for the prompt response. One quick question too - how can I prevent the Start state's id from getting printed too ? The generated output is now the Start->State1->State2, however, I want it to be just State1->State2.
Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 556
Post Options Post Options   Thanks (1) Thanks(1)   Quote stevek Quote  Post ReplyReply Direct Link To This Post Posted: 23.Jun.2018 at 17:48
Just navigate from Start to the next state before calling _handleStateAndRecurse
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 10.16
Copyright ©2001-2013 Web Wiz Ltd.

This page was generated in 0.047 seconds.