statemachine: Guard vs. Change Event

Moderator: Moderators

statemachine: Guard vs. Change Event

Postby maximilian.von-arnim@airbus.com » Thu Jan 10, 2019 10:01 am

Hi,

I've been trying to make a state machine executable and ran into a few problems. One of them is that I want the machine to leave state "A" whenever a certain variable "a" is set to "True". I've tried using Change Events on the transition but if "a==True" before state A is entered, the state machine will not detect it (probably because there is no change to True). When I use a guard and no trigger event, the transition will be triggered if "a==True" before state A is entered but the guard condition will not be evaluated again if it was found to be False once, so it will not detect any change in a's value anymore.

For example in the attached sample, if Value1==True before the state machine is activated, the lower transition will trigger but the upper one will not. If Value1==False before the state machine is activated and then changed to Value1==True, the upper transition will trigger but the lower one will not. I would expect both transitions to trigger if Value1==True at any time while in the state _init_. This is especially important if the value of Value1 can be changed at any time by an external process, i. e. before or while in state _init_.

Do I misunderstand the UML definition for Change Event: "A ChangeEvent occurs when a Boolean changeExpression becomes true." ? Or is this a feature / bug of MagicDraw? Do I have to change any settings?

One workaround is to reenter the state _init_ periodically via a transition to itself, which then re-evaluates the guard on every reentry. But that forces me to leave the state I'm in and adds unneccessary extra complexity to the diagram.

Thanks,
Maximilian

edit: If I do the workaround, the transition to itself will be done multiple times (seemingly randomly) before it notices that the value has changed. :?
You do not have the required permissions to view the files attached to this post.
maximilian.von-arnim@airbus.com
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Thu Jan 10, 2019 2:59 am
Full name: Maximilian von Arnim

Re: statemachine: Guard vs. Change Event

Postby axel.scheithauer@oose.de » Tue Jan 15, 2019 4:03 am

Hi Maximilian,

the change event is created in the very moment when the value changes. If there is no transition in the current state waiting for this event, it will get discarded. This is the standard behavior of State Machines. However you could defer an event in all the states leading to the state. Then it will stay in the event pool until a state is reached where it is no longer deferred. If this state has a transition for this event, it will get triggered, otherwise it will get discarded (I have not tried, whether the simulation engine treats deferred events correctly).

A transition without event doesn't mean, that it doesn't wait for an event. This event is the completion of the behavior of the state. If the state doesn't have a behavior (entry, do or internal states), it is triggered immediately. Exactly in this moment the guard gets evaluated.

As a matter of taste, I prefer not to use change events. Usually there is a signal or operation-call that leads to this change, and using this event conveys more clearly the cause of the transition.

best regards

Axel Scheithauer
oose eG
axel.scheithauer@oose.de
Forum Beginner
Forum Beginner
 
Posts: 67
Posts Rating:0
Joined: Fri May 21, 2010 7:19 am

Re: statemachine: Guard vs. Change Event

Postby maximilian.von-arnim@airbus.com » Thu Jan 17, 2019 10:31 am

Dear Axel,

thank you very much for your reply. I see that I had misunderstood the definition for the change event. Still, this is somewhat limiting. And while I can defer events while in proper states, I cannot defer events that occur while the state machine is transitioning from one state to another. In the attached example, changing value1 in state _init_ from "False" to "True" will create the change event and fire the transition. If value1 is reset to "False" while the state machine is in the transition, the change event will be discarded and will stay in state _state_.
While transitions should be instantaneous (per definition), the slow animation of CST allows the state machine to be in no state for some time. Same thing happens while an entry activity is executing - the state machine has left the prior state but has not yet entered the next state.

Further:
I can see why you'd usually prefer signal events but they don't always work best. If more than one condition needs to be satisfied, e. g. Temperature > 30°C AND time is past noon, the ChangeEvent shows this fairly well.

Best regards,
Maximilian
You do not have the required permissions to view the files attached to this post.
maximilian.von-arnim@airbus.com
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Thu Jan 10, 2019 2:59 am
Full name: Maximilian von Arnim

Re: statemachine: Guard vs. Change Event

Postby axel.scheithauer@oose.de » Mon Jan 21, 2019 8:18 am

Dear Maximilian,

Well, the duration of a transition is intentionally not specified in UML:
The duration of a Transition traversal is undefined, allowing for different semantic interpretations, including both “zero” and non-“zero” time. [UML 2.5]


And during the entry and effect-Behaviors of the State resp. the Transition, the State is already active:
Regardless of how a State is entered, the StateMachine is deemed to be “in” that State even before any entry Behavior or
effect Behavior (if defined) of that State start executing.

I think there is a minor error in the specification, because the Transition is not explicitely mentioned (I wrote an Issue https://issues.omg.org/browse/UMLR-755).

Actually it doesn't even matter, what State it is in, since the Statemachine will store any events in a pool, until it reaches a stable state (a wait state). Then it processes the events in the pool or waits for new events, if it is empty. During a transition, the current state is irrelevant, since the reaction to events will only start as soon as the run-to-completion step completes.

It is possible, that the simulation engine doesn't treat these events correctly. I have not tested this.

In your diagram, you defer events that will never occur. When value1 becomes true, the _init_ state is left. Since there is no Transition that returns to _init_, except for the one that consumes the event of value1 returning to false, _init_ will never be active during this event.

best regards

Axel
axel.scheithauer@oose.de
Forum Beginner
Forum Beginner
 
Posts: 67
Posts Rating:0
Joined: Fri May 21, 2010 7:19 am

Re: statemachine: Guard vs. Change Event

Postby maximilian.von-arnim@airbus.com » Tue Feb 05, 2019 9:43 am

axel.scheithauer@oose.de wrote:Dear Maximilian,

Well, the duration of a transition is intentionally not specified in UML:
The duration of a Transition traversal is undefined, allowing for different semantic interpretations, including both “zero” and non-“zero” time. [UML 2.5]


And during the entry and effect-Behaviors of the State resp. the Transition, the State is already active:
Regardless of how a State is entered, the StateMachine is deemed to be “in” that State even before any entry Behavior or
effect Behavior (if defined) of that State start executing.

I think there is a minor error in the specification, because the Transition is not explicitely mentioned (I wrote an Issue https://issues.omg.org/browse/UMLR-755).

Actually it doesn't even matter, what State it is in, since the Statemachine will store any events in a pool, until it reaches a stable state (a wait state). Then it processes the events in the pool or waits for new events, if it is empty. During a transition, the current state is irrelevant, since the reaction to events will only start as soon as the run-to-completion step completes.

It is possible, that the simulation engine doesn't treat these events correctly. I have not tested this.

Understood, and thanks for your support. From what I have seen so far, the simulation engine does store SignalEvents but not ChangeEvents. I have not tested CallEvents.

I also found a setting (in Options>Environment>Simulation) that allows me to set whether a state is entered before or after the entry behavior has executed. So that's one problem less.

Regarding the initial problem of "automatic transitions" not being quite as automatic as I'd like, this seems to be related to the W3C SCXML standard that is the basis for the state machine simulation engine, and of course the underlying UML specification. (https://www.w3.org/TR/scxml/)
1)Complete the macrostep by repeatedly taking any internally enabled transitions, namely those that don't require an event or that are triggered by an internal event. [...] 4) When the internal event queue is empty, wait for an external event and then execute any transitions that it triggers

This means that once all queued events have been used, the simulation will wait for an external event (e. g. SignalEvent). The conditions of eventless transitions will then only be evaluated again after such an external event was received. However, I would interpret the following statement:
If the 'event' attribute is missing, the transition is taken whenever the 'cond' evaluates to true.

that 'cond' should be evaluated continuously instead of only once. Clearly, though, this does not happen, and the latter quote is from an 'informative' section whereas the former is part of a 'normative' section.

Best,
Maximilian
maximilian.von-arnim@airbus.com
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Thu Jan 10, 2019 2:59 am
Full name: Maximilian von Arnim

Re: statemachine: Guard vs. Change Event

Postby axel.scheithauer@oose.de » Thu Feb 07, 2019 3:52 am

Hi Maximilian,

UML doesn't know the concept of "internal events". Therefore I think this should not play a role in the simulation. And the sentence about the missing 'event' attribute is from the notation section. My feeling is, that it is not referring to the executable semantics, but I didn't read through all this handling of events in the mainEventLoop().

regards

Axel
axel.scheithauer@oose.de
Forum Beginner
Forum Beginner
 
Posts: 67
Posts Rating:0
Joined: Fri May 21, 2010 7:19 am


Return to Cameo Simulation Toolkits

Who is online

Users browsing this forum: No registered users and 1 guest