iconofthestoneage-doodl 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/lib/app/selfrunning.rb +185 -0
  2. data/lib/app/simple_app.rb +49 -0
  3. data/lib/app/simple_controller.rb +584 -0
  4. data/lib/app/simple_model.rb +292 -0
  5. data/lib/app/simple_view.rb +148 -0
  6. data/lib/breadth_first_search.rb +69 -0
  7. data/lib/connected_components.rb +29 -0
  8. data/lib/depth_first_search.rb +73 -0
  9. data/lib/edge.rb +57 -0
  10. data/lib/graph.rb +365 -0
  11. data/lib/graph_canvas.rb +187 -0
  12. data/lib/graph_generator.rb +121 -0
  13. data/lib/jruby/renderer.rb +413 -0
  14. data/lib/layout/collapse_layout.rb +23 -0
  15. data/lib/layout/fr_layout.rb +105 -0
  16. data/lib/layout/isom_layout.rb +77 -0
  17. data/lib/layout/kk_layout.rb +203 -0
  18. data/lib/layout/layout.rb +240 -0
  19. data/lib/layout/morph_layout.rb +65 -0
  20. data/lib/node.rb +57 -0
  21. data/lib/shortest_path/all_pair.rb +35 -0
  22. data/lib/shortest_path/bellman_ford.rb +60 -0
  23. data/lib/shortest_path/dijkstra.rb +74 -0
  24. data/lib/shortest_path/floyd_warshall.rb +68 -0
  25. data/lib/shortest_path/johnson_all_pair.rb +64 -0
  26. data/lib/shortest_path/single_source.rb +32 -0
  27. data/spec/breadth_first_search_spec.rb +145 -0
  28. data/spec/connected_components_spec.rb +50 -0
  29. data/spec/depth_first_search_spec.rb +89 -0
  30. data/spec/edge_spec.rb +58 -0
  31. data/spec/graph_generator_spec.rb +277 -0
  32. data/spec/graph_spec.rb +269 -0
  33. data/spec/jruby/renderer_spec.rb +214 -0
  34. data/spec/layout/layout_spec.rb +146 -0
  35. data/spec/node_spec.rb +179 -0
  36. data/spec/rspec_helper.rb +9 -0
  37. data/spec/rspec_suite.rb +12 -0
  38. data/spec/shortest_path/bellman_ford_spec.rb +101 -0
  39. data/spec/shortest_path/dijkstra_spec.rb +133 -0
  40. data/spec/shortest_path/floyd_warshall_spec.rb +84 -0
  41. data/spec/shortest_path/johnson_all_pair_spec.rb +90 -0
  42. metadata +43 -2
@@ -0,0 +1,269 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rspec_helper'
4
+ require 'enumerator'
5
+
6
+ require "graph"
7
+ require "edge"
8
+
9
+ include Doodl
10
+
11
+ describe "All Graphs", :shared => true do
12
+
13
+ describe "Initialization" do
14
+ it "should be initinalized with empty nodes" do
15
+ @graph.nodes.should == []
16
+ end
17
+ end
18
+
19
+ describe "Convenience" do
20
+ it "should return whether it contains a node when sent contains_node?(node)" do
21
+ node1 = @graph.add_node
22
+ node2 = :node2
23
+ @graph.contains_node?(node1).should == true
24
+ @graph.contains_node?(node2).should == false
25
+ end
26
+
27
+ it "should return whether it contains an edge " do
28
+ a = @graph.add_node
29
+ b = @graph.add_node
30
+ edge = @graph.add_edge(a, b)
31
+ @graph.includes_edge?(edge).should == true
32
+ @graph.del_edge(edge)
33
+ @graph.includes_edge?(edge).should == false
34
+ end
35
+
36
+ it "should return the edge when sent get_edge(source, target)" do
37
+ a = @graph.add_node
38
+ b = @graph.add_node
39
+ c = @graph.add_node
40
+ edge = @graph.add_edge(a, b)
41
+ @graph.get_edge(a, b).should == edge
42
+ @graph.get_edge(a, c).should == nil
43
+ end
44
+ end
45
+
46
+ describe "MutableGraph" do
47
+ it "should add and return a new node when sent #add_node" do
48
+ a = @graph.add_node
49
+ a.is_a?(Node)
50
+ @graph.nodes.should == [a]
51
+ end
52
+
53
+ it "should delete a node when sent #del_node(node)" do
54
+ a = @graph.add_node
55
+ @graph.del_node(a)
56
+ @graph.nodes.should == []
57
+ end
58
+
59
+ it "should add and return an edge when sent #add_edge(source, target)" do
60
+ a, b = @graph.add_node, @graph.add_node
61
+ edge = @graph.add_edge(a, b)
62
+ edge.is_a?(Edge)
63
+ @graph.num_edges.should == 1
64
+ end
65
+
66
+ it "should delete an edge when sent #del_edge(edge)" do
67
+ a, b = @graph.add_node, @graph.add_node
68
+ edge = @graph.add_edge(a, b)
69
+ @graph.del_edge(edge)
70
+ @graph.num_edges.should == 0
71
+ end
72
+
73
+ end
74
+
75
+ describe "NodeListGraph" do
76
+ it "should return the total number of nodes the graph contains when sent #num_nodes" do
77
+ @graph.should respond_to(:num_nodes)
78
+ @graph.num_nodes.should == 0
79
+ @graph.add_node
80
+ @graph.num_nodes.should == 1
81
+ @graph.add_node
82
+ @graph.num_nodes.should == 2
83
+ end
84
+
85
+ it "should return a Iterator that iterates the nodes when sent #each_node" do
86
+ @graph.should respond_to(:each_node)
87
+ n = @graph.add_node
88
+ m = @graph.add_node
89
+ e = Enumerable::Enumerator.new(@graph, :each_node)
90
+ e.to_a.should == [n, m]
91
+ end
92
+
93
+ it "should return a Iterator that iterates the nodes with an index when sent #each_node_with_index" do
94
+ @graph.should respond_to(:each_node_with_index)
95
+ n = @graph.add_node
96
+ m = @graph.add_node
97
+ e = Enumerable::Enumerator.new(@graph, :each_node_with_index)
98
+ e.to_a.should == [[n, 0], [m, 1]]
99
+ end
100
+
101
+ end
102
+
103
+ describe "EdgeListGraph" do
104
+ it "should iterate all edges when sent #each_edge" do
105
+ @graph.should respond_to(:each_edge)
106
+ Enumerable::Enumerator.new(@graph, :each_edge).to_a.should == []
107
+ n = @graph.add_node
108
+ m = @graph.add_node
109
+ o = @graph.add_node
110
+ e = @graph.add_edge(n, m)
111
+ Enumerable::Enumerator.new(@graph, :each_edge).to_a.should == [e]
112
+ f = @graph.add_edge(m, o)
113
+ Enumerable::Enumerator.new(@graph, :each_edge).to_a.should == [e, f]
114
+ end
115
+
116
+ it "should reveal the source of an edge" do
117
+ @graph.should respond_to(:source)
118
+ n = @graph.add_node
119
+ m = @graph.add_node
120
+ e = @graph.add_edge(n, m)
121
+ @graph.source(e).should == n
122
+ end
123
+
124
+ it "should reveal the target of an edge" do
125
+ @graph.should respond_to(:target)
126
+ n = @graph.add_node
127
+ m = @graph.add_node
128
+ e = @graph.add_edge(n, m)
129
+ @graph.target(e).should == m
130
+ end
131
+
132
+ end
133
+
134
+ describe "IncidentGraph" do
135
+ it "should return the outdegree of a node when sent #out_degree" do
136
+ a = @graph.add_node
137
+ b = @graph.add_node
138
+ @graph.out_degree(a).should == 0
139
+ @graph.add_edge(a, b)
140
+ @graph.out_degree(a).should == 1
141
+ end
142
+
143
+ it "should iterate all outgoing edges of a graph" do
144
+ a = @graph.add_node
145
+ b = @graph.add_node
146
+ Enumerable::Enumerator.new(@graph, :each_adjacent_node, a).to_a.should == []
147
+ e = @graph.add_edge(a, b)
148
+ Enumerable::Enumerator.new(@graph, :each_adjacent_edge, a).to_a.should == [e]
149
+ end
150
+ end
151
+
152
+ describe "AdjacentGraph" do
153
+ it "should iterate all adjacent nodes" do
154
+ a = @graph.add_node
155
+ b = @graph.add_node
156
+ Enumerable::Enumerator.new(@graph, :each_adjacent_node, a).to_a.should == []
157
+ e = @graph.add_edge(a, b)
158
+ Enumerable::Enumerator.new(@graph, :each_adjacent_node, a).to_a.should == [b]
159
+ end
160
+ end
161
+
162
+ describe "Filtering Iterator, an iterator that filters with an lambda argument" do
163
+
164
+ before(:each) do
165
+ @a = @graph.add_node
166
+ @b = @graph.add_node
167
+ @c = @graph.add_node
168
+ end
169
+
170
+ it "should return every node if condition is lambda { true }" do
171
+ c = lambda { true }
172
+ result = []
173
+ @graph.each_node_passing(c) { |node| result << node }
174
+ result.should == [@a, @b, @c]
175
+ end
176
+
177
+ it "should return no node if condition is lambda { false }" do
178
+ c = lambda { false }
179
+ result = []
180
+ @graph.each_node_passing(c) { |node| result << node }
181
+ result.should == []
182
+ end
183
+
184
+ it "should return @a if condition tests for existence of @a" do
185
+ c = lambda { |node| node == @a }
186
+ result = []
187
+ @graph.each_node_passing(c) { |node| result << node }
188
+ result.should == [@a]
189
+ end
190
+
191
+ end
192
+
193
+ describe "Adding edges to the graph via #add_edge(source, target)" do
194
+
195
+ it "should raise an ArgumentError if either source or target or both are not nodes of the graph" do
196
+ other_graph = DirectedGraph.new
197
+ other_source = other_graph.add_node
198
+ other_target = other_graph.add_node
199
+ source = @graph.add_node
200
+ target = @graph.add_node
201
+ lambda { @graph.add_edge(other_source, other_target) }.should raise_error(ArgumentError)
202
+ lambda { @graph.add_edge(other_source, target) }.should raise_error(ArgumentError)
203
+ lambda { @graph.add_edge(source, other_target) }.should raise_error(ArgumentError)
204
+ lambda { @graph.add_edge(source, target) }.should_not raise_error(ArgumentError)
205
+ end
206
+
207
+ it "should raise an ArgumentError if there is already an edge (source->target) defined" do
208
+ source = @graph.add_node
209
+ target = @graph.add_node
210
+ @graph.add_edge(source, target)
211
+ lambda { @graph.add_edge(source, target) }.should raise_error(ArgumentError)
212
+ end
213
+
214
+ end
215
+
216
+ end
217
+
218
+ describe DirectedGraph do
219
+
220
+ before(:each) do
221
+ @graph = DirectedGraph.new
222
+ end
223
+
224
+ it_should_behave_like "All Graphs"
225
+
226
+ it "should be directed" do
227
+ @graph.directed?.should == true
228
+ end
229
+
230
+ it "should NOT raise an ArgumentError if there is already a mirroring edge (target->source) defined" do
231
+ source = @graph.add_node
232
+ target = @graph.add_node
233
+ @graph.add_edge(target, source)
234
+ lambda { @graph.add_edge(source, target) }.should_not raise_error(ArgumentError)
235
+ end
236
+
237
+ end
238
+
239
+ describe UndirectedGraph do
240
+
241
+ before(:each) do
242
+ @graph = UndirectedGraph.new
243
+ end
244
+
245
+ it_should_behave_like "All Graphs"
246
+
247
+ it "should be undirected" do
248
+ @graph.directed?.should == false
249
+ end
250
+
251
+ it "should hava undirected edges" do
252
+ a = @graph.add_node
253
+ b = @graph.add_node
254
+ e = @graph.add_edge(a, b)
255
+ e.directed?.should == false
256
+ end
257
+
258
+ it "should return adjacent_edges and nodes" do
259
+ a = @graph.add_node
260
+ b = @graph.add_node
261
+ c = @graph.add_node
262
+ e = @graph.add_edge(a, b)
263
+ Enumerable::Enumerator.new(@graph, :each_adjacent_edge, a).to_a.should == [e]
264
+ Enumerable::Enumerator.new(@graph, :each_adjacent_edge, b).to_a.should == [e]
265
+ Enumerable::Enumerator.new(@graph, :each_adjacent_node, a).to_a.should == [b]
266
+ Enumerable::Enumerator.new(@graph, :each_adjacent_node, b).to_a.should == [a]
267
+ end
268
+
269
+ end
@@ -0,0 +1,214 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rspec_helper'
4
+
5
+ require 'renderer'
6
+
7
+ include Doodl
8
+
9
+ describe PluggableRenderer do
10
+ before(:each) do
11
+ @graph = mock("graph")
12
+ @renderer = PluggableRenderer.new
13
+ end
14
+
15
+ describe "Node Functions" do
16
+
17
+ it "should accept node predicates" do
18
+ lambda { @renderer.node_predicate_function = mock("predicate") }.should_not raise_error(Exception)
19
+ end
20
+
21
+ it "should accept node_predicate with a block defining the predicate" do
22
+ @renderer.node_predicate_function { true }
23
+ @renderer.node_predicate(mock("node")).should == true
24
+ end
25
+
26
+ it "should call the node_predicate, when sent #node_predicate" do
27
+ node = mock("node")
28
+ predicate = mock("predicate")
29
+ predicate.should_receive(:call).with(nil, nil, node)
30
+ @renderer.node_predicate_function = predicate
31
+ @renderer.node_predicate(node)
32
+ end
33
+
34
+ it "should return true/false when node_predicate.call(graph, node) returns true/false" do
35
+ n = mock("painted node")
36
+ m = mock("not painted node")
37
+ predicate = mock("predicate")
38
+ predicate.should_receive(:call).with(nil, nil, n).once.and_return(true)
39
+ @renderer.node_predicate_function = predicate
40
+ @renderer.node_predicate(n).should == true
41
+ predicate.should_receive(:call).with(nil, nil, m).once.and_return(false)
42
+ @renderer.node_predicate(m).should == false
43
+ end
44
+
45
+ it "should raise an ArgumentError when #node_shape_function is called without a block" do
46
+ lambda { @renderer.node_shape_function }.should raise_error(ArgumentError)
47
+ lambda { @renderer.node_shape_function(:dodo) }.should raise_error(ArgumentError)
48
+ end
49
+
50
+ it "should accept a node_shape_fuction when sent #node_shape_fuction=" do
51
+ lambda { @renderer.node_shape_function = mock("shape_fuction") }.should_not raise_error(Exception)
52
+ end
53
+
54
+ it "should retun the shape defined by node_shape_function when sent #node_shape(node)" do
55
+ n = mock("node a")
56
+ m = mock("node b")
57
+ shape = mock("shape")
58
+ @renderer.node_shape_function = lambda { shape }
59
+ @renderer.node_shape(n).should == shape
60
+ @renderer.node_shape_function = lambda { |graph, layout, node| node == n ? :n : :m }
61
+ @renderer.node_shape(n).should == :n
62
+ @renderer.node_shape(m).should == :m
63
+ @renderer.node_shape_function { |graph, layout, node| node == n ? :n : :m }
64
+ @renderer.node_shape(n).should == :n
65
+ @renderer.node_shape(m).should == :m
66
+ end
67
+
68
+ it "should raise an ArgumentError when #node_stroke_color_function is called without a block" do
69
+ lambda { @renderer.node_stroke_color_function }.should raise_error(ArgumentError)
70
+ lambda { @renderer.node_stroke_color_function(:dodo) }.should raise_error(ArgumentError)
71
+ end
72
+
73
+ it "should accept a node_shape_fuction when sent #node_shape_fuction=" do
74
+ lambda { @renderer.node_stroke_color_function = mock("shape_fuction") }.should_not raise_error(Exception)
75
+ end
76
+
77
+ it "should retun the shape defined by node_stroke_color_function when sent #node_shape(node)" do
78
+ n = mock("node a")
79
+ m = mock("node b")
80
+ color = mock("color")
81
+ @renderer.node_stroke_color_function = lambda { color }
82
+ @renderer.node_stroke_color(n).should == color
83
+ @renderer.node_stroke_color_function = lambda { |graph, layout, node| node == n ? :n : :m }
84
+ @renderer.node_stroke_color(n).should == :n
85
+ @renderer.node_stroke_color(m).should == :m
86
+ @renderer.node_stroke_color_function { |graph, layout, node| node == n ? :n : :m }
87
+ @renderer.node_stroke_color(n).should == :n
88
+ @renderer.node_stroke_color(m).should == :m
89
+ end
90
+
91
+ it "should retun the shape defined by node_shape_function when sent #node_shape(node)" do
92
+ n = mock("node a")
93
+ m = mock("node b")
94
+ shape = mock("shape")
95
+ @renderer.node_shape_function = lambda { shape }
96
+ @renderer.node_shape(n).should == shape
97
+ @renderer.node_shape_function = lambda { |graph, layout, node| node == n ? :n : :m }
98
+ @renderer.node_shape(n).should == :n
99
+ @renderer.node_shape(m).should == :m
100
+ @renderer.node_shape_function { |graph, layout, node| node == n ? :n : :m }
101
+ @renderer.node_shape(n).should == :n
102
+ @renderer.node_shape(m).should == :m
103
+ end
104
+
105
+ it "should raise an ArgumentError when #node_stroke_color_function is called without a block" do
106
+ lambda { @renderer.node_stroke_color_function }.should raise_error(ArgumentError)
107
+ lambda { @renderer.node_stroke_color_function(:dodo) }.should raise_error(ArgumentError)
108
+ end
109
+
110
+ it "should accept a node_shape_fuction when sent #node_shape_fuction=" do
111
+ lambda { @renderer.node_stroke_color_function = mock("shape_fuction") }.should_not raise_error(Exception)
112
+ end
113
+
114
+ it "should retun the shape defined by node_stroke_color_function when sent #node_shape(node)" do
115
+ n = mock("node a")
116
+ m = mock("node b")
117
+ color = mock("color")
118
+ @renderer.node_stroke_color_function = lambda { color }
119
+ @renderer.node_stroke_color(n).should == color
120
+ @renderer.node_stroke_color_function = lambda { |graph, layout, node| node == n ? :n : :m }
121
+ @renderer.node_stroke_color(n).should == :n
122
+ @renderer.node_stroke_color(m).should == :m
123
+ @renderer.node_stroke_color_function { |graph, layout, node| node == n ? :n : :m }
124
+ @renderer.node_stroke_color(n).should == :n
125
+ @renderer.node_stroke_color(m).should == :m
126
+ end
127
+ end
128
+
129
+ describe "Edge Functions" do
130
+ it "should raise an ArgumentError when #edge_shape_function is called without a block" do
131
+ lambda { @renderer.edge_shape_function }.should raise_error(ArgumentError)
132
+ lambda { @renderer.edge_shape_function(:dodo) }.should raise_error(ArgumentError)
133
+ end
134
+
135
+ it "should accept a edge_shape_fuction when sent #edge_shape_fuction=" do
136
+ lambda { @renderer.edge_shape_function = mock("shape_fuction") }.should_not raise_error(Exception)
137
+ end
138
+
139
+ it "should retun the shape defined by edge_shape_function when sent #edge_shape(edge)" do
140
+ n = mock("edge a")
141
+ m = mock("edge b")
142
+ shape = mock("shape")
143
+ @renderer.edge_shape_function = lambda { shape }
144
+ @renderer.edge_shape(n).should == shape
145
+ @renderer.edge_shape_function = lambda { |graph, layout, edge| edge == n ? :n : :m }
146
+ @renderer.edge_shape(n).should == :n
147
+ @renderer.edge_shape(m).should == :m
148
+ @renderer.edge_shape_function { |graph, layout, edge| edge == n ? :n : :m }
149
+ @renderer.edge_shape(n).should == :n
150
+ @renderer.edge_shape(m).should == :m
151
+ end
152
+
153
+ it "should raise an ArgumentError when #edge_stroke_color_function is called without a block" do
154
+ lambda { @renderer.edge_stroke_color_function }.should raise_error(ArgumentError)
155
+ lambda { @renderer.edge_stroke_color_function(:dodo) }.should raise_error(ArgumentError)
156
+ end
157
+
158
+ it "should accept a edge_shape_fuction when sent #edge_shape_fuction=" do
159
+ lambda { @renderer.edge_stroke_color_function = mock("shape_fuction") }.should_not raise_error(Exception)
160
+ end
161
+
162
+ it "should retun the shape defined by edge_stroke_color_function when sent #edge_shape(edge)" do
163
+ n = mock("edge a")
164
+ m = mock("edge b")
165
+ color = mock("color")
166
+ @renderer.edge_stroke_color_function = lambda { color }
167
+ @renderer.edge_stroke_color(n).should == color
168
+ @renderer.edge_stroke_color_function = lambda { |graph, layout, edge| edge == n ? :n : :m }
169
+ @renderer.edge_stroke_color(n).should == :n
170
+ @renderer.edge_stroke_color(m).should == :m
171
+ @renderer.edge_stroke_color_function { |graph, layout, edge| edge == n ? :n : :m }
172
+ @renderer.edge_stroke_color(n).should == :n
173
+ @renderer.edge_stroke_color(m).should == :m
174
+ end
175
+
176
+ it "should retun the shape defined by edge_shape_function when sent #edge_shape(edge)" do
177
+ n = mock("edge a")
178
+ m = mock("edge b")
179
+ shape = mock("shape")
180
+ @renderer.edge_shape_function = lambda { shape }
181
+ @renderer.edge_shape(n).should == shape
182
+ @renderer.edge_shape_function = lambda { |graph, layout, edge| edge == n ? :n : :m }
183
+ @renderer.edge_shape(n).should == :n
184
+ @renderer.edge_shape(m).should == :m
185
+ @renderer.edge_shape_function { |graph, layout, edge| edge == n ? :n : :m }
186
+ @renderer.edge_shape(n).should == :n
187
+ @renderer.edge_shape(m).should == :m
188
+ end
189
+
190
+ it "should raise an ArgumentError when #edge_stroke_color_function is called without a block" do
191
+ lambda { @renderer.edge_stroke_color_function }.should raise_error(ArgumentError)
192
+ lambda { @renderer.edge_stroke_color_function(:dodo) }.should raise_error(ArgumentError)
193
+ end
194
+
195
+ it "should accept a edge_shape_fuction when sent #edge_shape_fuction=" do
196
+ lambda { @renderer.edge_stroke_color_function = mock("shape_fuction") }.should_not raise_error(Exception)
197
+ end
198
+
199
+ it "should retun the shape defined by edge_stroke_color_function when sent #edge_shape(edge)" do
200
+ n = mock("edge a")
201
+ m = mock("edge b")
202
+ color = mock("color")
203
+ @renderer.edge_stroke_color_function = lambda { color }
204
+ @renderer.edge_stroke_color(n).should == color
205
+ @renderer.edge_stroke_color_function = lambda { |graph, layout, edge| edge == n ? :n : :m }
206
+ @renderer.edge_stroke_color(n).should == :n
207
+ @renderer.edge_stroke_color(m).should == :m
208
+ @renderer.edge_stroke_color_function { |graph, layout, edge| edge == n ? :n : :m }
209
+ @renderer.edge_stroke_color(n).should == :n
210
+ @renderer.edge_stroke_color(m).should == :m
211
+ end
212
+ end
213
+
214
+ end