forked from awalterschulze/gographviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
182 lines (173 loc) · 4.37 KB
/
example_test.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
//Copyright 2013 GoGraphviz Authors
//
//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 gographviz
import (
"fmt"
"strconv"
)
func ExampleRead() {
g, err := Read([]byte(`digraph G {Hello->World}`))
if err != nil {
panic(err)
}
s := g.String()
fmt.Println(s)
// Output: digraph G {
// Hello->World;
// Hello;
// World;
//}
}
func ExampleNewGraph() {
g := NewGraph()
if err := g.SetName("G"); err != nil {
panic(err)
}
if err := g.SetDir(true); err != nil {
panic(err)
}
if err := g.AddNode("G", "Hello", nil); err != nil {
panic(err)
}
if err := g.AddNode("G", "World", nil); err != nil {
panic(err)
}
if err := g.AddEdge("Hello", "World", true, nil); err != nil {
panic(err)
}
s := g.String()
fmt.Println(s)
// Output: digraph G {
// Hello->World;
// Hello;
// World;
//}
}
type MyOwnGraphStructure struct {
weights map[int]map[int]int
max int
}
func NewMyOwnGraphStructure() *MyOwnGraphStructure {
return &MyOwnGraphStructure{
make(map[int]map[int]int),
0,
}
}
func (myown *MyOwnGraphStructure) SetStrict(strict bool) error { return nil }
func (myown *MyOwnGraphStructure) SetDir(directed bool) error { return nil }
func (myown *MyOwnGraphStructure) SetName(name string) error { return nil }
func (myown *MyOwnGraphStructure) AddPortEdge(src, srcPort, dst, dstPort string, directed bool, attrs map[string]string) error {
srci, err := strconv.Atoi(src)
if err != nil {
return err
}
dsti, err := strconv.Atoi(dst)
if err != nil {
return err
}
ai, err := strconv.Atoi(attrs["label"])
if err != nil {
return err
}
if _, ok := myown.weights[srci]; !ok {
myown.weights[srci] = make(map[int]int)
}
myown.weights[srci][dsti] = ai
if srci > myown.max {
myown.max = srci
}
if dsti > myown.max {
myown.max = dsti
}
return nil
}
func (myown *MyOwnGraphStructure) AddEdge(src, dst string, directed bool, attrs map[string]string) error {
return myown.AddPortEdge(src, "", dst, "", directed, attrs)
}
func (myown *MyOwnGraphStructure) AddNode(parentGraph string, name string, attrs map[string]string) error {
return nil
}
func (myown *MyOwnGraphStructure) AddAttr(parentGraph string, field, value string) error {
return nil
}
func (myown *MyOwnGraphStructure) AddSubGraph(parentGraph string, name string, attrs map[string]string) error {
return nil
}
func (myown *MyOwnGraphStructure) String() string { return "" }
//An Example of how to parse into your own simpler graph structure and output it back to graphviz.
//This example reads in only numbers and outputs a matrix graph.
func ExampleMyOwnGraphStructure() {
name := "matrix"
parsed, err := Parse([]byte(`
digraph G {
1 -> 2 [ label = 5 ];
4 -> 2 [ label = 1 ];
4 -> 1 [ label = 2 ];
1 -> 1 [ label = 0 ];
}
`))
if err != nil {
panic(err)
}
mine := NewMyOwnGraphStructure()
if err := Analyse(parsed, mine); err != nil {
panic(err)
}
output := NewGraph()
if err := output.SetName(name); err != nil {
panic(err)
}
if err := output.SetDir(true); err != nil {
panic(err)
}
for i := 1; i <= mine.max; i++ {
if err := output.AddNode(name, fmt.Sprintf("%v", i), nil); err != nil {
panic(err)
}
if _, ok := mine.weights[i]; !ok {
mine.weights[i] = make(map[int]int)
}
}
for i := 1; i <= mine.max; i++ {
for j := 1; j <= mine.max; j++ {
if err := output.AddEdge(fmt.Sprintf("%v", i), fmt.Sprintf("%v", j), true, map[string]string{"label": fmt.Sprintf("%v", mine.weights[i][j])}); err != nil {
panic(err)
}
}
}
s := output.String()
fmt.Println(s)
// Output: digraph matrix {
// 1->1 [ label=0 ];
// 1->2 [ label=5 ];
// 1->3 [ label=0 ];
// 1->4 [ label=0 ];
// 2->1 [ label=0 ];
// 2->2 [ label=0 ];
// 2->3 [ label=0 ];
// 2->4 [ label=0 ];
// 3->1 [ label=0 ];
// 3->2 [ label=0 ];
// 3->3 [ label=0 ];
// 3->4 [ label=0 ];
// 4->1 [ label=2 ];
// 4->2 [ label=1 ];
// 4->3 [ label=0 ];
// 4->4 [ label=0 ];
// 1;
// 2;
// 3;
// 4;
//}
}