Use whentrue in MDriven Action-Language (EAL) when you want to run an updating expression only when a Boolean condition is true, while keeping that condition as the result.
Syntax
booleanExpression.whentrue(expressionToRun) : Boolean
whentrue has two parts:
| Part | Meaning |
|---|---|
booleanExpression
|
The condition to test. This is also the value returned by whentrue.
|
expressionToRun
|
The expression to evaluate only when the condition is true. In EAL, this can update an object or invoke an action.
|
How it behaves
whentrue evaluates the Boolean expression first.
- If the Boolean expression is
true, it evaluates the argument expression. - If the Boolean expression is
false, it does not evaluate the argument expression. - It always returns the original Boolean expression value.
For example, the following expression marks an order as completed only when isPaid is true:
isPaid.whentrue(Order.markAsCompleted)
If isPaid is true, Order.markAsCompleted runs and the expression returns true. If isPaid is false, Order.markAsCompleted does not run and the expression returns false.
Use it for conditional actions
Use whentrue when the purpose of the expression is a conditional EAL action rather than choosing between two returned values. It keeps the condition next to the action it guards.
For example, this sequence checks an enum value and runs the matching action:
let x=SomeObject.SomeEnum in (
(x=#Enum1).whentrue(SomeObject.DoYourThing1);
(x=#Enum2).whentrue(SomeObject.DoYourThing2);
(x=#Enum3).whentrue(SomeObject.DoYourThing3)
)
Each line works independently:
- When
x=#Enum1is true,SomeObject.DoYourThing1runs. - When
x=#Enum2is true,SomeObject.DoYourThing2runs. - When
x=#Enum3is true,SomeObject.DoYourThing3runs. - An action whose condition is false is not evaluated.
This pattern is appropriate when the enum values are mutually exclusive and each action is guarded by its own Boolean test.
Return value and lazy evaluation
The return value is always the Boolean value before .whentrue(...). The action expression does not determine the result.
false.whentrue(SomeObject.DoYourThing)
The action does not run, and the expression returns false.
true.whentrue(SomeObject.DoYourThing)
The action runs, and the expression returns true.
This conditional evaluation is lazy evaluation: the argument expression is evaluated only if the Boolean receiver is true. Logical and also evaluates its right-hand expression only when its left-hand expression is true. The following is therefore equivalent in evaluation behavior, but is less direct because it must explicitly produce a Boolean value after the action:
let x=SomeObject.SomeEnum in (
(x=#Enum1) and (SomeObject.DoYourThing1;true);
(x=#Enum2) and (SomeObject.DoYourThing2;true);
(x=#Enum3) and (SomeObject.DoYourThing3;true)
)
Prefer whentrue when you mean "when this condition is true, run this action." Use and when you are expressing a logical relationship between Boolean expressions; see Documentation:OCL Boolean Operators.
Opposite operator
To run an expression only when a Boolean condition is false, use whenfalse. For example:
isValid.whenfalse(ShowErrorMessage)
When isValid is false, ShowErrorMessage runs and the result remains false.
