UML Constraint
A constraint is a packageable element which represents some condition, restriction or assertion related to some element (that owns the constraint) or several elements. Constraint is usually specified by a Boolean expression which must evaluate to a true or false. Constraint must be satisfied (i.e. evaluated to true) by a correct design of the system. Constraints are commonly used for various elements on class diagrams.
In general there are many possible kinds of owners for a constraint. Owning element must have access to the constrained elements to verify constraint. The owner of the constraint will determine when the constraint is evaluated. For example, operation can have pre-condition and/or a post-condition constraints.
Constraint could have an optional name, though usually it is anonymous. A constraint is shown as a text string in curly braces according to the following syntax:
constraint ::= ‘{‘ [ name ‘:’ ] boolean-expression ‘}’
UML specification does not restrict languages which could be used to express constraint. Some examples of constraint languages are:
- OCL
- Java
- some machine readable language
- natural language
OCL is a constraint language predefined in UML but if some UML tool is used to draw diagrams, any constraint language supported by that tool could be applied.
For an element whose notation is a text string (such as a class attribute), the constraint string may follow the element text string in curly braces.
Bank account attribute constraints- non empty owner and positive balance.
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 UML 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 curly braces.
_Account owner is either Person or Corporation,
{xor} is predefined UML constraint.
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.
The constraint string may be placed in a note symbol (same as used for comments) and attached to each of the symbols for the constrained elements by a dashed line.
Bank account constraints - non empty owner and positive balance.
Association