Skip to content

Constraints

Tobias Spears edited this page Mar 24, 2020 · 13 revisions

Back to modelling

Back to diagram design


What are constraints

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" (ref. ISO19103). Constraints are important as they add precision to the models, for instance by setting conditions under which an attribute is mandatory. Expressing constraints at the conceptual level is important for validation, management and maintenance of data.

ISO19109 has some examples of constraints:

  • A constraint may specify an acceptable combination of attribute values in one or more feature instances (that may belong to different types);
  • A constraint may restrict the cardinality of an association between feature instances;
  • A constraint may require that if the real-world phenomenon is of a certain size, the feature instance be represented by a certain subtype of GM_Object;
  • The behaviour of a feature, as defined in a feature operation, may be restricted by a constraint.

How to express constraints

Constraints in UML can be expressed in natural language or in a machine readable language, normally Object Constraint Language (OCL). Recommendation 14 in ISO19103 states that "Constraints should be expressed using the Object Constraint Language (OCL) as defined in OCL 2.0. In addition, constraints should be expressed in natural language in the accompanying documentation of the model". The OCL expression is important for implementation (but not mandatory), while the natural language expression is important for human understanding of the model.

Write the natural language first

Writing constraints in OCL is not very intuitive, so it is recommended to write the natural language first, and then add the OCL expression based on this.

How to write OCL

The use of constraints in implementations

For the use of constraints in implementations, it should be noted that ISO19136 (GML) - annex E states that "All OCL constraints are ignored. The assessment of the validity of the instance model with respect to these constraints is the task of the application processing the GML instances. NOTE: The Schematron language may be used to express OCL constraints as part of the XML Schema representing the GML application schema.". Which means that the GML application schemas does not include the constraints, they have to be handled in Schematron or in the applications used for processing data.

ShapeChange can parse OCL constraints that are defined in a schema. It supports the derivation of Schematron rules from OCL constraints. See the [OCL Conversion to Schematron[(http://shapechange.net/targets/xsd/extensions/ocl/) page for further information.

Working with constraints in Enterprise Architect

Describe constraints at the class level

  • Constraints should always be described in the properties for the class, to make sure the constraint always follows the class in diagrams and in other uses and documentations of the model.
  • It is possible to write constraints at the attribute level as well, but this is not recommended. There does not seem to be a way to display constraints attached to attributes in diagrams.
  • It is also possible to write constraints as diagram notes, but this should not be done, as the constraint will then be a part of the diagram, not of the class.

Constraints on a class/enumeration/data type are accessed from the properties dialog (Alt + Enter), under Constraints.

  • To write an OCL expression: select Type = OCL. The natural language expression can be added
    • as the name of the constraint
    • in the constraint itself, using the comment syntax: /* ... */
  • To write a pure natural language expression: select Type = Invariant

Do not add the context part, since that is implied via the GUI.

When saving a constraint with type OCL, EA will perform a (simple) syntax validation and will display a message about whether the OCL constraint is valid or not according to EA.

Show constraints in diagrams:

Constraints may be presented in two different ways in UML diagrams: as a note that is attached to the element, or within the element representing the class. Normally, it is recommended to present the constraints within the class. The figure below shows the difference between constraints on the class itself, and as a note.

Constraints

  • To show constraints for all elements in a diagram: Select Diagram (or F5) - Properties - Elements and check Constraints on.
  • To show constraints for a single element: Select Element - Feature and Compartment Visibility (or Ctrl - Shift - Y) and check Constraints on.

In some cases it may be useful to show one particular constraint as a note attached to the element. This can be done by adding a note to the diagram and linking it to the element. Right click on the link, and select Link this note to an element feature. From the dialog, select Feature Type = Constraint, and select the constraint from the Feature list.

Constraint as note

Clone this wiki locally