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.
[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: --