-
Notifications
You must be signed in to change notification settings - Fork 11
/
walk.go
162 lines (144 loc) · 4.02 KB
/
walk.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
// Copyright 2018 Yaacov Zamir <[email protected]>
// and other contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package graphviz helps to create graphviz dot files using the TSL package.
package graphviz
import (
"fmt"
"math/rand"
"strings"
"github.com/yaacov/tree-search-language/v5/pkg/tsl"
)
// Character poll for random string genrator.
const pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Default styles for .dot file output.
const identStyle = "shape=record color=red"
const numberStyle = "shape=record color=blue"
const stringStyle = "shape=record color=blue"
const dateStyle = "shape=record color=orange"
const opStyle = "shape=box color=black"
// Generate a random string.
func randStr(l int) string {
bytes := make([]byte, l)
for i := 0; i < l; i++ {
bytes[i] = pool[rand.Intn(len(pool))]
}
return string(bytes)
}
// Walk travel the TSL tree to create Graphviz dot nodes.
//
// Users can call the Walk method to get the dot file graph string.
//
// s, err = graphviz.Walk("", tree, "")
//
// The return string will be a node list for a .dot file:
//
// tcuA [shape=box color=black label="$eq"]
// xhxK [shape=record color=red label="$ident | 'city'" ]
// QFDa [shape=record color=blue label="$string | 'rome'" ]
// tcuA -> { xhxK, QFDa }
//
// For valid Graphviz dot file, the nodes must be wrapped in a `digraph` object:
//
// s, err = graphviz.Walk("", tree, "")
// s = fmt.Sprintf("digraph {\n%s\n}\n", s)
func Walk(in string, n tsl.Node, nodeID string) (out string, err error) {
// If node ID is missing, create one.
if nodeID == "" {
nodeID = randStr(4)
}
// If we have in string, add a new line break.
if in != "" {
in = fmt.Sprintf("%s\n", in)
}
switch n.Func {
case tsl.IdentOp:
// Add leaf label and value.
nodeLabel := fmt.Sprintf("%s [%s label=\"%s | '%s'\" ]",
nodeID,
identStyle,
n.Func,
n.Left)
out = fmt.Sprintf("%s%s", in, nodeLabel)
case tsl.StringOp:
// Add leaf label and value.
nodeLabel := fmt.Sprintf("%s [%s label=\"%s | '%s'\" ]",
nodeID,
stringStyle,
n.Func,
n.Left)
out = fmt.Sprintf("%s%s", in, nodeLabel)
case tsl.NumberOp:
// Add leaf label and value.
nodeLabel := fmt.Sprintf("%s [%s label=\"%s | %g\" ]",
nodeID,
numberStyle,
n.Func,
n.Left)
out = fmt.Sprintf("%s%s", in, nodeLabel)
case tsl.BooleanOp, tsl.DateOp:
// Add leaf label and value.
nodeLabel := fmt.Sprintf("%s [%s label=\"%s | %v\" ]",
nodeID,
dateStyle,
n.Func,
n.Left)
out = fmt.Sprintf("%s%s", in, nodeLabel)
default:
// Add node label.
st := fmt.Sprintf("%s [%s label=\"%s\"]",
nodeID,
opStyle,
n.Func)
childrens := []string{}
// Add left child.
if n.Left != nil {
leftID := randStr(4)
childrens = append(childrens, leftID)
l, err := Walk(in, n.Left.(tsl.Node), leftID)
if err != nil {
return "", err
}
st = fmt.Sprintf("%s\n%s", st, l)
}
// Add right child.
if n.Right != nil {
var nn []tsl.Node
// Check if right hand arg is a node, or an array of nodes.
if n.Func == tsl.ArrayOp {
nn = n.Right.([]tsl.Node)
} else {
nn = []tsl.Node{n.Right.(tsl.Node)}
}
// Add all nodes to graph.
for _, v := range nn {
rightID := randStr(4)
childrens = append(childrens, rightID)
r, err := Walk(in, v, rightID)
if err != nil {
return "", err
}
st = fmt.Sprintf("%s\n%s", st, r)
}
}
// Add parrent node.
in = fmt.Sprintf("%s%s\n%s -> { %s }",
in,
st,
nodeID,
strings.Join(childrens, ", "))
return in, nil
}
return
}