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