-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphviz.go
More file actions
152 lines (140 loc) · 3.58 KB
/
graphviz.go
File metadata and controls
152 lines (140 loc) · 3.58 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
package gasync
import (
"fmt"
"reflect"
"strconv"
"github.com/awalterschulze/gographviz"
"github.com/gorchestrate/async"
)
type Grapher struct {
g *gographviz.Graph
}
func (g *Grapher) Dot(s async.Stmt) string {
g.g = gographviz.NewGraph()
g.g.Directed = true
ctx := GraphCtx{}
start := ctx.node(g, "start", "start", "circle")
end := ctx.node(g, "", "end", "circle")
ctx.Prev = []string{start}
octx := g.Walk(s, ctx)
g.AddEdges(octx.Prev, end)
return g.g.String()
}
func (g *Grapher) AddEdges(from []string, to string) {
for _, v := range from {
_ = g.g.AddEdge(v, to, true, nil)
}
}
func (g *Grapher) AddEdge(from string, to string) {
if from == "" || to == "" {
return
}
_ = g.g.AddEdge(from, to, true, nil)
}
type GraphCtx struct {
Parent string
Prev []string
Break []string
}
var ncount int
func (ctx *GraphCtx) node(g *Grapher, id, name string, shape string) string {
if id == "" {
ncount++
id = fmt.Sprint(ncount)
} else {
id = strconv.Quote(id)
}
_ = g.g.AddNode("", id, map[string]string{
"label": strconv.Quote(name),
"shape": shape,
})
return id
}
func (g *Grapher) Walk(s async.Stmt, ctx GraphCtx) GraphCtx {
switch x := s.(type) {
case nil:
return GraphCtx{}
case async.ReturnStmt:
n := ctx.node(g, "", "end", "circle")
g.AddEdges(ctx.Prev, n)
return GraphCtx{}
case async.BreakStmt:
return GraphCtx{Break: ctx.Prev}
case async.ContinueStmt:
return GraphCtx{}
case async.StmtStep:
id := ctx.node(g, x.Name, "⚙️ "+x.Name+" ", "box")
g.AddEdges(ctx.Prev, id)
return GraphCtx{Prev: []string{id}}
case async.WaitCondStmt:
id := ctx.node(g, x.Name, "⏸ wait for "+x.Name, "hexagon")
g.AddEdges(ctx.Prev, id)
return GraphCtx{Prev: []string{id}}
case async.WaitEventsStmt:
id := ctx.node(g, x.Name, "⏸ wait "+x.Name, "hexagon")
g.AddEdges(ctx.Prev, id)
prev := []string{}
breaks := []string{}
for _, v := range x.Cases {
var cid string
_, ok := v.Handler.(*async.ReflectEvent)
_, ok2 := v.Handler.(*TimeoutHandler)
if ok {
cid = ctx.node(g, v.Callback.Name, "▶️ /"+v.Callback.Name+" ", "component")
} else if ok2 {
cid = ctx.node(g, v.Callback.Name, "🕑"+v.Callback.Name+" ", "component")
} else {
cid = ctx.node(g, v.Callback.Name, "⚡"+v.Callback.Name+" ", "component")
}
_ = g.g.AddEdge(id, cid, true, nil)
octx := g.Walk(v.Stmt, GraphCtx{
Prev: []string{cid},
})
prev = append(prev, octx.Prev...)
breaks = append(breaks, octx.Break...)
}
return GraphCtx{Prev: prev}
case *async.GoStmt:
id := ctx.node(g, x.Name, x.Name, "ellipse")
for _, v := range ctx.Prev {
_ = g.g.AddEdge(v, id, true, map[string]string{
"style": "dashed",
"label": "parallel",
})
}
_ = g.Walk(x.Stmt, GraphCtx{Prev: []string{id}})
return GraphCtx{Prev: ctx.Prev}
case async.ForStmt:
id := ctx.node(g, x.Name, "↺ while "+x.Name, "hexagon")
g.AddEdges(ctx.Prev, id)
breaks := []string{}
curCtx := GraphCtx{Prev: []string{id}}
for _, v := range x.Section {
curCtx = g.Walk(v, GraphCtx{
Prev: curCtx.Prev,
Parent: "sub",
})
breaks = append(breaks, curCtx.Break...)
}
g.AddEdges(curCtx.Prev, id)
return GraphCtx{Prev: append(breaks, id)}
case *async.SwitchStmt:
for _, v := range x.Cases {
g.Walk(v.Stmt, ctx)
}
return GraphCtx{}
case async.Section:
curCtx := ctx
breaks := []string{}
for _, v := range x {
curCtx = g.Walk(v, GraphCtx{
Prev: curCtx.Prev,
Parent: ctx.Parent,
})
breaks = append(breaks, curCtx.Break...)
}
return GraphCtx{Prev: curCtx.Prev, Break: breaks}
default:
panic(reflect.TypeOf(s))
}
}