Transitions in UML state charts: better to use triggers or guards?

落爺英雄遲暮 提交于 2019-12-06 11:48:19

Those two are different concepts.

Trigger is an event occurrence which enables the transition, while guard is a condition that must be evaluated to true in order for the transition to proceed.

So you cannot use them interchangeably — they have different roles.

Also note that the default guard (if none is specified) is [true], so the trigger is often sufficient to move from one state to another.

Update:

Summary:

  • Trigger (event) is some new data (of any data type) that was received by the object.
  • Guard is boolean expression on some data that is alrady present in the object.

Trigger (event) is an outside event that some other actor fired - user has pressed a button, browser requested a page load, etc. So in the image above, every time user presses a digit on a digital lock it fires "pressed digit" event.

If the pin (sequence of digits) is valid, then the transition to unlocked state will be enabled.

Yet another way to look at it:

If you press a keyboard key the system fires a keypress event, that would be a trigger whose value is the pressed key. Then you can make a guard [pressedKey = enter] (guard is always a boolean expression).

However having just the guard is not enough here, because there would be nothing to compare against.

Strictly speaking, guards cannot be used without triggers.

UML 2.5.1 specification (Section 14.2.4.8, page 331) defines State Machine's transitions by the following BNF expression:

[<trigger> [‘,’ <trigger>]* [‘[‘ <guard>’]’] [‘/’ <behavior-expression>]]

While UML 2.0 defined them as:

<transition> ::= <trigger> [‘,’ <trigger>]* [‘[‘ <guard-constraint>’]’] [‘/’ <activity-expression>]

Triggers are defined as:

<trigger> ::= <call-event> | <signal-event> | <any-receive-event> | <time-event> | <change-event>

So, in both cases, there cannot be a transition with a guard that doesn't have any trigger.

The only exception, according to UML 2.5.1, are internal transitions, which are specified by:

{<trigger>}* ['[' <guard>']'] [/<behavior-expression>]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!