💐Complex example
After the above few chapters, do you have a general understanding of how to write EL rules?
In this chapter, we combine the above chapters to see two examples of complex processes.
# Complex example(1)
Flow chart
The above diagram can be expressed as the following EL rules:
<chain name="chain1">
THEN(
A,
WHEN(
THEN(B, C),
THEN(D, E, F),
THEN(
SWITCH(G).to(
THEN(H, I, WHEN(J, K)).id("t1"),
THEN(L, M).id("t2")
),
N
)
),
Z
);
</chain>
I believe most people should be able to understand, but if you re-optimize with sub variables, it will be more clear, the above can be optimized into:
<chain name="chain1">
item1 = THEN(B, C);
item2 = THEN(D, E, F);
item3_1 = THEN(H, I, WHEN(J, K)).id("t1");
item3_2 = THEN(L, M).id("t2");
item3 = THEN(SWITCH(G).to(item3_1, item3_2), N);
THEN(
A,
WHEN(item1, item2, item3),
Z
);
</chain>
Is it much clearer?
If you already understand the above example, let's look at a more complex example
# Complex example(2)
Flow chart
The above diagram can be expressed as the following EL rules:
<chain name="chain1">
THEN(
A,
SWITCH(B).to(
THEN(D, E, F).id("t1"),
THEN(
C,
WHEN(
THEN(
SWITCH(G).to(THEN(H, I).id("t2"), J),
K
),
THEN(L, M)
)
).id("t3")
),
Z
);
</chain>
I think some people will be confused about the above expressions, and it takes half a day to count the parentheses. Of course, if you study it carefully, you should be able to understand it.
For such expressions that are difficult to read, the official recommendation is to split them into sub-processes or sub-variables. Below I optimize it by splitting the sub-variables:
<chain name="chain1">
item1 = THEN(D, E, F).id("t1");
item2_1 = THEN(
SWITCH(G).to(
THEN(H, I).id("t2"),
J
),
K
);
item2_2 = THEN(L, M);
item2 = THEN(C, WHEN(item2_1, item2_2)).id("t3");
THEN(
A,
SWITCH(B).to(item1, item2),
Z
);
</chain>
tip
The above 2 examples can be found in the test case in the source code, you can run and test.
Complex case one:com.yomahub.liteflow.test.complex.ComplexELSpringbootTest1
Complex case two:com.yomahub.liteflow.test.complex.ComplexELSpringbootTest2
# Summarize
LiteFlow's EL rule syntax is simple, but it can describe most scenarios. Try to keep your rules as simple as possible.
In actual scenarios, if you encounter complex processes, you can use sub chain or sub variables to simplify your entire rules. Make your rules elegant and easier to read!