-
Notifications
You must be signed in to change notification settings - Fork 56
/
Graphviz.jl
278 lines (237 loc) · 8.13 KB
/
Graphviz.jl
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
""" AST and pretty printer for Graphviz's DOT language.
References:
- DOT grammar: http://www.graphviz.org/doc/info/lang.html
- DOT language guide: http://www.graphviz.org/pdf/dotguide.pdf
"""
module Graphviz
export Expression, Statement, Attributes, Graph, Digraph, Subgraph,
Node, NodeID, Edge, Label, pprint, run_graphviz
using DataStructures: OrderedDict
using StructEquality
const USE_GV_JLL = Ref(true)
gv_backend(backend::Symbol, prog) = gv_backend(Val{backend}, prog)
gv_backend(::Type{<:Val}, prog) = prog
# AST
#####
abstract type Expression end
abstract type Statement <: Expression end
""" AST type for Graphviz's "HTML-like" node labels.
For now, the HTML content is just a string.
"""
@struct_hash_equal struct Html
content::String
end
Base.print(io::IO, html::Html) = print(io, html.content)
const AttributeValue = Union{String,Html}
const Attributes = OrderedDict{Symbol,AttributeValue}
as_attributes(attrs::Attributes) = attrs
as_attributes(d::OrderedDict) = Attributes(Symbol(k) => d[k] for k in keys(d))
as_attributes(d::AbstractDict) =
Attributes(Symbol(k) => d[k] for k in sort!(collect(keys(d))))
struct Graph <: Expression
name::String
directed::Bool
prog::String
stmts::Vector{Statement}
graph_attrs::Attributes
node_attrs::Attributes
edge_attrs::Attributes
end
Graph(; name::AbstractString="g", directed::Bool=false,
prog::AbstractString="dot", stmts::Vector{Statement}=Statement[],
graph_attrs::AbstractDict=Attributes(),
node_attrs::AbstractDict=Attributes(),
edge_attrs::AbstractDict=Attributes()) =
Graph(name, directed, prog, stmts, as_attributes(graph_attrs),
as_attributes(node_attrs), as_attributes(edge_attrs))
Graph(name::AbstractString, stmts::Vector{Statement}; kw...) =
Graph(; name=name, directed=false, stmts=stmts, kw...)
Graph(name::AbstractString, stmts::Vararg{Statement}; kw...) =
Graph(; name=name, directed=false, stmts=collect(Statement, stmts), kw...)
Digraph(name::AbstractString, stmts::Vector{Statement}; kw...) =
Graph(; name=name, directed=true, stmts=stmts, kw...)
Digraph(name::AbstractString, stmts::Vararg{Statement}; kw...) =
Graph(; name=name, directed=true, stmts=collect(Statement, stmts), kw...)
struct Subgraph <: Statement
name::String
stmts::Vector{Statement}
graph_attrs::Attributes
node_attrs::Attributes
edge_attrs::Attributes
end
Subgraph(; name::AbstractString="", stmts::Vector{Statement}=Statement[],
graph_attrs::AbstractDict=Attributes(),
node_attrs::AbstractDict=Attributes(),
edge_attrs::AbstractDict=Attributes()) =
Subgraph(name, stmts, as_attributes(graph_attrs),
as_attributes(node_attrs), as_attributes(edge_attrs))
Subgraph(stmts::Vector{Statement}; kw...) = Subgraph(; stmts=stmts, kw...)
Subgraph(stmts::Vararg{Statement}; kw...) =
Subgraph(; stmts=collect(Statement, stmts), kw...)
Subgraph(name::AbstractString, stmts::Vector{Statement}; kw...) =
Subgraph(; name=name, stmts=stmts, kw...)
Subgraph(name::AbstractString, stmts::Vararg{Statement}; kw...) =
Subgraph(; name=name, stmts=collect(Statement, stmts), kw...)
struct Node <: Statement
name::String
attrs::Attributes
end
Node(name::String, attrs::AbstractDict) = Node(name, as_attributes(attrs))
Node(name::String; attrs...) = Node(name, attrs)
@struct_hash_equal struct NodeID <: Expression
name::String
port::String
anchor::String
NodeID(name::String, port::String="", anchor::String="") = new(name, port, anchor)
end
@struct_hash_equal struct Edge <: Statement
path::Vector{NodeID}
attrs::Attributes
end
Edge(path::Vector{NodeID}, attrs::AbstractDict) = Edge(path, as_attributes(attrs))
Edge(path::Vector{NodeID}; attrs...) = Edge(path, attrs)
Edge(path::Vararg{NodeID}; attrs...) = Edge(collect(path), attrs)
Edge(path::Vector{String}, attrs::AbstractDict) = Edge(map(NodeID, path), attrs)
Edge(path::Vector{String}; attrs...) = Edge(map(NodeID, path), attrs)
Edge(path::Vararg{String}; attrs...) = Edge(map(NodeID, collect(path)), attrs)
"""
labelloc defaults: "t" (clusters) , "b" (root graphs) , "c" (nodes)
For graphs and clusters, only labelloc=t and labelloc=b are allowed
"""
@struct_hash_equal struct Label <: Statement
labelloc::String
label::String
end
Label(; labelloc::AbstractString="", label::AbstractString="") =
Label(labelloc, label)
# Useful in unit tests. Not exported.
filter_statements(graph::Union{Graph,Subgraph}, type::Type) =
[ stmt for stmt in graph.stmts if stmt isa type ]
filter_statements(graph::Union{Graph,Subgraph}, type::Type, attr::Symbol) =
[ stmt.attrs[attr] for stmt in graph.stmts
if stmt isa type && haskey(stmt.attrs, attr) ]
# Bindings
##########
""" Run a Graphviz program.
Invokes Graphviz through its command-line interface. If the `Graphviz_jll`
package is installed and loaded, it is used; otherwise, Graphviz must be
installed on the local system.
For bindings to the Graphviz C API, see the the package
[GraphViz.jl](https://github.com/Keno/GraphViz.jl). At the time of this writing,
GraphViz.jl is unmaintained.
"""
function run_graphviz(io::IO, graph::Graph; prog::Union{String,Nothing}=nothing,
format::String="json0")
if isnothing(prog)
prog = graph.prog
end
@assert prog in ("dot","neato","fdp","sfdp","twopi","circo")
if USE_GV_JLL[]
prog = gv_backend(:graphviz_jll, prog)
end
open(`$prog -T$format`, io, write=true) do gv
pprint(gv, graph)
end
end
function run_graphviz(graph::Graph; kw...)
io = IOBuffer()
run_graphviz(io, graph; kw...)
seekstart(io)
end
function Base.show(io::IO, ::MIME"image/svg+xml", graph::Graph)
run_graphviz(io, graph, format="svg")
end
# Pretty-print
##############
""" Pretty-print the Graphviz expression.
"""
pprint(expr::Expression) = pprint(stdout, expr)
pprint(io::IO, expr::Expression) = pprint(io, expr, 0)
function pprint(io::IO, graph::Graph, n::Int)
indent(io, n)
print(io, graph.directed ? "digraph " : "graph ")
print(io, graph.name)
println(io, " {")
pprint_attrs(io, graph.graph_attrs, n+2; pre="graph", post=";\n")
pprint_attrs(io, graph.node_attrs, n+2; pre="node", post=";\n")
pprint_attrs(io, graph.edge_attrs, n+2; pre="edge", post=";\n")
for stmt in graph.stmts
pprint(io, stmt, n+2, directed=graph.directed)
println(io)
end
indent(io, n)
println(io, "}")
end
function pprint(io::IO, subgraph::Subgraph, n::Int; directed::Bool=false)
indent(io, n)
if isempty(subgraph.name)
println(io, "{")
else
print(io, "subgraph ")
print(io, subgraph.name)
println(io, " {")
end
pprint_attrs(io, subgraph.graph_attrs, n+2; pre="graph", post=";\n")
pprint_attrs(io, subgraph.node_attrs, n+2; pre="node", post=";\n")
pprint_attrs(io, subgraph.edge_attrs, n+2; pre="edge", post=";\n")
for stmt in subgraph.stmts
pprint(io, stmt, n+2, directed=directed)
println(io)
end
indent(io, n)
print(io, "}")
end
function pprint(io::IO, node::Node, n::Int; directed::Bool=false)
indent(io, n)
print(io, node.name)
pprint_attrs(io, node.attrs)
print(io, ";")
end
function pprint(io::IO, node::NodeID, n::Int)
print(io, node.name)
if !isempty(node.port)
print(io, ":")
print(io, node.port)
end
if !isempty(node.anchor)
print(io, ":")
print(io, node.anchor)
end
end
function pprint(io::IO, edge::Edge, n::Int; directed::Bool=false)
indent(io, n)
for (i, node) in enumerate(edge.path)
if i > 1
print(io, directed ? " -> " : " -- ")
end
pprint(io, node, n)
end
pprint_attrs(io, edge.attrs)
print(io, ";")
end
function pprint_attrs(io::IO, attrs::Attributes, n::Int=0;
pre::String="", post::String="")
if !isempty(attrs)
indent(io, n)
print(io, pre)
print(io, " [")
for (i, (key, value)) in enumerate(attrs)
if (i > 1) print(io, ",") end
print(io, key)
print(io, "=")
print(io, value isa Html ? "<" : "\"")
print(io, value)
print(io, value isa Html ? ">" : "\"")
end
print(io, "]")
print(io, post)
end
end
function pprint(io::IO, lab::Label, n::Int; directed::Bool=false)
if !isempty(lab.labelloc)
print(io, "labelloc=\"$(lab.labelloc)\";")
end
print(io, "label=\"$(lab.label)\";")
end
indent(io::IO, n::Int) = print(io, " "^n)
end