Starting from version 2.8.5, LiteFlow provides conditional expression combinations.
Conditional expression is a variant of switch expression,
SWITCH selects one of multiple subitems based on logic. The conditional expression has only two sub-items, true and false, which is very useful in the process of dealing with certain businesses.
In fact, conditional expression is to become an if else in the language. It's just that there are some different usages in the LiteFlow EL syntax.
The first parameter of the following
ELIF requires the definition of a
if component. For how to define it, please refer to the chapter If component.
# IF with two arguments
<chain name="chain1"> THEN( IF(x, a), b ); </chain>
x is a conditional node. If it is true, the execution chain is x->a->b, and if it is false, it is x->b.
# IF with three arguments
<chain name="chain1"> THEN( IF(x, a, b), c ); </chain>
x is a conditional node. If it is true, the execution chain is x->a->c, and if it is false, it is x->b->c.
# ELSE expression
LiteFlow also provides the
ELSE expression, the
IF with two arguments +
ELSE expression is equivalent to the
IF with three arguments, for example:
<chain name="chain1"> IF(x, a).ELSE(b); </chain>
It is equivalent to:
<chain name="chain1"> IF(x, a, b); </chain>
# ELIF expression
The usage of the
ELIF keyword is actually similar to the else if in the java language. It can be used with multiple
ELIF, It will be followed by an
ELSE at the end, which is used to judge multiple conditions:
<chain name="chain1"> IF(x1, a).ELIF(x2, b).ELIF(x3, c).ELIF(x4, d).ELSE(THEN(m, n)); </chain>
Those who have written code should have a good understanding of this expression.
It is worth noting that only the two arguments expression of
IF can be followed by
ELIF. If the
IF with three arguments is followed by
ELIF, the last expression will be overwritten by the expression of
ELIF. for example:
<chain name="chain1"> IF(x1, a, b).ELIF(x2, c).ELSE(d); </chain>
In this way, even if x1 is false, it will not execute to b, and will judge x2. Although the framework does fault-tolerant processing, it is not recommended to write expressions in this way. It is easy to cause confusion in understanding.
In fact, the
IF with three arguments can already express all possibilities, which can be done by nesting, for example:
<chain name="chain1"> IF( x1, a, IF( x2, b, IF(x3, c, d) ) ); </chain>
But it is still not recommended that you write this way. Multiple nesting will be difficult to understand, so try to use