-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow_merge.go
91 lines (81 loc) · 2.65 KB
/
flow_merge.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
package visibleIdeas
import "fmt"
// returns the index of a node with id id in the array of nodes nodes
func findFirstNodeIndexByID(nodes []Node, id string) int {
for idx, node := range nodes {
if node.ID == id {
return idx
}
}
return -1
}
// returns the index of a first node with identifier identifier in the array of nodes nodes
func findFirstNodeIndexByIdentifier(nodes []Node, identifier Identifier) int {
for idx, node := range nodes {
if node.Data.Identifier.ID == identifier.ID {
return idx
}
}
return -1
}
func sameConnectionAlreadyExists(edges []Edge, theEdge Edge) bool {
for _, e := range edges {
if e.SourceID == theEdge.SourceID && e.TargetID == theEdge.TargetID {
return true
}
}
return false
}
// MergeFlows Assumes the sensor nodes
func MergeFlows(newID string, newTitle string, flows ...DiagnosisFlow) (DiagnosisFlow, error) {
r := DiagnosisFlow{
ID: newID,
Title: newTitle,
Nodes: []Node{},
Edges: []Edge{},
}
// sanity checks
// allNodeIDs := []string
// a maps of node ids that points duplicate nodes to the nodes already in the resulting merged flow
duplicateNodeIDMapping := map[string]string{}
for _, f := range flows {
// Do the nodes first -----------------------------------------------------------------
for _, n := range f.Nodes {
switch n.Type {
case ContainerNode:
// we only need only one set of container nodes, but for now let's ignore them
case SensorNode, DDxNode:
existingNodeID := findFirstNodeIndexByIdentifier(r.Nodes, n.Data.Identifier)
if existingNodeID > -1 {
duplicateNodeIDMapping[r.Nodes[existingNodeID].ID] = n.ID
break
}
if findFirstNodeIndexByID(r.Nodes, n.ID) > -1 {
return r, fmt.Errorf("two %s nodes with the exact same id but different identifiers detected which is unexpected: '%s'", n.Type, n.ID)
}
r.Nodes = append(r.Nodes, n)
case FindingComputeNode, LogicNode, WeightNode:
if findFirstNodeIndexByID(r.Nodes, n.ID) > -1 {
fmt.Printf("two %s nodes with the exact same id detected which is must mean a real duplicate: '%s'", n.Type, n.ID)
break
}
r.Nodes = append(r.Nodes, n)
default:
return r, fmt.Errorf("flow contains '%s' nodes which are not supported", n.Type)
}
}
// Then do the edges -----------------------------------------------------------------
for _, e := range f.Edges {
if duplicateNodeIDMapping[e.SourceID] != "" {
e.SourceID = duplicateNodeIDMapping[e.SourceID]
}
if duplicateNodeIDMapping[e.TargetID] != "" {
e.TargetID = duplicateNodeIDMapping[e.TargetID]
}
if !sameConnectionAlreadyExists(r.Edges, e) {
r.Edges = append(r.Edges, e)
}
}
}
return r, nil
}