OCLOperators Or
No edit summary
No edit summary
Line 5: Line 5:


context Person
context Person
  self.age >= 18 or self.hasDriverLicense
  (self.age >= 18) or self.hasDriverLicense
This constraint specifies that the <code>age</code> attribute of a person object must be greater than or equal to 18, or its "hasDriverLicense" attribute must be true for the object to satisfy the constraint. If a person is at least 18 years old or has a valid driver's license, then they are allowed to drive a car.
This constraint specifies that the <code>age</code> attribute of a person object must be greater than or equal to 18, or its "hasDriverLicense" attribute must be true for the object to satisfy the constraint. If a person is at least 18 years old or has a valid driver's license, then they are allowed to drive a car.


Line 11: Line 11:


context Product
context Product
  def: is_interesting = self.category = 'Electronics' or self.category = 'Books'
  is_interesting = (self.category = 'Electronics') or (self.category = 'Books')
This expression uses the "Or" operator to combine two conditions that check whether the <code>category</code> attribute of a product object is equal to either "Electronics" or "Books". If either condition is true, then the <code>is_interesting</code> variable is set to true, indicating that the product is interesting.
This expression uses the "Or" operator to combine two conditions that check whether the <code>category</code> attribute of a product object is equal to either "Electronics" or "Books". If either condition is true, then the <code>is_interesting</code> variable is set to true, indicating that the product is interesting.


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

Revision as of 08:30, 3 May 2023

The "Or" operator evaluates to true if at least one of its operands is true, and false otherwise. It can be used to express a disjunctive relationship between two conditions or requirements. For example, if "a" and "b" are Boolean expressions, then "a or b" will be true if either "a" or "b" (or both) are true.

Examples:

1. Suppose we have a class called Person with the attribute age. We want to define a constraint that allows persons who are either at least 18 years old or have a valid driver's license to drive a car. The OCL expression for this constraint would be:

context Person

(self.age >= 18) or self.hasDriverLicense

This constraint specifies that the age attribute of a person object must be greater than or equal to 18, or its "hasDriverLicense" attribute must be true for the object to satisfy the constraint. If a person is at least 18 years old or has a valid driver's license, then they are allowed to drive a car.

2. Consider a class called Product with two attributes: name and category. We want to define an operation that returns true if a product belongs to either the "Electronics" category or the "Books" category. The OCL expression for this operation would be:

context Product

is_interesting = (self.category = 'Electronics') or (self.category = 'Books')

This expression uses the "Or" operator to combine two conditions that check whether the category attribute of a product object is equal to either "Electronics" or "Books". If either condition is true, then the is_interesting variable is set to true, indicating that the product is interesting.

This page was edited 84 days ago on 02/10/2024. What links here