Specifying statechart guards
A statechart guard is a conditional expression that is evaluated based on object attributes and event data.
About this task
IBM® Engineering Systems Design Rhapsody® does not interpret guards. They are host-language expressions, or simply code chunks, that must resolve to either a Boolean or an integer value that can be tested. Otherwise, the statechart code generated for guards do not compile.
The following example shows a
transition label that consists of a guard and an action that uses
the GEN
macro to generate an event:
[x > 7]/controller->GEN(A7Failures)
A transition can consist of only a guard. The low-to-high transition of the condition (or Boolean value) is considered to be the triggering event. For example, the following guard is a valid transition label:
[x > 7]
During animation, all guards without triggers are tested every time an event happens. The following statechart uses several guards without transitions.
This statechart is for the keypad of a home alarm system.
When the keypad of the alarm system is in the idle state, you can
enter a code to arm the alarm before leaving the house. After entering
the code, you press the On button to turn on the alarm. Pressing the
On button issues an evKeyOn
event. Each time this
event occurs, the state machine evaluates the two guards that come
after the condition connector, [IS_IN(correct)]
or [IS_IN(notEntered)]
,
and follows the path of the one that evaluates to true
.
By using an animated sequence diagram, you can see when a guard is tested. If you want to test a condition more frequently or at a more regular interval than whenever an event occurs, you can create a polling mechanism. To do this, create a short timeout transition from the state to itself so the guard is evaluated on at least these occasions. Alternatively, you can poll using another object and replace the guard in the current statechart with an event signaled from the polling object.