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

You can use allInstances in OCL to retrieve every existing instance of a model class, including instances of its subclasses.

Syntax and return value

ClassName.allInstances
Item Description
Receiver A class in your model, such as Complaint.
Return type Set{T}, where T is the receiving class.
Result All existing instances of that class and instances of classes that inherit from it.

Retrieve all instances of a class

For example, this expression returns the set of all existing Complaint objects:

Complaint.allInstances

You can continue with collection operations to select or inspect objects. For example, the following expression takes one Complaint from the returned set and returns its broken constraint names:

Complaint.allInstances->first.brokenConstraints

See brokenConstraints for details about inspecting violated constraints.

Here are some example expressions and their results:

Expression Result
Thing.allInstances all the Thing object instances currently in your system - if only in DB objects will be loaded

You can combine allInstances with collection operations for more specific queries:

Operators Description
Thing.allinstances Gives you a list of all things
Thing.allinstances->select(someInt>3) Only things with someInt bigger than 3
Thing.allinstances->select( (someInt>3) and (someInt<6)) Only things with someInt bigger than 3 but less than 6. Notice the extra parenthesis to or the Boolean expressions together
Thing.allinstances->select(x|x.someInt>3) Here we introduce the loop variable x. We separate the definition of x from the usage of x with the pipe sign “|”. Loop variables are optional but if names are unique – but you will need to use them to give precision or to if you want to perform operations on the loop context itself.
Thing.allinstances.Details Gives a list of all Detail objects that are connected to a Thing. The Detail objects that float around without a Thing will not be on the list
Thing.allinstances.Details.Attribute1 A list of nullable strings from the contents of the details attribute1. Note that OCL is null-tolerant – you do not need to check if the Details exist or not – the language handles null checks for you.
SubClassThing1.allinstances.Details Inherited features of classes are directly accessible
Thing.allInstances->select(x|x.safeCast(SubClassThing1). OnlyAvailableInSubClass='Hello') Filtering on Specialization is done with an operator SafeCast. This is null safe so for all objects that do not fit the profile the expression returns false

Inheritance is included

allInstances includes objects of inherited classifiers (subclasses). If Fruit is a superclass and Mango inherits from it, Fruit.allInstances includes both Fruit instances and Mango instances.

Use the type operations superTypes and allSuperTypes when you need to examine the inheritance hierarchy itself rather than retrieve object instances.

Choose the correct operator

allInstances retrieves all existing objects of the class. It is different from allLoadedObjects, which returns only objects currently loaded in memory and does not load additional objects from the database.

Need Use
All existing objects of a class, including subclass objects ClassName.allInstances
Only objects currently loaded in the application ClassName.allLoadedObjects
Objects as they existed at a historical point in time ClassName.allInstancesAtTime(timeStamp)

Find operators in the OCL Editor

To explore operations available for a class:

  1. Open the OCL Editor.
  2. Type the name of a model class, for example Complaint.
  3. Use the editor's available operations to find applicable operators.

For the general operator overview, see Documentation:OCL General Operators.

See also