Previous UML Classes Table of Contents UML Packages Next

7.3.10 Constraint


   A constraint is a condition or restriction expressed in natural language text or in a machine readable language for the purpose of declaring some of the semantics of an element.



    PackageableElement (from Kernel ) on page 111


   Constraint contains a ValueSpecification that specifies additional semantics for one or more elements. Certain kinds of constraints (such as an association xor constraint) are predefined in UML, others may be user-defined. A user-defined Constraint is described using a specified language, whose syntax and interpretation is a tool responsibility. One predefined language for writing constraints is OCL. In some situations, a programming language such as Java may be appropriate for expressing a constraint. In other situations natural language may be used.

   Constraint is a condition (a Boolean expression) that restricts the extension of the associated element beyond what is imposed by the other language constructs applied to that element.

   Constraint contains an optional name, although they are commonly unnamed.


   No additional attributes


   • constrainedElement: Element[*] The ordered set of Elements referenced by this Constraint.

Issue 9086 - change to subsets constraint

   • / context: Namespace [0..1] Specifies the Namespace that is the context for evaluating this constraint. Subsets NamedElement ::namespace.

Issue 8090 - Correct multiplicity.

   • specification: ValueSpecification [1] A condition that must be true when evaluated in order for the constraint to be satisfied. Subsets Element::ownedElement.


Issue 8509 -capitalize ‘boolean’

   [1] The value specification for a constraint must evaluate to a Boolean value.

   Cannot be expressed in OCL.

   [2] Evaluating the value specification for a constraint must not have side effects.

   Cannot be expressed in OCL.

   [3] A constraint cannot be applied to itself.

   not constrainedElement->includes(self)


   A Constraint represents additional semantic information attached to the constrained elements. A constraint is an assertion that indicates a restriction that must be satisfied by a correct design of the system. The constrained elements are those elements required to evaluate the constraint specification. In addition, the context of the Constraint may be accessed, and may be used as the namespace for interpreting names used in the specification. For example, in OCL ‘self’ is used to refer to the context element.

   Constraints are often expressed as a text string in some language. If a formal language such as OCL is used, then tools may be able to verify some aspects of the constraints.

   In general there are many possible kinds of owners for a Constraint. The only restriction is that the owning element must have access to the constrainedElements.

   The owner of the Constraint will determine when the constraint specification is evaluated. For example, this allows an Operation to specify if a Constraint represents a precondition or a postcondition.


   A Constraint is shown as a text string in braces ({}) according to the following BNF:

   <constraint> ::= ‘{‘ [ <name> ‘:’ ] <Boolean-expression> ‘ }’

   For an element whose notation is a text string (such as an attribute, etc.), the constraint string may follow the element text string in braces. Figure 7.33 shows a constraint string that follows an attribute within a class symbol.

   For a Constraint that applies to a single element (such as a class or an association path), the constraint string may be placed near the symbol for the element, preferably near the name, if any. A tool must make it possible to determine the constrained element.

   For a Constraint that applies to two elements (such as two classes or two associations), the constraint may be shown as a dashed line between the elements labeled by the constraint string (in braces). Figure 7.34 shows an {xor} constraint between two associations.

*Presentation Options

   The constraint string may be placed in a note symbol and attached to each of the symbols for the constrained elements by a dashed line. Figure 7.35 shows an example of a constraint in a note symbol.

   If the constraint is shown as a dashed line between two elements, then an arrowhead may be placed on one end. The direction of the arrow is relevant information within the constraint. The element at the tail of the arrow is mapped to the first position and the element at the head of the arrow is mapped to the second position in the constrainedElements collection.

   For three or more paths of the same kind (such as generalization paths or association paths), the constraint may be attached to a dashed line crossing all of the paths.


   Figure 7.33 - Constraint attached to an attribute

   Figure 7.34 - {xor} constraint

Figure 7.35 - Constraint in a note symbol


employee * employer 0..1