RuleGo RuleGo
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文
🏠Home
  • Quick Start
  • Rule Chain
  • Standard Components
  • Extension Components
  • Custom Components
  • Visualization
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • Trigger
  • Advanced Topics
  • Performance
  • Standard Components
  • Extension Components
  • Custom Components
  • Components Marketplace
  • Overview
  • Quick Start
  • Routing
  • DSL
  • API
  • Options
  • Components
🔥Editor (opens new window)
  • RuleGo Editor (opens new window)
  • RuleGo Server (opens new window)
  • Github (opens new window)
  • Gitee (opens new window)
  • Changelog (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • Quick Start

  • Rule Chain

  • Standard Components

    • Standard Components Overview
    • filter

    • action

    • transform

    • external

    • flow

      • Sub Rule Chain
        • Configuration
        • Relation Type
        • Example
      • Node Reference
  • Extension Components

  • Custom Components

  • Components marketplace

  • Visualization

  • AOP

  • Trigger

  • Advanced Topic

  • RuleGo-Server

  • FAQ

  • Endpoint Module

  • Support

目录

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
extend v0.27.0+ bool false Whether to inherit the output relationType and messages of the child rule false
  • extend
    • extend=true The outputs and relationships of the child rule chain are used as inputs for the next node without merging the results.
    • extend=false The outputs of all child rules are merged, and the combined results are sent to the next node through the Success relationship. The format of the merged message is: []WrapperMsg

# 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

img

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"
      }
    ]
  }
}
1
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
1

Define the sub-rule chain (subChain3) configuration:

//Omitted
1

Define the sub-rule chain (subChainN) configuration:

//Omitted
1

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"
      }
    ]
  }
}
1
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))
1
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))
1

Process Messages:

ruleEngine.OnMsg(msg)
1

`

Edit this page on GitHub (opens new window)
Last Updated: 2025/03/31, 01:52:11
net
Node Reference

← net Node Reference→

Theme by Vdoing | Copyright © 2023-2025 RuleGo Team | Apache 2.0 License

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式