Guard

A guard is an expression that may be linked to a Transition in a StateMachine. An OCL expression acting as the guard of a transition restricts the transition. An OCL expression acting as value of a guard is of type Boolean. The expresion is evaluated at the moment that the transition attached to the guard is attempted.

The placement of a guard in the UML metamodel is shown in Figure 33. The following well-formedness rule must hold.

In order to state the rule a number of additional operations are defined. The rule also defines the value of the contextual Classifier.
 

Well-formedness rules

[1] The statemachine in which the guard appears must have a context (A), that is a Classifier (B). The contextual classifier is the owner of the statemachine, and the type of the OCL expression must be Boolean.

context ExpressionInOcl
inv: not self.guard.transition.getStateMachine().context.oclIsUndefined() -- A
    and
    self.guard.transition.getStateMachine().context.oclIsKindOf(Classifier) -- B
    implies
        contextualClassifier =
            self.guard.transition.getStateMachine().context.oclAsType(Classifier)
    and
            self.bodyExpression.type.name = 'Boolean'

[2] Inside an guard the use of @pre is not allowed.

    context ExpressionInOcl
  inv: --