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

You use the OCL and operator to require two Boolean conditions to be true at the same time, for example when an object must meet both an age requirement and a gender requirement.

Syntax

condition1 and condition2

Both operands must be Boolean expressions. The complete expression evaluates to true only when condition1 and condition2 are both true.

First condition Second condition Result of and
true true true
true false false
false true false
false false false

Combine requirements

Use and when all listed requirements must hold. Parentheses make each requirement clear, especially when an expression contains more than one operator.

For example, this expression is true for a Person whose age is at least 18 and whose gender is 'Male':

(self.age >= 18) and (self.gender = 'Male')

If either condition is false, the result is false. A person aged 20 with gender 'Female' does not satisfy the expression; a person aged 17 with gender 'Male' does not satisfy it either.

Validate several attribute values

You can use and to require that several values are valid together. For an Order, the following expression requires both price and quantity to be greater than zero:

(self.price > 0) and (self.quantity > 0)

Do not use and for calculations. For example, calculating an order total uses the multiplication operator:

self.price * self.quantity

See Documentation:OCL Number Operators for numeric operators.

Build longer conditions

You can chain and when every condition must be true. This example requires an order to have a positive price, a positive quantity, and a non-empty reference:

(self.price > 0) and (self.quantity > 0) and (self.reference.length > 0)

The expression is false when any one of these requirements is false.

Find operators in the OCL Editor

To inspect the operators available for a type, open the OCL Editor and enter a class. See Documentation:OCL General Operators for this approach and the wider operator list.

See also