You can use collection operators in OCL expressions to filter, combine, compare, count, and calculate values from a collection of model objects or values.
A collection is a group of values or objects, such as all Products in a Category or all instances of a class. Collection operators take a collection as input and return a new value, object, or collection. In OCL, this is a transformation: evaluating an expression does not change the underlying data.
Find collection operators in the OCL-Editor
Use the OCL-Editor to inspect the operators available for the collection currently produced by your expression.
- Open the OCL-Editor in the context where you are writing the expression.
- Enter or select an expression that returns a collection. For example, start from an association that contains many objects, such as
self.Products. - Inspect the operators offered for that collection type.
- Select an operator and continue the expression. The available operators depend on the type of values in the collection.
For example, if self.Products is a collection of Product objects, you can use collection operations to select Products by a condition or derive a value from their attributes. If an expression produces strings, numbers, dates, or Booleans instead, the editor exposes operations for that resulting simple type.
For the complete operator reference, see Documentation:OCLOperators. For operators that apply beyond collections, see Documentation:OCL General Operators.
What you can do with a collection
Collection operators support common query and calculation tasks. The exact operators and result types are shown by the OCL-Editor for the expression you are editing.
| Goal | Example scenario | Result |
|---|---|---|
| Filter objects | Find Products in a Category whose Price is greater than 1000. | A collection containing only the matching Products. |
| Calculate from values | Calculate the total of product prices in a Category. | A numeric value derived from the collection. |
| Compare collections | Compare two collections to find objects present in one but not the other. | A collection representing the difference. |
| Test a collection | Determine whether two collections contain the same objects. | Compare their symmetric difference with an empty collection. |
| Provide an empty typed result | Return no Customer objects from one branch of conditional logic while another branch returns Customers. | An empty collection whose element type is Customer. |
See Documentation:Examples on collection operators for worked collection expressions and Documentation:Collections for collection concepts and further examples.
Collection results are flattened
When an expression navigates from a collection through an association, MDriven expands the individual results into one collection rather than returning a collection of collections. This reduction of nested collection structure is called flattening.
For example, if every Thing has a collection of Details, the expression Thing.allinstances.Details returns one set containing the Details from all Things. It does not return one separate Details collection for each Thing.
This matters when you navigate across a to-many association: you can continue to work with the combined result using collection operators.
Use the right expression context
OCL is functional: it takes input and produces a result without changing data. Use it for queries, derivations, constraints, and values displayed in a ViewModel.
For example, an OCL expression can select Products with a price above a threshold or calculate a total. It does not add or remove association links.
If you need to change class-object collections, such as adding Products to a Category association, perform that work in the Action Editor using mutable class-object collections. This distinction is important: raw-data collections are immutable in expressions, while class-object collections can be changed in the Action Editor.
Empty and comparison results
OCL is strongly typed, including when a collection has no elements. Use emptyList when you need an empty collection of a specific class type. For example, Customer.emptyList is an empty collection that remains known as a Customer collection, so later collection operations can use it safely.
To identify objects that occur in one of two collections but not both, use symmetricDifference. A practical equality check is to evaluate the symmetric difference of two collections: if the result is empty, the collections contain the same objects.
Learn by evaluating live data
Evaluate expressions against sample data in the debugger to see both the value and type returned at each step. Start with a collection, apply one operator, and inspect the result before extending the expression. This makes it easier to distinguish an object collection from a scalar result such as a number or Boolean.
For a live walkthrough of finding and testing collection operators, watch OCL Operators, explained with live data in MDriven.
