🌾Switch mode
When we write business logic, we usually encounter the problem of selectivity, that is, if the result 1 is returned, the A process is entered, if the result 2 is returned, the B process is entered, and if the result 3 is returned, the C process is entered. It is also defined as an exclusive gateway in some process definitions.
This expression via LiteFLow is also very easy to implement. You can use the combination of SWITCH
...to
keywords. Note that SWITCH
must be uppercase and to
lowercase.
# The basic example
If you choose to execute one of b, c, and d according to component a, you can declare it as follows:
<chain name="chain1">
SWITCH(a).to(b, c, d);
</chain>
Flow chart
# Nested with THEN, WHEN
We combine the previous two chapters to see some examples by nesting the three expressions
<chain name="chain1">
THEN(
a,
WHEN(
b,
SWITCH(c).to(d,e)
),
f
);
</chain>
Flow chart
# Id syntax in switch
Next, an example of THEN
and WHEN
in SWITCH
is shown.
If you've read the chapter Switch Components, you should know that LiteFlow determines what to select by returning a switch result.
So if there is a THEN
in SWITCH
, what should the switch component return if you want to select this THEN
?
It is stipulated in LiteFlow that each expression can have an id value, and you can set the id value to set the id value of an expression. Then return this id in the switch component. The usage is as follows:
<chain name="chain1">
THEN(
a,
SWITCH(b).to(
c,
THEN(d, e).id("t1")
),
f
);
</chain>
Flow chart
If you want to select the expression THEN
, then you can return t1 in the switch component:
@LiteflowComponent("b")
public class BCmp extends NodeSwitchComponent {
@Override
public String processSwitch() throws Exception {
//do your biz
return "t1";
}
}