-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.go
57 lines (47 loc) · 1.32 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
package yafsm
import (
"github.com/awalterschulze/gographviz"
)
func CreateDOT(transitions []Transition) string {
root := "G"
graph := gographviz.NewEscape()
graph.SetDir(true)
graph.Attrs.Add("rankdir", "LR")
for _, t := range transitions {
var attrs map[string]string
if t.Name() != "" {
attrs = map[string]string{"label": t.Name()}
}
graph.AddNode(root, string(t.To()), nil)
for _, f := range t.From() {
graph.AddNode(root, string(f), nil)
graph.AddEdge(string(f), string(t.To()), true, attrs)
}
}
return graph.String()
}
func CreateTransitionsFromDOT(dot string) (States, []Transition, error) {
graph, err := gographviz.Read([]byte(dot))
if err != nil {
return nil, nil, err
}
trans := make([]Transition, 0, len(graph.Edges.DstToSrcs))
states := make(States, 0, len(graph.Edges.DstToSrcs))
for key, val := range graph.Edges.DstToSrcs {
var name string
states = append(states, State(key))
from := make([]State, 0, len(val))
for srcKey, edges := range val {
from = append(from, State(srcKey))
if len(edges) != 0 {
name = edges[0].Attrs["label"]
}
}
options := make([]TransitionConfig, 0, 1)
if name != "" {
options = append(options, WithName(name))
}
trans = append(trans, NewTransition(NewStates(from...), State(key), options...))
}
return states, trans, nil
}