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

You can use Object Constraint Language (OCL) in MDriven Designer to query your model, calculate values, validate rules, and control how a ViewModel presents and enables information.

What OCL is

OCL is a declarative expression language defined for UML and other MOF-based models. In MDriven, you use it to describe what value, condition, or set of objects you need; the expression does not describe a sequence of updates.

OCL is read-only and must have no side effects. An expression can inspect objects, navigate associations, filter collections, calculate a result, or return a Boolean value, but it must not change data. When you need an expression that performs changes, use EAL (Extended Action Language), which uses OCL-like syntax for actions.

For the language overview and the full topic structure, start at Documentation:OCL. For expression syntax and further examples, see Documentation:OCL Expressions.

What you can do with OCL in MDriven

Use OCL wherever MDriven expects a value or condition derived from the model.

Use What the expression returns Example purpose
Class constraint A Boolean value Require a person's age to be at least 18.
Derived attribute The calculated attribute value Calculate a total from related values.
Derived association The related objects that meet a rule Derive a set of objects through model navigation.
ViewModel column or nesting definition A value or collection to show Show a selected set of model objects in the user interface.
ViewModel Visible or Enable expression A Boolean value Show or enable a column only when a condition is true.
ViewModel style expression Style information Apply presentation based on model data.
Object presentation A presentation value Define how an instance is represented to the user.
Action Enable expression A Boolean value Enable an action only when the current state permits it.
State-machine guard A Boolean value Allow a transition only when its guard condition is true.

Read expressions in their model context

An OCL expression is evaluated from a context: the current object, an association, or a collection of objects. The keyword self refers to the current object.

For a class named Person with an age attribute, a constraint can state that every person must be an adult:

context Person inv: self.age >= 18

This expression returns true when the current person's age is 18 or higher, and false otherwise. The context and invariant form are useful for expressing a rule on a class. See Documentation:OCLOperators for more operator examples.

Work with collections

Many MDriven expressions return a collection. Collection operations let you inspect, filter, order, and reduce that result.

For example, allinstances starts from all instances of a referenced class. You can then chain collection operations:

Car.allinstances->size

If four Car objects exist, the expression returns 4. To return the first or last item from a retrieved collection, use the corresponding collection operation. To sort a collection, use orderBy; to take only part of the result, use subSequence.

Filtering is one of the most common collection tasks. select evaluates a Boolean condition for each object and keeps the objects for which the condition is true. Name the loop variable before the pipe character (|) and use that name in the condition:

Car.allinstances->select(oneCar | oneCar.registrationNumber = 'ABC123')

Here, oneCar is the loop variable. In a more complex filter, combine Boolean conditions with and, or, and not.

Operation Use it to Example result
size Count objects in a collection The number of cars
first / last Pick an endpoint from a collection The first or last retrieved car
orderBy Sort objects by a property Cars ordered by registration number
subSequence Return a portion of a collection A selected range of returned cars
select Keep objects matching a Boolean rule Cars with a matching registration number

Learn these and other operators in Documentation:Part 2 OCL: Operators and Documentation:OCLOperators.

A practical learning path

  1. Start with a small expression in the context of one class. Use self to read an attribute and return a value or Boolean result.
  2. Add model navigation when you need information from associated objects.
  3. When the result is a collection, apply an operation such as size, select, orderBy, or subSequence.
  4. Test the expression in the MDriven Debugger so that you can inspect the result while you model.
  5. Place the tested expression in the relevant model location: a constraint, derivation, ViewModel definition, enable or visible condition, style expression, action enable expression, or state-machine guard.
  6. If the requirement changes data rather than querying it, do not put the update in OCL; use EAL instead.

The OCL sessions introduce this workflow through examples and the MDriven Debugger. Begin with Documentation:Part 1 OCL Common Expressions, then continue with Documentation:Part 2 OCL: Operators and Documentation:Part 3 OCL: Derivation properties.

Choose the right expression type

Before writing an expression, identify the result MDriven expects.

If you need to... Write an expression that returns... Example
Validate a rule, control visibility, enable an action, or guard a transition A Boolean self.age >= 18
Display or derive one calculated value A single value A calculated total or presentation value
Display or derive related objects A collection of objects A filtered collection produced with select
Change model data EAL, not OCL An action that updates an object

Continue learning

OCL is a functional language optimized for querying the model. If you are used to imperative languages such as C# or JavaScript, focus first on evaluating expressions over objects and collections rather than on issuing update statements. If you are used to SQL, use Documentation:Understanding OCL with reference to SQL to understand the difference in query perspective.

Use Documentation:OCL Expressions for expression-oriented documentation, and Documentation:Turnkey session 7: Expressions for a walkthrough of querying objects and applying collection operators in a Turnkey context.

See also

OCL and EAL

OCL and EAL

OCL expressions must be without side effects. OCL is a query language and is not expected to change data while an expression is evaluated.

MDriven uses OCL in, among other places, class constraints, derivation rules, ViewModel definitions and conditions, object presentation, action-enable expressions, and state-machine guards.

When model logic must change data, MDriven uses EAL (Extended Action Language). EAL uses the same syntax as OCL, but is used for actions where changing data is appropriate.

For example, class methods can contain implementation logic in the Action Editor Body. In that context, side effects are allowed so that the method can change data.

Use Language behavior
Constraints, queries, derivations, and conditions Use OCL expressions without side effects.
Logic that must change data Use EAL in the relevant action context.

When writing model logic, first decide whether the expression only reads, calculates, or evaluates a rule, or whether it must change data. Use OCL for the first case and EAL for the second.