Precondition

A precondition is a constraint that may be linked to an Operation of a Classifier. The purpose of a precondition is to specify the conditions that must hold before the operation executes. A precondition consists of an OCL expression of type Boolean. The expression must evaluate to true whenever the operation starts executing, but only for the instance that will execute the operation.

The placement of a precondition in the UML metamodel is shown in Figure 31. 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 <<precondition>> (A), and is attached to only one model element (B), and to a BehavioralFeature (C), which has an owner (D). The contextual classifier is the owner of the operation to which the constraint is attached, and the type of the OCL expression must be Boolean

context Expression
inv: self.constraint.stereotype.name = 'precondition'    -- A
      and
      self.constraint.constrainedElement->size() = 1     -- B
      and
      self.constraint.constrainedElement->any(true).oclIsKindOf(BehavioralFeature) -- C
      and
      self.constraint.constrainedElement->any(true)          -- D
                            .oclAsType(BehavioralFeature).owner->size() = 1
            implies
                contextualClassifier =
                        self.constraint.constrainedElement->any(true)
                                .oclAsType(BehavioralFeature).owner
      and
            self.bodyExpression.type.name = 'Boolean'

[2] Inside a precondtion constraint the use of @pre is not allowed.

context ExpressionInOcl
inv: --