Lesson 19, accept when versus if

Hi,

When trying to get my lesson 18 model to work with the simulator I ran into a problem.

It comes to down how I implemented the state transition in lesson 18. I simply used if instead of accept when. To give example my implementation of the foggyAndWindy state looks like this:

state foggyAndWindy;
if visibility >= lowVisibility and windSpeed <= highWindSpeed then clear;
if visibility >= lowVisibility then windy;
if windSpeed <= highWindSpeed then foggy;

While the provided solution implemented is like this:

state foggyAndWindy;
accept when visibility >= lowVisibility and windSpeed <= highWindSpeed then clear;
accept when visibility >= lowVisibility then windy;
accept when windSpeed <= highWindSpeed then foggy;

Both seem to be fine to me, of course accept when, is more explicit about that we are dealing with a change event here, also the simulator doesn’t like the if, so I have to change my file anyway.

My question is what is common/best practice, using the accept when or the if ?
Is there a reason to one of the two in specific situations and if so when?

Hello Maurice,

Great question, this is a very important distinction.

The “accept when” construct is a trigger, which can make a transition execute. The “if” construct is a guard, which can prevent the execution of a triggered transition.

If you don’t specify a trigger, the assumption is that the transition will execute immediately after the source state (although this is not clearly specified in the specification and is currently under discussion). The guard can prevent this, but then the transition will not try to execute again. So, if you use the “if” and not the “accept when”, your transition will check the guard expression once and execute only if it’s true - nothing will happen if it’s not, and it will not be checked again.

If you use the “accept when” and not the “if”, the moment when the expression becomes true will trigger the transition, and you don’t need the guard because you know the expression is true. This construct can delay the transition until the expression is true, so you will not “lose” the event.

I hope this clarified things. This is a complex topic that is not well formalized or explained in the specification, so please let me know if you would like to discuss it further.

Vince