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

You use Object Constraint Language (OCL) in MDriven Designer to query your model and define declarative rules for classes, derived values, ViewModels, and state-machine behavior.

What OCL does in MDriven

OCL is a precise, declarative language for expressing rules and queries over a UML model. Declarative means that you describe the result or rule you require rather than the sequence of steps used to obtain it.

For example, a rule on a Person class can require an adult age:

self.age >= 18

An OCL expression is a query and must not have side effects: evaluating it must not change model data. When logic needs to change data, use EAL (Executable Action Language), which uses OCL-like syntax for actions.

Where you can use OCL

MDriven uses OCL throughout a model. The same expression language lets you describe behavior close to the model elements it concerns.

Model use What the expression describes Example purpose
Class constraint A rule that instances of a class must satisfy. Require self.age >= 18 for a Person.
Derived attribute or derived association How MDriven calculates a value or relationship from other model data. Calculate a percentage or expose the related objects that meet a condition.
ViewModel column or nesting definition Which values or objects a ViewModel presents. Show a collection of objects selected from the model.
ViewModel visible, enabled, style, or presentation expression How a UI element is shown or made available. Enable an action only when its required model condition is true.
Action enable expression Whether an action is available. Prevent a user from starting an action until required data exists.
State-machine guard Whether a transition is permitted. Allow a House to enter construction only when its address is not null.

For a walkthrough that combines OCL, methods, state-machine guards, the system prototyper, and ViewModels, see Documentation:OCL Editor, system prototyper and ViewModel.

Start here

Follow this sequence if you are new to OCL:

  1. Read Documentation:Learn OCL for the language concepts, MDriven use cases, and further learning topics.
  2. Work through Documentation:Part 1 OCL Common Expressions to see common expressions and the MDriven Debugger in use.
  3. Read Documentation:OCL Expressions to understand how OCL expressions are used in MDriven.
  4. Continue with operators and derivation after you can read a basic expression such as self.age >= 18.

Work with expressions and operators

OCL can navigate objects and collections, evaluate conditions, calculate values, and return selected objects. Expressions can be compact because operators can be chained.

For example, an expression can begin with all instances of a class, select matching objects, order the result, and take a subset. If a model contains four Car objects, applying a collection size operation to that collection returns 4. A selection expression evaluates a Boolean condition for each iterated object; an iterator variable identifies the current object.

Car.allInstances->select(oneCar | /* Boolean condition for oneCar */)

Learn the available expression forms in Documentation:OCL Expressions. For operator descriptions and examples, use Documentation:OCLOperators and Documentation:Part 2 OCL: Operators. Documentation:Turnkey session 7: Expressions explains how expressions query model objects in a Turnkey application, including collection operations such as allInstances, size, first, last, ordering, subsequences, and select.

Define derived values

Use a derivation expression when a value should be calculated from other model data rather than stored independently. For example, a derived percentage can be calculated from related values and then displayed wherever the derived attribute is used.

Study Documentation:Part 3 OCL: Derivation properties for an example-based introduction to derivation properties.

Validate and debug expressions

Write OCL against the names and types in your model. The editor reports type and name errors while you define model logic. For example, if a method parameter is named param2, an expression that refers to param2x cannot resolve that name and produces an error.

Use the system prototyper and MDriven Debugger to evaluate expressions against running model data. This is particularly useful when an expression navigates associations or returns a collection: you can inspect the result before relying on it in a ViewModel or derivation. Documentation:Part 1 OCL Common Expressions and Documentation:Part 2 OCL: Operators introduce the debugger in their examples.

OCL and EAL

Keep query rules and data-changing actions separate:

Language Use it when you need to Side effects
OCL Query model data or describe a constraint, derivation, guard, or UI expression. Not allowed.
EAL Implement logic that changes data, such as a method action. Allowed.

For example, use OCL to decide whether a state transition is allowed. Use EAL in an action method when that method must update the model. See Documentation:OCL Editor, system prototyper and ViewModel for a method example and the relationship between OCL-style expressions and action logic.

Choose the right next page

Your goal Go to
Learn the language and its role in MDriven Documentation:Learn OCL
Write or understand an OCL expression Documentation:OCL Expressions
Learn common expression patterns and debug them Documentation:Part 1 OCL Common Expressions
Navigate collections and use operators Documentation:OCLOperators and Documentation:Part 2 OCL: Operators
Define calculated properties Documentation:Part 3 OCL: Derivation properties
Use expressions in a Turnkey application Documentation:Turnkey session 7: Expressions
Combine OCL with ViewModels, methods, state machines, and prototyping Documentation:OCL Editor, system prototyper and ViewModel

See also