OCLOperators And
No edit summary
No edit summary
Line 2: Line 2:


Examples:  
Examples:  
# Suppose we have a class called "Person" with two attributes "age" and "gender". We want to define a constraint that only allows persons who are at least 18 years old and are male to enroll in a certain program. The OCL expression for this constraint would be:
 
1. Suppose we have a class called "Person" with two attributes: "age" and "gender". We want to define a constraint that only allows persons who are at least 18 years old and are male to enroll in a certain program. The OCL expression for this constraint would be:  
  context Person
  context Person
  inv: self.age >= 18 and self.gender = 'Male'
  inv: self.age >= 18 and self.gender = 'Male'
Line 8: Line 9:
This constraint specifies that the "age" attribute of a Person object must be greater than or equal to 18, and its "gender" attribute must be equal to "Male" for the object to satisfy the constraint.
This constraint specifies that the "age" attribute of a Person object must be greater than or equal to 18, and its "gender" attribute must be equal to "Male" for the object to satisfy the constraint.


2. Consider a class called "Order" with two attributes "price" and "quantity". We want to define an operation that calculates the total cost of an order, which is the product of its price and quantity. The OCL expression for this operation would be:
2. Consider a class called "Order" with two attributes "price" and "quantity". We want to define an operation that calculates the total cost of an order, which is the product of its price and quantity. The OCL expression for this operation would be:  
  context Order
  context Order
  def: total_cost = self.price * self.quantity
  def: total_cost = self.price * self.quantity

Revision as of 06:33, 24 April 2023

This operator is represented by the keyword "and" and evaluates to true only if both of its operands are true and false otherwise. It can be used to combine multiple conditions to form a more complex condition that must be satisfied for an object or a set of objects to meet a certain requirement. For example, if "a" and "b" are Boolean expressions, then "a and b" will be true only if both "a" and "b" are true.

Examples:

1. Suppose we have a class called "Person" with two attributes: "age" and "gender". We want to define a constraint that only allows persons who are at least 18 years old and are male to enroll in a certain program. The OCL expression for this constraint would be:

context Person
inv: self.age >= 18 and self.gender = 'Male'

This constraint specifies that the "age" attribute of a Person object must be greater than or equal to 18, and its "gender" attribute must be equal to "Male" for the object to satisfy the constraint.

2. Consider a class called "Order" with two attributes "price" and "quantity". We want to define an operation that calculates the total cost of an order, which is the product of its price and quantity. The OCL expression for this operation would be:

context Order
def: total_cost = self.price * self.quantity

This expression defines a new variable called "total_cost" that is equal to the product of the "price" and "quantity" attributes of an order object. The "And" operator is not used in this example, but it could be used in a constraint to specify that the "price" and "quantity" attributes must both be greater than zero for an order object to be valid.

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