Invariant

An invariant constraint is a constraint that is linked to a Classifier. The purpose of an invariant constraint is to specify invariants for the Classifier. An invariant constraint consists of an OCL expression of type Boolean. The expression must be true for each instance of the classifier at any moment in time. Only when an instance is executing an operation, this does not need to evaluate to true.

The placement of an invariant constraint in the UML metamodel is equal to the placement of a definition constraint, which is shown in Figure 30. The following well-formedness rule must hold. This rule also defines the value of the contextual Classifier.

Well-formedness rules

[1] The constraint has the stereotype <<invariant>> (A) and the constraint is attached to only one model element (B) the constraint is attached to a Classifier (C). The contextual classifier is the constrained element and the type of the OCL expression must be Boolean.

context ExpressionInOcl
inv: self.constraint.stereotype.name = 'invariant' -- A
    and
    self.constraint.constrainedElement->size() = 1 -- B
    and
    self.constraint.constrainedElement.any(true).oclIsKindOf(Classifier) -- C
        implies
            contextualClassifier =
                    self.constraint.constrainedElement->any(true).oclAsType(Classifier)
            and
                    self.bodyExpression.type.name = 'Boolean'

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

context ExpressionInOcl
inv: --