🍕Define script component
The script node definition is in the rule file, you need to do the following definition (for example in xml file format):
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<nodes>
<node id="s1" name="common script" type="script" language="groovy">
<![CDATA[
def a=3;
def b=2;
defaultContext.setData("s1",a*b);
]]>
</node>
<node id="s2" name="switch script" type="switch_script" language="groovy">
<![CDATA[
def count = defaultContext.getData("count");
if(count > 100){
return "a";
}else{
return "b";
}
]]>
</node>
<node id="s3" name="if script" type="if_script" language="groovy">
<![CDATA[
return false;
]]>
</node>
<node id="s4" name="fix number loop script" type="for_script" language="groovy">
<![CDATA[
//This can be used to get the number of your loops, you can call the java object to get it, and the demo directly returns the number of loops
return 10;
]]>
</node>
<node id="s5" name="条件循环脚本" type="while_script" language="groovy">
<![CDATA[
//It is used here to get the identifier of when to continue the loop. You can call the java object to get it. This is just a demo.
def flag = yourJavaBean.getFlag();
return flag;
]]>
</node>
<node id="s6" name="退出循环脚本" type="break_script" language="groovy">
<![CDATA[
//This is used to get the identifier of when to exit the loop. You can call the java object to get it. This is just a demo.
def breakFlag = yourJavaBean.getFlag();
return flag;
]]>
</node>
</nodes>
<chain name="chain1">
THEN(a, b, c, s1);
</chain>
<chain name="chain2">
THEN(d, SWITCH(s2).to(a, b));
</chain>
<chain name="chain3">
THEN(d, IF(s3, b, c));
</chain>
<chain name="chain4">
FOR(s4).DO(THEN(a,b)).BREAK(s6);
</chain>
<chain name="chain5">
WHILE(s5).DO(THEN(a,b));
</chain>
</flow>
It should be noted that there are 6 types of type
:
script
: normal script node, no need to return in the script.
switch_script
: Select the script node, the selected node ID needs to be returned in the script.
if_script
: Conditional script node, the script needs to return true/false.
for_script
: The number of loop nodes, the script needs to return a value, indicating the number of loops.
while_script
: Conditional loop node, the script needs to return true/false, indicating what conditions will continue the loop.
break_script
: Exit the loop node, the script needs to return true/false, indicating when to exit the loop.
You can write dynamic scripts to handle business logic in the place of the example.
提示
Here language
is not necessary, because LiteFlow dynamically selects the scripting language according to the jar package you depend on, and uses the SPI mechanism.
The language
declaration here is mainly used for the LiteFlowX
plugin for syntax highlighting. If you don't add it, there will be no corresponding syntax highlighting and hinting.
Of course make sure your LiteFlowX
plugin version is 1.1.1
and above.
notice
The context bean passed in the example is the default DefaultContext
. If it is your custom context bean, please call the corresponding method to set and get data.