You use if ... then ... else ... endif in OCL to choose one value based on a Boolean condition; use it in derived expressions, constraints, and EAL where your logic needs two outcomes.
Syntax
if BooleanCondition then
ValueWhenTrue
else
ValueWhenFalse
endifThe condition before then must evaluate to a Boolean value. When it is true, OCL evaluates the expression after then; when it is false, OCL evaluates the expression after else. The complete conditional expression returns the value from the selected branch.
| Part | Purpose | Example |
|---|---|---|
if
|
Starts the conditional and introduces the Boolean condition. | if self.IsActive
|
then
|
Introduces the result for a true condition. | then 'Active'
|
else
|
Introduces the result for a false condition. | else 'Inactive'
|
endif
|
Ends the conditional expression. | endif
|
Return a value from either branch
Both branches must return compatible types: the same type, or types with a common superclass. This lets MDriven determine one type for the complete conditional expression.
For example, both outcomes below are strings, so the expression returns a string:
if self.IsActive then
'Active'
else
'Inactive'
endifDo not return unrelated types from the two branches. For example, this has no common result type because one branch returns a string and the other returns an integer:
if self.IsActive then
'Active'
else
0
endifChoose a value of the expected type in both branches. If the expression is expected to return a string, use a string such as '0' rather than the integer 0.
Use conditionals in EAL
Executable Action Language (EAL) uses OCL syntax for conditions and can use an if expression to decide whether to perform one action or another. For example, ScriptEvalCheck returns 'ok' when a dynamic expression passes validation; otherwise it returns an error message. Use the result to decide whether to evaluate the expression or retain the validation message:
let validationInfo = self.ScriptEvalCheck(false, Decimal, self.DynamicFormula) in
(
if validationInfo = 'ok' then
self.FormulaResult := self.ScriptEval(false, Decimal, self.DynamicFormula).asstring
else
self.FormulaResult := validationInfo
endif
)In this example, both branches are assignments. The true branch stores the evaluated formula result, while the false branch stores the validation error.
Control precedence with parentheses
In MDriven OCL, if-then-else-endif has its own precedence level. It binds after arithmetic operations and before and, or, and xor. Use parentheses when a conditional is part of a larger expression so that the intended grouping is clear.
For example, parentheses make it explicit that the conditional supplies the value added to self.BaseAmount:
self.BaseAmount +
(if self.IsActive then
self.BonusAmount
else
0
endif)See OCL precedence rules for the complete MDriven precedence order. MDriven's order is not exactly the current OCL standard, so do not assume precedence from another OCL implementation.
Alternative: caseTrueFalse
For a short two-branch value selection, MDriven also provides caseTrueFalse. It expresses the same choice as an operation on a Boolean value:
self.IsActive.caseTrueFalse('Active', 'Inactive')This is equivalent to:
if self.IsActive then
'Active'
else
'Inactive'
endifUse the full if ... then ... else ... endif form when its layout makes multi-line expressions or EAL actions easier to read. Use caseTrueFalse when the compact Boolean-operation form is clearer. Like if, its true and false expressions need compatible result types.
Checklist
- Make the expression after
ifevaluate to Boolean. - Provide a result or action for both the true and false cases.
- Keep the
thenandelseresults type-compatible. - Parenthesize a conditional when it appears inside a longer expression.
- Use ScriptEvalCheck before evaluating user-provided dynamic OCL.
