jumoku 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/lib/jumoku.rb +4 -2
- data/lib/jumoku/version.rb +1 -1
- data/spec/raw_tree_spec.rb +353 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +14 -0
- data/spec/tree_spec.rb +553 -0
- data/vendor/git/graphy/CREDITS.md +31 -0
- data/vendor/git/graphy/LICENSE +35 -0
- data/vendor/git/graphy/README.md +186 -0
- data/vendor/git/graphy/Rakefile +61 -0
- data/vendor/git/graphy/TODO.md +20 -0
- data/vendor/git/graphy/VERSION +1 -0
- data/vendor/git/graphy/examples/graph_self.rb +56 -0
- data/vendor/git/graphy/examples/module_graph.jpg +0 -0
- data/vendor/git/graphy/examples/module_graph.rb +14 -0
- data/vendor/git/graphy/examples/self_graph.jpg +0 -0
- data/vendor/git/graphy/examples/visualize.jpg +0 -0
- data/vendor/git/graphy/examples/visualize.rb +10 -0
- data/vendor/git/graphy/graphy.gemspec +149 -0
- data/vendor/git/graphy/lib/graphy.rb +90 -0
- data/vendor/git/graphy/lib/graphy/adjacency_graph.rb +224 -0
- data/vendor/git/graphy/lib/graphy/arc.rb +65 -0
- data/vendor/git/graphy/lib/graphy/arc_number.rb +52 -0
- data/vendor/git/graphy/lib/graphy/biconnected.rb +84 -0
- data/vendor/git/graphy/lib/graphy/chinese_postman.rb +91 -0
- data/vendor/git/graphy/lib/graphy/classes/graph_classes.rb +28 -0
- data/vendor/git/graphy/lib/graphy/common.rb +63 -0
- data/vendor/git/graphy/lib/graphy/comparability.rb +63 -0
- data/vendor/git/graphy/lib/graphy/directed_graph.rb +76 -0
- data/vendor/git/graphy/lib/graphy/directed_graph/algorithms.rb +92 -0
- data/vendor/git/graphy/lib/graphy/directed_graph/distance.rb +167 -0
- data/vendor/git/graphy/lib/graphy/dot.rb +94 -0
- data/vendor/git/graphy/lib/graphy/edge.rb +37 -0
- data/vendor/git/graphy/lib/graphy/ext.rb +79 -0
- data/vendor/git/graphy/lib/graphy/graph.rb +631 -0
- data/vendor/git/graphy/lib/graphy/graph_api.rb +35 -0
- data/vendor/git/graphy/lib/graphy/labels.rb +113 -0
- data/vendor/git/graphy/lib/graphy/maximum_flow.rb +77 -0
- data/vendor/git/graphy/lib/graphy/ruby_compatibility.rb +17 -0
- data/vendor/git/graphy/lib/graphy/search.rb +511 -0
- data/vendor/git/graphy/lib/graphy/strong_components.rb +93 -0
- data/vendor/git/graphy/lib/graphy/support/support.rb +9 -0
- data/vendor/git/graphy/lib/graphy/undirected_graph.rb +57 -0
- data/vendor/git/graphy/lib/graphy/undirected_graph/algorithms.rb +90 -0
- data/vendor/git/graphy/spec/biconnected_spec.rb +27 -0
- data/vendor/git/graphy/spec/chinese_postman_spec.rb +27 -0
- data/vendor/git/graphy/spec/community_spec.rb +44 -0
- data/vendor/git/graphy/spec/complement_spec.rb +27 -0
- data/vendor/git/graphy/spec/digraph_distance_spec.rb +121 -0
- data/vendor/git/graphy/spec/digraph_spec.rb +339 -0
- data/vendor/git/graphy/spec/dot_spec.rb +48 -0
- data/vendor/git/graphy/spec/edge_spec.rb +159 -0
- data/vendor/git/graphy/spec/inspection_spec.rb +40 -0
- data/vendor/git/graphy/spec/multi_edge_spec.rb +32 -0
- data/vendor/git/graphy/spec/neighborhood_spec.rb +38 -0
- data/vendor/git/graphy/spec/properties_spec.rb +146 -0
- data/vendor/git/graphy/spec/search_spec.rb +227 -0
- data/vendor/git/graphy/spec/spec.opts +4 -0
- data/vendor/git/graphy/spec/spec_helper.rb +56 -0
- data/vendor/git/graphy/spec/strong_components_spec.rb +61 -0
- data/vendor/git/graphy/spec/triangulated_spec.rb +125 -0
- data/vendor/git/graphy/spec/undirected_graph_spec.rb +220 -0
- data/vendor/git/graphy/vendor/priority-queue/CHANGELOG +33 -0
- data/vendor/git/graphy/vendor/priority-queue/Makefile +140 -0
- data/vendor/git/graphy/vendor/priority-queue/README +133 -0
- data/vendor/git/graphy/vendor/priority-queue/benchmark/dijkstra.rb +171 -0
- data/vendor/git/graphy/vendor/priority-queue/compare_comments.rb +49 -0
- data/vendor/git/graphy/vendor/priority-queue/doc/c-vs-rb.png +0 -0
- data/vendor/git/graphy/vendor/priority-queue/doc/compare_big.gp +14 -0
- data/vendor/git/graphy/vendor/priority-queue/doc/compare_big.png +0 -0
- data/vendor/git/graphy/vendor/priority-queue/doc/compare_small.gp +15 -0
- data/vendor/git/graphy/vendor/priority-queue/doc/compare_small.png +0 -0
- data/vendor/git/graphy/vendor/priority-queue/doc/results.csv +37 -0
- data/vendor/git/graphy/vendor/priority-queue/ext/priority_queue/CPriorityQueue/extconf.rb +2 -0
- data/vendor/git/graphy/vendor/priority-queue/ext/priority_queue/CPriorityQueue/priority_queue.c +947 -0
- data/vendor/git/graphy/vendor/priority-queue/lib/priority_queue.rb +14 -0
- data/vendor/git/graphy/vendor/priority-queue/lib/priority_queue/c_priority_queue.rb +1 -0
- data/vendor/git/graphy/vendor/priority-queue/lib/priority_queue/poor_priority_queue.rb +46 -0
- data/vendor/git/graphy/vendor/priority-queue/lib/priority_queue/ruby_priority_queue.rb +526 -0
- data/vendor/git/graphy/vendor/priority-queue/priority_queue.so +0 -0
- data/vendor/git/graphy/vendor/priority-queue/setup.rb +1551 -0
- data/vendor/git/graphy/vendor/priority-queue/test/priority_queue_test.rb +371 -0
- data/vendor/git/graphy/vendor/rdot.rb +360 -0
- metadata +83 -1
@@ -0,0 +1,371 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# Priority Queue tests
|
4
|
+
|
5
|
+
$:.unshift '../ext/priority_queue/CPriorityQueue'
|
6
|
+
$:.unshift '../lib/'
|
7
|
+
|
8
|
+
require 'test/unit'
|
9
|
+
|
10
|
+
require 'priority_queue/ruby_priority_queue'
|
11
|
+
require 'priority_queue/poor_priority_queue'
|
12
|
+
begin
|
13
|
+
require 'priority_queue/CPriorityQueue'
|
14
|
+
rescue LoadError
|
15
|
+
require 'CPriorityQueue'
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
module PriorityQueueTest
|
20
|
+
# Check that the order is maintained
|
21
|
+
def teardown
|
22
|
+
last = @q.min_priority
|
23
|
+
while priority = @q.delete_min_return_priority
|
24
|
+
assert_operator(last, :<=, priority)
|
25
|
+
last = priority
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_length
|
30
|
+
20.times do | i |
|
31
|
+
assert_equal(i, @q.length)
|
32
|
+
@q[i] = i
|
33
|
+
end
|
34
|
+
10.times do | i |
|
35
|
+
assert_equal(20-i, @q.length)
|
36
|
+
@q.delete_min
|
37
|
+
end
|
38
|
+
10.times do | i |
|
39
|
+
assert_equal(10+i, @q.length)
|
40
|
+
@q[i] = i
|
41
|
+
end
|
42
|
+
@q.delete(5)
|
43
|
+
assert_equal(19, @q.length)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_merge
|
47
|
+
@q1 = @q.class.new
|
48
|
+
@q2 = @q.class.new
|
49
|
+
|
50
|
+
20.times do | i |
|
51
|
+
@q1[i] = i
|
52
|
+
@q2[i+20] = i+20
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# Assure that delete min works
|
57
|
+
def test_delete_min
|
58
|
+
assert_equal(nil, @q.delete_min, "Empty queue should pop nil")
|
59
|
+
@q["n1"] = 0
|
60
|
+
assert_equal(["n1", 0], @q.delete_min)
|
61
|
+
@q["n1"] = 0
|
62
|
+
@q["n2"] = -1
|
63
|
+
assert_equal(["n2", -1], @q.delete_min)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_delete_min_return_key
|
67
|
+
assert_equal(nil, @q.delete_min_return_key, "Empty queue should pop nil")
|
68
|
+
@q["n1"] = 0
|
69
|
+
assert_equal("n1", @q.delete_min_return_key)
|
70
|
+
@q["n1"] = 0
|
71
|
+
@q["n2"] = -1
|
72
|
+
assert_equal("n2", @q.delete_min_return_key)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_delete_min_return_priority
|
76
|
+
assert_equal(nil, @q.delete_min_return_priority, "Empty queue should pop nil")
|
77
|
+
@q["n1"] = 0
|
78
|
+
assert_equal(0, @q.delete_min_return_priority)
|
79
|
+
@q["n1"] = 0
|
80
|
+
@q["n2"] = -1
|
81
|
+
assert_equal(-1, @q.delete_min_return_priority)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_has_key?
|
85
|
+
assert(!@q.has_key?(1))
|
86
|
+
@q[1] = 1
|
87
|
+
assert(@q.has_key?(1))
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_empty?
|
91
|
+
assert_equal(true, @q.empty?, "Empty queue should return true on empty?")
|
92
|
+
@q["node1"] = 10
|
93
|
+
assert_equal(false, @q.empty?, "Filled queue should return false on empty?")
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_push
|
97
|
+
20.times do | i |
|
98
|
+
@q.push i, i+10
|
99
|
+
end
|
100
|
+
|
101
|
+
20.times do | i |
|
102
|
+
@q.push i, i
|
103
|
+
end
|
104
|
+
|
105
|
+
20.times do | i |
|
106
|
+
assert_equal([i, i], @q.delete_min)
|
107
|
+
end
|
108
|
+
|
109
|
+
assert_equal(nil, @q.delete_min)
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_push_pop
|
113
|
+
20.times do | i |
|
114
|
+
@q.push i, i
|
115
|
+
end
|
116
|
+
|
117
|
+
20.times do | i |
|
118
|
+
assert_equal([i, i], @q.delete_min)
|
119
|
+
end
|
120
|
+
|
121
|
+
assert_equal(nil, @q.delete_min)
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_push_decrease_pop
|
125
|
+
50.times do | i |
|
126
|
+
@q.push i, i
|
127
|
+
end
|
128
|
+
|
129
|
+
10.times do | i |
|
130
|
+
assert_equal([i, i], @q.delete_min)
|
131
|
+
end
|
132
|
+
|
133
|
+
10.times do | i |
|
134
|
+
@q[i+10] = i
|
135
|
+
end
|
136
|
+
|
137
|
+
10.times do | i |
|
138
|
+
assert_equal([i+10, i], @q.delete_min)
|
139
|
+
end
|
140
|
+
|
141
|
+
30.times do | i |
|
142
|
+
assert_equal([i+20, i+20], @q.delete_min)
|
143
|
+
end
|
144
|
+
|
145
|
+
assert_equal(nil, @q.delete_min)
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_min_key
|
149
|
+
assert_equal(nil, @q.min_key)
|
150
|
+
@q["node1"] = 0
|
151
|
+
assert_equal("node1", @q.min_key)
|
152
|
+
@q["node2"] = 1
|
153
|
+
assert_equal("node1", @q.min_key)
|
154
|
+
@q["node3"] = -1
|
155
|
+
assert_equal("node3", @q.min_key)
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_min_priority
|
159
|
+
assert_equal(nil, @q.min_priority)
|
160
|
+
@q["node1"] = 0
|
161
|
+
assert_equal(0, @q.min_priority)
|
162
|
+
@q["node2"] = 1
|
163
|
+
assert_equal(0, @q.min_priority)
|
164
|
+
@q["node3"] = -1
|
165
|
+
assert_equal(-1, @q.min_priority)
|
166
|
+
end
|
167
|
+
|
168
|
+
def test_access
|
169
|
+
assert_equal(0, @q["node1"] = 0)
|
170
|
+
assert_equal(["node1", 0], @q.min)
|
171
|
+
assert_equal(1, @q["node2"] = 1)
|
172
|
+
assert_equal(1, @q["node2"])
|
173
|
+
assert_equal("node1", @q.min_key)
|
174
|
+
assert_equal(2, @q["node3"] = 2)
|
175
|
+
assert_equal(2, @q["node3"])
|
176
|
+
assert_equal("node1", @q.min_key)
|
177
|
+
assert_equal(-1, @q["node3"] = -1)
|
178
|
+
assert_equal(-1, @q["node3"])
|
179
|
+
assert_equal("node3", @q.min_key)
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_min
|
183
|
+
assert_equal(nil, @q.min)
|
184
|
+
@q["node1"] = 10
|
185
|
+
assert_equal(["node1", 10], @q.min)
|
186
|
+
@q["node2"] = 5
|
187
|
+
assert_equal(["node2", 5], @q.min)
|
188
|
+
end
|
189
|
+
|
190
|
+
def test_decrease_priority
|
191
|
+
|
192
|
+
20.times do | i |
|
193
|
+
@q.push i, i / 20.0
|
194
|
+
end
|
195
|
+
|
196
|
+
assert_equal([0, 0], @q.delete_min)
|
197
|
+
|
198
|
+
@q[10] = -1
|
199
|
+
@q[11] = -0.5
|
200
|
+
|
201
|
+
[10, 11, (1..9).to_a, (12..19).to_a, nil].flatten.each do | shall |
|
202
|
+
key, priority = *@q.delete_min
|
203
|
+
assert_equal(shall, key)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_increase_priority
|
208
|
+
20.times do | i |
|
209
|
+
@q[i] = i
|
210
|
+
end
|
211
|
+
@q[10] = 5
|
212
|
+
assert_equal([0,0], @q.delete_min)
|
213
|
+
assert_equal(10, @q[10] = 10)
|
214
|
+
assert_equal(20, @q[11] = 20)
|
215
|
+
assert_equal([1,1], @q.delete_min)
|
216
|
+
end
|
217
|
+
|
218
|
+
def test_delete
|
219
|
+
@q[1] = 1
|
220
|
+
@q[2] = 2
|
221
|
+
@q[3] = 3
|
222
|
+
assert_equal(1, @q[1])
|
223
|
+
assert_equal([1,1], @q.min)
|
224
|
+
assert_equal([1,1], @q.delete(1))
|
225
|
+
assert_equal(nil, @q[1])
|
226
|
+
assert_equal([2,2], @q.min)
|
227
|
+
assert_equal(nil, @q.delete(1))
|
228
|
+
end
|
229
|
+
|
230
|
+
def test_example_1
|
231
|
+
assert_equal(0, @q["node1"] = 0)
|
232
|
+
assert_equal(1, @q["node2"] = 1)
|
233
|
+
assert_equal("node1", @q.min_key)
|
234
|
+
assert_equal(0, @q[@q.min_key])
|
235
|
+
assert_equal(0, @q.min_priority)
|
236
|
+
|
237
|
+
@q["node2"] = -1
|
238
|
+
assert_equal(["node2", -1], @q.delete_min)
|
239
|
+
assert_equal(nil, @q["node2"])
|
240
|
+
@q["node3"] = 1
|
241
|
+
|
242
|
+
assert_equal(["node3", 1], @q.delete("node3"))
|
243
|
+
assert_equal(nil, @q.delete("node2"))
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_dup
|
247
|
+
('a'..'z').each do | n |
|
248
|
+
@q[n] = n[0]
|
249
|
+
end
|
250
|
+
qq = @q.dup
|
251
|
+
until @q.empty?
|
252
|
+
assert_equal(@q.delete_min, qq.delete_min)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
|
256
|
+
def test_each
|
257
|
+
('a'..'z').each do | n |
|
258
|
+
@q[n] = n[0]
|
259
|
+
end
|
260
|
+
queue = ('a'..'z').inject([]) { | r, n | r << [n, n[0]] }
|
261
|
+
assert_equal(queue.sort, @q.to_a.sort)
|
262
|
+
end
|
263
|
+
|
264
|
+
extend self
|
265
|
+
end
|
266
|
+
|
267
|
+
class CPriorityQueueTest < Test::Unit::TestCase
|
268
|
+
include PriorityQueueTest
|
269
|
+
|
270
|
+
def setup
|
271
|
+
@q = CPriorityQueue.new
|
272
|
+
end
|
273
|
+
|
274
|
+
def test_to_dot
|
275
|
+
5.times do | i |
|
276
|
+
@q.push "N#{i}", i
|
277
|
+
end
|
278
|
+
@q.delete_min
|
279
|
+
assert_equal(
|
280
|
+
['digraph fibonacci_heap {',
|
281
|
+
' NODE [label="N1 (1)",shape=box];',
|
282
|
+
' NODE [label="N3 (3)",shape=box];',
|
283
|
+
' NODE [label="N4 (4)",shape=box];',
|
284
|
+
' NODE -> NODE;',
|
285
|
+
' NODE -> NODE;',
|
286
|
+
' NODE [label="N2 (2)",shape=box];',
|
287
|
+
' NODE -> NODE;',
|
288
|
+
'}',''].join("\n"), @q.to_dot.gsub(/NODE[0-9]*/, 'NODE'))
|
289
|
+
end
|
290
|
+
|
291
|
+
end
|
292
|
+
|
293
|
+
class PoorPriorityQueueTest < Test::Unit::TestCase
|
294
|
+
include PriorityQueueTest
|
295
|
+
|
296
|
+
def setup
|
297
|
+
@q = PoorPriorityQueue.new
|
298
|
+
end
|
299
|
+
|
300
|
+
end
|
301
|
+
|
302
|
+
class RubyPriorityQueueTest < Test::Unit::TestCase
|
303
|
+
include PriorityQueueTest
|
304
|
+
|
305
|
+
def setup
|
306
|
+
@q = RubyPriorityQueue.new
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_private_link_nodes
|
310
|
+
q = RubyPriorityQueue.new
|
311
|
+
q[0] = 0
|
312
|
+
q[1] = 1
|
313
|
+
tc = self
|
314
|
+
q.instance_eval do
|
315
|
+
n0 = @nodes[0]
|
316
|
+
n1 = @nodes[1]
|
317
|
+
n0.right = n0.left = n0
|
318
|
+
n1.right = n1.left = n1
|
319
|
+
tc.assert_equal(n0, link_nodes(n0, n1))
|
320
|
+
tc.assert_equal(n0.child, n1)
|
321
|
+
tc.assert_equal(n1.child, nil)
|
322
|
+
tc.assert_equal(n0.left, n0)
|
323
|
+
tc.assert_equal(n1.left, n1)
|
324
|
+
tc.assert_equal(n0.right, n0)
|
325
|
+
tc.assert_equal(n1.right, n1)
|
326
|
+
end
|
327
|
+
q = RubyPriorityQueue.new
|
328
|
+
q[0] = 0
|
329
|
+
q[1] = 1
|
330
|
+
q.instance_eval do
|
331
|
+
n0 = @nodes[0]
|
332
|
+
n1 = @nodes[1]
|
333
|
+
n0.right = n0.left = n0
|
334
|
+
n1.right = n1.left = n1
|
335
|
+
tc.assert_equal(n0, link_nodes(n1, n0))
|
336
|
+
tc.assert_equal(n0.child, n1)
|
337
|
+
tc.assert_equal(n1.child, nil)
|
338
|
+
tc.assert_equal(n0.left, n0)
|
339
|
+
tc.assert_equal(n1.left, n1)
|
340
|
+
tc.assert_equal(n0.right, n0)
|
341
|
+
tc.assert_equal(n1.right, n1)
|
342
|
+
end
|
343
|
+
end
|
344
|
+
|
345
|
+
|
346
|
+
def test_private_delete_first
|
347
|
+
q = RubyPriorityQueue.new
|
348
|
+
q[0] = 0
|
349
|
+
q[1] = 1
|
350
|
+
q[2] = 2
|
351
|
+
tc = self
|
352
|
+
q.instance_eval do
|
353
|
+
2.times do
|
354
|
+
r = @rootlist
|
355
|
+
tc.assert_equal(r, delete_first)
|
356
|
+
tc.assert_equal(r.right, r)
|
357
|
+
tc.assert_equal(r.left, r)
|
358
|
+
tc.assert_not_equal(r, @rootlist.left)
|
359
|
+
tc.assert_not_equal(r, @rootlist.right)
|
360
|
+
end
|
361
|
+
r = @rootlist
|
362
|
+
tc.assert_equal(r, delete_first)
|
363
|
+
tc.assert_equal(r.right, r)
|
364
|
+
tc.assert_equal(r.left, r)
|
365
|
+
tc.assert_equal(nil, @rootlist)
|
366
|
+
|
367
|
+
tc.assert_equal(nil, delete_first)
|
368
|
+
end
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
@@ -0,0 +1,360 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2007 Shawn Garbett (minor changes)
|
3
|
+
# Copyright (c) 2005 wsdng
|
4
|
+
#
|
5
|
+
# Redistribution and use in source and binary forms, with or without modification,
|
6
|
+
# are permitted provided that the following conditions are met:
|
7
|
+
#
|
8
|
+
# * Redistributions of source code must retain the above copyright notice(s),
|
9
|
+
# this list of conditions and the following disclaimer.
|
10
|
+
# * Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
# this list of conditions and the following disclaimer in the documentation
|
12
|
+
# and/or other materials provided with the distribution.
|
13
|
+
# * Neither the name of the Shawn Garbett nor the names of its contributors
|
14
|
+
# may be used to endorse or promote products derived from this software
|
15
|
+
# without specific prior written permission.
|
16
|
+
#
|
17
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
18
|
+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19
|
+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
20
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
21
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
22
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
23
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
24
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
25
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
26
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27
|
+
#++
|
28
|
+
|
29
|
+
# rdot.rb
|
30
|
+
#
|
31
|
+
# $Id: rdot.rb,v 1.4 2005/03/26 15:06:36 wsdng Exp $
|
32
|
+
#
|
33
|
+
# This is a modified version of dot.rb from Dave Thomas's rdoc project. I [Horst Duchene]
|
34
|
+
# renamed it to rdot.rb to avoid collision with an installed rdoc/dot.
|
35
|
+
#
|
36
|
+
# It also supports undirected edges.
|
37
|
+
|
38
|
+
class Hash
|
39
|
+
def stringify_keys
|
40
|
+
inject({}) {|options, (key, value)| options[key.to_s] = value; options}
|
41
|
+
end
|
42
|
+
end unless Hash.respond_to? :stringify_keys
|
43
|
+
|
44
|
+
module DOT
|
45
|
+
|
46
|
+
# These glogal vars are used to make nice graph source.
|
47
|
+
|
48
|
+
$tab = ' '
|
49
|
+
$tab2 = $tab * 2
|
50
|
+
|
51
|
+
# if we don't like 4 spaces, we can change it any time
|
52
|
+
|
53
|
+
def change_tab (t)
|
54
|
+
$tab = t
|
55
|
+
$tab2 = t * 2
|
56
|
+
end
|
57
|
+
|
58
|
+
# options for node declaration
|
59
|
+
|
60
|
+
NODE_OPTS = [
|
61
|
+
# attributes due to
|
62
|
+
# http://www.graphviz.org/Documentation/dotguide.pdf
|
63
|
+
# March, 26, 2005
|
64
|
+
'bottomlabel', # auxiliary label for nodes of shape M*
|
65
|
+
'color', # default: black; node shape color
|
66
|
+
'comment', # any string (format-dependent)
|
67
|
+
'distortion', # default: 0.0; node distortion for shape=polygon
|
68
|
+
'fillcolor', # default: lightgrey/black; node fill color
|
69
|
+
'fixedsize', # default: false; label text has no affect on node size
|
70
|
+
'fontcolor', # default: black; type face color
|
71
|
+
'fontname', # default: Times-Roman; font family
|
72
|
+
'fontsize', #default: 14; point size of label
|
73
|
+
'group', # name of node�s group
|
74
|
+
'height', # default: .5; height in inches
|
75
|
+
'label', # default: node name; any string
|
76
|
+
'layer', # default: overlay range; all, id or id:id
|
77
|
+
'orientation', # dafault: 0.0; node rotation angle
|
78
|
+
'peripheries', # shape-dependent number of node boundaries
|
79
|
+
'regular', # default: false; force polygon to be regular
|
80
|
+
'shape', # default: ellipse; node shape; see Section 2.1 and Appendix E
|
81
|
+
'shapefile', # external EPSF or SVG custom shape file
|
82
|
+
'sides', # default: 4; number of sides for shape=polygon
|
83
|
+
'skew' , # default: 0.0; skewing of node for shape=polygon
|
84
|
+
'style', # graphics options, e.g. bold, dotted, filled; cf. Section 2.3
|
85
|
+
'toplabel', # auxiliary label for nodes of shape M*
|
86
|
+
'URL', # URL associated with node (format-dependent)
|
87
|
+
'width', # default: .75; width in inches
|
88
|
+
'z', #default: 0.0; z coordinate for VRML output
|
89
|
+
|
90
|
+
# maintained for backward compatibility or rdot internal
|
91
|
+
'bgcolor',
|
92
|
+
'rank'
|
93
|
+
]
|
94
|
+
|
95
|
+
# options for edge declaration
|
96
|
+
|
97
|
+
EDGE_OPTS = [
|
98
|
+
'arrowhead', # default: normal; style of arrowhead at head end
|
99
|
+
'arrowsize', # default: 1.0; scaling factor for arrowheads
|
100
|
+
'arrowtail', # default: normal; style of arrowhead at tail end
|
101
|
+
'color', # default: black; edge stroke color
|
102
|
+
'comment', # any string (format-dependent)
|
103
|
+
'constraint', # default: true use edge to affect node ranking
|
104
|
+
'decorate', # if set, draws a line connecting labels with their edges
|
105
|
+
'dir', # default: forward; forward, back, both, or none
|
106
|
+
'fontcolor', # default: black type face color
|
107
|
+
'fontname', # default: Times-Roman; font family
|
108
|
+
'fontsize', # default: 14; point size of label
|
109
|
+
'headlabel', # label placed near head of edge
|
110
|
+
'headport', # n,ne,e,se,s,sw,w,nw
|
111
|
+
'headURL', # URL attached to head label if output format is ismap
|
112
|
+
'label', # edge label
|
113
|
+
'labelangle', # default: -25.0; angle in degrees which head or tail label is rotated off edge
|
114
|
+
'labeldistance', # default: 1.0; scaling factor for distance of head or tail label from node
|
115
|
+
'labelfloat', # default: false; lessen constraints on edge label placement
|
116
|
+
'labelfontcolor', # default: black; type face color for head and tail labels
|
117
|
+
'labelfontname', # default: Times-Roman; font family for head and tail labels
|
118
|
+
'labelfontsize', # default: 14 point size for head and tail labels
|
119
|
+
'layer', # default: overlay range; all, id or id:id
|
120
|
+
'lhead', # name of cluster to use as head of edge
|
121
|
+
'ltail', # name of cluster to use as tail of edge
|
122
|
+
'minlen', # default: 1 minimum rank distance between head and tail
|
123
|
+
'samehead', # tag for head node; edge heads with the same tag are merged onto the same port
|
124
|
+
'sametail', # tag for tail node; edge tails with the same tag are merged onto the same port
|
125
|
+
'style', # graphics options, e.g. bold, dotted, filled; cf. Section 2.3
|
126
|
+
'taillabel', # label placed near tail of edge
|
127
|
+
'tailport', # n,ne,e,se,s,sw,w,nw
|
128
|
+
'tailURL', # URL attached to tail label if output format is ismap
|
129
|
+
'weight', # default: 1; integer cost of stretching an edge
|
130
|
+
|
131
|
+
# maintained for backward compatibility or rdot internal
|
132
|
+
'id'
|
133
|
+
]
|
134
|
+
|
135
|
+
# options for graph declaration
|
136
|
+
|
137
|
+
GRAPH_OPTS = [
|
138
|
+
'bgcolor',
|
139
|
+
'center', 'clusterrank', 'color', 'concentrate',
|
140
|
+
'fontcolor', 'fontname', 'fontsize',
|
141
|
+
'label', 'layerseq',
|
142
|
+
'margin', 'mclimit',
|
143
|
+
'nodesep', 'nslimit',
|
144
|
+
'ordering', 'orientation',
|
145
|
+
'page',
|
146
|
+
'rank', 'rankdir', 'ranksep', 'ratio',
|
147
|
+
'size'
|
148
|
+
]
|
149
|
+
|
150
|
+
# a root class for any element in dot notation
|
151
|
+
|
152
|
+
class DOTSimpleElement
|
153
|
+
|
154
|
+
attr_accessor :name
|
155
|
+
|
156
|
+
def initialize (params = {})
|
157
|
+
@label = params['name'] ? params['name'] : ''
|
158
|
+
end
|
159
|
+
|
160
|
+
def to_s
|
161
|
+
@name
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# an element that has options ( node, edge, or graph )
|
166
|
+
|
167
|
+
class DOTElement < DOTSimpleElement
|
168
|
+
|
169
|
+
# attr_reader :parent
|
170
|
+
attr_accessor :name, :options
|
171
|
+
|
172
|
+
def initialize (params = {}, option_list = [])
|
173
|
+
super(params)
|
174
|
+
@name = params['name'] ? params['name'] : nil
|
175
|
+
@parent = params['parent'] ? params['parent'] : nil
|
176
|
+
@options = {}
|
177
|
+
p = params.stringify_keys
|
178
|
+
option_list.each {|i| @options[i] = p[i] if p[i] }
|
179
|
+
@options['label'] ||= @name if @name != 'node'
|
180
|
+
end
|
181
|
+
|
182
|
+
def each_option
|
183
|
+
@options.each{ |i| yield i }
|
184
|
+
end
|
185
|
+
|
186
|
+
def each_option_pair
|
187
|
+
@options.each_pair{ |key, val| yield key, val }
|
188
|
+
end
|
189
|
+
|
190
|
+
#def parent=( thing )
|
191
|
+
# @parent.delete( self ) if defined?( @parent ) and @parent
|
192
|
+
# @parent = thing
|
193
|
+
#end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
|
198
|
+
# This is used when we build nodes that have shape=record
|
199
|
+
# ports don't have options :)
|
200
|
+
|
201
|
+
class DOTPort < DOTSimpleElement
|
202
|
+
|
203
|
+
attr_accessor :label
|
204
|
+
|
205
|
+
def initialize (params = {})
|
206
|
+
super(params)
|
207
|
+
@name = params['label'] ? params['label'] : ''
|
208
|
+
end
|
209
|
+
|
210
|
+
def to_s
|
211
|
+
( @name && @name != "" ? "<#{@name}>" : "" ) + "#{@label}"
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
# node element
|
216
|
+
|
217
|
+
class DOTNode < DOTElement
|
218
|
+
|
219
|
+
@ports
|
220
|
+
|
221
|
+
def initialize (params = {}, option_list = NODE_OPTS)
|
222
|
+
super(params, option_list)
|
223
|
+
@ports = params['ports'] ? params['ports'] : []
|
224
|
+
end
|
225
|
+
|
226
|
+
def each_port
|
227
|
+
@ports.each { |i| yield i }
|
228
|
+
end
|
229
|
+
|
230
|
+
def << (thing)
|
231
|
+
@ports << thing
|
232
|
+
end
|
233
|
+
|
234
|
+
def push (thing)
|
235
|
+
@ports.push(thing)
|
236
|
+
end
|
237
|
+
|
238
|
+
def pop
|
239
|
+
@ports.pop
|
240
|
+
end
|
241
|
+
|
242
|
+
def to_s (t = '')
|
243
|
+
|
244
|
+
# This code is totally incomprehensible; it needs to be replaced!
|
245
|
+
|
246
|
+
label = @options['shape'] != 'record' && @ports.length == 0 ?
|
247
|
+
@options['label'] ?
|
248
|
+
t + $tab + "label = \"#{@options['label']}\"\n" :
|
249
|
+
'' :
|
250
|
+
t + $tab + 'label = "' + " \\\n" +
|
251
|
+
t + $tab2 + "#{@options['label']}| \\\n" +
|
252
|
+
@ports.collect{ |i|
|
253
|
+
t + $tab2 + i.to_s
|
254
|
+
}.join( "| \\\n" ) + " \\\n" +
|
255
|
+
t + $tab + '"' + "\n"
|
256
|
+
|
257
|
+
t + "#{@name} [\n" +
|
258
|
+
@options.to_a.collect{ |i|
|
259
|
+
i[1] && i[0] != 'label' ?
|
260
|
+
t + $tab + "#{i[0]} = #{i[1]}" : nil
|
261
|
+
}.compact.join( ",\n" ) + ( label != '' ? ",\n" : "\n" ) +
|
262
|
+
label +
|
263
|
+
t + "]\n"
|
264
|
+
end
|
265
|
+
|
266
|
+
end # class DOTNode
|
267
|
+
|
268
|
+
# A subgraph element is the same to graph, but has another header in dot
|
269
|
+
# notation.
|
270
|
+
|
271
|
+
class DOTSubgraph < DOTElement
|
272
|
+
|
273
|
+
@nodes
|
274
|
+
@dot_string
|
275
|
+
|
276
|
+
def initialize (params = {}, option_list = GRAPH_OPTS)
|
277
|
+
super(params, option_list)
|
278
|
+
@nodes = params['nodes'] ? params['nodes'] : []
|
279
|
+
@dot_string = 'subgraph'
|
280
|
+
end
|
281
|
+
|
282
|
+
def each_node
|
283
|
+
@nodes.each{ |i| yield i }
|
284
|
+
end
|
285
|
+
|
286
|
+
def << (thing)
|
287
|
+
@nodes << thing
|
288
|
+
end
|
289
|
+
|
290
|
+
def push (thing)
|
291
|
+
@nodes.push( thing )
|
292
|
+
end
|
293
|
+
|
294
|
+
def pop
|
295
|
+
@nodes.pop
|
296
|
+
end
|
297
|
+
|
298
|
+
def to_s (t = '')
|
299
|
+
hdr = t + "#{@dot_string} #{@name} {\n"
|
300
|
+
|
301
|
+
options = @options.to_a.collect{ |name, val|
|
302
|
+
val && name != 'label' ?
|
303
|
+
t + $tab + "#{name} = #{val}" :
|
304
|
+
name ? t + $tab + "#{name} = \"#{val}\"" : nil
|
305
|
+
}.compact.join( "\n" ) + "\n"
|
306
|
+
|
307
|
+
nodes = @nodes.collect{ |i|
|
308
|
+
i.to_s( t + $tab )
|
309
|
+
}.join( "\n" ) + "\n"
|
310
|
+
hdr + options + nodes + t + "}\n"
|
311
|
+
end
|
312
|
+
|
313
|
+
end # class DOTSubgraph
|
314
|
+
|
315
|
+
# This is a graph.
|
316
|
+
|
317
|
+
class DOTDigraph < DOTSubgraph
|
318
|
+
|
319
|
+
def initialize (params = {}, option_list = GRAPH_OPTS)
|
320
|
+
super(params, option_list)
|
321
|
+
@dot_string = 'digraph'
|
322
|
+
end
|
323
|
+
|
324
|
+
end # class DOTDigraph
|
325
|
+
|
326
|
+
# This is an edge.
|
327
|
+
|
328
|
+
class DOTArc < DOTElement
|
329
|
+
|
330
|
+
attr_accessor :from, :to
|
331
|
+
|
332
|
+
def initialize (params = {}, option_list = EDGE_OPTS)
|
333
|
+
super(params, option_list)
|
334
|
+
@from = params['from'] ? params['from'] : nil
|
335
|
+
@to = params['to'] ? params['to'] : nil
|
336
|
+
end
|
337
|
+
|
338
|
+
def edge_link
|
339
|
+
'--'
|
340
|
+
end
|
341
|
+
|
342
|
+
def to_s (t = '')
|
343
|
+
t + "#{@from} #{edge_link} #{to} [\n" +
|
344
|
+
@options.to_a.collect{ |i|
|
345
|
+
i[1] && i[0] != 'label' ?
|
346
|
+
t + $tab + "#{i[0]} = #{i[1]}" :
|
347
|
+
i[1] ? t + $tab + "#{i[0]} = \"#{i[1]}\"" : nil
|
348
|
+
}.compact.join( "\n" ) + "\n" + t + "]\n"
|
349
|
+
end
|
350
|
+
|
351
|
+
end # class DOTArc
|
352
|
+
|
353
|
+
class DOTDirectedArc < DOTArc
|
354
|
+
|
355
|
+
def edge_link
|
356
|
+
'->'
|
357
|
+
end
|
358
|
+
|
359
|
+
end # class DOTDirectedArc
|
360
|
+
end # module DOT
|