RuleGo RuleGo
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文
🏠首页
  • 快速入门
  • 规则链
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 可视化
  • RuleGo-Server
  • RuleGo-MCP-Server
  • AOP
  • 触发器
  • 高级主题
  • 性能
  • 标准组件
  • 扩展组件
  • 自定义组件
  • 组件市场
  • 概述
  • 快速入门
  • 路由
  • DSL
  • API
  • Options
  • 组件
🔥编辑器 (opens new window)
  • 可视化编辑器 (opens new window)
  • RuleGo-Server (opens new window)
  • ❓问答

    • FAQ
💖支持
👥加入社区
  • Github (opens new window)
  • Gitee (opens new window)
  • GitCode (opens new window)
  • 更新日志 (opens new window)
  • English
  • 简体中文

广告采用随机轮播方式显示 ❤️成为赞助商
  • 快速入门

  • 规则链

  • 标准组件

    • 标准组件概述
    • 过滤器

    • 动作

    • 转换器

    • 外部的

    • 流

      • 子规则链
        • 配置
        • Relation Type
        • 示例
      • 节点引用
  • 扩展组件

  • 自定义组件

  • 组件市场

  • 可视化

  • AOP

  • 触发器

  • 高级主题

  • RuleGo-Server

  • 问题

目录

子规则链

flow:子规则链组件,用于规则链嵌套。对于复杂的规则链配置,可以通过这种嵌套方式提高规则链的可维护性和重复利用性。

例如:不同的业务类型它的消息结构不一样,可以根据不同的业务类型创建对应的子规则链,然后在根规则链使用路由节点把不同业务路由到对应的子规则链进行业务处理。

另外子规则链组件执行完之后可以接其他组件。用于解决:异步执行A组件和B组件,都执行结束后执行C。那么可以把A和B放到子规则链使用并行的连接方式,然后在根规则链连上C。

# 配置

字段 类型 是否必填 说明 默认值
targetId string 是 子规则链ID 无
extend v0.27.0+ bool false 是否继承子规则输出关系和消息 false
  • extend
    • extend=true 子规则链的每一个输出和关系作为下一个节点的输入,不合并结果
    • extend=false 合并子规则所有输出结果,通过Success关系发送到下一个节点。合并后消息格式:[]WrapperMsg

# Relation Type

如果extend=true 则子规则链的每一个输出和关系作为下一个节点的输入。否则按以下规则合并子规则链处理结果:

  • Success: 子规则链所有分支执行完后,把每个结束链消息合后发送到Success链
  • Failure: 找不到子规则链实例或者子规则链某个分支执行失败,把消息发送到Failure链

成功消息合并的消息格式:

  • metadata: 合并每个结束节点处理后的metadata,如果相同key则覆盖。
  • data: 把每个结束节点处理后的消息封装成WrapperMsg数组。 WrapperMsg:
字段 类型 说明 默认值
msg types.RuleMsg 消息 无
err string ""
nodeId string 最后的处理节点 ""

# 示例

img

嵌套规则链图

其中 subChain01、subChain02、subChain03、subChainN 是子规则链节点。

定义子规则链(subChain1)配置:

{
  "ruleChain": {
    "id":"subChain01",
    "name": "子规则链"
  },
  "metadata": {
    "nodes": [
      {
        "id": "sub_s1",
        "type": "jsFilter",
        "name": "过滤",
        "debugMode": true,
        "configuration": {
          "jsScript": "return msg=='aa';"
        }
      },
      {
        "id": "sub_s2",
        "type": "jsTransform",
        "name": "转换",
        "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

定义子规则链(subChain2)配置:

//省略
1

定义子规则链(subChain3)配置:

//省略
1

定义子规则链(subChainN)配置:

//省略
1

定义根规则链(rootRule01)配置:

{
  "ruleChain": {
    "id":"rootRule01",
    "name": "测试根规则链"
  },
  "metadata": {
    "nodes": [
      {
        "id": "s1",
        "type": "msgTypeSwitch",
        "name": "消息路由"
      },
      {
        "id": "sub_s1",
        "type": "flow",
        "name": "子节点1",
        "configuration": {
          "targetId": "subChain01"
        }
      },
      {
        "id": "sub_s2",
        "type": "flow",
        "name": "子节点2",
        "configuration": {
          "targetId": "subChain02"
        }
      },
      {
        "id": "sub_s3",
        "type": "flow",
        "name": "子节点3",
        "configuration": {
          "targetId": "subChain03"
        }
      },
      {
        "id": "sub_n",
        "type": "flow",
        "name": "子节点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

始化子规则链和根规则链:

//初始化子规则链
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))
//初始化根规则链
ruleEngine, err := rulego.New("rootRule01", []byte(rootRuleChain), WithConfig(config))
1
2
3
4
5
6
7

也可以通过加载指定文件夹方式批量初始化规则链:

err := rulego.Load("./chains/", rulego.WithConfig(config))
1

处理消息:

ruleEngine.OnMsg(msg)
1
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/03/31, 01:52:11
TCP/UDP客户端
节点引用

← TCP/UDP客户端 节点引用→

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

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