🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators Xor
This page was created by Stephanie on 2023-04-20. Last edited by Wikiadmin on 2026-07-29.

You use the OCL xor operator when a Boolean expression must be true for exactly one of two conditions; it is for modelers writing Boolean expressions and constraints.

Syntax

conditionA xor conditionB

xor is the exclusive-or operator. It returns true when one operand is true and the other is false. It returns false when both operands have the same value.

For example, hasDriverLicense xor hasMotorcycleLicense means that a person must have one license, but cannot have both or neither.

Truth table

Left operand Right operand left xor right
false false false
false true true
true false true
true true false

Use xor for an exclusive choice

Use xor when the rule requires one alternative to apply. Do not use it when the rule only prohibits both alternatives: xor also rejects the case where neither alternative applies.

Require exactly one license

The following constraint requires each Person to have exactly one of the two Boolean attributes:

context Person
self.hasDriverLicense xor self.hasMotorcycleLicense

A person with only a driver's license satisfies the constraint. A person with only a motorcycle license also satisfies it. A person with both licenses, or with neither license, does not satisfy it.

Mark a product as free or expensive

The following definition is true when a product costs zero or when its price is at least 100:

context Product
def: is_special = (self.price = 0) xor (self.price >= 100)

With these two particular conditions, a price cannot be both 0 and at least 100. The result is therefore true for price 0 and for price 100 or above, and false for a price such as 50. The parentheses make each comparison explicit.

Precedence and parentheses

In MDriven OCL, and, or, and xor have the same precedence. Use parentheses when you combine them so that the intended grouping is clear.

For example, write:

(self.hasDriverLicense xor self.hasMotorcycleLicense) and self.isActive

This requires exactly one license and also requires isActive to be true. For the complete precedence order and the difference from the current OCL standard, see Documentation:OCL Precedence rules.

Related operators

xor is one of the Boolean operators available in OCL. Use it for an exclusive choice; use another Boolean operator when your rule has different requirements.

See also