You can use brokenConstraints in OCL when you need the names of constraints currently violated by one model object.
Syntax
anObject.brokenConstraintsbrokenConstraints is an OCL operator on a single object. It returns a collection containing the names of the constraints that are broken for that object.
If no constraint is broken, the returned collection is empty.
Check one object
For example, to inspect the first Complaint object:
Complaint.allInstances->first.brokenConstraints| Expression part | Meaning |
|---|---|
Complaint.allInstances
|
Gets all instances of the Complaint class.
|
->first
|
Selects one Complaint object from that collection.
|
.brokenConstraints
|
Returns the names of constraints violated by the selected object. |
For example, if the selected complaint violates a constraint named MustHaveCustomer, the result includes MustHaveCustomer. If it violates no constraints, the result is empty.
What is evaluated
The result reflects constraints that are broken for the object at the time the expression is evaluated. This includes constraints defined in the model and implicit constraints from association-end cardinalities. For example, an association with cardinality 1..4 is broken when an object has no related objects at that association end.
Define the constraint and its user-facing severity in Training:Constraints. That page also explains delete constraints and the association delete rules evaluated when an object is deleted by MDriven.
Use the result
Use brokenConstraints when you need to inspect constraint failures programmatically in an OCL expression. The result contains only broken constraints; it does not include constraints that currently evaluate successfully.
When you need the constraint definitions associated with an object rather than only the names of broken ones, use the related constraints OCL operator.
Notes
- Apply
brokenConstraintsto one object, not directly to a collection. In the example,->firstreducesComplaint.allInstancesto one object before the operator is called. - The result can be empty. Treat an empty collection as meaning that no constraints are currently broken for that object.
- Constraints can also appear automatically in ViewModels. A ViewModel can opt out of showing them; see Training:Constraints.
