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.
|
Here are some examples of equality comparisons:
| Expression | Result |
|---|---|
| let a : String = 'a', b : String = 'a' in a = b | true |
| let a : Integer = 2, b : Real = 2.0 in a = b | true |
| let a : Integer = -2, b : Integer = 2 in a = b | false |
Here are some examples of inequality comparisons:
| Expression | Result |
|---|---|
| let a : String = 'a', b : String = 'a' in a <> b | false |
| let a : Integer = 2, b : Real = 2.0 in a <> b | false |
| let a : Integer = -2, b : Integer = 2 in a <> b | 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.
Here are some examples of less-than comparisons:
| Expression | Result |
|---|---|
| let a : Integer = 1, b : Integer = 2 in a < b | true |
| let a : Real = 1.5, b : Integer = 2 in a < b | true |
| let a : String = 'Anteater', b : String = 'Aardvark' in a < b | false |
Here are some examples of greater-than comparisons:
| Expression | Result |
|---|---|
| let a : Integer = 1, b : Integer = 2 in a > b | false |
| let a : Real = 1.5, b : Integer = 2 in a > b | false |
| let a : String = 'Anteater', b : String = 'Aardvark' in a > b | true |
Here are some examples of less-than-or-equal comparisons:
| Expression | Result |
|---|---|
| let a : Integer = 1, b : Integer = 2 in a <= b | true |
| let a : Real = 1.5, b : Integer = 2 in a <= b | true |
| let a : String = 'Anteater', b : String = 'Aardvark' in a <= b | false |
Here are some examples of greater-than-or-equal comparisons:
| Expression | Result |
|---|---|
| let a : Integer = 1, b : Integer = 2 in a >= b | false |
| let a : Real = 1.5, b : Integer = 2 in a >= b | false |
| let a : String = 'Anteater', b : String = 'Aardvark' in a >= b | true |
