You can define reusable business expressions as methods in your UML model and make side-effect-free methods available to OCL expressions.
Define reusable model logic
A method on a class can hold business logic that you want to name and reuse. In MDriven Designer, the implementation is written in the method's Body property.
For example, a class named Thing can have a method named MyMethod that accepts an integer argument and returns a value derived from the Thing object and that argument.
Methods are useful when the same rule would otherwise be repeated in several places, such as derived attributes, validation expressions, ViewModel expressions, or action logic. Give the method a name that describes the business rule rather than its technical implementation.
Understand OCL and EAL
OCL is used to express rules and retrieve or calculate information from your model. An OCL expression should be usable without changing model data.
When you implement a model method, MDriven treats the method body as EAL. EAL is the expression form used for logic that can have side effects, such as changing values or creating, deleting, or linking objects.
This distinction matters when you want to call a method from OCL:
| Method type | May intentionally change model data | Available to call from OCL |
|---|---|---|
| Regular method | Yes | No |
| Method marked IsQuery | No — you promise that it has no intentional side effects | Yes |
Make a method available in OCL
If a method only calculates or evaluates information and does not intentionally change data, mark it as a query.
- Define the method on the relevant class in MDriven Designer.
- Enter the implementation in the method's Body property.
- Verify that the method has no intentional side effects.
- Set the method definition's IsQuery flag.
- Use the method in an OCL expression where its result is needed.
Setting IsQuery is a promise about the method's behavior. Do not set it for a method that is intended to update the model. Keep update logic as a regular method and call it from the appropriate action or other EAL context.
Call a query method from an OCL expression
After you set IsQuery, OCL recognizes the method. For example, this expression selects all Thing instances for which MyMethod returns true when passed the object's SomeInt value:
Thing.allinstances->select(x | x.MyMethod(x.SomeInt))In this example:
Thing.allinstancessupplies all instances ofThing.selectfilters that collection.xis the currentThingin the filter.x.MyMethod(x.SomeInt)calls the query method for that object.
The method is reusable because the business rule has one definition. A ViewModel column, a derived attribute, or another OCL expression can call the same query method instead of restating the rule.
Keep data available to the model
OCL can express the rules needed by a line-of-business application when the arguments and result are representable in the model. The expression can navigate model relationships, evaluate attributes, and combine those values into a result.
For example, a rule that depends on a Thing, its related objects, and its SomeInt value can be expressed when those objects, links, and values are in the model.
OCL cannot use external or ambient data that the model cannot access. If a rule depends on such data, first make the required data available to the model. For example, security rules can use the current user after the user has been made available in the model; the SysSingleton.oclSingleton.CurrentUser.IsAdmin=true pattern is described in Training:Security.
Write rules as compact model expressions
Use OCL to state rules directly against the information you designed. A compact expression is easier to compare with a business requirement than the same rule spread across imperative code.
This works best when you start with the information the business handles: the classes, attributes, and associations in the UML model. See Training:Information design for the information-first approach and Training:Praise to UML for why UML is used to describe the system's essential structure.
