ruby-graphviz 1.0.2 → 1.0.3

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 (53) hide show
  1. data/README.rdoc +9 -0
  2. data/examples/sample01.rb +17 -17
  3. data/examples/sample02.rb +18 -18
  4. data/examples/sample03.rb +8 -8
  5. data/examples/sample04.rb +5 -5
  6. data/examples/sample05.rb +17 -17
  7. data/examples/sample07.rb +5 -5
  8. data/examples/sample08.rb +15 -15
  9. data/examples/sample09.rb +25 -25
  10. data/examples/sample10.rb +25 -25
  11. data/examples/sample11.rb +21 -21
  12. data/examples/sample16.rb +2 -2
  13. data/examples/sample17.rb +5 -5
  14. data/examples/sample20.rb +18 -18
  15. data/examples/sample29.rb +2 -2
  16. data/examples/sample30.rb +4 -4
  17. data/examples/sample31.rb +3 -3
  18. data/examples/sample36.rb +5 -5
  19. data/examples/sample37.rb +8 -8
  20. data/examples/sample38.rb +2 -2
  21. data/examples/sample39.rb +6 -6
  22. data/examples/sample40.rb +4 -4
  23. data/examples/sample42.rb +14 -14
  24. data/examples/sample43.rb +7 -7
  25. data/examples/sample44.rb +3 -3
  26. data/examples/sample45.rb +1 -1
  27. data/examples/sample46.rb +18 -18
  28. data/examples/sample54.rb +2 -2
  29. data/examples/sample55.rb +4 -4
  30. data/examples/sample58.rb +7 -7
  31. data/examples/sample60.rb +3 -3
  32. data/examples/sample61.rb +3 -3
  33. data/examples/sample62.rb +3 -3
  34. data/examples/sample66.rb +4 -0
  35. data/examples/sample67.rb +10 -0
  36. data/examples/sample68.rb +27 -0
  37. data/lib/ext/gvpr/dot2ruby.g +2 -2
  38. data/lib/graphviz.rb +91 -27
  39. data/lib/graphviz/constants.rb +1 -1
  40. data/lib/graphviz/core_ext.rb +0 -9
  41. data/lib/graphviz/dsl.rb +3 -3
  42. data/lib/graphviz/edge.rb +6 -6
  43. data/lib/graphviz/family_tree/couple.rb +8 -8
  44. data/lib/graphviz/family_tree/person.rb +10 -10
  45. data/lib/graphviz/graphml.rb +3 -3
  46. data/lib/graphviz/node.rb +2 -2
  47. data/lib/graphviz/theory.rb +37 -2
  48. data/lib/graphviz/xml.rb +4 -4
  49. data/ruby-graphviz.gemspec +2 -13
  50. data/test/test_graph.rb +25 -25
  51. data/test/test_search.rb +32 -0
  52. data/test/test_theory.rb +1 -1
  53. metadata +11 -13
@@ -0,0 +1,4 @@
1
+ $:.unshift("../lib")
2
+
3
+ require 'graphviz'
4
+ GraphViz.generate(30, 44, true, (1..8)).output(:png => "#{$0}.png")
@@ -0,0 +1,10 @@
1
+ $:.unshift("../lib")
2
+
3
+ require 'graphviz'
4
+ graph = GraphViz.graph(:G)
5
+ graph.add_nodes(["A", "B", "C", "D", "E", "F", "G"])
6
+ graph.add_edges("A", ["B", "C", "E"])
7
+ graph.add_edges("B", ["D", "F"])
8
+ graph.add_edges("C", "G")
9
+ graph.add_edges("F", "E")
10
+ graph.output(:png => "#{$0}.png")
@@ -0,0 +1,27 @@
1
+ $:.unshift("../lib")
2
+
3
+ require 'graphviz'
4
+ require 'graphviz/theory'
5
+ g = GraphViz.graph(:G)
6
+ g.add_nodes(["A", "B", "C", "D", "E", "F", "G"])
7
+ g.add_edges("A", ["B", "C", "E"])
8
+ g.add_edges("B", ["D", "F"])
9
+ g.add_edges("C", "G")
10
+ g.add_edges("F", "E")
11
+ g.output(:png => "#{$0}000.png")
12
+
13
+ t = GraphViz::Theory.new(g)
14
+ i = 1
15
+ t.dfs("A") { |node|
16
+ name = sprintf("%s%03d.png", $0, i)
17
+ node[:color => :lightblue, :style => :filled]
18
+ g.output(:png => name)
19
+ i = i + 1
20
+ }
21
+
22
+ t.bfs("A") { |node|
23
+ name = sprintf("%s%03d.png", $0, i)
24
+ node[:color => :red, :style => :filled]
25
+ g.output(:png => name)
26
+ i = i + 1
27
+ }
@@ -107,7 +107,7 @@ N {
107
107
  subgraph = nxtsubg( subgraph );
108
108
  }
109
109
 
110
- printf( " node_%s = graph_%s.add_node( \"%s\"", rubyfy($.name), rubyfy(ofgraph.name), $.name );
110
+ printf( " node_%s = graph_%s.add_nodes( \"%s\"", rubyfy($.name), rubyfy(ofgraph.name), $.name );
111
111
 
112
112
  // Attributs of N
113
113
  attr = fstAttr($G, "N");
@@ -136,7 +136,7 @@ E {
136
136
  subgraph = nxtsubg( subgraph );
137
137
  }
138
138
 
139
- printf( " graph_%s.add_edge( \"%s\", \"%s\"", rubyfy(ofgraph.name), $.tail.name, $.head.name );
139
+ printf( " graph_%s.add_edges( \"%s\", \"%s\"", rubyfy(ofgraph.name), $.tail.name, $.head.name );
140
140
 
141
141
  // Attributs of E
142
142
  attr = fstAttr($G, "E");
data/lib/graphviz.rb CHANGED
@@ -93,6 +93,9 @@ class GraphViz
93
93
  # Return the GraphViz::Node object created
94
94
  #
95
95
  def add_node( xNodeName, hOpts = {} )
96
+ warn "GraphViz#add_node is deprecated, please use GraphViz#add_nodes"
97
+ return add_nodes(xNodeName, hOpts)
98
+ =begin
96
99
  node = @hoNodes[xNodeName]
97
100
 
98
101
  if node.nil?
@@ -117,6 +120,38 @@ class GraphViz
117
120
  end
118
121
 
119
122
  return node
123
+ =end
124
+ end
125
+
126
+ def add_nodes(node_name, options = {})
127
+ if node_name.kind_of? Array
128
+ node_name.each { |n| add_nodes(n, options.clone) }
129
+ else
130
+ node = @hoNodes[node_name]
131
+
132
+ if node.nil?
133
+ @hoNodes[node_name] = GraphViz::Node::new( node_name, self )
134
+ @hoNodes[node_name].index = @elements_order.size_of( "node" )
135
+
136
+ unless options.keys.include?(:label) or options.keys.include?("label")
137
+ options[:label] = node_name
138
+ end
139
+
140
+ @elements_order.push( {
141
+ "type" => "node",
142
+ "name" => node_name,
143
+ "value" => @hoNodes[node_name]
144
+ } )
145
+
146
+ node = @hoNodes[node_name]
147
+ end
148
+
149
+ options.each do |xKey, xValue|
150
+ @hoNodes[node_name][xKey.to_s] = xValue
151
+ end
152
+
153
+ return node
154
+ end
120
155
  end
121
156
 
122
157
  # Return the node object for the given name (or nil) in the current graph
@@ -166,47 +201,49 @@ class GraphViz
166
201
  end
167
202
  end
168
203
 
169
- #
170
204
  # Get the number of nodes
171
- #
172
205
  def node_count
173
206
  @hoNodes.size
174
207
  end
175
208
 
176
- ##
209
+ #<b>DEPRECATED</b>
210
+ def add_edge( oNodeOne, oNodeTwo, hOpts = {} )
211
+ warn "GraphViz#add_edge is deprecated, please use GraphViz#add_edges"
212
+ add_edges(oNodeOne, oNodeTwo, hOpts)
213
+ end
214
+
177
215
  # Create a new edge
178
216
  #
179
217
  # In:
180
- # * oNodeOne : First node (or node list)
181
- # * oNodeTwo : Second Node (or node list)
182
- # * hOpts : Edge attributs
183
- #
184
- def add_edge( oNodeOne, oNodeTwo, hOpts = {} )
218
+ # * node_one : First node (or node list)
219
+ # * node_two : Second Node (or node list)
220
+ # * options : Edge attributs
221
+ def add_edges( node_one, node_two, options = {} )
185
222
 
186
- if( oNodeOne.class == Array )
187
- oNodeOne.each do |no|
188
- add_edge( no, oNodeTwo, hOpts )
223
+ if( node_one.class == Array )
224
+ node_one.each do |no|
225
+ add_edges( no, node_two, options )
189
226
  end
190
227
  else
191
- if( oNodeTwo.class == Array )
192
- oNodeTwo.each do |nt|
193
- add_edge( oNodeOne, nt, hOpts )
228
+ if( node_two.class == Array )
229
+ node_two.each do |nt|
230
+ add_edges( node_one, nt, options )
194
231
  end
195
232
  else
196
- oEdge = GraphViz::Edge::new( oNodeOne, oNodeTwo, self )
197
- oEdge.index = @elements_order.size_of( "edge" )
233
+ edge = GraphViz::Edge::new( node_one, node_two, self )
234
+ edge.index = @elements_order.size_of( "edge" )
198
235
 
199
- hOpts.each do |xKey, xValue|
200
- oEdge[xKey.to_s] = xValue
236
+ options.each do |xKey, xValue|
237
+ edge[xKey.to_s] = xValue
201
238
  end
202
239
 
203
240
  @elements_order.push( {
204
241
  "type" => "edge",
205
- "value" => oEdge
242
+ "value" => edge
206
243
  } )
207
- @loEdges.push( oEdge )
244
+ @loEdges.push( edge )
208
245
 
209
- return( oEdge )
246
+ return( edge )
210
247
  end
211
248
  end
212
249
  end
@@ -349,7 +386,7 @@ class GraphViz
349
386
  end
350
387
  return( @hoGraphs[xName] ) if @hoGraphs.keys.include?( xName )
351
388
 
352
- rCod = add_node( xName, args[0]||{} )
389
+ rCod = add_nodes( xName, args[0]||{} )
353
390
  end
354
391
 
355
392
  return rCod
@@ -678,7 +715,7 @@ class GraphViz
678
715
  self << no
679
716
  end
680
717
  else
681
- return GraphViz::commonGraph( oNode, self ).add_edge( self, oNode )
718
+ return GraphViz::commonGraph( oNode, self ).add_edges( self, oNode )
682
719
  end
683
720
  end
684
721
  alias :> :<<
@@ -888,11 +925,11 @@ class GraphViz
888
925
  def add_hash_edge(node, hash)
889
926
  if hash.kind_of? Hash
890
927
  hash.each do |nt, data|
891
- add_edge(node, nt)
928
+ add_edges(node, nt)
892
929
  add_hash_edge(nt, data)
893
930
  end
894
931
  else
895
- add_edge(node, hash)
932
+ add_edges(node, hash)
896
933
  end
897
934
  end
898
935
 
@@ -914,15 +951,42 @@ class GraphViz
914
951
  new( xGraphName, hOpts.symbolize_keys.merge( {:type => "digraph"} ), &block )
915
952
  end
916
953
 
917
- #
918
954
  # Create a new strict directed graph
919
955
  #
920
956
  # See also GraphViz::new
921
- #
922
957
  def self.strict_digraph( xGraphName, hOpts = {}, &block )
923
958
  new( xGraphName, hOpts.symbolize_keys.merge( {:type => "digraph", :strict => true} ), &block )
924
959
  end
925
960
 
961
+ # Create a random graph.
962
+ def self.generate(num_nodes, num_edges, directed = false, weight_range = (1..1))
963
+ g = nil
964
+ if directed
965
+ g = GraphViz.digraph(:G)
966
+ else
967
+ g = GraphViz.graph(:G)
968
+ end
969
+
970
+ nodes = (1..num_nodes).map{ |e| e.to_s }
971
+ x = g.add_nodes(nodes)
972
+
973
+ edges = []
974
+ nodes.each do |head|
975
+ nodes.each do |tail|
976
+ if (directed and head != tail) or (head < tail)
977
+ edges << {:head => head, :tail => tail, :weight => weight_range.to_a.shuffle[0]}
978
+ end
979
+ end
980
+ end
981
+ edges.shuffle!
982
+
983
+ (num_edges - 1).times do |i|
984
+ g.add_edges(edges[i][:head], edges[i][:tail], :label => edges[i][:weight].to_s, :weight => edges[i][:weight])
985
+ end
986
+
987
+ return g
988
+ end
989
+
926
990
  #
927
991
  # Escape a string to be acceptable as a node name in a graphviz input file
928
992
  #
@@ -40,7 +40,7 @@
40
40
  # C => cluster
41
41
  #
42
42
  module Constants
43
- RGV_VERSION = "1.0.2"
43
+ RGV_VERSION = "1.0.3"
44
44
 
45
45
  ## Const: Output formats
46
46
  FORMATS = [
@@ -23,15 +23,6 @@ class Object
23
23
  end
24
24
  end
25
25
 
26
- class Array
27
- def all?(&b)
28
- r = self.delete_if { |x|
29
- yield x
30
- }
31
- r.size == 0
32
- end
33
- end
34
-
35
26
  # From : http://www.geekmade.co.uk/2008/09/ruby-tip-normalizing-hash-keys-as-symbols/
36
27
  class Hash
37
28
  def symbolize_keys
data/lib/graphviz/dsl.rb CHANGED
@@ -17,14 +17,14 @@ class GraphViz::DSL
17
17
  elsif(block)
18
18
  @graph.add_graph(GraphViz::DSL.new(sym, { :parent => @graph, :type => @graph.type }, &block).graph)
19
19
  else
20
- @graph.add_node(sym.to_s, *args)
20
+ @graph.add_nodes(sym.to_s, *args)
21
21
  end
22
22
  end
23
23
 
24
24
  # Add a new node
25
25
  def n(name)
26
26
  return @graph.get_node(name) unless @graph.get_node(name.to_s).nil?
27
- @graph.add_node(name)
27
+ @graph.add_nodes(name)
28
28
  end
29
29
 
30
30
  # Create edges
@@ -32,7 +32,7 @@ class GraphViz::DSL
32
32
  e = nil
33
33
  last = args.shift
34
34
  while current = args.shift
35
- e = @graph.add_edge(last, current)
35
+ e = @graph.add_edges(last, current)
36
36
  last = current
37
37
  end
38
38
  return e
data/lib/graphviz/edge.rb CHANGED
@@ -36,11 +36,11 @@ class GraphViz
36
36
  @index = nil
37
37
 
38
38
  unless @parent_graph.directed?
39
- (@parent_graph.find_node(@node_one_id) || @parent_graph.add_node(@node_one_id)).incidents << (@parent_graph.find_node(@node_two_id) || @parent_graph.add_node(@node_two_id))
40
- (@parent_graph.find_node(@node_two_id) || @parent_graph.add_node(@node_two_id)).neighbors << (@parent_graph.find_node(@node_one_id) || @parent_graph.add_node(@node_one_id))
39
+ (@parent_graph.find_node(@node_one_id) || @parent_graph.add_nodes(@node_one_id)).incidents << (@parent_graph.find_node(@node_two_id) || @parent_graph.add_nodes(@node_two_id))
40
+ (@parent_graph.find_node(@node_two_id) || @parent_graph.add_nodes(@node_two_id)).neighbors << (@parent_graph.find_node(@node_one_id) || @parent_graph.add_nodes(@node_one_id))
41
41
  end
42
- (@parent_graph.find_node(@node_one_id) || @parent_graph.add_node(@node_one_id)).neighbors << (@parent_graph.find_node(@node_two_id) || @parent_graph.add_node(@node_two_id))
43
- (@parent_graph.find_node(@node_two_id) || @parent_graph.add_node(@node_two_id)).incidents << (@parent_graph.find_node(@node_one_id) || @parent_graph.add_node(@node_one_id))
42
+ (@parent_graph.find_node(@node_one_id) || @parent_graph.add_nodes(@node_one_id)).neighbors << (@parent_graph.find_node(@node_two_id) || @parent_graph.add_nodes(@node_two_id))
43
+ (@parent_graph.find_node(@node_two_id) || @parent_graph.add_nodes(@node_two_id)).incidents << (@parent_graph.find_node(@node_one_id) || @parent_graph.add_nodes(@node_one_id))
44
44
  end
45
45
 
46
46
  # Return the node one as string (so with port if any)
@@ -113,7 +113,7 @@ class GraphViz
113
113
  def <<( node ) #:nodoc:
114
114
  n = @parent_graph.get_node(@node_two_id)
115
115
 
116
- GraphViz::commonGraph( node, n ).add_edge( n, node )
116
+ GraphViz::commonGraph( node, n ).add_edges( n, node )
117
117
  end
118
118
  alias :> :<< #:nodoc:
119
119
  alias :- :<< #:nodoc:
@@ -133,7 +133,7 @@ class GraphViz
133
133
  # Set edge attributs
134
134
  #
135
135
  # Example :
136
- # e = graph.add_edge( ... )
136
+ # e = graph.add_edges( ... )
137
137
  # ...
138
138
  # e.set { |_e|
139
139
  # _e.color = "blue"
@@ -19,7 +19,7 @@ class GraphViz
19
19
  return
20
20
 
21
21
  if z.size == 1
22
- @graph.add_edge( @node, z[0].node, "dir" => "none" )
22
+ @graph.add_edges( @node, z[0].node, "dir" => "none" )
23
23
  else
24
24
  cluster = @graph.add_graph( "#{@node.id}Kids" )
25
25
  cluster["rank"] = "same"
@@ -32,23 +32,23 @@ class GraphViz
32
32
  z.each do |person|
33
33
  count = count + 1
34
34
  if count == add
35
- middle = cluster.add_node( "#{@node.id}Kids", "shape" => "point" )
36
- @graph.add_edge( @node, middle, "dir" => "none" )
35
+ middle = cluster.add_nodes( "#{@node.id}Kids", "shape" => "point" )
36
+ @graph.add_edges( @node, middle, "dir" => "none" )
37
37
  unless last.nil?
38
- cluster.add_edge( last, middle, "dir" => "none" )
38
+ cluster.add_edges( last, middle, "dir" => "none" )
39
39
  end
40
40
  last = middle
41
41
  end
42
42
 
43
- kid = cluster.add_node( "#{person.node.id}Kid", "shape" => "point" )
44
- @graph.add_edge( kid, person.node, "dir" => "none" )
43
+ kid = cluster.add_nodes( "#{person.node.id}Kid", "shape" => "point" )
44
+ @graph.add_edges( kid, person.node, "dir" => "none" )
45
45
 
46
46
  if add == 0 and count == link
47
- @graph.add_edge( @node, kid, "dir" => "none" )
47
+ @graph.add_edges( @node, kid, "dir" => "none" )
48
48
  end
49
49
 
50
50
  unless last.nil?
51
- cluster.add_edge( last, kid, "dir" => "none" )
51
+ cluster.add_edges( last, kid, "dir" => "none" )
52
52
  end
53
53
  last = kid
54
54
  end
@@ -6,7 +6,7 @@ class GraphViz
6
6
  class Person
7
7
  def initialize( graph, tree, generation, id ) #:nodoc:
8
8
  @graph = graph
9
- @node = @graph.add_node( id )
9
+ @node = @graph.add_nodes( id )
10
10
  @node["shape"] = "box"
11
11
  @tree = tree
12
12
  @generation = generation
@@ -71,10 +71,10 @@ class GraphViz
71
71
  #
72
72
  # mu.is_maried_with greg
73
73
  def is_maried_with( x )
74
- node = @graph.add_node( "#{@node.id}And#{x.node.id}" )
74
+ node = @graph.add_nodes( "#{@node.id}And#{x.node.id}" )
75
75
  node["shape"] = "point"
76
- @graph.add_edge( @node, node, "dir" => "none" )
77
- @graph.add_edge( node, x.node, "dir" => "none" )
76
+ @graph.add_edges( @node, node, "dir" => "none" )
77
+ @graph.add_edges( node, x.node, "dir" => "none" )
78
78
  @tree.add_couple( self, x, node )
79
79
  end
80
80
 
@@ -82,11 +82,11 @@ class GraphViz
82
82
  #
83
83
  # sophie.is_divorced_with john
84
84
  def is_divorced_with( x )
85
- node = @graph.add_node( "#{@node.id}And#{x.node.id}" )
85
+ node = @graph.add_nodes( "#{@node.id}And#{x.node.id}" )
86
86
  node["shape"] = "point"
87
87
  node["color"] = "red"
88
- @graph.add_edge( @node, node, "dir" => "none", "color" => "red" )
89
- @graph.add_edge( node, x.node, "dir" => "none", "color" => "red" )
88
+ @graph.add_edges( @node, node, "dir" => "none", "color" => "red" )
89
+ @graph.add_edges( node, x.node, "dir" => "none", "color" => "red" )
90
90
  @tree.add_couple( self, x, node )
91
91
  end
92
92
 
@@ -94,11 +94,11 @@ class GraphViz
94
94
  #
95
95
  # simon.is_widower_of elisa
96
96
  def is_widower_of( x ) #veuf
97
- node = @graph.add_node( "#{@node.id}And#{x.node.id}" )
97
+ node = @graph.add_nodes( "#{@node.id}And#{x.node.id}" )
98
98
  node["shape"] = "point"
99
99
  node["color"] = "green"
100
- @graph.add_edge( @node, node, "dir" => "none", "color" => "green" )
101
- @graph.add_edge( node, x.node, "dir" => "none", "color" => "green" )
100
+ @graph.add_edges( @node, node, "dir" => "none", "color" => "green" )
101
+ @graph.add_edges( node, x.node, "dir" => "none", "color" => "green" )
102
102
  @tree.add_couple( self, x, node )
103
103
  end
104
104
 
@@ -152,7 +152,7 @@ class GraphViz
152
152
  end
153
153
 
154
154
  unless @current_node.nil?
155
- node = @current_graph.add_node( node.attributes['id'] )
155
+ node = @current_graph.add_nodes( node.attributes['id'] )
156
156
  @current_node.each do |k, v|
157
157
  node[k] = v
158
158
  end
@@ -182,7 +182,7 @@ class GraphViz
182
182
  target = node.attributes['target']
183
183
  target = {target => node.attributes['targetport']} if node.attributes['targetport']
184
184
 
185
- @current_edge = @current_graph.add_edge( source, target )
185
+ @current_edge = @current_graph.add_edges( source, target )
186
186
 
187
187
  node.each_element( ) do |child|
188
188
  #begin
@@ -214,7 +214,7 @@ class GraphViz
214
214
 
215
215
  list.each { |s|
216
216
  list.each { |t|
217
- @current_graph.add_edge( s, t ) unless s == t
217
+ @current_graph.add_edges( s, t ) unless s == t
218
218
  }
219
219
  }
220
220
  end