OCLOperators Xor

This logical operator is represented by the keyword "xor" and evaluates to true if exactly one of its operands is true, and false otherwise. It is used to express an exclusive or relationship between two Boolean expressions. The expression "a xor b" is true if either a is true and b is false, or a is false and b is true. If both a and b are true or both are false, the expression is false. For example, "I will either go to the beach (a) or go hiking (b), but not both" can be expressed as "a xor b" in OCL.

Examples:

1. Suppose we have a class called Person with two attributes: hasDriverLicense and hasMotorcycleLicense. We want to define a constraint that prohibits persons from having both a driver's license and a motorcycle license at the same time. The OCL expression for this constraint would be:

context Person

self.hasDriverLicense xor self.hasMotorcycleLicense

This constraint specifies that the hasDriverLicense and hasMotorcycleLicense attributes of a person object cannot both be true at the same time. If a person has a driver's license, then they cannot have a motorcycle license, and vice versa. This ensures that the object satisfies the constraint.

2. Consider a class called Product with the attribute price. We want to define an operation that returns true if a product is either free or expensive, but not both. The OCL expression for this operation would be:

context Product

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

This expression uses the "Xor" operator to combine two conditions that check whether the price attribute of a product object is equal to zero or greater than or equal to 100. If exactly one of the conditions is true, then the is_special variable is set to true, indicating that the product is special. If both conditions are true or false, then the is_special variable is set to false, indicating that the product is not special. This ensures that the object satisfies the constraint.

This page was edited 130 days ago on 08/14/2024. What links here