🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Constraints and Validation (OCL)
This page was created by Wikiadmin on 2026-07-29. Last edited by Wikiadmin on 2026-07-29.

You can define and evaluate OCL constraints when you need business rules on a class, and this page is for MDriven modelers who need to validate those rules and present their result to users.

Define a constraint

A constraint is a rule on a class. Its OCL expression must evaluate to true; when it evaluates to false, the constraint is broken.

  1. Select the class, such as Car.
  2. In the Property Inspector, find Constraint.
  3. Click ... and select Add a new Constraint.
  4. Enter the OCL expression in the constraint body.
  5. Enter a description that explains the rule to the user, such as Cars MUST have a Brand!.
  6. Choose whether a broken constraint is Information, Warning, or Error.

The Description can use <asString> to include the broken object's string representation.

Validate the model and inspect broken constraints

OCL is a query language without side effects. When you save the model, MDriven validates OCL expressions; an invalid expression is shown with a red dot. For example, an expression that refers to Name2 when the related class has no such member is shown with a red dot.

You can inspect a constraint in the debugger by changing an object so that its expression becomes false. The constraint is then broken.

Constraints also appear in ViewModels automatically. You can opt them out for an individual ViewModel when you do not want that ViewModel to show the constraint result.

Query constraint status in OCL

Use constraints to retrieve constraint metadata and its evaluated, subscribed broken state. The returned tuples include the constraint name, description, delete-constraint status, error level, and broken state.

For example, this expression returns true when no Error-level constraint is broken:

self.constraints->select(c|(c.ErrorLevel = #Error) and c.Broken)->isEmpty

This expression returns the descriptions of broken Warning-level constraints:

self.constraints->select(c|(c.ErrorLevel = #Warning) and c.Broken)->collect(c|c.Description)

Reuse constraints as state-machine guards

A guard controls whether a state-machine transition is available. Use GuardConstraints when the same class rules must be valid for multiple transitions. OCL can be used for state-machine guards.

Delete constraints

A delete constraint is evaluated when an object is deleted by MDriven. Use one when the model must reject deletion under a specific condition. For example, a model can prevent deletion of a Car while a deposit remains unless the Car is in the Scrapped state.

Deletion also evaluates the Business Delete Rules on association endpoints. Choose the appropriate rule for each endpoint; for example, an association can require that related objects are empty before deletion.

See also