-
Notifications
You must be signed in to change notification settings - Fork 1
/
graphviz.rb
297 lines (257 loc) · 5.71 KB
/
graphviz.rb
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
#!/usr/bin/ruby
# vim: set ts=2 sw=2:
require 'set'
########## MODELLING ##########
class GNode
class <<self
def [](k,a={}); self.new(k,a); end
end
@@node_counter = 0
attr_accessor :key, :node, :attrs
attr_accessor :inc, :out
attr_accessor :rank
def initialize(key, attrs = {})
@key = key
@attrs = attrs
self[:label] = key
@node = "n#@@node_counter"
@@node_counter += 1
@inc = Set[]
@out = Set[]
end
def initialize_copy(src)
super
@inc = Set[]
@out = Set[]
end
def label; self[:label]; end
def label=(v); self[:label] = v; end
def inc_nodes; @inc.map {|i| i.snode }; end
def out_nodes; @out.map {|i| i.dnode }; end
def [](k); attrs[k]; end
def []=(k, v); attrs[k] = v; end
def inspect
hsh = (Graph.include_hashes and " ##{hash}" or "")
"N[#@key#{hsh} #{inc.count}/#{out.count}]"
end
end
class GEdge
class <<self
def [](s,d,a={}); self.new(s,d,a); end
end
attr_accessor :snode, :dnode, :attrs
def initialize(snode, dnode, attrs = {})
@snode, @dnode = snode, dnode
@attrs = attrs
end
def initialize_copy(src)
raise "Don't dup edges"
end
def ==(o)
snode == o.snode and dnode == o.dnode
end
def src; @snode.label; end
def dst; @dnode.label; end
def [](k); attrs[k]; end
def []=(k, v); attrs[k] = v; end
def inspect
hsh = (Graph.include_hashes and ", ##{hash}" or "")
"E[#{snode.inspect} -> #{dnode.inspect}#{hsh}]"
end
end
class Graph
class <<self
def include_hashes
false
end
def from_hash(hash)
g = Graph.new
hash.each_key {|k|
g.add GNode[k]
}
hash.each_pair {|k, run_list|
src = g.get(k)
run_list.each_with_index { |i,ix|
dst = g.get(i)
if dst == nil
dst = GNode[i]
g.add dst
end
g.add GEdge[src, dst, {
:taillabel => ix + 1,
:labeldistance => 2.0 }]
}
}
g
end
end
attr_accessor :nodes, :edges, :attrs
def initialize(nodes = [], edges = [], attrs = {})
@nodes = Set[*nodes]
@edges = Set[]
add(*edges)
@attrs = attrs
end
def initialize_copy(src)
super
@nodes = @nodes.map {|i| i.dup }.to_set
@edges = Set[]
src.edges.each {|i|
add GEdge[get(i.snode.key), get(i.dnode.key), i.attrs.dup]
}
@attrs = @attrs.dup
end
def [](k); attrs[k]; end
def []=(k, v); attrs[k] = v; end
def get(k1, k2=nil)
case k2
when nil
@nodes.each {|n|
return n if n.key == k1
}
else
@edges.each {|e|
return n if n.snode.key == k1 and n.dnode.key == k2
}
end
nil
end
def get_or_make(k)
n = get(k)
if n == nil
n = GNode[k]
add n
end
n
end
def match(*pats)
Set.new(@nodes).keep_if {|n|
pats.any? {|p| n.label =~ p }
}
end
def match_src(*pats)
Set.new(@edges).keep_if {|e|
pats.any? {|p| e.src =~ p }
}
end
def match_dst(*pats)
Set.new(@edges).keep_if {|e|
pats.any? {|p| e.dst =~ p }
}
end
def add(*items)
items.each {|i|
case i
when GNode
# puts "N+++: #{i.inspect}"
@nodes.add i
when GEdge
# puts "E+++: #{i.inspect}"
@nodes.add i.snode
@nodes.add i.dnode
@edges.add i
i.snode.out.add i
i.dnode.inc.add i
# puts "E---: #{i.inspect}"
else raise "Unexpected item: #{i.inspect}"
end
}
end
def cut(*items)
items.each {|i|
case i
when GNode
@nodes.delete i
cut(*(i.out + i.inc))
when GEdge
@edges.delete i
i.snode.out.delete i
i.dnode.inc.delete i
else raise "Unexpected item: #{i.inspect}"
end
}
end
def lowercut(*items)
items.each {|i|
# puts "cut: #{i.inspect}"
case i
when GNode
@nodes.delete i
lowercut(*(i.out + i.inc))
when GEdge
cut i
if i.dnode.inc.empty?
lowercut i.dnode
end
else raise "Unexpected item: #{i.inspect}"
end
}
end
def focus(*nodes)
# TODO: make new nodes + edges
keep_nodes = Set[]
keep_edges = Set[]
to_walk = Array.new(nodes)
while !to_walk.empty?
item = to_walk.pop
keep_nodes << item
keep_edges += item.inc
to_walk += item.inc_nodes - keep_nodes.to_a
end
to_walk = Array.new(nodes)
while !to_walk.empty?
item = to_walk.pop
keep_nodes << item
keep_edges += item.out
to_walk += item.out_nodes - keep_nodes.to_a
end
Graph.new(keep_nodes, keep_edges, @attrs)
end
def inspect
[(Graph.include_hashes and "Hash: ##{hash}" or nil),
"Nodes:",
@nodes.map {|n| " "+ n.inspect },
"Edges:",
@edges.map {|e| " "+ e.inspect }].
compact.
join("\n")
end
end
########## GENERATE DOT #############
def joinattrs(h)
h.select {|k,v| v != nil }.
map {|k,v| "#{k}=\"#{v}\""}
end
def wrapattrs(h)
a = joinattrs(h).join(", ")
a.length > 0 and " [#{a}]" or ""
end
def g2dot(graph)
dot_ranks = graph.nodes.
# TODO pass rank_node in somehow
classify {|v| rank_node(v) }.
map {|k,v|
nl = "\n "
nl += " " if k != nil
rnodes = v.map {|n|
nl + n.node + wrapattrs(n.attrs) +";"
}.join("")
case k
when nil then rnodes
when :source, :min, :max, :sink
"{ rank = #{k};#{rnodes} }"
else
"{ rank = same;#{rnodes} }"
end
}.join("\n ")
dot_edges = graph.edges.map {|e|
"#{e.snode.node} -> #{e.dnode.node}#{wrapattrs(e.attrs)};"
}.join("\n ")
<<-EOT
digraph G {
#{joinattrs(graph.attrs).join("\n ")}
#{dot_ranks}
#{dot_edges}
}
EOT
end