-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
244 lines (211 loc) · 5.2 KB
/
graph.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
package fsm
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os/exec"
"strings"
"github.com/emicklei/dot"
)
// Action represents an action
type Action string
// Status represent a status
type Status string
// StatusAction represents either
// an action and its origin state (Source)
// an action and its state resulting of the action applied (Outcome)
type StatusAction struct {
Action Action `json:"action"`
Status Status `json:"status"`
}
// Node represent a Node in the state graph
type Node struct {
Sources []StatusAction `json:"sources,omitempty"`
Status Status `json:"status"`
Outcomes []StatusAction `json:"outcomes,omitempty"`
}
func (n *Node) String() string {
return fmt.Sprintf(
`{status: "%s", sources: %v, outcomes: %v}`,
n.Status, n.Sources, n.Outcomes,
)
}
// Graph represent the container of all nodes
type Graph map[Status]*Node
// NewGraph will create a new Graph
func NewGraph() Graph {
return Graph(make(map[Status]*Node))
}
// func (g Graph) String() string {
// var str []string
// for s, n := range g {
// str = append(str, fmt.Sprintf("%s: %v", s, n))
// }
// return strings.Join(str, "\n")
// }
// AddNode will add a node to the state graph
func (g Graph) AddNode(n *Node) error {
// Make sure it doesn't already exists
if _, ok := g[n.Status]; ok {
return fmt.Errorf("AddNode(%s): node already exists", n.Status)
}
// Make sure all srcs and outcomes nodes exists
for _, nso := range append(n.Sources, n.Outcomes...) {
// Make sure it doesn't already exists
if _, ok := g[nso.Status]; !ok {
return fmt.Errorf(
"AddNode(%s): node %s doesn't exist",
n.Status, nso.Status,
)
}
}
// make sure the source node has this one as an outcome
if err := g.appendOutcomeToSourceNodes(n.Sources, n.Status); err != nil {
return fmt.Errorf("AddNode(%s): %v", n.Status, err)
}
// make sure the outcome node has this one as a source
if err := g.appendSourceToOutcomeNodes(n.Status, n.Outcomes); err != nil {
return fmt.Errorf("AddNode(%s): %v", n.Status, err)
}
g[n.Status] = n
return nil
}
func (g Graph) appendOutcomeToSourceNodes(
srcs []StatusAction,
st Status,
) error {
for _, src := range srcs {
if _, ok := g[src.Status]; !ok {
return fmt.Errorf(
"appendOutcomeToSourceNodes: source node %s doesn't exist",
src.Status,
)
}
// Add the outcome to the source node, if it doesn't already exist
outcomeExists := false
for _, srcO := range g[src.Status].Outcomes {
if srcO.Action == src.Action && srcO.Status == st {
outcomeExists = true
}
}
if !outcomeExists {
g[src.Status].Outcomes = append(
g[src.Status].Outcomes,
StatusAction{
Action: src.Action,
Status: st,
},
)
}
}
return nil
}
func (g Graph) appendSourceToOutcomeNodes(
st Status,
os []StatusAction,
) error {
for _, o := range os {
if _, ok := g[o.Status]; !ok {
return fmt.Errorf(
"appendSourceToOutcomeNodes: outcome node %s doesn't exist",
o.Status,
)
}
// Add the outcome to the source node, if it doesn't already exist
srcExists := false
for _, oSrc := range g[o.Status].Sources {
if oSrc.Action == o.Action && oSrc.Status == st {
srcExists = true
}
}
if !srcExists {
g[o.Status].Sources = append(
g[o.Status].Sources,
StatusAction{
Action: o.Action,
Status: st,
},
)
}
}
return nil
}
// Viz will create the graphviz graph for the graph
func (g Graph) Viz() *dot.Graph {
gv := dot.NewGraph(dot.Directed)
ns := make(map[string]dot.Node)
for _, n := range g {
ns[string(n.Status)] = gv.Node(string(n.Status))
}
for _, n := range g {
for _, o := range n.Outcomes {
gv.Edge(
ns[string(n.Status)],
ns[string(o.Status)],
string(o.Action),
)
}
}
return gv
}
// GetOutcomeStatus will return the potential outcomes for a curr status
func (g Graph) GetOutcomeStatus(st Status, ac Action) (Status, error) {
n, ok := g[st]
if !ok {
return "",
fmt.Errorf(
"GetOutcomeStatus(%s, %s): node %s not found",
st, ac, st,
)
}
for _, o := range n.Outcomes {
if o.Action == ac {
return o.Status, nil
}
}
return "", fmt.Errorf(
"GetOutcomeStatus(%s, %s): action %s not found",
st, ac, ac,
)
}
// GeneratePNG will generate a png from the graph
// dependency on dot
func (g Graph) GeneratePNG(f io.Writer) error {
path, err := exec.LookPath("dot")
if err != nil {
return fmt.Errorf("GeneratePNG: you must have dot installed")
}
// Generate PNG graph
v := g.Viz()
// nolint: gosec
dotgraph := exec.Command(path, "-Tpng")
dotgraph.Stdin = strings.NewReader(v.String())
dotgraph.Stdout = f
if err := dotgraph.Run(); err != nil {
return fmt.Errorf("GeneratePNG: %v", err)
}
return nil
}
// UnmarshalJSON will parse a json into a tree of nodes
func (g Graph) UnmarshalJSON(data []byte) error {
r := bytes.NewReader(data)
var aux []*Node
if err := json.NewDecoder(r).Decode(&aux); err != nil {
return fmt.Errorf("UnmarshalJSON: %v", err)
}
for _, a := range aux {
if err := g.AddNode(a); err != nil {
return fmt.Errorf("UnmarshalJSON: %v", err)
}
}
return nil
}
// MarshalJSON will return json
func (g Graph) MarshalJSON() ([]byte, error) {
var ns []*Node
for _, n := range g {
ns = append(ns, n)
}
return json.Marshal(ns)
}