🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Turnkey session 7: Expressions
This page was created by Alexandra on 2017-01-22. Last edited by Wikiadmin on 2026-07-29.

You can use OCL expressions in MDriven Turnkey to query model objects, sort and limit collections, and select the objects that meet your rules.

What you learn in this session

This session introduces expressions written in OCL (Object Constraint Language). OCL is a functional language for working with the objects and collections in your model. It differs from imperative languages such as JavaScript and C#: instead of describing a sequence of changes, you describe the result you want from the model.

In a Turnkey application, expressions help determine which objects are brought into, shown in, or used by the application. For example, you can retrieve Cars, keep only Cars with a sufficiently large tire radius, sort them by registration number, and return only the first two results.

OCL is defined by the Object Management Group (OMG). It is designed for concise model queries and includes concepts from set theory.

Work with collections

A collection is a group of objects. Session 7 uses a Car collection to demonstrate common collection operations. You can chain operations: the result of one operation becomes the input to the next.

For example, if the model contains four Car objects, retrieving all Car instances and then asking for the size returns 4.

Operation What it does Car example
allInstances Retrieves all instances of a referenced class. Start with all Car objects in the model.
size Returns the number of objects in a collection. Four Cars result in 4.
first Returns the first object in the current collection. Return the first Car after the collection has been ordered.
last Returns the last object in the current collection. Return the last Car in the retrieved or selected collection.
orderBy Orders objects by a property. Order Cars by registration number so values such as B, C, A become A, B, C.
subSequence Returns a specified part of a collection. From four ordered Cars, return positions 1 through 2.

Chain operations deliberately

Apply ordering before taking a subsequence when you need a predictable subset. For example, order Cars by registration number and then take positions 1 and 2. If you take a subsequence before ordering, you are ordering only the already selected subset.

Use first and last after you have made the collection order meaningful. For example, order Cars by registration number before choosing the first Car if you mean the Car with the earliest value in that order.

Filter objects with select

select is a central collection operation. It evaluates a Boolean condition for every object in the collection and returns only the objects for which the condition is true.

For example, selecting Cars whose tire radius is greater than 5 may return two Cars. Changing the condition to tire radius greater than or equal to 5 may return three Cars, depending on the model data.

You can combine Boolean conditions with logical operators such as and, or, and not. For example, a filter can require both of these conditions:

  • The Car has a tire radius greater than or equal to 5.
  • The Car's Brand name is Volvo.

The result contains only Cars that satisfy both conditions. If you change the brand condition to another name, such as Odeon, the returned collection changes to match the new rule.

Name the loop variable

A loop variable is the name you give to the object currently evaluated by select. The variable appears before a pipe character (|), followed by the Boolean condition. Naming it makes longer expressions easier to read because you can explicitly refer to the current object and its related objects.

For a Car filter, you can name the loop variable oneCar. Conceptually, the condition then reads:

oneCar | oneCar.TireRadius >= 5 and oneCar.Brand.Name = 'Volvo'

Here, oneCar identifies the Car currently being tested. The expression keeps the Car only when both conditions are true.

Practice in the OCL tools

Use the OCL tooling shown in the session to test an expression and inspect its result as you build it.

  1. Start with all instances of a class, such as Car.
  2. Add size to confirm how many objects are currently returned.
  3. Add orderBy for a property such as registration number and inspect the new order.
  4. Add subSequence to return a smaller range of the ordered collection.
  5. Replace or extend the query with select and a Boolean condition.
  6. Introduce a descriptive loop variable when the condition refers to more than one property or association.

Build and test one operation at a time. This makes it clear whether an unexpected result comes from the initial collection, the order, the selected range, or the Boolean condition.

Watch the session

Watch MDriven Turnkey Part 7: Expressions for the Car examples, including ordering, subsequences, Boolean filtering, and loop variables.

For a broader introduction to OCL expressions and the MDriven Debugger, continue with Documentation:Part 1 OCL Common Expressions.

See also