Add a Constraint to a Model

Constraints are restrictions you specify in the Object Constraint Language (OCL). Specify preconditions, postconditions, invariants, and guards to describe logical tests that your software must satisfy. Define queries and derivations to relate values in your software to attributes of other elements.

Consider the following class diagram, which shows a partial model of a software system that manages library loans. A library member can take out a variable number of loans.

A screenshot of Axminster, showing a class diagram with Member, Loan, and Book classes.

It’s an invariant of this model that the number of loans any member has shouldn’t be greater than the maximum allowed. To add this constraint to the model, follow these steps:

  1. Select the Member class in the diagram canvas.

  2. Navigate to the Constraints inspector, by clicking the shield icon in the inspector view.

  3. Under Add Constraint, use the Kind button to choose Invariant.

  4. Enter the constraint self.loans->size() <= self.maxLoans in the text field.

  5. Click Add.

The Constraints inspector shows that the Member class has the new constraint.

A screenshot of Axminster, showing a class diagram in which the Member class is being inspected. The class has an invariant constraint.

As you type your constraint into the inspector, Axminster checks the syntax and colors your constraint red if it’s invalid. Until you fix the syntax, you cannot add your constraint to the model. When you add a constraint, Axminster checks that the types you use are all part of your model or OCL basic types (String, Boolean, Integer, or Real).