-
Notifications
You must be signed in to change notification settings - Fork 0
/
transition.go
144 lines (122 loc) · 3.5 KB
/
transition.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
package loong
import (
"context"
"fmt"
"sync"
"github.com/it512/loong/bpmn"
)
func choose(ctx context.Context, ae ActivationEvaluator, in []bpmn.TSequenceFlow) (out []bpmn.TSequenceFlow, err error) {
if len(in) == 1 {
return in, nil
}
for _, flow := range in {
if flow.HasConditionExpression() {
var b bool
if b, _, err = eval[bool](ctx, ae, flow.GetConditionExpression()); err != nil {
return
}
if b {
out = append(out, flow)
}
}
}
if len(out) == 0 {
out = in
}
return
}
func chooseDefault(me outer, flows []bpmn.TSequenceFlow) bpmn.TSequenceFlow {
if len(flows) == 1 {
return flows[0]
}
if d, ok := me.(bpmn.DefaultAttrElement); ok {
if key := d.GetDefault(); key != "" {
if ele, ok := bpmn.Find(flows, key); ok {
return ele
}
}
}
for _, flow := range flows {
if !flow.HasConditionExpression() {
return flow
}
}
return flows[0] // 默认返回第一条
}
type sequenceFlow struct {
Variable
bpmn.TSequenceFlow
target BpmnElement
UnimplementedActivity
}
func (c *sequenceFlow) Do(_ context.Context) error {
c.Exec.InTag = c.GetId()
var ok bool
c.target, ok = c.ProcInst.Template.FindElementByID(c.TargetRef)
if !ok {
panic(fmt.Errorf("未找到目标 TargetRef = %s", c.TargetRef))
}
c.elementID = c.target.GetId()
c.elementType = c.target.GetType()
return nil
}
func (c *sequenceFlow) Emit(_ context.Context, commit Emitter) (err error) {
switch c.target.GetType() {
case bpmn.UserTask:
err = commit.Emit(&userTaskOp{UserTask: UserTask{Variable: c.Variable}, InOut: newInOut(), TUserTask: bpmn.Cast[bpmn.TUserTask](c.target)})
case bpmn.ExclusiveGateway:
err = commit.Emit(&exclusivGatewayOp{Variable: c.Variable, TExclusiveGateway: bpmn.Cast[bpmn.TExclusiveGateway](c.target)})
case bpmn.ParallelGateway:
err = commit.Emit(¶llelGatewayCmd{TParallelGateway: bpmn.Cast[bpmn.TParallelGateway](c.target), Exec: c.Exec})
case bpmn.ServiceTask:
err = commit.Emit(&serviceTaskOp{Variable: c.Variable, InOut: newInOut(), TServiceTask: bpmn.Cast[bpmn.TServiceTask](c.target)})
case bpmn.EndEvent:
err = commit.Emit(&EndEventOp{Exec: c.Exec, TEndEvent: bpmn.Cast[bpmn.TEndEvent](c.target)})
case bpmn.IntermediateThrowEvent:
op := doIntermediationThrowEvent(c.Variable, bpmn.Cast[bpmn.TIntermediateThrowEvent](c.target))
err = commit.Emit(op)
case bpmn.Task:
err = commit.Emit(&taskOp{Variable: c.Variable, TTask: bpmn.Cast[bpmn.TTask](c.target)})
default:
panic(fmt.Errorf("不支持的类型 Type: %s, ID: %s", c.target.GetType(), c.target.GetId()))
}
putToPool(c)
return
}
var sfPool = sync.Pool{
New: func() any {
return &sequenceFlow{}
},
}
func getFromPool(v Variable, f bpmn.TSequenceFlow) *sequenceFlow {
sf := sfPool.Get().(*sequenceFlow)
sf.Variable.Exec = v.Exec
sf.Variable.Input = v.Input
sf.Variable.isVarChanged = false
sf.TSequenceFlow = f
return sf
}
func putToPool(sf *sequenceFlow) {
sfPool.Put(sf)
}
type outer interface {
GetOutgoingAssociation() []string
FindSequenceFlow(string) (bpmn.TSequenceFlow, bool)
FindSequenceFlows([]string) []bpmn.TSequenceFlow
ActivationEvaluator
}
func fromVariable(v Variable, out string) *sequenceFlow {
if f, ok := v.ProcInst.Template.FindSequenceFlow(out); ok {
return getFromPool(v, f)
}
panic("未找到Sequenceflow")
}
func fromOuter(ctx context.Context, v Variable, o outer) *sequenceFlow {
flows := o.FindSequenceFlows(o.GetOutgoingAssociation())
out, err := choose(ctx, o, flows)
if err != nil {
panic(err)
}
f := chooseDefault(o, out)
return getFromPool(v, f)
}