You use the equal-to operator (=) in OCL expressions to test whether two values are equal; it is for anyone writing constraints, validations, or conditional expressions.
Symbol and result
=
The operator compares the value on its left with the value on its right and returns a Boolean value:
| Expression | Result |
|---|---|
| The two values are equal | true
|
| The two values differ | false
|
As defined in Documentation:Mathematical symbols, equality applies to an OclAny value and returns a Boolean result.
Compare an attribute with a value
Assume that the Department class has a DepartmentName attribute. Use = to test whether a Department is named Projects:
context Department
inv: self.DepartmentName = 'Projects'For a Department whose DepartmentName is Projects, the expression returns true. For a Department whose name is Sales, it returns false.
Use equality in a combined condition
You can combine an equality test with another Boolean condition. In this example, a Person meets the condition only when both tests are true:
context Person
inv: (self.age >= 18) and (self.gender = 'Male')The self.gender = 'Male' part returns true when the value of gender is Male. The and operator then evaluates the two Boolean results together.
Related comparisons
Use the operator that expresses the rule you need:
| Requirement | Operator | Example |
|---|---|---|
| Values must be equal | =
|
self.DepartmentName = 'Projects'
|
| Values must differ | <>
|
self.DepartmentName <> 'Projects'
|
| A value may be at most a limit | <=
|
self.EmployeeCount <= 10
|
| A value must meet a minimum | >=
|
self.EmployeeCount >= 5
|
