You use the < operator in OCL expressions to test whether one comparable value is strictly less than another; it is for anyone writing constraints, validations, or conditional expressions in MDriven.
Syntax
leftValue < rightValueThe expression returns a Boolean value:
truewhenleftValueis less thanrightValue.falsewhenleftValueis equal to or greater thanrightValue.
Both operands must be comparable. The mathematical symbols reference defines < as returning true when self is comparable to, and less than, the value on the right.
Compare an attribute to a value
For a Department class with an EmployeeCount attribute, use the following expression to test whether the department has fewer than 10 employees:
self.EmployeeCount < 10| EmployeeCount | Result | Meaning |
|---|---|---|
| 9 | true
|
The department has fewer than 10 employees. |
| 10 | false
|
10 is not less than 10. |
| 12 | false
|
The department has more than 10 employees. |
For example, use this expression as an invariant when every Department must contain fewer than 10 employees:
context Department inv: self.EmployeeCount < 10Strict comparison
The < operator excludes the limit value. If 10 employees is allowed, use less than or equal to (<=) instead:
self.EmployeeCount <= 10| Requirement | Operator | Example |
|---|---|---|
| Fewer than 10 employees; 10 is not allowed. | <
|
self.EmployeeCount < 10
|
| At most 10 employees; 10 is allowed. | <=
|
self.EmployeeCount <= 10
|
Use in conditional expressions
You can use the Boolean result wherever an OCL expression expects a condition. For example, the following evaluates whether a person is below an age threshold:
self.Age < 18This expression returns true for an age of 17 and false for an age of 18.
Related comparisons
- Use greater than (
>) when the left value must be strictly larger. - Use greater than or equal to (
>=) when the boundary value is allowed on the larger side. - Use less than or equal to (
<=) when the boundary value is allowed on the smaller side.
