You can use these comparison operators in OCL expressions to test whether objects or values are equal, different, or ordered; this page is for anyone writing conditions and constraints in MDriven.
Comparison operators
A comparison operator evaluates two expressions and returns a Boolean value: true or false. Use the result where an OCL condition is expected.
| Operator | Signature | Returns true when
|
Example |
|---|---|---|---|
<>
|
<> ( object : OclAny ) : Boolean
|
self is a different object from object.
|
person1 <> person2 is true when person1 and person2 are different objects.
|
=
|
= ( object : OclAny ) : Boolean
|
self is equal to object.
|
5 = 5 evaluates to true.
|
<
|
< ( object : T ) : Boolean
|
self is comparable to object and is less than it.
|
3 < 5 evaluates to true.
|
>
|
> ( object : T ) : Boolean
|
self is comparable to object and is greater than it.
|
5 > 3 evaluates to true.
|
<=
|
<= ( object : T ) : Boolean
|
self is comparable to object and is less than or equal to it.
|
5 <= 5 evaluates to true.
|
>=
|
>= ( object : T ) : Boolean
|
self is comparable to object and is greater than or equal to it.
|
5 >= 5 evaluates to true.
|
Choose the right operator
Use = when your condition requires equality. For example, 5 = 5 is true, while 5 = 3 is false.
Use <> when you need to establish that two references denote different objects. For example:
person1 <> person2
This expression is true when person1 and person2 are different objects.
Use <, >, <=, and >= only when the two values are comparable. The inclusive operators, <= and >=, also return true when both values have the same value.
| Requirement | Use | Example result |
|---|---|---|
| Value must be exactly equal | =
|
5 = 5 is true.
|
| Objects must be different | <>
|
person1 <> person2 is true when they are different objects.
|
| First value must be lower | <
|
3 < 5 is true.
|
| First value may be equal to the limit | <=
|
5 <= 5 is true.
|
| First value must be higher | >
|
5 > 3 is true.
|
| First value may be equal to the limit | >=
|
5 >= 5 is true.
|
Compare numeric values
Integers are whole-number values and can participate in comparisons. Double values represent floating-point numbers and can also participate in mathematical expressions and comparisons.
For example, an expression such as 3 < 5 compares two integer values. When comparing floating-point values, remember that floating-point calculations can have approximation errors; see Documentation:Double for details.
