-
Notifications
You must be signed in to change notification settings - Fork 0
/
sand.go
261 lines (214 loc) · 5.92 KB
/
sand.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/yaricom/goGraphML/graphml"
)
const (
DESC = 0
OPER = 1
)
type SANDTree struct {
Nodes []SANDNode
Filename string
Name string
}
type SANDNode struct {
ID int
Desc string
Oper string
Child []int
Indent int
Parent int
}
/*********************************************************************/
func (st *SANDTree) ParseFile(file string) error {
/* 1. Parse the whole file and create inital tree */
st.Filename = file
st.Name = strings.TrimSuffix(filepath.Base(st.Filename), filepath.Ext(st.Filename)) + " SAND"
rawdata, err := os.ReadFile(file)
if err != nil {
return err
}
/* For each line in the file create a Node. */
for n, line := range strings.Split(string(rawdata), "\n") {
res := SANDNode{}
/* NOTE: IDs are based on their line number of the source file. */
res.ID = n
res.Parent = -1
tmp := strings.Split(line, ":")
res.Indent = strings.Count(tmp[DESC], "\t")
res.Desc = fmt.Sprintf(" %d. ", res.ID) + strings.TrimSpace(tmp[DESC])
/* If missing, the default operator is OR. */
if len(tmp) != 2 {
res.Oper = "OR"
} else {
res.Oper = strings.ToUpper(strings.TrimSpace(tmp[OPER]))
}
/* Don't add empty lines or comments 'i.e. starts with #' */
if len(res.Desc) == 0 || strings.HasPrefix(res.Desc, "#") {
continue
}
if res.Oper == "SAND" {
res.Desc = res.Desc + " (S)"
}
if res.Oper == "AND" {
res.Desc = res.Desc + " (A)"
}
st.Nodes = append(st.Nodes, res)
}
/* 2. Expand Tree: Identify Parents and children. */
prefix := []string{st.Nodes[0].Desc}
for i, node := range st.Nodes {
/* 2.1. Figure out the order. */
/* For every node that's not root */
if i != 0 {
/* Prefix */
cni := node.Indent /* Curent Node Indent */
pni := st.Nodes[i-1].Indent /* Past Node Indent */
pl := len(prefix) /* Prefix Length */
// fmt.Println("\n", cni, pni, pl, cni, node.Desc)
/*
* If Past node indent less than the current node indent,
* and the prefix length is the same as the current node
* indent. Then we need to append the prefix.
*/
if pni < cni && pl == cni {
prefix = append(prefix, node.Desc)
}
/*
* If the prefix length is bigger than the current node
* indent, then we need to remove the diference in current
* node indent and the prefix length, then we need to
* append the prefix.
*/
if pl > cni {
prefix = prefix[:len(prefix)-(pl-cni)]
prefix = append(prefix, node.Desc)
}
}
/* Debug prefix output */
/* Usefull for Treemaps */
// for i := range prefix {
// fmt.Printf("%s/", prefix[i])
// }
// fmt.Printf("\n")
/* 2.2. Assign Parents */
if len(prefix) >= 2 {
// fmt.Printf("%d %s parent %s \n", i, prefix, prefix[len(prefix)-2])
st.Nodes[i].Parent = st.GetByName(prefix[len(prefix)-2]).ID
}
}
/* 2.3. Assign Children */
for _, child := range st.Nodes {
if child.Parent != -1 {
for ii, parent := range st.Nodes {
if child.Parent == parent.ID {
st.Nodes[ii].Child = append(parent.Child, child.ID)
}
}
}
}
return nil
}
/*********************************************************************/
func (st *SANDTree) ParseFileGraphML(file string) error {
/* 1. Parse the whole file and create inital tree */
st.Filename = file
st.Name = strings.TrimSuffix(filepath.Base(st.Filename), filepath.Ext(st.Filename)) + " SAND"
rawdata, err := os.Open(file)
if err != nil {
return err
}
defer rawdata.Close()
/* GraphML */
gml := graphml.NewGraphML(st.Name)
err = gml.Decode(rawdata)
if err != nil {
return err
}
/* Make sure there is only one graph */
if len(gml.Graphs) != 1 {
return fmt.Errorf("%d graphs found. Needs 1.", len(gml.Graphs))
}
graph := gml.Graphs[0]
/* 2. Get nodes */
nodeIndex := make(map[string]int)
for i, node := range graph.Nodes {
if len(node.Data) != 1 {
return fmt.Errorf("Node %s '%s' has more data '%d' than I wanted 1.", node.ID, node.Description, len(node.Data))
}
res := SANDNode{}
res.ID = i
res.Parent = -1
res.Indent = -1
res.Desc = node.Description
res.Oper = node.Data[0].Value
st.Nodes = append(st.Nodes, res)
nodeIndex[node.ID] = i
}
/* 3. Get Edges */
for _, edge := range graph.Edges {
for i := range st.Nodes {
/* Add Children */
if nodeIndex[edge.Source] == i {
st.Nodes[i].Child = append(st.Nodes[i].Child, nodeIndex[edge.Target])
}
/* Add Parent */
if nodeIndex[edge.Target] == i {
st.Nodes[i].Parent = nodeIndex[edge.Source]
}
}
}
return nil
}
/*********************************************************************/
func (st *SANDTree) GraphML() (*graphml.GraphML, error) {
/* TODO: Make sure st is populated */
/* GraphML */
gml := graphml.NewGraphML(st.Name)
/* Graph */
attributes := make(map[string]interface{})
attributes["SrcFormat"] = "SAND"
graph, err := gml.AddGraph(st.Name, graphml.EdgeDirectionDirected, attributes)
if err != nil {
return nil, err
}
/* Add Nodes */
nodeIndex := make(map[int]*graphml.Node)
for i := range st.Nodes {
attributes := make(map[string]interface{})
attributes["Operator"] = st.Nodes[i].Oper
node, err := graph.AddNode(attributes, st.Nodes[i].Desc)
if err != nil {
return nil, err
}
/* Keep new nodes mapping to old IDs */
nodeIndex[st.Nodes[i].ID] = node
}
/* Add Edges */
for _, node := range st.Nodes {
if node.Parent != -1 {
n1 := nodeIndex[node.Parent]
n2 := nodeIndex[node.ID]
attributes = make(map[string]interface{})
attributes["Operator"] = st.Nodes[node.Parent].Oper
_, err = graph.AddEdge(n1, n2, attributes, graphml.EdgeDirectionDefault, st.Nodes[node.Parent].Oper)
if err != nil {
return nil, err
}
}
}
return gml, nil
}
/*********************************************************************/
func (st *SANDTree) GetByName(desc string) SANDNode {
for i := range st.Nodes {
if st.Nodes[i].Desc == desc {
return st.Nodes[i]
}
}
return SANDNode{}
}