🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Part 2 OCL: Operators
This page was created by Alexandra on 2016-12-21. Last edited by Wikiadmin on 2026-07-29.

Use this session to learn how to query model objects and collections with OCL operators; it is for MDriven users who already know the common OCL expression basics.

Object Constraint Language (OCL) is MDriven's declarative query language for describing rules and retrieving information from a model. An OCL expression has no side effects: evaluating it must not change data. When you need to change data, use Extended Action Language (EAL), which uses the same syntax family for actions.

What you will learn

This second OCL session focuses on operators: expressions that inspect an object, evaluate a condition, or transform a collection of objects.

After working through the session, you should be able to:

  • Start with a class or an existing collection of objects.
  • Apply collection operators such as obtaining a collection's size, first item, last item, ordering, and a subsequence.
  • Filter a collection with select and a Boolean condition.
  • Name and use a loop variable in an iterator expression.
  • Combine conditions with Boolean operators such as and, or, and not.
  • Recognize when an expression navigates an association, returns a collection, or returns one value.

Operator categories

Use the OCL Editor to inspect the operators available for the type of expression you have entered. The available operators depend on whether the expression represents one object, a value, or a collection.

Category What it does Example use More detail
General operators Work on values and object expressions. Compare an age with age >= 18. Documentation:OCL General Operators
Boolean operators Combine or negate conditions that evaluate to true or false. Keep a car when two conditions are both true. Documentation:OCL Boolean Operators
Collection operators Inspect, filter, order, or take part of a collection. Count cars, select matching cars, or take the first two ordered cars. Documentation:OCL Collection Operators

For the complete operator reference and additional constraint examples, see Documentation:OCLOperators.

Work through a collection expression

A common OCL task is to retrieve objects, filter them, then apply another collection operator to the result. For example, a collection of cars can be counted with an expression that obtains all instances and then applies size. If there are four cars, the result is 4.

  1. Begin with the class or collection you want to inspect. For example, start with all instances of a car class.
  2. Apply select to retain only objects that meet a condition.
  3. Give the iterated object a meaningful loop variable after the pipe character (|).
  4. Write the Boolean condition using that variable.
  5. Apply a further operator to the returned collection, such as ordering, selecting the first or last object, counting it, or taking a subsequence.

The general form is:

collection->select(oneObject | booleanCondition)

Here, oneObject is the loop variable. OCL evaluates booleanCondition once for every object in collection. Objects for which the condition is true are included in the result.

For example, if cars is a collection and each car has a registration number, the condition can refer to the current car through the loop variable:

cars->select(oneCar | oneCar.registrationNumber = 'A')

Use a descriptive variable such as oneCar when the condition navigates several properties or associations. This makes a longer expression easier to read and avoids ambiguity about which object a property belongs to.

Combine filtering, ordering, and limiting

Collection operators can be chained. A useful sequence is:

  1. Filter the objects with select.
  2. Order the matching objects by a property.
  3. Take a subsequence when the result should contain only a range of items.

For example, if registration numbers are B, C, A, and D, ordering by registration number puts A before B, C, and D. Taking a subsequence after ordering lets you work with only part of that ordered result, such as the first two items. Apply ordering before taking the subsequence when the selected range must be predictable.

first and last return the first and last object from a retrieved or selected collection. Their result depends on the collection order, so order the collection first when the intended first or last item is based on a property.

Use Boolean conditions deliberately

select needs a Boolean expression. You can combine conditions with and, or, and not.

For example, this constraint accepts a person only when the age is at least 18:

self.age >= 18

This condition accepts either of two gender values:

self.gender = 'Male' or self.gender = 'Female'

Keep each condition focused on one question, then combine them. For example, use and when both conditions must hold and or when either condition is sufficient. See Documentation:OCL Boolean Operators for Boolean operator guidance.

Where to use these expressions

In MDriven, OCL expressions can define constraints, derived attributes, derived associations, and ViewModel behavior. A ViewModel can use OCL for columns and nesting definitions, and for visible, enabled, and style expressions. OCL can also provide object presentation, action-enable expressions, and state-machine guards.

For example, a derived attribute can calculate a value from related objects, while a ViewModel visibility expression can determine whether a column is shown. In both cases, the expression queries model data; it does not update it. Continue with Documentation:OCL Expressions for the places where MDriven uses OCL, and with the next session on derivation properties when you are ready to define calculated model values.

Suggested learning path

  1. Review Documentation:Part 1 OCL Common Expressions if you need the expression fundamentals.
  2. Use this session to practice operators on objects and collections.
  3. Look up exact operators in Documentation:OCLOperators and inspect context-sensitive choices in the OCL Editor.
  4. Continue to derivation properties after you can read and compose collection queries.

See also