🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators casetruefalse
This page was created by Hans.karlsen on 2021-12-03. Last edited by Wikiadmin on 2026-07-29.

You can use caseTrueFalse to choose one of two expressions from a Boolean result in OCL or to select an action in EAL.

Purpose

caseTrueFalse is the compact operator form of an if ... then ... else ... endif expression. Call it on a Boolean expression and provide the value or action for the true and false cases.

if someBool then
  expressionWithTypeX
else
  otherExpressionWithTypeX
endif

Write the same conditional expression as:

someBool.caseTrueFalse(expressionWithTypeX, otherExpressionWithTypeX)

The expressions supplied for the true and false cases must produce the same type. For example, both branches below produce strings:

self.IsActive.caseTrueFalse('Active', 'Inactive')

Syntax

booleanExpression.caseTrueFalse(trueExpression, falseExpression)
Part Meaning
booleanExpression An expression that evaluates to true or false.
trueExpression The value or action selected when the Boolean expression is true.
falseExpression The value or action selected when the Boolean expression is false.

Use Boolean operators to form the condition. For example, the condition self.Amount > 0 determines which string is returned:

(self.Amount > 0).caseTrueFalse('Positive', 'Not positive')

Use in EAL

In EAL, use caseTrueFalse when you want switch-like action selection. Store the value to test in a local variable, then invoke one action for each matching enum value. Separate the expressions with semicolons so that each test is evaluated.

let x = SomeObject.SomeEnum in
(
  (x = #Enum1).caseTrueFalse(SomeObject.DoYourThing1, DoNothing);
  (x = #Enum2).caseTrueFalse(SomeObject.DoYourThing2, DoNothing);
  (x = #Enum3).caseTrueFalse(SomeObject.DoYourThing3, DoNothing)
)

In this example:

  • x holds the enum value from SomeObject.SomeEnum.
  • Each equality test, such as x = #Enum1, is a Boolean expression.
  • The action associated with the matching enum value is selected.
  • DoNothing is the false-case action for tests that do not match.

This structure is useful when each enum value maps to a different method. Keep the tests explicit: the example does not provide one final default action for an enum value that matches none of the listed cases.

Use in OCL: derive a value with a fallback

You can use several caseTrueFalse expressions to derive a string from a set of conditions. The following example evaluates states in order and uses self.State as the fallback when none of the candidate values is non-null.

(self.oclIsInState(#New).caseTrueFalse('New/Ordering', String.nullValue) +
 self.oclIsInState(#Waiting).caseTrueFalse('Waiting', String.nullValue) +
 self.oclIsInState(#Ordered).caseTrueFalse('Ordered', String.nullValue))
->collect(s | s.notNull.caseTrueFalse(s, self.State))
->first

Each state test returns its label when true and String.nullValue when false. The collect expression replaces a null result with self.State, and first returns the resulting value.

Keep the parentheses around the string-concatenation expression. They ensure that the complete combined expression is the input to collect, rather than only part of the expression.

Choose the appropriate form

Use caseTrueFalse when a Boolean condition has exactly two outcomes. Use a regular if ... then ... else ... endif expression when that form makes a longer conditional easier to read. For related conditional behavior, see whenTrue.

See also