-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplantuml.go
123 lines (104 loc) · 2.85 KB
/
plantuml.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
package gdag
import (
"fmt"
)
// UML outputs dag PlantUML format.
func (start *Node) UML() (string, error) {
start.startPoint = true
cc := newCriticalPathCalculator()
ug := newUMLGenerator(cc.getCriticalPaths(start))
ret := "@startuml" + "\n"
ret += ug.generateComponents(start) + "\n"
ret += ug.generateRelations(start) + "\n"
ret += "@enduml"
start.startPoint = false
return ret, nil
}
// UMLNoCritical outputs dag PlantUML format that does not represent critical path.
func (start *Node) UMLNoCritical() (string, error) {
start.startPoint = true
ug := newUMLGenerator(nil)
ret := "@startuml" + "\n"
ret += ug.generateComponents(start) + "\n"
ret += ug.generateRelations(start) + "\n"
ret += "@enduml"
start.startPoint = false
return ret, nil
}
type umlGenerator struct {
criticalPaths []*criticalPath
uniqueComponents map[int]struct{}
uniqueRelations map[string]struct{}
}
func newUMLGenerator(criticalPaths []*criticalPath) *umlGenerator {
return ¨Generator{
criticalPaths: criticalPaths,
uniqueComponents: map[int]struct{}{},
uniqueRelations: map[string]struct{}{},
}
}
func (ug *umlGenerator) generateComponents(start *Node) string {
return ug.generateComponent(start)
}
func (ug *umlGenerator) generateComponent(node *Node) string {
if _, ok := ug.uniqueComponents[node.index]; ok {
return ""
}
ug.uniqueComponents[node.index] = struct{}{}
ret := ug.renderComponent(node)
for _, d := range node.downstream {
ret += ug.generateComponent(d)
}
return ret
}
func (ug *umlGenerator) renderComponent(node *Node) string {
ret := ""
switch node.nodeType {
case rectangle, usecase:
s := fmt.Sprintf("%s \"%s\" as %d", node.nodeType, node.text, node.index)
if node.hour > 0 {
s = fmt.Sprintf("%s \"%s (%.1fh)\" as %d", node.nodeType, node.text, node.hour, node.index)
}
if len(node.color) != 0 {
s += fmt.Sprintf(" %s", node.color)
if ug.isCritical(node) && !node.isDAG() {
s += fmt.Sprintf("-%s", "Yellow")
}
} else {
if ug.isCritical(node) && !node.isDAG() {
s += fmt.Sprintf(" %s", "#Yellow")
}
}
s += "\n"
ret += s
}
if len(node.note) != 0 {
ret += fmt.Sprintf("note left\n%s\nend note\n", node.note)
}
return ret
}
func (ug *umlGenerator) isCritical(current *Node) bool {
for _, cp := range ug.criticalPaths {
if cp.contains(current) {
return true
}
}
return false
}
func (ug *umlGenerator) generateRelations(start *Node) string {
return ug.generateRelation(start, "")
}
func (ug *umlGenerator) generateRelation(node *Node, out string) string {
edge := fmt.Sprintf("%d --> ", node.index)
ret := out
for _, d := range node.downstream {
key := fmt.Sprintf("%d-%d", node.index, d.index)
if _, ok := ug.uniqueRelations[key]; ok {
continue
}
ug.uniqueRelations[key] = struct{}{}
tmp := fmt.Sprintf("%s%d\n", edge, d.index)
ret += ug.generateRelation(d, tmp)
}
return ret
}