You use OCL expressions in MDriven Designer to query model objects, calculate values, enforce rules, and control ViewModel and user-interface behavior; this page is for modelers who need to write and validate those expressions.
What an OCL expression does
Object Constraint Language (OCL) is a declarative, object-oriented expression language. An expression evaluates information that is already present in your model. It returns a value, such as a Boolean, number, string, object, or collection; it does not change model data.
In MDriven, OCL is used throughout the model, including:
- Class constraints.
- Derivation rules for derived attributes and derived associations.
- ViewModel columns and nesting definitions.
- Visible, enabled, and style expressions for ViewModel columns.
- Object presentation expressions on classes.
- Action enable expressions.
- State-machine guards.
For example, a Boolean expression such as self.age >= 18 can express a rule that a Person must be at least 18 years old. A string expression can provide a readable object presentation, and a collection expression can select the objects to show in a ViewModel.
Start with context and result type
Every expression has a context: the object or location from which MDriven evaluates it. In an expression evaluated for a Car, self refers to that Car object.
Use dot navigation to follow an attribute or association from the current object. For a Car with a registration number and an associated BrandOfCar that has a name, an object presentation expression that returns text can be:
self.registrationnumber + ' - ' + self.brandofcar.nameThis expression has a String result:
self.registrationnumberreads an attribute on the current Car.self.brandofcarfollows an association to the related brand..namereads the name on that related object.+concatenates the string values and the literal' - '.
Write an expression whose result matches what its location requires. For example, a visibility or enable expression must evaluate to a Boolean, while a default string representation must evaluate to a String. MDriven validates expressions against the model, so navigating to a member that does not exist—for example self.brandofcar.name2 when the attribute is named name—is an error.
Query objects and collections
OCL is optimized for querying objects and collections. You can compose operations: one operation produces a result that the next operation uses.
The following examples use a Car class. Adapt the class and property names to your own model.
| Goal | Example expression | Result |
|---|---|---|
| Count every Car | Car.allinstances->size
|
The number of Car objects. |
| Select cars with a matching brand | oneCar.brandofcar.name = 'Volvo') | A collection containing only cars whose related brand is Volvo. |
| Take the first object in a result | Car.allinstances->first
|
The first object in the returned collection. |
| Choose between two values | if self.age >= 18 then 'Adult' else 'Minor' endif
|
A String determined by the condition. |
allinstances obtains all instances of the referenced class. Collection operations such as size, first, last, orderBy, subSequence, and select can then operate on the result. See Turnkey session 7: Expressions for a walkthrough of these query patterns and OCL operators for operator examples.
Use a loop variable with select
select evaluates a Boolean condition for each object in a collection. The name before the pipe character (|) is the loop variable: it represents the current object during that evaluation.
Car.allinstances->select(oneCar | oneCar.registrationnumber = 'ABC123')Here, oneCar is the loop variable. The expression retains each Car for which the condition is true. You can combine conditions with Boolean operators, for example:
Car.allinstances->select(oneCar |
oneCar.registrationnumber = 'ABC123' or oneCar.brandofcar.name = 'Volvo')For Boolean syntax and combinations, see OCL Boolean Operators. For expression separators and related syntax, see OCL operators expressions separator.
Use conditional expressions
Use if ... then ... else ... endif when an expression must return one value for a true condition and another for a false condition. Both branches must return compatible types so that the complete expression has a defined result type.
if self.age >= 18 then
'Adult'
else
'Minor'
endifThis is an expression, not an imperative statement: it evaluates to either 'Adult' or 'Minor'. Read If else endif for the complete conditional-expression rules.
Keep OCL free of side effects
OCL is a query language. Do not use an OCL expression to alter data while it is evaluated. This matters because MDriven can evaluate expressions for validation, derived values, presentation, ViewModel state, and queries.
When you need to change data, use EAL (Extended Action Language). EAL uses the same syntax as OCL where appropriate, but it is intended for actions that update data. Keep the distinction clear:
| Language | Use it for | Example outcome |
|---|---|---|
| OCL | Reading, calculating, filtering, validating, and determining UI state. | Return the cars that match a condition. |
| EAL | Actions that change model data. | Update data as part of an action. |
Validate expressions in MDriven Designer
MDriven validates expressions against the model when you save the model. A red dot indicates a validation error.
- Enter the expression in the property that requires it, such as a default string representation, a derived property, or a ViewModel expression.
- Save the model.
- Check for red dots that identify validation errors.
- Select the error to locate the invalid expression.
- Correct the model member name, navigation, syntax, or result type, and save again.
For example, if self.brandofcar.name2 is invalid because BrandOfCar has no name2 member, change it to the actual member name, such as self.brandofcar.name, then save to revalidate.
The default string representation is used when MDriven needs a string form of an object without an explicit attribute. A Car representation such as self.registrationnumber + ' - ' + self.brandofcar.name makes the object easier to identify in the debugger and autoforms.
Learn OCL in order
- Start with Learn OCL for the language overview and where MDriven uses it.
- Work through Part 1 OCL Common Expressions to practice common syntax and use the MDriven Debugger for immediate results.
- Continue with OCL operators to learn collection, numeric, string, and other operations.
- Use Turnkey session 7: Expressions for examples of querying and transporting model objects into an application.
- Browse the OCL documentation landing page when you need a topic-specific operator or debugging guide.
You can also watch the expression walkthrough for an example of `self`, association navigation, string concatenation, object presentation, and model validation.
