Sub Rule Chain
flow
: Sub-rule chain component, used for rule chain nesting. For complex rule chain configurations, this nesting method can improve the maintainability and reusability of the rule chain.
For example: The message structure of different business types is different, you can create corresponding sub-rule chains according to different business types, and then use the routing node
in the root rule chain to route different businesses to the corresponding sub-rule chains for business processing.
In addition, the sub-rule chain component can be connected to other components after execution. Used to solve: Asynchronously execute A component and B component, and execute C after both are finished. Then you can put A and B into the sub-rule chain using a parallel connection method, and then connect C on the root rule chain.
# Configuration
Field | Type | Required | Description | Default value |
---|---|---|---|---|
targetId | string | Yes | Sub-rule chain ID | None |
# Relation Type
- Success: After all branches of the sub-rule chain are executed, the messages of each end chain are merged and sent to the
Success
chain - Failure: If the sub-rule chain instance is not found, send the message to the
Failure
chain
The message format of the successful message merge:
- metadata: Merge the metadata processed by each end node, if the same key is overwritten.
- data: Wrap the messages processed by each end node into a WrapperMsg array. WrapperMsg:
Field | Type | Description | Default value |
---|---|---|---|
msg | types.RuleMsg | Message | None |
err | string | null | |
nodeId | string | The last processing node | None |
# Example
Nested rule chain diagram
Where subChain01, subChain02, subChain03, subChainN are sub-rule chain nodes.
Define the sub-rule chain (subChain1) configuration:
{
"ruleChain": {
"id":"subChain01",
"name": "Sub-rule chain"
},
"metadata": {
"nodes": [
{
"id": "sub_s1",
"type": "jsFilter",
"name": "Filter",
"debugMode": true,
"configuration": {
"jsScript": "return msg=='aa';"
}
},
{
"id": "sub_s2",
"type": "jsTransform",
"name": "Transform",
"debugMode": true,
"configuration": {
"jsScript": "metadata['test']='Modified by sub chain';\n metadata['index']=52;\n msgType='TEST_MSG_TYPE2';var msg2={};\n msg2['bb']=22\n return {'msg':msg2,'metadata':metadata,'msgType':msgType};"
}
}
],
"connections": [
{
"fromId": "sub_s1",
"toId": "sub_s2",
"type": "True"
}
]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Define the sub-rule chain (subChain2) configuration:
//Omitted
Define the sub-rule chain (subChain3) configuration:
//Omitted
Define the sub-rule chain (subChainN) configuration:
//Omitted
Define the root rule chain (rootRule01) configuration:
{
"ruleChain": {
"id":"rootRule01",
"name": "Test root rule chain"
},
"metadata": {
"nodes": [
{
"id": "s1",
"type": "msgTypeSwitch",
"name": "Message routing"
},
{
"id": "sub_s1",
"type": "flow",
"name": "Sub-node 1",
"configuration": {
"targetId": "subChain01"
}
},
{
"id": "sub_s2",
"type": "flow",
"name": "Sub-node 2",
"configuration": {
"targetId": "subChain02"
}
},
{
"id": "sub_s3",
"type": "flow",
"name": "Sub-node 3",
"configuration": {
"targetId": "subChain03"
}
},
{
"id": "sub_n",
"type": "flow",
"name": "Sub-node N",
"configuration": {
"targetId": "subChainN"
}
}
],
"connections": [
{
"fromId": "root_s1",
"toId": "sub_s1",
"type": "MSG_TYPE1"
},
{
"fromId": "root_s1",
"toId": "sub_s2",
"type": "MSG_TYPE2"
},
{
"fromId": "root_s1",
"toId": "sub_s3",
"type": "MSG_TYPE3"
},
{
"fromId": "root_s1",
"toId": "sub_n",
"type": "MSG_TYPE_N"
}
]
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Initialize the sub-rule chain and root rule chain:
//Initialize the sub-rule chain
subRuleEngine1, err := rulego.New("subChain01", []byte(subRuleChain01Fille), WithConfig(config))
subRuleEngine2, err := rulego.New("subChain02", []byte(subRuleChain02Fille), WithConfig(config))
subRuleEngine3, err := rulego.New("subChain03", []byte(subRuleChain03Fille), WithConfig(config))
subRuleEngineN, err := rulego.New("subChainN", []byte(subRuleChainNFille), WithConfig(config))
//Initialize the root rule chain
ruleEngine, err := rulego.New("rootRule01", []byte(rootRuleChain), WithConfig(config))
2
3
4
5
6
7
You can also batch initialize rule chains by loading a specified folder:
err := rulego.Load("./chains/", rulego.WithConfig(config))
Process Messages:
ruleEngine.OnMsg(msg)
`