-
Notifications
You must be signed in to change notification settings - Fork 36
/
node_router.go
323 lines (277 loc) · 7.29 KB
/
node_router.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package flow
import (
"context"
"encoding/json"
"github.com/antlinker/flow/schema"
"github.com/pkg/errors"
)
// 定义错误
var (
ErrNotFound = errors.New("未找到流程相关的信息")
)
type (
// NextNodeHandle 定义下一节点处理函数
NextNodeHandle func(*schema.Node, *schema.NodeInstance, []*schema.NodeCandidate)
// EndHandle 定义流程结束处理函数
EndHandle func(*schema.FlowInstance)
)
type nodeRouterOptions struct {
autoStart bool
onNextNode NextNodeHandle
onFlowEnd EndHandle
}
// NodeRouterOption 节点路由配置
type NodeRouterOption func(*nodeRouterOptions)
// AutoStartOption 自动开始流程配置
func AutoStartOption(autoStart bool) NodeRouterOption {
return func(o *nodeRouterOptions) {
o.autoStart = autoStart
}
}
// OnNextNodeOption 注册下一节点处理事件配置
func OnNextNodeOption(fn NextNodeHandle) NodeRouterOption {
return func(o *nodeRouterOptions) {
o.onNextNode = fn
}
}
// OnFlowEndOption 注册流程结束事件
func OnFlowEndOption(fn EndHandle) NodeRouterOption {
return func(o *nodeRouterOptions) {
o.onFlowEnd = fn
}
}
// NodeRouter 节点路由
type NodeRouter struct {
ctx context.Context
node *schema.Node
flowInstance *schema.FlowInstance
nodeInstance *schema.NodeInstance
inputData []byte
engine *Engine
opts *nodeRouterOptions
parent *NodeRouter
stop bool
}
// Init 初始化节点路由
func (n *NodeRouter) Init(ctx context.Context, engine *Engine, nodeInstanceID string, inputData []byte, options ...NodeRouterOption) (*NodeRouter, error) {
opts := &nodeRouterOptions{
autoStart: true,
}
for _, opt := range options {
opt(opts)
}
if ctx == nil {
ctx = context.Background()
}
n.ctx = ctx
n.opts = opts
n.inputData = inputData
n.engine = engine
nodeInstance, err := n.engine.flowBll.GetNodeInstance(nodeInstanceID)
if err != nil {
return nil, err
} else if nodeInstance == nil {
return nil, ErrNotFound
}
n.nodeInstance = nodeInstance
flowInstance, err := n.engine.flowBll.GetFlowInstance(nodeInstance.FlowInstanceID)
if err != nil {
return nil, err
} else if flowInstance == nil {
return nil, ErrNotFound
}
n.flowInstance = flowInstance
node, err := n.engine.flowBll.GetNode(nodeInstance.NodeID)
if err != nil {
return nil, err
} else if node == nil {
return nil, ErrNotFound
}
n.node = node
return n, nil
}
func (n *NodeRouter) next(nodeInstanceID, processor string) (*NodeRouter, error) {
nextRouter, err := new(NodeRouter).Init(n.ctx, n.engine, nodeInstanceID, n.inputData)
if err != nil {
return nil, err
}
nextRouter.opts = n.opts
nextRouter.parent = n
err = nextRouter.Next(processor)
if err != nil {
return nil, err
}
return nextRouter, nil
}
// GetFlowInstance 获取流程实例
func (n *NodeRouter) GetFlowInstance() *schema.FlowInstance {
return n.flowInstance
}
// Next 流向下一节点
func (n *NodeRouter) Next(processor string) error {
nodeType, err := GetNodeTypeByName(n.node.TypeCode)
if err != nil {
return err
}
if nodeType == UserTask && n.parent != nil {
pNodeType, err := GetNodeTypeByName(n.parent.node.TypeCode)
if err != nil {
return err
}
if !(pNodeType == StartEvent && n.parent.opts.autoStart) {
// 通知下一节点实例事件
if fn := n.opts.onNextNode; fn != nil {
candidates, err := n.engine.flowBll.QueryNodeCandidates(n.nodeInstance.RecordID)
if err != nil {
return err
}
fn(n.node, n.nodeInstance, candidates)
}
return nil
}
}
// 完成当前节点
err = n.engine.flowBll.DoneNodeInstance(n.nodeInstance.RecordID, processor, n.inputData)
if err != nil {
return err
}
// 如果当前节点是人工任务,检查下一节点是否是并行网关,如果是则检查还未完成的待办事项,如果有则停止流转
if nodeType == UserTask && n.parent == nil {
ok, err := n.checkNextNodeType(ParallelGateway)
if err != nil {
return err
} else if ok {
exists, err := n.engine.flowBll.CheckFlowInstanceTodo(n.flowInstance.RecordID)
if err != nil {
return err
} else if exists {
return nil
}
}
}
// 如果是结束事件或终止事件,则停止流转
if nodeType == EndEvent ||
nodeType == TerminateEvent {
isEnd := false
// 如果是结束事件,则检查还未完成的待办事项,如果没有则结束流程并通知结束事件
if nodeType == EndEvent {
exists, err := n.engine.flowBll.CheckFlowInstanceTodo(n.flowInstance.RecordID)
if err != nil {
return err
} else if !exists {
isEnd = true
}
}
// 如果是终止事件,则结束流程并通知结束事件
if nodeType == TerminateEvent {
isEnd = true
}
if isEnd {
// 流程实例结束处理
err = n.engine.flowBll.DoneFlowInstance(n.flowInstance.RecordID)
if err != nil {
return err
}
n.stop = true
if fn := n.opts.onFlowEnd; fn != nil {
fn(n.flowInstance)
}
}
return nil
}
// 增加下一节点
nodeInstanceIDs, err := n.addNextNodeInstances()
if err != nil {
return err
}
for _, instanceID := range nodeInstanceIDs {
nextRouter, err := n.next(instanceID, processor)
if err != nil {
return err
}
if nextRouter.stop {
break
}
}
return nil
}
// 增加下一处理节点实例
func (n *NodeRouter) addNextNodeInstances() ([]string, error) {
routers, err := n.engine.flowBll.QueryNodeRouters(n.node.RecordID)
if err != nil {
return nil, err
} else if len(routers) == 0 {
return nil, nil
}
var nodeInstanceIDs []string
for _, r := range routers {
if r.Expression != "" {
allow, err := n.engine.execer.ExecReturnBool(n.ctx, []byte(r.Expression), n.getExpData())
if err != nil {
return nil, err
} else if !allow {
continue
}
}
// 查询指派人表达式
assigns, err := n.engine.flowBll.QueryNodeAssignments(r.TargetNodeID)
if err != nil {
return nil, err
}
var candidates []string
for _, assign := range assigns {
ss, err := n.engine.execer.ExecReturnStringSlice(n.ctx, []byte(assign.Expression), n.getExpData())
if err != nil {
return nil, err
}
candidates = append(candidates, ss...)
}
instanceID, err := n.engine.flowBll.CreateNodeInstance(n.flowInstance.RecordID, r.TargetNodeID, n.inputData, candidates)
if err != nil {
return nil, err
}
nodeInstanceIDs = append(nodeInstanceIDs, instanceID)
}
return nodeInstanceIDs, nil
}
// 检查下一节点类型
func (n *NodeRouter) checkNextNodeType(t NodeType) (bool, error) {
routers, err := n.engine.flowBll.QueryNodeRouters(n.node.RecordID)
if err != nil {
return false, err
} else if len(routers) == 0 {
return false, nil
}
for _, r := range routers {
if r.Expression != "" {
allow, err := n.engine.execer.ExecReturnBool(n.ctx, []byte(r.Expression), n.getExpData())
if err != nil {
return false, err
} else if !allow {
continue
}
}
node, err := n.engine.flowBll.GetNode(r.TargetNodeID)
if err != nil {
return false, err
} else if node == nil {
return false, nil
}
if node.TypeCode == t.String() {
return true, nil
}
}
return false, nil
}
// 获取表达式数据
func (n *NodeRouter) getExpData() []byte {
var input map[string]interface{}
json.Unmarshal(n.inputData, &input)
r := map[string]interface{}{
"input": input,
"flow": n.flowInstance,
"node": n.nodeInstance,
}
b, _ := json.Marshal(r)
return b
}