UML constraints

In UML models, a constraint is an extension mechanism that enables you to refine the semantics of a UML model element. A constraint refines a model element by expressing a condition or a restriction to which the model element must conform.

An example of a constraint is a condition such as an attribute having a specific value. A constraint must be enforced in the design of a system. You specify the condition or restriction in the body of the constraint.

Typically, constraints do not have names; instead, they are identified by the contents of their bodies. However, some commonly used constraints are identified by names so that the contents of their bodies do not have to be repeated. The XOR constraint is applied when more than one association has a common connection to one class.

You can write the body of a constraint in the following languages:
  • Natural languages such as English
  • Programming languages such as Java™
  • Mathematical notations
  • Object Constraint Language (OCL)

For more information about OCL, read the Unified Modeling Language (UML) specification that is available on the Object Management Group (OMG) Web site. In general, syntax for the body or body language properties is not enforced.

A constraint is displayed as text enclosed in braces ({}) and appears in a rectangle with a folded upper-right corner.

You can add constraints to your model for the following purposes:

The constraints in a model can appear in any type of UML diagram as well as in freeform diagrams.

Constraint validation

You can validate a constraint. The level of validation that occurs depends on the type of constraint being validated. There are three types of constraints:

  • A constraint with the «metaConstraint» stereotype applied
  • A constraint defined in a profile
  • A constraint that does not have the «metaConstraint» stereotype applied and is not defined in a profile

The syntax and the expression of a constraint are validated if the constraint has the «metaConstraint» stereotype applied to it or if it is defined in a profile.

Only the syntax of a constraint is validated if it does not have the «metaConstraint» stereotype applied to it and it is not defined in a profile.