As an example, we have modelled a simple coach company in UML. In this design, a coach has a specific number of seats and can be used for regular trips or for private trips. In regular trips, the tickets are bought individually. In private trips, the whole coach is rented for a trip. The model is shown in UML notation in the next figure.

Coach company model.

The example provides a specific UML model, and the queries are applied to its instances. We are going to consider the following instance.

Object diagram defined as an instance of the model defined in the previous figure.

OCL invariants permit a more precise definition of the above model by adding constraints. For instance, we can indicate that overbooking is not allowed in a regular trip by means of the following invariant:

context Coach: inv: self.trips -> select( t:Trip | t.oclIsTypeOf(RegularTrip)) -> forAll(r:Trip | r.oclAsType(RegularTrip).passengers -> size() <= r.coach.numberOfSeats)

Next steps will guide the user to introduce and evaluate this ocl invariant over the instance model:

- Build the model using the reflective model editor of EMF. As a result a file with name
*coach_model.ecore*is created. - Build the instance model using the reflective model editor too. As a result a file with name
*coach_instance.xmi*is created. - Introduce the data trough the OCLEditor interface.
- Evaluate the OCL invariant by means of pressing the
*Execute OCL*option of the pop-up menu.

Coach Company Model.

Object diagram defined as an instance of the model defined in the previous figure.

Inserting the data of the OCL invariant (I): Model.

Inserting the data of the OCL invariant (II): Context.

Inserting the data of the OCL invariant (III): Invariant Group.

Inserting the data of the OCL invariant (IV): Invariant.

Inserting the data of the OCL invariant (V): Invariant expression.

Evaluating the OCL invariant.