-
Notifications
You must be signed in to change notification settings - Fork 2
/
graphData.go
491 lines (415 loc) · 10.7 KB
/
graphData.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
package dot
import (
"bytes"
"fmt"
"io"
"github.com/wwmoraes/dot/attributes"
"github.com/wwmoraes/dot/generators"
)
// graphData is a dot-compatible graph that stores child components, and
// auto-generates IDs internally
type graphData struct {
*attributes.Attributes
id string
graphType GraphType
strict bool
generator generators.IDGenerator
nodes map[string]Node
edgesFrom map[string][]StyledEdge
subgraphs map[string]Graph
parent Graph
sameRank map[string][]Node
//
nodeInitializer NodeInitializerFn
edgeInitializer EdgeInitializerFn
}
// New return a Graph after all option functions are processed
func New(optionsFn ...GraphOptionFn) (Graph, error) {
options, err := NewGraphOptions(optionsFn...)
if err != nil {
return nil, err
}
return NewWithOptions(options)
}
// NewWithOptions returns a Graph using the options values
func NewWithOptions(options GraphOptions) (Graph, error) {
if options.Generator() == nil {
return nil, ErrGraphWithoutGenerator
}
if options.Type() == GraphTypeSub {
if options.Parent() == nil {
return nil, ErrSubgraphWithoutParent
}
} else {
if options.Parent() != nil {
return nil, ErrRootWithParent
}
if options.Cluster() {
return nil, ErrRootAsCluster
}
}
return &graphData{
id: options.ID(),
parent: options.Parent(),
Attributes: attributes.NewAttributes(),
graphType: options.Type(),
strict: options.Strict(),
generator: options.Generator(),
nodes: map[string]Node{},
edgesFrom: map[string][]StyledEdge{},
subgraphs: map[string]Graph{},
sameRank: map[string][]Node{},
nodeInitializer: options.NodeInitializer(),
edgeInitializer: options.EdgeInitializer(),
}, nil
}
// ID returns the immutable id
func (thisGraph *graphData) ID() string {
return thisGraph.id
}
// Root returns the root graph (i.e. the topmost, without a parent graph)
func (thisGraph *graphData) Root() Graph {
if thisGraph.parent == nil {
return thisGraph
}
return thisGraph.parent.Root()
}
// Type returns the graph type: directed, undirected or sub
func (thisGraph *graphData) Type() GraphType {
return thisGraph.graphType
}
func (thisGraph *graphData) Subgraph(optionsFn ...GraphOptionFn) (Graph, error) {
subgraphOptions := [...]GraphOptionFn{
WithParent(thisGraph),
WithGenerator(thisGraph.generator),
}
newOptionsFn := make([]GraphOptionFn, 0, len(subgraphOptions)+len(optionsFn))
for _, subgraphOption := range subgraphOptions {
newOptionsFn = append(newOptionsFn, subgraphOption)
}
newOptionsFn = append(newOptionsFn, optionsFn...)
graph, err := New(newOptionsFn...)
if err != nil {
return nil, err
}
// save on parent with the generated ID
thisGraph.subgraphs[graph.ID()] = graph
return graph, nil
}
// FindSubgraph returns the subgraph of the graph or one from its parents.
func (thisGraph *graphData) FindSubgraph(id string) (Graph, bool) {
if sub, ok := thisGraph.subgraphs[id]; ok {
return sub, ok
}
if thisGraph.parent == nil {
return nil, false
}
return thisGraph.parent.FindSubgraph(id)
}
func (thisGraph *graphData) FindNode(id string) (Node, bool) {
if n, ok := thisGraph.nodes[id]; ok {
return n, ok
}
if thisGraph.parent == nil {
return nil, false
}
return thisGraph.parent.FindNode(id)
}
// Node returns the node created with this id or creates a new node if absent
// This method can be used as both a constructor and accessor.
// not thread safe!
func (thisGraph *graphData) Node(id string) Node {
if n, ok := thisGraph.FindNode(id); ok {
return n
}
if len(id) == 0 {
id = thisGraph.generator.String()
}
n := &nodeData{
id: id,
Attributes: attributes.NewAttributes(),
graph: thisGraph,
}
if thisGraph.nodeInitializer != nil {
thisGraph.nodeInitializer(n)
}
// store local
thisGraph.nodes[id] = n
return n
}
// Edge creates a new edge between two nodes
func (thisGraph *graphData) Edge(fromNode, toNode Node) StyledEdge {
return thisGraph.EdgeWithAttributes(fromNode, toNode, nil)
}
// Edge creates a new edge between two nodes, and set the given attributes
func (thisGraph *graphData) EdgeWithAttributes(fromNode, toNode Node, attr attributes.Reader) StyledEdge {
e := &edgeData{
from: fromNode,
to: toNode,
Attributes: attributes.NewAttributesFrom(attr),
graph: thisGraph}
if thisGraph.edgeInitializer != nil {
thisGraph.edgeInitializer(e)
}
thisGraph.edgesFrom[fromNode.ID()] = append(thisGraph.edgesFrom[fromNode.ID()], e)
return e
}
// FindEdges finds all edges in the graph that go from the fromNode to the toNode.
// Otherwise, returns an empty slice.
func (thisGraph *graphData) FindEdges(fromNode, toNode Node) (found []Edge) {
found = make([]Edge, 0)
if edges, ok := thisGraph.edgesFrom[fromNode.ID()]; ok {
for _, e := range edges {
if e.To().ID() == toNode.ID() {
found = append(found, e)
}
}
}
return found
}
// AddToSameRank adds the given nodes to the specified rank group, forcing them to be rendered in the same row
func (thisGraph *graphData) AddToSameRank(group string, nodes ...Node) {
thisGraph.sameRank[group] = append(thisGraph.sameRank[group], nodes...)
}
// String returns the graph transformed into string dot notation
func (thisGraph *graphData) String() (string, error) {
var b bytes.Buffer
_, err := thisGraph.WriteTo(&b)
return b.String(), err
}
func (thisGraph *graphData) WriteTo(w io.Writer) (n int64, err error) {
var written32 int
var written64 int64
// write strict tag
written64, err = thisGraph.writeStrictTagTo(w)
n += written64
if err != nil {
return
}
// graph type
written32, err = fmt.Fprintf(w, `%s`, thisGraph.graphType)
n += int64(written32)
if err != nil {
return
}
// write graph id
if len(thisGraph.id) > 0 {
written32, err = fmt.Fprintf(w, ` "%s"`, thisGraph.id)
n += int64(written32)
if err != nil {
return
}
}
// write open block
written32, err = fmt.Fprint(w, " {")
n += int64(written32)
if err != nil {
return
}
// write graph body
written64, err = thisGraph.writeGraphBodyTo(w)
n += written64
if err != nil {
return
}
// write close block
written32, err = fmt.Fprintf(w, "}")
n += int64(written32)
return
}
// VisitNodes visits all nodes recursively
func (thisGraph *graphData) VisitNodes(callback func(node Node) (done bool)) {
for _, node := range thisGraph.nodes {
done := callback(node)
if done {
return
}
}
for _, subGraph := range thisGraph.subgraphs {
subGraph.VisitNodes(callback)
}
}
// FindNodeByID return node by id
func (thisGraph *graphData) FindNodeByID(id string) (foundNode Node, found bool) {
thisGraph.VisitNodes(func(node Node) (done bool) {
if node.ID() == id {
found = true
foundNode = node
return true
}
return false
})
return
}
// FindNodes returns all nodes recursively
func (thisGraph *graphData) FindNodes() (nodes []Node) {
var foundNodes []Node
thisGraph.VisitNodes(func(node Node) (done bool) {
foundNodes = append(foundNodes, node)
return false
})
return foundNodes
}
// HasSubgraphs returns true if the graph has any subgraphs
func (thisGraph *graphData) HasSubgraphs() bool {
return len(thisGraph.subgraphs) > 0
}
// HasNodes returns true if the graph has any nodes
func (thisGraph *graphData) HasNodes() bool {
return len(thisGraph.nodes) > 0
}
// HasEdges returns true if the graph has any edges
func (thisGraph *graphData) HasEdges() bool {
return len(thisGraph.edgesFrom) > 0
}
// HasSameRankNodes returns true if the graph has nodes grouped as same rank
func (thisGraph *graphData) HasSameRankNodes() bool {
return len(thisGraph.sameRank) > 0
}
func (thisGraph *graphData) writeGraphBodyTo(w io.Writer) (n int64, err error) {
// write attributes
written64, err := thisGraph.writeAttributesTo(w)
n += written64
if err != nil {
return
}
// write subgraphs
written64, err = thisGraph.writeSubgraphsTo(w)
n += written64
if err != nil {
return
}
// write nodes
written64, err = thisGraph.writeNodesTo(w)
n += written64
if err != nil {
return
}
// node groups
written64, err = thisGraph.writeSameRankNodesTo(w)
n += written64
if err != nil {
return
}
// write edges
written64, err = thisGraph.writeEdgesTo(w)
n += written64
if err != nil {
return
}
return n, err
}
func (thisGraph *graphData) writeAttributesTo(w io.Writer) (n int64, err error) {
if !thisGraph.HasAttributes() {
return 0, nil
}
written32, err := fmt.Fprintf(w, "graph ")
n += int64(written32)
if err != nil {
return n, err
}
written64, err := thisGraph.Attributes.WriteTo(w)
n += written64
if err != nil {
return n, err
}
written32, err = fmt.Fprintf(w, ";")
n += int64(written32)
if err != nil {
return n, err
}
return n, err
}
func (thisGraph *graphData) writeSubgraphsTo(w io.Writer) (n int64, err error) {
if !thisGraph.HasSubgraphs() {
return 0, nil
}
for _, key := range thisGraph.sortedSubgraphsKeys() {
each := thisGraph.subgraphs[key]
written64, err := each.WriteTo(w)
n += written64
if err != nil {
return n, err
}
}
return n, err
}
func (thisGraph *graphData) writeNodesTo(w io.Writer) (n int64, err error) {
if !thisGraph.HasNodes() {
return 0, nil
}
for _, key := range thisGraph.sortedNodesKeys() {
each := thisGraph.nodes[key]
written64, err := each.WriteTo(w)
n += written64
if err != nil {
return n, err
}
}
return n, err
}
func (thisGraph *graphData) writeEdgesTo(w io.Writer) (n int64, err error) {
if !thisGraph.HasEdges() {
return 0, nil
}
for _, each := range thisGraph.sortedEdgesFromKeys() {
all := thisGraph.edgesFrom[each]
for _, each := range all {
written64, err := each.WriteTo(w)
n += written64
if err != nil {
return n, err
}
}
}
return n, err
}
func (thisGraph *graphData) writeSameRankNodesTo(w io.Writer) (n int64, err error) {
if !thisGraph.HasSameRankNodes() {
return 0, nil
}
for _, nodes := range thisGraph.sameRank {
// open group
written32, err := fmt.Fprintf(w, "{")
n += int64(written32)
if err != nil {
return n, err
}
// set rank attribute
written32, err = fmt.Fprintf(w, "rank=same;")
n += int64(written32)
if err != nil {
return n, err
}
// write group nodes
for _, node := range nodes {
written64, err := node.WriteTo(w)
n += written64
if err != nil {
return n, err
}
}
// close group
written32, err = fmt.Fprintf(w, "}")
n += int64(written32)
if err != nil {
return n, err
}
}
return n, err
}
func (thisGraph *graphData) writeStrictTagTo(w io.Writer) (n int64, err error) {
if !thisGraph.IsStrict() {
return 0, nil
}
written32, err := fmt.Fprint(w, "strict ")
n += int64(written32)
if err != nil {
return n, err
}
return n, err
}
// IsStrict return true if the graph is set as strict
func (thisGraph *graphData) IsStrict() bool {
return thisGraph.strict
}