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
endifWrite 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:
xholds the enum value fromSomeObject.SomeEnum.- Each equality test, such as
x = #Enum1, is a Boolean expression. - The action associated with the matching enum value is selected.
DoNothingis 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))
->firstEach 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.
