forAll lets you test whether every item in a collection meets a Boolean condition; use it when writing an OCL expression that must validate all related objects or values.
Syntax
collection->forAll(variable | condition)collection is the collection to inspect. variable represents one element at a time, and condition must evaluate to true or false for that element.
The expression returns true only when every element validates the condition. It returns false when an element does not validate the condition.
Example
constraints returns information about a class's constraints, including whether each constraint is broken. Use forAll to verify that none of them are broken:
self.constraints->forAll(c | c.broken = false)In this expression:
self.constraintsis the collection being tested.crepresents one constraint in that collection.c.broken = falseis the condition that every constraint must satisfy.
The result is true when every constraint has broken set to false. If one constraint is broken, the result is false.
Compare forAll and exists
forAll tests whether every element matches a condition. exists is the reverse test: it returns true when an element matches the condition.
For example, to test whether at least one error-level constraint is broken, use exists:
self.constraints->exists(c | (c.ErrorLevel = #Error) and c.Broken)Use forAll when all items must comply. Use exists when finding one matching item is sufficient.
Use with a Set
You can apply forAll to a Set as well as to collections returned by model expressions. A Set contains no duplicates.
Set{14,22,12}->forAll(i | i > 10)This expression returns true because each value in the Set is greater than 10.
