OCLOperators Xor
No edit summary
No edit summary
Line 1: Line 1:
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.
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:
=== Examples: ===
'''1.''' Suppose we have a class called <code>Person</code> with two attributes: <code>hasDriverLicense</code> and <code>hasMotorcycleLicense</code>. 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:


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
context Person
  self.hasDriverLicense xor self.hasMotorcycleLicense
  inv: 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.
This constraint specifies that the <code>hasDriverLicense</code> and <code>hasMotorcycleLicense</code> 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:
'''2.''' Consider a class called <code>Product</code> with the attribute <code>price</code>. 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
 
context Product
  def: is_special = (self.price = 0) xor (self.price >= 100)
  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 expression uses the "Xor" operator to combine two conditions that check whether the <code>price</code> 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 <code>is_special</code> variable is set to true, indicating that the product is special. If both conditions are true or false, then the <code>is_special</code> variable is set to false, indicating that the product is not special. This ensures that the object satisfies the constraint.  


[[Category:OCL Boolean Operators]]
[[Category:OCL Boolean Operators]]

Revision as of 07:30, 1 May 2023

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 95 days ago on 02/10/2024. What links here