-
Notifications
You must be signed in to change notification settings - Fork 74
/
write.go
172 lines (153 loc) · 4.19 KB
/
write.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
//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
//
// https://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"
"github.com/awalterschulze/gographviz/ast"
)
type writer struct {
*Graph
writtenLocations map[string]bool
}
func newWriter(g *Graph) *writer {
return &writer{g, make(map[string]bool)}
}
func appendAttrs(list ast.StmtList, attrs Attrs) ast.StmtList {
for _, name := range attrs.sortedNames() {
stmt := &ast.Attr{
Field: ast.ID(name),
Value: ast.ID(attrs[name]),
}
list = append(list, stmt)
}
return list
}
func (w *writer) newSubGraph(name string) (*ast.SubGraph, error) {
sub := w.SubGraphs.SubGraphs[name]
w.writtenLocations[sub.Name] = true
s := &ast.SubGraph{}
s.ID = ast.ID(sub.Name)
s.StmtList = appendAttrs(s.StmtList, sub.Attrs)
children := w.Relations.SortedChildren(name)
for _, child := range children {
if w.IsNode(child) {
s.StmtList = append(s.StmtList, w.newNodeStmt(child))
} else if w.IsSubGraph(child) {
subgraph, err := w.newSubGraph(child)
if err != nil {
return nil, err
}
s.StmtList = append(s.StmtList, subgraph)
} else {
return nil, fmt.Errorf("%v is not a node or a subgraph", child)
}
}
return s, nil
}
func (w *writer) newNodeID(name string, port string) *ast.NodeID {
node := w.Nodes.Lookup[name]
return ast.MakeNodeID(node.Name, port)
}
func (w *writer) newNodeStmt(name string) *ast.NodeStmt {
node := w.Nodes.Lookup[name]
id := ast.MakeNodeID(node.Name, "")
w.writtenLocations[node.Name] = true
return &ast.NodeStmt{
NodeID: id,
Attrs: ast.PutMap(node.Attrs.toMap()),
}
}
func (w *writer) newLocation(name string, port string) (ast.Location, error) {
if w.IsNode(name) {
return w.newNodeID(name, port), nil
} else if w.isClusterSubGraph(name) {
if len(port) != 0 {
return nil, fmt.Errorf("subgraph cannot have a port: %v", port)
}
return ast.MakeNodeID(name, port), nil
} else if w.IsSubGraph(name) {
if len(port) != 0 {
return nil, fmt.Errorf("subgraph cannot have a port: %v", port)
}
return w.newSubGraph(name)
}
return nil, fmt.Errorf("%v is not a node or a subgraph", name)
}
func (w *writer) newEdgeStmt(edge *Edge) (*ast.EdgeStmt, error) {
src, err := w.newLocation(edge.Src, edge.SrcPort)
if err != nil {
return nil, err
}
dst, err := w.newLocation(edge.Dst, edge.DstPort)
if err != nil {
return nil, err
}
stmt := &ast.EdgeStmt{
Source: src,
EdgeRHS: ast.EdgeRHS{
&ast.EdgeRH{
Op: ast.EdgeOp(edge.Dir),
Destination: dst,
},
},
Attrs: ast.PutMap(edge.Attrs.toMap()),
}
return stmt, nil
}
func (w *writer) Write() (*ast.Graph, error) {
t := &ast.Graph{}
t.Strict = w.Strict
t.Type = ast.GraphType(w.Directed)
t.ID = ast.ID(w.Name)
t.StmtList = appendAttrs(t.StmtList, w.Attrs)
for _, edge := range w.Edges.Sorted() {
e, err := w.newEdgeStmt(edge)
if err != nil {
return nil, err
}
t.StmtList = append(t.StmtList, e)
}
subGraphs := w.SubGraphs.Sorted()
for _, s := range subGraphs {
if _, ok := w.writtenLocations[s.Name]; !ok {
if _, ok := w.Relations.ParentToChildren[w.Name][s.Name]; ok {
s, err := w.newSubGraph(s.Name)
if err != nil {
return nil, err
}
t.StmtList = append(t.StmtList, s)
}
}
}
nodes := w.Nodes.Sorted()
for _, n := range nodes {
if _, ok := w.writtenLocations[n.Name]; !ok {
t.StmtList = append(t.StmtList, w.newNodeStmt(n.Name))
}
}
return t, nil
}
// WriteAst creates an Abstract Syntrax Tree from the Graph.
func (g *Graph) WriteAst() (*ast.Graph, error) {
w := newWriter(g)
return w.Write()
}
// String returns a DOT string representing the Graph.
func (g *Graph) String() string {
w, err := g.WriteAst()
if err != nil {
return fmt.Sprintf("error: %v", err)
}
return w.String()
}