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.
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:
- Open the OCL Editor.
- Type the name of a model class, for example
Complaint. - Use the editor's available operations to find applicable operators.
For the general operator overview, see Documentation:OCL General Operators.
