包容分支
inclusive组件:包容分支组件。与switch条件分支不同,inclusive会评估所有配置的case表达式,并将消息同时路由到所有匹配的关系;如果无任何匹配,则路由到默认的Default关系;当表达式执行出错时,消息将被路由到Failure关系。
# 配置
| 字段 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| cases | Case列表 | 条件表达式列表 | 无 |
Case配置项:
| 字段 | 类型 | 说明 | 默认值 |
|---|---|---|---|
| case | string | 条件表达式 | 无 |
| then | string | 路由关系名称 | 无 |
表达式使用 expr (opens new window) 引擎,支持以下内置变量:
id- 消息IDts- 消息时间戳(毫秒)data- 消息原始内容msg- 消息体。若dataType为JSON,可通过msg.field访问字段,例如:msg.temperature > 50metadata- 消息元数据,例如metadata.customerNametype- 消息类型dataType- 数据类型
表达式示例:
msg.temperature > 50msg.temperature > 50 && metadata.customerName == 'rulego'upper(metadata.customerName[:4]) == 'GO'replace(toJSON(msg),'name','productName')msg.humidity >= 80 || msg.temperature >= 30
更多expr表达式语法和函数请参考:expr语言定义 (opens new window)
# Relation Type
- Case.then: 当匹配到某个
case表达式时,消息将被路由到该case配置的then关系链(可能同时命中多个分支) - Default: 当所有
case表达式都匹配失败时,消息将被路由到Default关系链 - Failure: 当表达式执行出错时,消息将被路由到
Failure关系链
# 执行结果
该组件是纯路由组件,不会修改传入的msg、metadata和msgType内容。它会为每个匹配到的case分支分别转发一次消息;当无任何匹配时转发到Default关系。
实现参考:在 d:\github\rulego\components\common\inclusive_node.go:66 中,组件遍历所有case表达式,命中后收集对应关系并通过 TellNext 同时路由到多个关系;未命中则路由到默认关系。
# 与条件分支的区别
switch(条件分支):按顺序匹配case表达式,命中第一个后停止匹配,仅路由到一个分支。inclusive(包容分支):评估所有case表达式,命中的所有分支会被同时路由;若全部未命中则路由到Default。- 错误处理:两者一致,表达式执行错误时路由到
Failure。
# 配置示例
{
"ruleChain": {
"id": "inc-01",
"name": "测试包容分支",
"debugMode": true,
"root": true
},
"metadata": {
"endpoints": [],
"nodes": [
{
"id": "node_inclusive",
"additionalInfo": {
"description": "",
"layoutX": 480,
"layoutY": 280
},
"type": "inclusive",
"name": "包容分支",
"debugMode": false,
"configuration": {
"cases": [
{
"case": "msg.temperature>=20 && msg.temperature<=50",
"then": "Case1"
},
{
"case": "msg.temperature>50",
"then": "Case2"
}
]
}
},
{
"id": "node_case1",
"additionalInfo": {
"description": "",
"layoutX": 840,
"layoutY": 160
},
"type": "jsTransform",
"name": "case1",
"debugMode": false,
"configuration": {
"jsScript": "msg=msg||{}\nmsg.match='Case1'\nreturn {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
},
{
"id": "node_case2",
"additionalInfo": {
"description": "",
"layoutX": 840,
"layoutY": 280
},
"type": "jsTransform",
"name": "case2",
"debugMode": false,
"configuration": {
"jsScript": "msg=msg||{}\nmsg.match='Case2'\nreturn {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
},
{
"id": "node_default",
"additionalInfo": {
"description": "",
"layoutX": 840,
"layoutY": 380
},
"type": "jsTransform",
"name": "default",
"debugMode": false,
"configuration": {
"jsScript": "msg=msg||{}\nmsg.match='Default'\nreturn {'msg':msg,'metadata':metadata,'msgType':msgType};"
}
}
],
"connections": [
{ "fromId": "node_inclusive", "toId": "node_case1", "type": "Case1" },
{ "fromId": "node_inclusive", "toId": "node_case2", "type": "Case2" },
{ "fromId": "node_inclusive", "toId": "node_default", "type": "Default" }
]
}
}
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/11/27, 10:15:33