🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCL General Operators
This page was created by Stephanie on 2025-03-10. Last edited by Wikiadmin on 2026-08-17.

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.

  1. Open the OCL Editor for the expression you are editing.
  2. Enter a class or expression that produces the value you want to work with.
  3. Inspect the operators offered for that class or value type.
  4. 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.

The OCL Editor interface displays available operators for your current type.

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 >= 18

You 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.

Common collection operators are summarized in the following table.

Operators Description
allInstances All the objects of the class
allinstancesAtTime All the currently loaded instances
allLoadedObjects All objects currently in memory, excluding deleted objects
allStates Meta information about available states in state machines the class may contain
allSubClasses Meta information on all the sub-classes this class has
allSuperTypes Meta information on all the super-classes – in inheritance order the class has
ASCII The set of symbols and characters used to specify constraints on objects in software development.
associationEnds Returns a collection of strings with association names for the class
associationEndsWithType Returns a collection of strings with association names and type information for the class
asString The string representation of the class – the asString operation is available on everything
asTaJson Returns a json string defined by the ViewModel
atTime Gives the read-only representation of a versioned object as it was at time X.
Attributes Meta information about what attributes the class has
BigEndianUnicode A data encoding format for Unicode characters
brokenConstraints Returns a collection of the names of broken constraints for the class and object.
canAccess Check ViewModels Access expression for root
Constraints Meta information on what constraints the class has
count Returns how many times the object is in the collection self.
Empty Used to check if a collection or string is empty or not
emptylist Returns an empty list typed to hold objects of the class
existing The existing operator returns true if the object hasn't been deleted.
externalId Returns the external ID for the object operated on.
format Used to create formatted strings based on a given pattern.
IsDirty Returns true if the objects have been changed since the last save.
IsNew IsNew operator returns true if the object has never been saved to the database.
isDirtyMember Called on a class attribute to check if this attribute has changes pending to be saved.
isNull Returns true if the value is null (not assigned any value).
maxValue Gets the maximum value in a collection of values
minValue Gets the minimum value in a collection of values
nullValue A typed null value
objectFromExternalId An external identity will be resolved to the object
oclAsType The type of the class
oclGetStates Gets a list of the objects all state machines currently state as strings.
oclIsInvalid Returns true if self is equal to *invalid*.
oclIsKindOf This is to if a class is a subclass or as the class itself and not unrelated
oclIsTypeOf Returns true if
OnCreate Is often called when a new object has been created.
OnUpdate Is called just before the object is saved to the database.
oclIsUndefined Returns true if self is equal to invalid or null.
OclType When used on an object, you get the type of the object.
random Used to generate a random value within a specified range or collection
safeCast Used to safely cast an object to a subtype.
sqlpassthrough Calls a stored procedure
sqlpassthroughobjects Return a primary key of SomeClass. This can be combined with other Ocl-PS via ->intersection or union.
superTypes Returns the set of all direct supertypes of the type.
TaggedValue Meta information on tagged values set in the class
TaggedValueOnFeature Meta information on Tagged values set on a named feature in the class
Typename The type name as a string
ViewModels A tuple with the ViewModels for this class a members

See also: OCLOperators

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.

See also