-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathvisualizer_dot.go
More file actions
245 lines (203 loc) · 5.54 KB
/
visualizer_dot.go
File metadata and controls
245 lines (203 loc) · 5.54 KB
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
package gotaskflow
import (
"fmt"
"io"
"strings"
)
type dotVizer struct{}
// dotGraph represents a graph in DOT format
type dotGraph struct {
name string
isSubgraph bool
nodes map[string]*dotNode
edges []*dotEdge
attributes map[string]string
subgraphs []*dotGraph
indent string
}
// dotNode represents a node in DOT format
type dotNode struct {
id string
attributes map[string]string
}
// dotEdge represents an edge in DOT format
type dotEdge struct {
from *dotNode
to *dotNode
attributes map[string]string
}
func newDotGraph(name string) *dotGraph {
return &dotGraph{
name: name,
isSubgraph: false,
nodes: make(map[string]*dotNode),
edges: make([]*dotEdge, 0),
attributes: make(map[string]string),
subgraphs: make([]*dotGraph, 0),
indent: "",
}
}
func (g *dotGraph) CreateNode(name string) *dotNode {
if node, exists := g.nodes[name]; exists {
return node
}
node := &dotNode{
id: name,
attributes: make(map[string]string),
}
g.nodes[name] = node
return node
}
func (g *dotGraph) CreateEdge(from, to *dotNode, label string) *dotEdge {
edge := &dotEdge{
from: from,
to: to,
attributes: make(map[string]string),
}
if label != "" {
edge.attributes["label"] = label
}
g.edges = append(g.edges, edge)
return edge
}
func (g *dotGraph) SubGraph(name string) *dotGraph {
subgraph := &dotGraph{
name: name,
isSubgraph: true,
nodes: make(map[string]*dotNode),
edges: make([]*dotEdge, 0),
attributes: make(map[string]string),
subgraphs: make([]*dotGraph, 0),
indent: g.indent + " ",
}
g.subgraphs = append(g.subgraphs, subgraph)
return subgraph
}
func (g *dotGraph) String() string {
var sb strings.Builder
if g.isSubgraph {
sb.WriteString(g.indent + "subgraph " + quote("cluster_"+g.name) + " {\n")
} else {
sb.WriteString(g.indent + "digraph " + quote(g.name) + " {\n")
}
for k, v := range g.attributes {
sb.WriteString(g.indent + " " + k + "=" + quote(v) + ";\n")
}
for _, node := range g.nodes {
sb.WriteString(node.Format(g.indent + " "))
}
for _, edge := range g.edges {
sb.WriteString(edge.Format(g.indent + " "))
}
for _, subgraph := range g.subgraphs {
sb.WriteString(subgraph.String())
}
sb.WriteString(g.indent + "}\n")
return sb.String()
}
func (node *dotNode) Format(indent string) string {
attrs := formatAttributes(node.attributes)
if attrs == "" {
return indent + quote(node.id) + ";\n"
}
return indent + quote(node.id) + " [" + attrs + "];\n"
}
func (edge *dotEdge) Format(indent string) string {
from := edge.from.id
to := edge.to.id
attrs := formatAttributes(edge.attributes)
if attrs == "" {
return indent + quote(from) + " -> " + quote(to) + ";\n"
}
return indent + quote(from) + " -> " + quote(to) + " [" + attrs + "];\n"
}
func quote(s string) string {
return "\"" + s + "\""
}
func formatAttributes(attrs map[string]string) string {
if len(attrs) == 0 {
return ""
}
result := make([]string, 0, len(attrs))
for k, v := range attrs {
result = append(result, k+"="+quote(v))
}
return strings.Join(result, ", ")
}
// visualizeG recursively visualizes the graph and its subgraphs in DOT format
func (v *dotVizer) visualizeG(g *eGraph, parentGraph *dotGraph) error {
graph := parentGraph
graph.attributes["rankdir"] = "LR"
nodeMap := make(map[string]*dotNode)
for _, node := range g.nodes {
color := "black"
if node.priority == HIGH {
color = "#f5427b"
} else if node.priority == LOW {
color = "purple"
}
switch p := node.ptr.(type) {
case *Static:
dotNode := graph.CreateNode(node.name)
dotNode.attributes["color"] = color
nodeMap[node.name] = dotNode
case *Condition:
dotNode := graph.CreateNode(node.name)
dotNode.attributes["shape"] = "diamond"
dotNode.attributes["color"] = "green"
nodeMap[node.name] = dotNode
case *Subflow:
subgraph := graph.SubGraph(node.name)
subgraph.attributes["label"] = node.name
subgraph.attributes["style"] = "dashed"
subgraph.attributes["rankdir"] = "LR"
subgraph.attributes["bgcolor"] = "#F5F5F5"
subgraph.attributes["fontcolor"] = color
subgraphDot := subgraph.CreateNode(node.name)
subgraphDot.attributes["shape"] = "point"
subgraphDot.attributes["height"] = "0.05"
subgraphDot.attributes["width"] = "0.05"
nodeMap[node.name] = subgraphDot
err := v.visualizeG(p.g, subgraph)
if err != nil {
errorNodeName := "unvisualized_subflow_" + p.g.name
dotNode := graph.CreateNode(errorNodeName)
dotNode.attributes["color"] = "#a10212"
dotNode.attributes["comment"] = "cannot visualize due to instantiate panic or failed"
nodeMap[node.name] = dotNode
}
}
}
for _, node := range g.nodes {
for idx, deps := range node.successors {
if from, ok := nodeMap[node.name]; ok {
if to, ok := nodeMap[deps.name]; ok {
label := ""
style := "solid"
if _, ok := node.ptr.(*Condition); ok {
label = fmt.Sprintf("%d", idx)
style = "dashed"
}
edge := graph.CreateEdge(from, to, label)
if style != "solid" {
edge.attributes["style"] = style
}
}
}
}
}
return nil
}
// Visualize generates raw dag text in dot format and writes to writer
func (v *dotVizer) Visualize(tf *TaskFlow, writer io.Writer) error {
graph := newDotGraph(tf.graph.name)
err := v.visualizeG(tf.graph, graph)
if err != nil {
return fmt.Errorf("visualize %v -> %w", tf.graph.name, err)
}
_, err = writer.Write([]byte(graph.String()))
if err != nil {
return fmt.Errorf("write dot output -> %w", err)
}
return nil
}