You can use the implies operator in OCL to state that one Boolean condition requires another condition to be true; it is intended for modelers writing conditional rules and constraints.
Syntax
conditionA implies conditionBBoth operands must be Boolean expressions. Read the expression as: if conditionA is true, conditionB must also be true.
The expression is false only when the left-hand condition is true and the right-hand condition is false.
| conditionA | conditionB | conditionA implies conditionB
|
|---|---|---|
| false | false | true |
| false | true | true |
| true | false | false |
| true | true | true |
How to use implies
- Put the condition that activates the rule on the left side.
- Put the requirement that must hold when that condition is true on the right side.
- Use parentheses around comparisons and compound conditions so that the intended grouping is explicit.
For example, a Product with a name must have a positive price:
context Product
(not self.name.isNull) implies (self.price > 0)If self.name is not null, the price must be greater than zero. If self.name is null, this expression is true and does not impose a requirement on self.price.
Choose the correct direction
The order of the operands changes the rule. Use the direction that matches the business rule you want to express.
| Requirement | OCL expression | Meaning |
|---|---|---|
| A named product must have a positive price. | (not self.name.isNull) implies (self.price > 0)
|
A name activates the positive-price requirement. |
| A product with a positive price must have a name. | (self.price > 0) implies (not self.name.isNull)
|
A positive price activates the name requirement. |
These expressions are not equivalent. In the first expression, an unnamed product may have any price. In the second expression, a product with a zero or negative price may still have a name.
Avoid a common null-condition error
This expression:
self.name.isNull implies (self.price <= 0)means that a product without a name must have a price less than or equal to zero. It does not mean that named products must have a positive price. Place not before self.name.isNull when the rule should apply to named products.
Precedence and parentheses
In MDriven, implies has its own position in the OCL precedence rules. Comparisons such as > and <= have lower precedence than implies. Therefore, always parenthesize each comparison when it appears with implies.
Write:
(not self.name.isNull) implies (self.price > 0)Do not rely on implicit grouping in an expression such as:
not self.name.isNull implies self.price > 0Parentheses make the Boolean operands clear and prevent a precedence-dependent interpretation.
Related Boolean operators
implies is a Boolean operator. Use it for a one-way conditional requirement. Combine Boolean conditions with and, or, xor, and not when the rule requires more than one condition. You can find the available operators in the OCL General Operators overview.
