结束节点
end
组件:结束节点。用于触发规则链的结束回调。如果规则链设置了结束节点组件,则会替代默认的分支结束行为,只有运行到结束节点组件时,才会触发结束回调。
# 功能说明
- 接收消息并触发DoOnEnd回调 - 当消息到达end节点时,会立即触发规则链的结束回调
- 使用上一个节点传入的关系类型 - 保持消息流的关系类型传递
- 不会继续传递消息到下一个节点 - 作为规则链的终点,不再向后传递消息
# 使用场景
- 规则链的明确结束点 - 在复杂的规则链中明确标识结束位置
- 触发特定的结束处理逻辑 - 在特定条件下触发结束回调
- 替代默认的分支结束行为 - 控制何时触发OnEnd回调
# 重要说明
如果需要规则链只有一个返回,在分支增加这个节点,规则链只会在这个节点触发OnEnd回调,否则每个结束分支都会调用OnEnd回调。
这意味着:
- 没有end节点:每个分支结束时都会触发OnEnd回调
- 有end节点:只有到达end节点时才会触发OnEnd回调,其他分支结束不会触发
# 配置
end组件无需任何配置参数。
# Relation Type
end组件不产生新的关系类型,它使用上一个节点传入的关系类型来触发DoOnEnd回调。
# 配置示例
{
"ruleChain": {
"id": "rule01",
"name": "测试规则链",
"root": true,
"debugMode": false
},
"metadata": {
"nodes": [
{
"id": "s1",
"type": "jsFilter",
"name": "过滤器",
"debugMode": true,
"configuration": {
"jsScript": "return msg.temperature > 50;"
}
},
{
"id": "s2",
"type": "log",
"name": "记录日志",
"debugMode": true,
"configuration": {
"jsScript": "return 'temperature:' + msg.temperature;"
}
},
{
"id": "s3",
"type": "end",
"name": "结束节点",
"debugMode": true
}
],
"connections": [
{
"fromId": "s1",
"toId": "s2",
"type": "True"
},
{
"fromId": "s2",
"toId": "s3",
"type": "Success"
}
],
"ruleChainConnections": null
}
}
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
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
# 应用示例
# 场景1:单一结束点
// 创建规则引擎
ruleEngine, err := rulego.New("rule01", []byte(ruleChainFile))
if err != nil {
panic(err)
}
// 设置结束回调
ruleEngine.OnEnd(func(ctx types.RuleContext, msg types.RuleMsg, err error, relationType string) {
fmt.Printf("规则链结束: relationType=%s, data=%s\n", relationType, msg.Data)
})
// 发送消息
msg := types.NewMsg(0, "TELEMETRY", types.JSON, `{"temperature":60}`)
ruleEngine.OnMsg(msg)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 场景2:多分支但单一结束点
{
"metadata": {
"nodes": [
{
"id": "filter",
"type": "jsFilter",
"configuration": {
"jsScript": "return msg.temperature > 50;"
}
},
{
"id": "highTemp",
"type": "log",
"configuration": {
"jsScript": "return '高温告警:' + msg.temperature;"
}
},
{
"id": "normalTemp",
"type": "log",
"configuration": {
"jsScript": "return '正常温度:' + msg.temperature;"
}
},
{
"id": "end",
"type": "end",
"name": "统一结束点"
}
],
"connections": [
{
"fromId": "filter",
"toId": "highTemp",
"type": "True"
},
{
"fromId": "filter",
"toId": "normalTemp",
"type": "False"
},
{
"fromId": "highTemp",
"toId": "end",
"type": "Success"
},
{
"fromId": "normalTemp",
"toId": "end",
"type": "Success"
}
]
}
}
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
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
在这个例子中,无论消息走哪个分支,最终都会到达end节点,确保OnEnd回调只被触发一次。
# 注意事项
- end节点不需要任何配置参数
- end节点不会向下游传递消息
- 使用end节点可以精确控制OnEnd回调的触发时机
- 在复杂的多分支规则链中,建议使用end节点来统一结束点
- end节点会保持上游节点传入的关系类型
在 GitHub 上编辑此页 (opens new window)
上次更新: 2025/08/18, 04:07:16