-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_helper.go
297 lines (281 loc) · 10.2 KB
/
process_helper.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
package bpmnio
import "fmt"
// FindSequenceFlows finds all the sequence flows in the process and sub processes of the process
// that have match one of the ids passed in
func (p *Process) FindSequenceFlows(ids []string) (sequenceFlows []*SequenceFlow) {
if len(ids) == 0 {
sequenceFlows = p.SequenceFlows
} else {
for _, flow := range p.SequenceFlows {
for _, id := range ids {
if id == flow.Id {
sequenceFlows = append(sequenceFlows, flow)
}
}
}
}
for _, subProcess := range p.SubProcesses {
if len(ids) == 0 {
sequenceFlows = append(sequenceFlows, subProcess.SequenceFlows...)
} else {
for _, flow := range subProcess.SequenceFlows {
for _, id := range ids {
if id == flow.Id {
sequenceFlows = append(sequenceFlows, flow)
}
}
}
}
}
return sequenceFlows
}
// FindSubProcessById will find the sub process with the Id given
func (p *Process) FindSubProcessById(id string) (subProcess *SubProcess) {
for _, sp := range p.SubProcesses {
if sp.Id == id {
subProcess = sp
break
}
}
return
}
// FindNodes finds all the nodes and returns with a process map[id]BaseElement
func (p *Process) FindNodes() map[string]BaseElement {
nodeTypes := NodeElementTypes()
nodeTypeMap := make(map[ElementType]bool, len(nodeTypes))
for _, nodeType := range nodeTypes {
nodeTypeMap[nodeType] = true
}
nodeTypeMap[B2SubProcess] = false
return p.MapBEs(nodeTypeMap)
}
// FindLinks finds all the Links and returns a process map[id]BaseElement
func (p *Process) FindLinks() map[string]BaseElement {
linkTypes := NodeElementTypes()
linkTypeMap := make(map[ElementType]bool, len(linkTypes))
for _, linkType := range linkTypes {
linkTypeMap[linkType] = true
}
return p.MapBEs(linkTypeMap)
}
// MapBEs takes a map of ElementTypes and builds a list of BaseElement that match the map
func (p *Process) MapBEs(etMap map[ElementType]bool) (m map[string]BaseElement) {
m = make(map[string]BaseElement, 20)
fMap := func(be BaseElement) bool {
var r = true
if downHierarchy, inMap := etMap[be.GetType()]; inMap {
m[be.GetId()] = be
r = downHierarchy
}
//fmt.Printf("checking BE %s %t (%d)\n", be.ToString(), r, len(m))
return r
}
p.ApplyFunctionToBaseElements(fMap)
return
}
// FindIntermediateCatchEventsForContinuation finds all intermediate catch events of a process
func (p *Process) FindIntermediateCatchEventsForContinuation() (ICEs []*IntermediateCatchEvent) {
// find potential event definitions
ICEs = p.IntermediateCatchEvents
return p.EliminateEventsWhichComeFromTheSameGateway(ICEs)
}
func (p *Process) EliminateEventsWhichComeFromTheSameGateway(events []*IntermediateCatchEvent) (ICEs []*IntermediateCatchEvent) {
// 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
}
ICEs = append(ICEs, event)
for i := 0; i < len(events); i++ {
if p.haveEqualInboundBaseElement(event, events[i]) {
events[i] = nil
}
}
}
return
}
func (p *Process) haveEqualInboundBaseElement(event1 *IntermediateCatchEvent, event2 *IntermediateCatchEvent) bool {
if event1 == nil || event2 == nil {
return false
}
p.checkOnlyOneAssociationOrPanic(event1)
p.checkOnlyOneAssociationOrPanic(event2)
ref1 := FindSourceRefs(p.SequenceFlows, event1.IncomingAssociations[0])[0]
ref2 := FindSourceRefs(p.SequenceFlows, event2.IncomingAssociations[0])[0]
baseElement1 := p.FindBaseElementById(ref1)
baseElement2 := p.FindBaseElementById(ref2)
return baseElement1.GetId() == baseElement2.GetId()
}
func (p *Process) checkOnlyOneAssociationOrPanic(event *IntermediateCatchEvent) {
if len(event.IncomingAssociations) != 1 {
panic(fmt.Sprintf("Element with id=%s has %d incoming associations, but only 1 is supported by this engine.",
event.Id, len(event.IncomingAssociations)))
}
}
// FindBaseElementById finds all base elements in the definition
func (p *Process) FindBaseElementById(id string) (baseElement BaseElement) {
fFindElementById := func(be BaseElement) bool {
if be.GetId() == id {
baseElement = be
}
return baseElement != nil
}
p.ApplyFunctionToBaseElements(fFindElementById)
return
}
// TopologicalSort
// Based upon algorithm published by Kai Kretschmann
// Design and Implementation of a Framework for the Creation of BPMN 2.0 Process Models based on Textual Descriptions
// But enhanced to prefer sequence flows over number of connections
//
// includeLinks is optional
func (p *Process) TopologicalSort(includeLinks bool) (orderedList []BaseElement) {
nodeMap := p.FindNodes()
linkMap := p.FindLinks() // Links/Edges with predecessor & successor
// incomingSeqCounts: elements with number of incoming sequence flows & number of processed incoming sequence flows
// If processed = total then this is next best step
type incomingSeqCount struct {
id string
be BaseElement
numberOfIncomingSequenceFlows int
numberOfProcessedIncomingSequenceFlows int
}
incomingSeqCounts := make(map[string]*incomingSeqCount, len(nodeMap))
for k, be := range nodeMap {
incomingSeqCounts[k] = &incomingSeqCount{id: k, be: be}
}
for _, l := range linkMap {
for _, incomingAssocBPMNId := range l.GetOutgoingAssociations() {
if incomingSeqCounts[incomingAssocBPMNId] == nil {
fmt.Println("id on link isn't a node", incomingAssocBPMNId)
} else {
incomingSeqCounts[incomingAssocBPMNId].numberOfIncomingSequenceFlows++
}
}
}
//for k := range nodeMap {
// fmt.Println(k, incomingSeqCounts[k].numberOfIncomingSequenceFlows, nodeMap[k].ToString())
//}
// incomingSeqCounts Initialised
inOrderedList := map[string]bool{} // id added if in the ordered list
var pathPreferredNodes = make(map[string]bool, 4) // This is to prefer a node joined by a link over anything else following the current route
var otherPreferredNodes = make(map[string]bool, 4) // This is to prefer a node we've seen that is a different path
// while incomingSeqCounts not empty do
for len(incomingSeqCounts) > 0 {
var isc *incomingSeqCount
// Go around this loop twice, the first time try and find a preferred node
//fmt.Println("preferred nodes:", len(pathPreferredNodes))
//for id := range pathPreferredNodes {
// fmt.Println("..", nodeMap[id].ToString())
//}
for i := range 3 {
var preferredNodes map[string]bool
if i == 0 {
preferredNodes = pathPreferredNodes
}
if i == 1 {
preferredNodes = otherPreferredNodes
}
if i < 2 && len(preferredNodes) == 0 {
continue
}
// triple: an element in incomingSeqCounts with incoming sequence flows = number of processed;
for _, iscNP := range incomingSeqCounts {
if iscNP.numberOfIncomingSequenceFlows == iscNP.numberOfProcessedIncomingSequenceFlows &&
(preferredNodes[iscNP.id] || i == 2) {
//fmt.Println("==", i, iscNP)
isc = iscNP
break
}
}
if isc != nil {
break
}
// if no such element can be found then
// triple:an element in incomingSeqCounts with the highest processed sequence flow number;
var maxNumberProcessed = -1
for _, iscMax := range incomingSeqCounts {
if iscMax.numberOfProcessedIncomingSequenceFlows > maxNumberProcessed &&
(preferredNodes[iscMax.id] || i == 2) {
isc = iscMax
maxNumberProcessed = isc.numberOfProcessedIncomingSequenceFlows
//fmt.Println("max", i, isc)
}
}
if isc != nil {
break
}
}
if isc == nil {
fmt.Println("How did we not find something!")
break
}
//fmt.Println("Selected node", isc.be.ToString())
// Remove the link from the map and add to ordered list if required
var fromId string
if len(orderedList) > 0 {
fromId = orderedList[len(orderedList)-1].GetId()
}
var usedLink BaseElement
var nextNodes []string
for _, l := range linkMap {
for _, ia := range l.GetIncomingAssociations() {
if ia == isc.id {
for _, id := range l.GetOutgoingAssociations() {
// We can only prefer things we haven't already processed
// If this is a gateway we should not prefer to continue but wait
// for other paths to catch up.
// that is isc.processed still less than isc.incoming
if id != isc.id && !inOrderedList[id] {
nextNodes = append(nextNodes, id)
}
}
}
if fromId > "" && ia == fromId && ia == isc.id {
usedLink = l
}
break // Only look at first incoming association
}
}
if usedLink != nil {
if includeLinks {
orderedList = append(orderedList, usedLink)
}
delete(linkMap, usedLink.GetId())
}
orderedList = append(orderedList, isc.be) //orderedList← orderedList ∪ {elementID from triple};
delete(pathPreferredNodes, isc.id) // Take the node out of path preferred list
delete(otherPreferredNodes, isc.id) // Take the node out of other preferred list
for k, v := range pathPreferredNodes { // Move any nodes in current path preferred list to otherPreferredNodes
otherPreferredNodes[k] = v
}
pathPreferredNodes = make(map[string]bool, len(nextNodes))
for _, id := range nextNodes {
pathPreferredNodes[id] = true
}
inOrderedList[isc.id] = true
delete(incomingSeqCounts, isc.id) //incomingSeqCounts← incomingSeqCounts\{triple};
newIncomingSequenceCounts := map[string]*incomingSeqCount{} // newIncomingSeqCount← {};
for k, v := range incomingSeqCounts { //for all x in incomingSeqCounts do
var processedLinks []BaseElement
// processedEdges ←{e| e ∈ sequenceFlowArray, triple.elementID = e.predecessorID, x.elementId = e.successorID };
for _, l := range linkMap {
if len(l.GetIncomingAssociations()) > 0 && len(l.GetOutgoingAssociations()) > 0 {
if isc.id == l.GetIncomingAssociations()[0] && k == l.GetOutgoingAssociations()[0] {
processedLinks = append(processedLinks, l)
}
}
}
newIncomingSequenceCounts[k] = &incomingSeqCount{
id: k,
be: v.be,
numberOfIncomingSequenceFlows: v.numberOfIncomingSequenceFlows,
numberOfProcessedIncomingSequenceFlows: v.numberOfProcessedIncomingSequenceFlows + len(processedLinks),
}
}
incomingSeqCounts = newIncomingSequenceCounts
}
return orderedList
}