You can use self.constraints in OCL to inspect the constraints defined for an objectâs class and determine which constraints are currently broken.
What constraints returns
The constraints operator returns a collection of tuples. Each tuple represents one constraint and exposes both information about that constraint and its current broken state.
For example, use self.constraints when you need to show the descriptions of failed warning constraints or test whether an object has a failed error constraint.
| Tuple member | Description |
|---|---|
Name
|
The constraint name. |
Description
|
The constraint description. |
IsDeleteConstraint
|
true when the constraint is a delete constraint; otherwise false.
|
ErrorLevel
|
The constraintâs error level: #Information, #Warning, or #Error.
|
Broken
|
The evaluated and subscribed state of the constraint. Use this value to test whether the constraint is currently broken. |
Find broken constraints by error level
Use select to retain tuples with the error level and broken state you need. The following expression tests whether the object has no broken constraints at error level #Error:
self.constraints->select(c | (c.ErrorLevel = #Error) and c.Broken)->isEmpty
The expression returns true when no error-level constraint is broken. It returns false when at least one error-level constraint is broken.
For example, an object with a broken #Warning constraint but no broken #Error constraint still produces true from this expression.
Return descriptions of broken warnings
Use collect after filtering the tuples to return values from the matching constraints:
self.constraints->select(c | (c.ErrorLevel = #Warning) and c.Broken)->collect(c | c.Description)
This returns a collection containing the descriptions of the broken warning constraints. If no warning constraint is broken, the result is an empty collection.
Migration from the earlier result
Earlier versions returned an array of Boolean constraint results from constraints. The current operator returns tuples instead. When you need the earlier Boolean-style test, evaluate each tupleâs Broken member explicitly.
For example, this expression verifies that every constraint is not broken:
self.constraints->forAll(c | c.Broken = false)
Use Broken = false inside the iterator predicate; applying it to the collection itself does not test each returned tuple.
Choose the right operator
Use constraints when you need metadata such as the constraint name, description, delete-constraint flag, or error level together with its broken state.
Use brokenConstraints when you only need the names of the constraints that are broken for a class and object. For example:
Complaint.allInstances->first.brokenConstraints
