-
Notifications
You must be signed in to change notification settings - Fork 58
/
engine.go
362 lines (330 loc) · 13.2 KB
/
engine.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package bpmn_engine
import (
"errors"
"fmt"
"time"
"github.com/nitram509/lib-bpmn-engine/pkg/bpmn_engine/exporter"
"github.com/nitram509/lib-bpmn-engine/pkg/spec/BPMN20"
"github.com/nitram509/lib-bpmn-engine/pkg/spec/BPMN20/activity"
"github.com/nitram509/lib-bpmn-engine/pkg/spec/BPMN20/process_instance"
)
type BpmnEngine interface {
LoadFromFile(filename string) (*ProcessInfo, error)
LoadFromBytes(xmlData []byte) (*ProcessInfo, error)
AddTaskHandler(taskId string, handler func(job ActivatedJob))
CreateInstance(processKey int64, variableContext map[string]interface{}) (*ProcessInstanceInfo, error)
CreateAndRunInstance(processKey int64, variableContext map[string]interface{}) (*ProcessInstanceInfo, error)
RunOrContinueInstance(processInstanceKey int64) (*ProcessInstanceInfo, error)
GetName() string
GetProcessInstances() []*ProcessInstanceInfo
FindProcessInstanceById(processInstanceKey int64) *ProcessInstanceInfo
}
const continueNextElement = true
// New creates an engine with an arbitrary name of the engine;
// useful in case you have multiple ones, in order to distinguish them.
func New(name string) BpmnEngineState {
snowflakeIdGenerator := initializeSnowflakeIdGenerator()
return BpmnEngineState{
name: name,
processes: []ProcessInfo{},
processInstances: []*ProcessInstanceInfo{},
handlers: map[string]func(job ActivatedJob){},
jobs: []*job{},
messageSubscriptions: []*MessageSubscription{},
snowflake: snowflakeIdGenerator,
exporters: []exporter.EventExporter{},
}
}
// CreateInstance creates a new instance for a process with given processKey
// will return (nil, nil), when no process with given was found
func (state *BpmnEngineState) CreateInstance(processKey int64, variableContext map[string]interface{}) (*ProcessInstanceInfo, error) {
if variableContext == nil {
variableContext = map[string]interface{}{}
}
for _, process := range state.processes {
if process.ProcessKey == processKey {
processInstanceInfo := ProcessInstanceInfo{
processInfo: &process,
instanceKey: state.generateKey(),
variableContext: variableContext,
createdAt: time.Now(),
state: process_instance.READY,
}
state.processInstances = append(state.processInstances, &processInstanceInfo)
state.exportProcessInstanceEvent(process, processInstanceInfo)
return &processInstanceInfo, nil
}
}
return nil, nil
}
// CreateAndRunInstance creates a new instance and executes it immediately.
// The provided variableContext can be nil or refers to a variable map,
// which is provided to every service task handler function.
func (state *BpmnEngineState) CreateAndRunInstance(processKey int64, variableContext map[string]interface{}) (*ProcessInstanceInfo, error) {
instance, err := state.CreateInstance(processKey, variableContext)
if err != nil {
return nil, err
}
if instance == nil {
return nil, errors.New(fmt.Sprint("can't find process with processKey=", processKey, "."))
}
err = state.run(instance)
return instance, err
}
// RunOrContinueInstance runs or continues a process instance by a given processInstanceKey.
// returns the process instances, when found
// does nothing, if process is already in ProcessInstanceCompleted State
// returns nil, when no process instance was found
// Additionally, every time this method is called, former completed instances are 'garbage collected'.
func (state *BpmnEngineState) RunOrContinueInstance(processInstanceKey int64) (*ProcessInstanceInfo, error) {
for _, pi := range state.processInstances {
if processInstanceKey == pi.instanceKey {
return pi, state.run(pi)
}
}
return nil, nil
}
func (state *BpmnEngineState) run(instance *ProcessInstanceInfo) (err error) {
type queueElement struct {
inboundFlowId string
baseElement BPMN20.BaseElement
}
queue := make([]queueElement, 0)
process := instance.processInfo
switch instance.state {
case process_instance.READY:
// use start events to start the instance
for _, event := range process.definitions.Process.StartEvents {
queue = append(queue, queueElement{
inboundFlowId: "",
baseElement: event,
})
}
instance.state = process_instance.ACTIVE
case process_instance.ACTIVE:
intermediateCatchEvents := state.findIntermediateCatchEventsForContinuation(process, instance)
for _, ice := range intermediateCatchEvents {
queue = append(queue, queueElement{
inboundFlowId: "",
baseElement: *ice,
})
}
case process_instance.COMPLETED:
return nil
case process_instance.FAILED:
return nil
default:
panic("Unknown process instance state.")
}
for len(queue) > 0 {
element := queue[0].baseElement
inboundFlowId := queue[0].inboundFlowId
queue = queue[1:]
continueNextElement := state.handleElement(process, instance, element)
if continueNextElement {
state.exportElementEvent(*process, *instance, element, exporter.ElementCompleted)
if inboundFlowId != "" {
state.scheduledFlows = remove(state.scheduledFlows, inboundFlowId)
}
nextFlows := BPMN20.FindSequenceFlows(&process.definitions.Process.SequenceFlows, element.GetOutgoingAssociation())
if element.GetType() == BPMN20.ExclusiveGateway {
nextFlows, err = exclusivelyFilterByConditionExpression(nextFlows, instance.variableContext)
if err != nil {
instance.state = process_instance.FAILED
break
}
}
for _, flow := range nextFlows {
state.exportSequenceFlowEvent(*process, *instance, flow)
// TODO: create test for that
// if len(flows) < 1 {
// panic(fmt.Sprintf("Can't find 'sequenceFlow' element with ID=%s. "+
// "This is likely because your BPMN is invalid.", flows[0]))
// }
state.scheduledFlows = append(state.scheduledFlows, flow.Id)
baseElements := BPMN20.FindBaseElementsById(process.definitions, flow.TargetRef)
// TODO: create test for that
// if len(baseElements) < 1 {
// panic(fmt.Sprintf("Can't find flow element with ID=%s. "+
// "This is likely because there are elements in the definition, "+
// "which this engine does not support (yet).", flow.Id))
// }
targetBaseElement := baseElements[0]
queue = append(queue, queueElement{
inboundFlowId: flow.Id,
baseElement: targetBaseElement,
})
}
}
}
if instance.state == process_instance.COMPLETED || instance.state == process_instance.FAILED {
// TODO need to send failed state
state.exportEndProcessEvent(*process, *instance)
}
return err
}
func (state *BpmnEngineState) findIntermediateCatchEventsForContinuation(process *ProcessInfo, instance *ProcessInstanceInfo) (ret []*BPMN20.BaseElement) {
messageRef2IntermediateCatchEventMapping := map[string]BPMN20.BaseElement{}
for _, ice := range process.definitions.Process.IntermediateCatchEvent {
messageRef2IntermediateCatchEventMapping[ice.MessageEventDefinition.MessageRef] = ice
}
for _, caughtEvent := range instance.caughtEvents {
if caughtEvent.isConsumed == true {
// skip consumed ones
continue
}
for _, msg := range process.definitions.Messages {
// find the matching message definition
if msg.Name == caughtEvent.name {
// find potential event definitions
event := messageRef2IntermediateCatchEventMapping[msg.Id]
if state.hasActiveMessageSubscriptionForId(event.GetId()) {
ret = append(ret, &event)
}
}
}
}
ice := checkDueTimersAndFindIntermediateCatchEvent(state.timers, process.definitions.Process.IntermediateCatchEvent, instance)
if ice != nil {
ret = append(ret, ice)
}
return eliminateEventsWhichComeFromTheSameGateway(process.definitions, ret)
}
func (state *BpmnEngineState) hasActiveMessageSubscriptionForId(id string) bool {
for _, subscription := range state.messageSubscriptions {
if id == subscription.ElementId && (subscription.State == activity.Ready || subscription.State == activity.Active) {
return true
}
}
return false
}
func eliminateEventsWhichComeFromTheSameGateway(definitions BPMN20.TDefinitions, events []*BPMN20.BaseElement) (ret []*BPMN20.BaseElement) {
// a bubble-sort-like approach to find elements, which have the same incoming association
for len(events) > 0 {
event := events[0]
events = events[1:]
if event == nil {
continue
}
ret = append(ret, event)
for i := 0; i < len(events); i++ {
if haveEqualInboundBaseElement(definitions, event, events[i]) && inboundIsEventBasedGateway(definitions, event) {
events[i] = nil
}
}
}
return ret
}
func inboundIsEventBasedGateway(definitions BPMN20.TDefinitions, event *BPMN20.BaseElement) bool {
ref := BPMN20.FindSourceRefs(definitions.Process.SequenceFlows, (*event).GetIncomingAssociation()[0])[0]
baseElement := BPMN20.FindBaseElementsById(definitions, ref)[0]
return baseElement.GetType() == BPMN20.EventBasedGateway
}
func haveEqualInboundBaseElement(definitions BPMN20.TDefinitions, event1 *BPMN20.BaseElement, event2 *BPMN20.BaseElement) bool {
if event1 == nil || event2 == nil {
return false
}
checkOnlyOneAssociationOrPanic(event1)
checkOnlyOneAssociationOrPanic(event2)
ref1 := BPMN20.FindSourceRefs(definitions.Process.SequenceFlows, (*event1).GetIncomingAssociation()[0])[0]
ref2 := BPMN20.FindSourceRefs(definitions.Process.SequenceFlows, (*event2).GetIncomingAssociation()[0])[0]
baseElement1 := BPMN20.FindBaseElementsById(definitions, ref1)[0]
baseElement2 := BPMN20.FindBaseElementsById(definitions, ref2)[0]
return baseElement1.GetId() == baseElement2.GetId()
}
func checkOnlyOneAssociationOrPanic(event *BPMN20.BaseElement) {
if len((*event).GetIncomingAssociation()) != 1 {
panic(fmt.Sprintf("Element with id=%s has %d incoming associations, but only 1 is supported by this engine.",
(*event).GetId(), len((*event).GetIncomingAssociation())))
}
}
// AddTaskHandler registers a handler function to be called for service tasks with a given taskId
func (state *BpmnEngineState) AddTaskHandler(taskId string, handler func(job ActivatedJob)) {
if nil == state.handlers {
state.handlers = make(map[string]func(job ActivatedJob))
}
state.handlers[taskId] = handler
}
func (state *BpmnEngineState) handleElement(process *ProcessInfo, instance *ProcessInstanceInfo, element BPMN20.BaseElement) bool {
state.exportElementEvent(*process, *instance, element, exporter.ElementActivated)
switch element.GetType() {
case BPMN20.StartEvent:
return true
case BPMN20.ServiceTask:
taskElement := element.(BPMN20.TaskElement)
return state.handleServiceTask(process, instance, &taskElement)
case BPMN20.UserTask:
taskElement := element.(BPMN20.TaskElement)
return state.handleUserTask(process, instance, &taskElement)
case BPMN20.ParallelGateway:
return state.handleParallelGateway(element)
case BPMN20.EndEvent:
state.handleEndEvent(process, instance)
state.exportElementEvent(*process, *instance, element, exporter.ElementCompleted) // special case here, to end the instance
return false
case BPMN20.IntermediateCatchEvent:
return state.handleIntermediateCatchEvent(process, instance, element.(BPMN20.TIntermediateCatchEvent))
case BPMN20.EventBasedGateway:
// TODO improve precondition tests
// simply proceed
return true
default:
// do nothing
// TODO: should we print a warning?
}
return true
}
func (state *BpmnEngineState) handleIntermediateCatchEvent(process *ProcessInfo, instance *ProcessInstanceInfo, ice BPMN20.TIntermediateCatchEvent) bool {
if ice.MessageEventDefinition.Id != "" {
return state.handleIntermediateMessageCatchEvent(process, instance, ice)
}
if ice.TimerEventDefinition.Id != "" {
return state.handleIntermediateTimerCatchEvent(process, instance, ice)
}
return false
}
func (state *BpmnEngineState) handleParallelGateway(element BPMN20.BaseElement) bool {
// check incoming flows, if ready, then continue
allInboundsAreScheduled := true
for _, inFlowId := range element.GetIncomingAssociation() {
allInboundsAreScheduled = contains(state.scheduledFlows, inFlowId) && allInboundsAreScheduled
}
return allInboundsAreScheduled
}
func (state *BpmnEngineState) handleEndEvent(process *ProcessInfo, instance *ProcessInstanceInfo) {
completedJobs := true
for _, job := range state.jobs {
if job.ProcessInstanceKey == instance.GetInstanceKey() && (job.State == activity.Ready || job.State == activity.Active) {
completedJobs = false
break
}
}
if completedJobs && !state.hasActiveSubscriptions(process, instance) {
instance.state = process_instance.COMPLETED
}
}
func (state *BpmnEngineState) hasActiveSubscriptions(process *ProcessInfo, instance *ProcessInstanceInfo) bool {
activeSubscriptions := map[string]bool{}
for _, ms := range state.messageSubscriptions {
if ms.ProcessInstanceKey == instance.GetInstanceKey() {
activeSubscriptions[ms.ElementId] = ms.State == activity.Ready || ms.State == activity.Active
}
}
// eliminate the active subscriptions, which are from one 'parent' EventBasedGateway
for _, gateway := range process.definitions.Process.EventBasedGateway {
flows := BPMN20.FindSequenceFlows(&process.definitions.Process.SequenceFlows, gateway.OutgoingAssociation)
isOneEventCompleted := true
for _, flow := range flows {
isOneEventCompleted = isOneEventCompleted && !activeSubscriptions[flow.TargetRef]
}
for _, flow := range flows {
activeSubscriptions[flow.TargetRef] = isOneEventCompleted
}
}
for _, v := range activeSubscriptions {
if v {
return true
}
}
return false
}