You can use general OCL operators to query objects and collections in your model; this page helps OCL authors discover which operators apply in the current expression context.
Object Constraint Language (OCL) is a declarative, side-effect-free query language used throughout MDriven for expressions such as constraints, derivations, and ViewModel expressions. An operator is an operation that evaluates a value, an object, or a collection and returns a result.
Find operators in the OCL Editor
Use the OCL Editor to inspect the operators available for the type you are currently working with.
- Open the OCL Editor for the expression you are editing.
- Enter a class or expression that produces the value you want to work with.
- Inspect the operators offered for that class or value type.
- Add an operator, then continue the expression with the result it returns.
For example, when an expression produces a collection of Car objects, the available collection operators can help you count, filter, sort, or select objects from that collection. When an expression produces a Boolean value, use Boolean operators to combine or negate conditions.
Choose the operator category
Operators depend on the value before the operator. Use the following pages for the operator category you need.
| When your expression works with | Use | Example purpose |
|---|---|---|
| An object or a property value | OCL Operators | Evaluate or compare a property on an object. |
| A Boolean condition | OCL Boolean Operators | Combine conditions with and, or, or not.
|
| A collection of objects | OCL Collection Operators | Filter a collection, count its members, sort it, or retrieve an item. |
| A complete expression or a constraint | OCL Expressions | Place operators in a valid expression for a derivation, ViewModel, or constraint. |
Common operator patterns
Compare values
Use comparison operators to make a condition evaluate to true or false. For example, an age rule can require a person to be at least 18:
self.age >= 18You can combine conditions. This example accepts either of two gender values:
self.gender = 'Male' or self.gender = 'Female'See Boolean operators for logical combinations and OCL Operators for the available general operators.
Work with collections
A collection expression can be followed by collection operators. For example, allinstances obtains the instances of a referenced class, and size returns how many objects are in the resulting collection. If there are four cars, applying size returns 4.
Use first or last when you need one end of a retrieved collection. Use an ordering operator before selecting an item when the intended result depends on a property value. For example, ordering cars by registration number before taking a subsequence makes the chosen subset depend on that ordering.
Use select to retain only the objects whose condition is true. A selection uses a loop variable, the name assigned to each object while the condition is evaluated:
Cars->select(oneCar | oneCar.registrationNumber = 'ABC123')Here, oneCar is the loop variable. The pipe character (|) separates that variable from the Boolean condition. For collection-specific syntax and examples, see OCL Collection Operators.
Keep OCL expressions query-only
OCL evaluates information; it must not change data as it runs. Use OCL for queries, constraints, derivations, and expression-based UI behavior. When you need to change data, use EAL (Extended Action Language) rather than placing updates in an OCL expression.
Learn operators in context
For a guided explanation of operator configuration, derivation, and association tools, see Part 2 OCL: Operators. Turnkey session 7: Expressions shows how operators are combined when retrieving and shaping model data.
