You use Boolean operators in OCL expressions to combine conditions into a result that is either true or false; this page is for modelers writing constraints, selection criteria, and other conditional rules in MDriven.
A Boolean expression answers a yes-or-no question. For example, self.age >= 18 is Boolean because it evaluates whether a person's age meets the condition. You can combine that result with other Boolean expressions by using logical operators.
Use Boolean operators
| Operator | Result | Example |
|---|---|---|
and
|
true only when both conditions are true.
|
self.age >= 18 and self.hasDriverLicense
|
or
|
true when at least one condition is true.
|
self.category = 'Electronics' or self.category = 'Books'
|
not
|
Reverses a Boolean result. | not self.hasDriverLicense
|
Combine required conditions with and
Use and when every requirement must be met. For example, this condition is true only for a person who is at least 18 and has a driver's license:
self.age >= 18 and self.hasDriverLicenseIf either the age condition or the license condition is false, the complete expression is false.
Allow alternatives with or
Use or when either condition is sufficient. For example, this expression identifies a product in either of two categories:
self.category = 'Electronics' or self.category = 'Books'Both conditions may also be true; the result remains true. For a fuller explanation and additional examples, see Documentation:OCLOperators Or.
Exclude a condition with not
Use not when the rule requires a condition to be false. For example, this expression is true when a person does not have a driver's license:
not self.hasDriverLicenseGroup complex conditions
Use parentheses to make the intended grouping clear when you combine and and or. For example, the following rule requires a driver's license and allows either an adult or a person in the Books category:
(self.age >= 18 or self.category = 'Books') and self.hasDriverLicenseParentheses make the alternatives explicit before the final license requirement is evaluated. Review OCL precedence rules when an expression depends on operator evaluation order.
Use Boolean expressions in a collection selection
The select collection operation evaluates a Boolean condition for each object and retains the objects for which the condition is true. Name the iterated object before the pipe character (|) and use that name in the condition.
For example, to select cars that are registered and have a positive registration number:
self.cars->select(oneCar | oneCar.isRegistered and oneCar.registrationNumber > 0)Here, oneCar is the loop variable. The expression after | must produce a Boolean result for each car. Learn more about collection-specific operations in Documentation:OCL Collection Operators and see the expression walkthrough in Documentation:Turnkey session 7: Expressions.
Use Boolean expressions in MDriven
MDriven uses OCL as a query language. An OCL expression must not have side effects: evaluating it is not expected to change data. Boolean expressions can be used in several places, including:
- Constraints on a class.
- Derivation rules for derived attributes and associations.
- ViewModel column visibility and enable-state expressions.
- ViewModel column style expressions.
- Action enable expressions.
- State-machine guards.
For example, an action enable expression can use self.age >= 18 and self.hasDriverLicense so that the action is enabled only when both requirements hold.
Find operators in the OCL Editor
To explore operators available for a model type, open the OCL Editor and type a class. The editor shows the available operators for that context. See Documentation:OCL General Operators for this approach and Documentation:OCLOperators for the general operator overview.
