链的构建方式(编排)
基础知识(字典,接口),可参考:《solon / solon-flow 插件》
1、使用配置构建(支持 json 或 yml)
- 示例1
demo1.chain.json
(审批风格)。示例中,使用了元信息和空任务(用于触发驱动器的任务处理)。
{
"id": "d1",
"title": "请假审批",
"nodes": [
{ "id": "n1", "type": "start", "title":"发起人", "meta": {"role": "employee"}, "link": "n2", "task":"."},
{ "id": "n2", "type": "exclusive", "link": [
{"nextId": "n3", "title": "3天以上", "condition": "day>=3 && day<7"},
{"nextId": "n4", "title": "7天以上", "condition": "day>=7"},
{"nextId": "n5", "title": "3天以内", "condition": "day<3"}
] },
{ "id": "n3", "type": "execute", "title":"主管批", "meta": {"role": "lead"}, "link": "n5", "task":"."},
{ "id": "n4", "type": "execute", "title":"部门经理批","meta": {"role": "boss"}, "link": "n5", "task":"."},
{ "id": "n5", "type": "end", "task":"."}
]
}
示意图:
- 示例2
demo2.chain.yml
(计算风格)。示例中,使用了"@"组件型任务
# 使用 yml 格式,可以更简洁些
id: "d2"
title: "风控计算"
nodes:
- { id: "n1", type: "start", link: "n2"}
- { id: "n2", type: "execute", title: "基本信息评分", link: "n3", task: "@base_score"}
- { id: "n3", type: "exclusive", title: "分流", link: [
{nextId: "n6", title: "优质用户(评分90以上)", condition: "user.score > 90"},
{nextId: "n4", title: "普通用户"}]}
- { id: "n4", type: "execute", title: "电商消费评分", link: "n5", task: "@ec_score"}
- { id: "n5", type: "execute", title: "黑名单检测", link: "n6", task: "@bl_score"}
- { id: "n6", type: "end"}
示意图:
2、使用代码构建
代码构建提供了更开放的机制,意味着可以自定义配置格式(自己解析),或者分解存放到持久层。
- 示例1(审批风格)。对就上面的配置
@Configuration
public class Demo {
@Bean
public void case1(FlowEngine flowEngine){
Chain chain = new Chain("d1", "请假审批");
chain.addNode(new NodeDecl("n1", NodeType.start).title("发起人").metaPut("role","employee").linkAdd("n2").task("."));
chain.addNode(new NodeDecl("n2", NodeType.exclusive)
.linkAdd("n3", l->l.title("3天以上").condition("day>=3 && day<7"))
.linkAdd("n4", l->l.title("7天以上").condition("day>=7"))
.linkAdd("n5", l->l.title("3天以内").condition("day<3"))
);
chain.addNode(new NodeDecl("n3", NodeType.execute).title("主管批").metaPut("role","lead").linkAdd("n5").task("."));
chain.addNode(new NodeDecl("n4", NodeType.execute).title("部门经理批").metaPut("role","boss").linkAdd("n5").task("."));
chain.addNode(new NodeDecl("n5", NodeType.end).task("."));
//配置好后,装载到引擎
flowEngine.load(chain);
//执行
flowEngine.eval("d1", ...);
}
}
- 示例2(计算风格)。对就上面的配置
@Configuration
public class Demo {
@Bean
public void case2(FlowEngine flowEngine){
Chain chain = new Chain("d2", "风控计算");
chain.addNode(new NodeDecl("n1", NodeType.start).linkAdd("n2"));
chain.addNode(new NodeDecl("n2", NodeType.execute).title("基本信息评分").linkAdd("n3").task("@base_score"));
chain.addNode(new NodeDecl("n3", NodeType.exclusive).title("分流")
.linkAdd("n6", l->l.title("优质用户(评分90以上)").condition("user.score > 90"))
.linkAdd("n4", l->l.title("普通用户")) //没条件时,做为默认
);
chain.addNode(new NodeDecl("n4", NodeType.execute).title("电商消费评分").linkAdd("n5").task("@ec_score"));
chain.addNode(new NodeDecl("n5", NodeType.execute).title("黑名单检测").linkAdd("n6").task("@bl_score"));
chain.addNode(new NodeDecl("n6", NodeType.end).task("."));
//配置好后,装载到引擎
flowEngine.load(chain);
//执行
flowEngine.eval("d2", ...);
}
}