-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
64 lines (59 loc) · 1.33 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
package decide
import (
"fmt"
graphviz "github.com/sjhitchner/go-graphviz"
"io"
"sort"
"strings"
)
// Generate a Graphviz representation of the node tree
func Graph(w io.Writer, node *Node) error {
graph := graphviz.NewGraph("DecisionTree")
GraphWalk(graph, nil, []*Node{node})
return graph.Output(w)
}
func GraphWalk(graph *graphviz.Graph, parent *Node, nodes []*Node) {
for i := 0; i < len(nodes); i++ {
node := nodes[i]
graph.AddNode(
fmt.Sprintf("Node%p", node),
map[string]string{
//"label": node.Expression.String(),
"label": fmt.Sprintf(
"{<f0>%s|<f1>%s}",
node.Expression.String(),
func() string {
list := make([]string, len(node.Payload))
for i := range node.Payload {
list[i] = string(node.Payload[i])
}
sort.Strings(list)
return strings.Join(list, "|")
}()),
"shape": "Mrecord",
})
if parent != nil {
graph.AddEdge(
fmt.Sprintf("Node%p", parent),
fmt.Sprintf("Node%p", node),
true,
map[string]string{
"label": func() string {
for _, parentTrue := range parent.True {
if node == parentTrue {
return "T"
}
}
return "F"
}(),
},
)
}
if len(node.True) > 0 {
GraphWalk(graph, node, node.True)
}
if node.False != nil {
GraphWalk(graph, node, []*Node{node.False})
}
}
}