You use the less than or equal to operator in OCL expressions when a value must not exceed a limit, for example when a Department may have at most 10 employees.
Symbol and meaning
<= is a comparison operator. It compares the value on its left with the value on its right and returns a Boolean value: true or false.
leftValue <= rightValueThe expression returns true when the left value is less than the right value or equal to it. The values must be comparable.
According to Documentation:Mathematical symbols, the operation is expressed as:
<= ( object : T ) : BooleanUse an inclusive upper limit
Use <= when the limit itself is allowed. For example, the following invariant states that a Department can have no more than 10 employees:
context Department inv:
self.EmployeeCount <= 10Here, self is the Department being evaluated, EmployeeCount is its value, and 10 is the permitted maximum.
| EmployeeCount | Expression | Result |
|---|---|---|
| 8 | 8 <= 10
|
true
|
| 10 | 10 <= 10
|
true
|
| 11 | 11 <= 10
|
false
|
The equality case is the important distinction: a department with exactly 10 employees satisfies this rule.
Choose the correct comparison operator
| Requirement | Expression | Meaning |
|---|---|---|
| Fewer than 10 employees | self.EmployeeCount < 10
|
10 is not allowed. See less than (<). |
| At most 10 employees | self.EmployeeCount <= 10
|
10 is allowed. |
| Exactly 10 employees | self.EmployeeCount = 10
|
Only 10 is allowed. See equal to (=). |
| At least 10 employees | self.EmployeeCount >= 10
|
10 and higher values are allowed. See greater than or equal to (>=). |
Combine a maximum with another condition
You can combine Boolean comparisons with and when more than one condition must hold. For example, this rule requires the employee count to be greater than zero and no greater than 10:
context Department inv:
(self.EmployeeCount > 0) and (self.EmployeeCount <= 10)For an EmployeeCount of 10, both comparisons are true, so the complete expression is true. For 0, the upper-limit comparison is true but the greater-than-zero comparison is false, so the complete expression is false.
