UML constraints
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.
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:
- In models that depict software systems, constraints represent conditions or restrictions that you can find no other way to model.
- In models that depict time-critical software systems, constraints provide a statement about the relative or absolute value of time during an interaction.
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:
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.