GraphvizR 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,7 @@
1
+ == 0.3.2 / 2007-02-21
2
+
3
+ * add rdoc comments
4
+
1
5
  == 0.3.1 / 2007-02-19
2
6
 
3
7
  * fixed a bug
data/README.txt CHANGED
@@ -46,7 +46,7 @@ To know more detail, please see test/test_graphviz_r.rb
46
46
 
47
47
  === On Rails :
48
48
 
49
- *use _render :rdot_ in controller*
49
+ <b>use _render :rdot_ in controller</b>
50
50
 
51
51
  def show_graph
52
52
  render :rdot do
@@ -61,7 +61,7 @@ To know more detail, please see test/test_graphviz_r.rb
61
61
  end
62
62
  end
63
63
 
64
- *use rdot view template*
64
+ <b>use rdot view template</b>
65
65
 
66
66
  class RdotGenController < ApplicationController
67
67
  def index
@@ -82,7 +82,7 @@ To know more detail, please see test/test_graphviz_r.rb
82
82
 
83
83
  == DEPENDENCIES:
84
84
 
85
- * Graphviz (http://www.graphviz.org/)
85
+ * Graphviz (http://www.graphviz.org)
86
86
 
87
87
  == TODO:
88
88
 
@@ -1,8 +1,122 @@
1
+ # GraphvizR is graphviz adapter for Ruby, and it can:
2
+ # * generate a graphviz dot file, and
3
+ # * generate an image file directly.
4
+ #
5
+ # A sample code to generate dot file is:
6
+ #
7
+ # gvr = GraphvizR.new 'sample'
8
+ # gvr.graph [:label => 'example', :size => '1.5, 2.5']
9
+ # gvr.alpha >> gvr.beta
10
+ # gvr.beta >> gvr.delta
11
+ # gvr.delta >> gvr.gamma
12
+ # gvr.to_dot
13
+ #
14
+ # The code generates a dot file look like:
15
+ #
16
+ # digraph sample {
17
+ # graph [label = "example", size = "1.5, 2.5"];
18
+ # beta [shape = box];
19
+ # alpha -> beta;
20
+ # beta -> delta;
21
+ # delta -> gamma;
22
+ # }
23
+ #
24
+ # == Node
25
+ #
26
+ # A node can be created by method calling or array accessing to GraphvizR instance. In short,
27
+ # both <tt>gvr.abc</tt> and <tt>gvr[:abc]</tt> generate the <tt>abc</tt> node in dot file.
28
+ #
29
+ # == Edge
30
+ #
31
+ # An edge is generated by <tt>&gt;&gt;</tt> of <tt>-</tt> method calling to a node; the former
32
+ # generate a directed graph while the latter a undirected one. For example,
33
+ # gvr = GraphvizR.new 'sample'
34
+ # gvr.alpha >> gvr.beta
35
+ # generates
36
+ # diagraph sample {
37
+ # alpla -> beta
38
+ # }
39
+ # while
40
+ # gvr = GraphvizR.new 'sample'
41
+ # gvr.alpha - gvr.beta
42
+ # generates
43
+ # graph sample {
44
+ # alpla -- beta
45
+ # }
46
+ #
47
+ # == Graph Attributes
48
+ #
49
+ # Attributes are specified by Hash in []. Thus, to set the fillcolor of a node abc, one would use
50
+ # gvr = GraphvizR.new 'sample'
51
+ # gvr.abc [:fillcolor => :red]
52
+ #
53
+ # Similarly, to set the arrowhead style of an edge abc -> def, one would use
54
+ # (gvr.abc >> gvr.def) [:arrowhead => :diamond]
55
+ #
56
+ # As you can expect, to set graph attributes, one would use
57
+ # gvr.graph [:label => 'example', :size => '1.5, 2.5']
58
+ #
59
+ # == Record
60
+ #
61
+ # To set a record label on a node, you can use ordinary [] method.
62
+ # gvr.node1 [:label => "<p_left> left|<p_center>center|<p_right> right"]
63
+ #
64
+ # To access a record in a node, you can use method calling whose argumemts is the name of a record.
65
+ # gvr.node1(:p_left) >> gvr.node2
66
+ #
67
+ # Accordingly, a full example looks like:
68
+ # gvr = GraphvizR.new 'sample'
69
+ # gvr.node [:shape => :record]
70
+ # gvr.node1 [:label => "<p_left> left|<p_center>center|<p_right> right"]
71
+ # gvr.node2
72
+ # gvr.node1(:p_left) >> gvr.node2
73
+ # gvr.node2 >> gvr.node1(:p_center)
74
+ # (gvr.node2 >> gvr.node1(:p_right)) [:label => 'record']
75
+ # gvr.to_dot
76
+ #
77
+ # == Subgraph
78
+ #
79
+ # Subgraph is a way to construct hierarchical graph in graphviz. GraphvizR allows you to use
80
+ # subgraphs by means of method calling with a block which has one argument. For example,
81
+ # gvr = GraphvizR.new 'sample'
82
+ # gvr.cluster0 do |c0|
83
+ # c0.graph [:color => :blue, :label => 'area 0', :style => :bold]
84
+ # c0.a >> c0.b
85
+ # c0.a >> c0.c
86
+ # end
87
+ # gvr.cluster1 do |c1|
88
+ # c1.graph [:fillcolor => '#cc9966', :label => 'area 1', :style => :filled]
89
+ # c1.d >> c1.e
90
+ # c1.d >> c1.f
91
+ # end
92
+ # (gvr.a >> gvr.f) [:lhead => :cluster1, :ltail => :cluster0]
93
+ # gvr.b >> gvr.d
94
+ # (gvr.c >> gvr.d) [:ltail => :cluster0]
95
+ # (gvr.c >> gvr.f) [:lhead => :cluster1]
96
+ # gvr.to_dot
97
+ # generates
98
+ # digraph sample {
99
+ # subgraph cluster0 {
100
+ # graph [color = blue, label ="area 0", style = bold];
101
+ # a -> b;
102
+ # a -> c;
103
+ # }
104
+ # subgraph cluster1 {
105
+ # graph [fillcolor = "#cc9966", label = "area 1", style = filled];
106
+ # d -> e;
107
+ # d -> f;
108
+ # }
109
+ # a -> f [lhead = cluster1, ltail = cluster0];
110
+ # b -> d;
111
+ # c -> d [ltail = cluster0];
112
+ # c -> f [lhead = cluster1];
113
+ #
1
114
  class GraphvizR
2
- VERSION = '0.3.1'
115
+ VERSION = '0.3.2'
3
116
  INDENT_UNIT = ' '
4
117
 
5
- class Attributes
118
+ # This represents graphviz attributes.
119
+ class Attributes
6
120
  def initialize(name, dot)
7
121
  @name = name
8
122
  @dot = dot
@@ -13,6 +127,7 @@ class GraphvizR
13
127
  end
14
128
  end
15
129
 
130
+ # This represents graphviz node.
16
131
  class Node
17
132
  attr_reader :prop, :name
18
133
 
@@ -27,11 +142,13 @@ class GraphvizR
27
142
  end
28
143
  end
29
144
 
145
+ # This generates a directed edge from this node to given node.
30
146
  def >>(node)
31
147
  @dot.directed!
32
148
  add_edge_on_dot node
33
149
  end
34
150
 
151
+ # This generates a undirected edge from this node to given node.
35
152
  def -(node)
36
153
  @dot.undirected!
37
154
  add_edge_on_dot node
@@ -65,6 +182,7 @@ class GraphvizR
65
182
  end
66
183
  end
67
184
 
185
+ # This represents a graphviz edge.
68
186
  class Edge
69
187
  def initialize(from, to, dot, prop={})
70
188
  @from = from
@@ -92,10 +210,65 @@ class GraphvizR
92
210
  end
93
211
  end
94
212
 
213
+ # This returns dot formatted string from given properties. To know more about <tt>prop</tt>,
214
+ # please see GraphvizR.new .
95
215
  def self.dot(name, prop)
96
216
  GraphvizR.new(name, prop).to_dot
97
217
  end
98
218
 
219
+ # This initialzes a GraphvizR instance.
220
+ # +name+:: the name of the graph
221
+ # +prop+:: <b>!deprecated!</b> all properties for a graph can be given as a hash instance.
222
+ # Please guess the contents of the hash from the test or examples below:
223
+ # gvr = GraphvizR.dot(:sample,
224
+ # :graph => {:size => "1.5, 2.5"},
225
+ # :label => 'example',
226
+ # :node_properties => {
227
+ # :beta => {:shape => :box},
228
+ # },
229
+ # :alpha => :beta,
230
+ # {:alpha => :gamma} => {:label => 'label1'},
231
+ # :beta => :delta,
232
+ # :delta => 'size'
233
+ # )
234
+ #
235
+ # gvr = GraphvizR.dot(:sample,
236
+ # :graph => {:size => "1.5, 2.5"},
237
+ # :node => {:shape => :record},
238
+ # :node_properties => {
239
+ # :node1 => {:label => "<p_left> left|<p_center>center|<p_right> right"},
240
+ # :node2 => {:label => "left|center|right"}
241
+ # },
242
+ # :node1 => :node2,
243
+ # [:node1, :p_left] => :node2,
244
+ # :node2 => [:node1, :p_center],
245
+ # {:node2 => [:node1, :p_right]} => {:label => 'record'}
246
+ # )
247
+ #
248
+ # gvr = GraphvizR.dot(:sample,
249
+ # :subgraphs => {
250
+ # :cluster0 => {
251
+ # :color => :blue,
252
+ # :style => :bold,
253
+ # :label => "area 0",
254
+ # {:a => :b} => {},
255
+ # :a => :c
256
+ # },
257
+ # :cluster1 => {
258
+ # :fillcolor => "#cc9966",
259
+ # :style => :filled,
260
+ # :label => "area 1",
261
+ # [:d] => :e,
262
+ # :d => :f
263
+ # }
264
+ # },
265
+ # :b => :d,
266
+ # {:c => :d} => {:ltail => :cluster0},
267
+ # {:c => :f} => {:lhead => :cluster1},
268
+ # {:a => :f} => {:ltail => :cluster0, :lhead => :cluster1}
269
+ # )
270
+ # +parent+:: a parent graph is given when this graph is a subgraph.
271
+ # +indent+:: indent level when this instance is converted to rdot.
99
272
  def initialize(name, prop={}, parent=nil, indent=0)
100
273
  if name.is_a? Array
101
274
  initialize(*name)
@@ -113,7 +286,7 @@ class GraphvizR
113
286
  end
114
287
  end
115
288
 
116
- def interprete(prop)
289
+ def interprete(prop) #:nodoc:
117
290
  prop.each do |k, v|
118
291
  case k
119
292
  when :node_property, :node_properties
@@ -132,10 +305,12 @@ class GraphvizR
132
305
  end
133
306
  end
134
307
 
308
+ # add properties for nodes
135
309
  def add_node_properties(props)
136
310
  @node_properties = props
137
311
  end
138
312
 
313
+ # add an edge to the graph
139
314
  def add_edge(from, to=nil)
140
315
  if from.is_a? Hash
141
316
  f = from.keys[0]
@@ -150,16 +325,19 @@ class GraphvizR
150
325
  end
151
326
  end
152
327
 
328
+ # add subgraph
153
329
  def add_subgraph(graphs)
154
330
  graphs.to_a.sort.each do |key_prop|
155
331
  @subgraphs << self.class.new(key_prop[0], key_prop[1], self, @indent)
156
332
  end
157
333
  end
158
334
 
335
+ # is this graph subgraph or not.
159
336
  def subgraph?
160
337
  not @parent.nil?
161
338
  end
162
339
 
340
+ # make this graph directed
163
341
  def directed!
164
342
  if subgraph?
165
343
  @parent.directed!
@@ -168,6 +346,7 @@ class GraphvizR
168
346
  end
169
347
  end
170
348
 
349
+ # make this graph undirected
171
350
  def undirected!
172
351
  if subgraph?
173
352
  @parent.undirected!
@@ -176,6 +355,7 @@ class GraphvizR
176
355
  end
177
356
  end
178
357
 
358
+ # is this graph directed?
179
359
  def directed?
180
360
  if subgraph?
181
361
  @parent.directed?
@@ -184,6 +364,7 @@ class GraphvizR
184
364
  end
185
365
  end
186
366
 
367
+ # is this graph undirected?
187
368
  def undirected?
188
369
  if subgraph?
189
370
  @parent.undirected?
@@ -192,6 +373,7 @@ class GraphvizR
192
373
  end
193
374
  end
194
375
 
376
+ # When block is given, create new subgraph. Otherwise, create new node.
195
377
  def [](key, *args, &block)
196
378
  if block
197
379
  subgraph = self.class.new key, {}, self, @indent
@@ -202,6 +384,7 @@ class GraphvizR
202
384
  end
203
385
  end
204
386
 
387
+ # set properties for a node
205
388
  def []=(key, *args, &block)
206
389
  @node_properties[key] = args[0]
207
390
  end
@@ -212,6 +395,7 @@ class GraphvizR
212
395
  end
213
396
  end
214
397
 
398
+ # set graph properties
215
399
  def graph(*args)
216
400
  if args.empty?
217
401
  Attributes.new :graph, self
@@ -220,21 +404,16 @@ class GraphvizR
220
404
  end
221
405
  end
222
406
 
407
+ # set graph properties
223
408
  def graph=(prop)
224
409
  @graph_properties.merge! prop
225
410
  end
226
411
 
412
+ # convert this instance to dot
227
413
  def to_dot
228
- graph_type = if subgraph?
229
- 'subgraph'
230
- elsif directed?
231
- 'digraph'
232
- else
233
- 'graph'
234
- end
414
+ graph_type = if subgraph?; 'subgraph'elsif directed?; 'digraph' else 'graph' end
235
415
  dot = indent_enter("#{graph_type} #{@name} {", true)
236
416
  dot += indent_enter("graph #{@graph_properties.to_dot};") unless @graph_properties.empty?
237
- #dot += indent_enter(@subgraphs.map{|e| e.to_dot}.join("\n")) unless @subgraphs.empty?
238
417
  dot += @subgraphs.map{|e| e.to_dot}.join('') unless @subgraphs.empty?
239
418
  dot += properties_to_dot(@default_node_properties) unless @default_node_properties.empty?
240
419
  dot += properties_to_dot(@node_properties) unless @node_properties.empty?
@@ -243,10 +422,13 @@ class GraphvizR
243
422
  dot
244
423
  end
245
424
 
246
- def indent_enter(str, is_first=false)
425
+ def indent_enter(str, is_first=false) #:nodoc:
247
426
  "#{INDENT_UNIT * (@indent - (is_first ? 1 : 0))}#{str}\n"
248
427
  end
249
428
 
429
+ # If <tt>format</tt> is 'dot', a dot string is generated. Otherwise, this generates image file
430
+ # in the given format, such as 'png', 'gif', 'jpg', and so on. To know correctly, please see
431
+ # the specification of graphviz: http://www.graphviz.org/doc/info/output.html
250
432
  def data(format='png')
251
433
  format = format.to_s
252
434
  if format == 'dot'
@@ -263,6 +445,7 @@ class GraphvizR
263
445
  end
264
446
  end
265
447
 
448
+ # store image data created from this instance to given file.
266
449
  def output(filename=nil, format='png')
267
450
  img = data(format)
268
451
  File.open(filename || "#{@name}.#{format || 'png'}", "w+") do |file|
@@ -270,7 +453,7 @@ class GraphvizR
270
453
  end
271
454
  end
272
455
 
273
- def method_missing(name, *args, &block)
456
+ def method_missing(name, *args, &block) #:nodoc:
274
457
  method =
275
458
  if name.to_s =~ /=$/
276
459
  name = name.to_s[0..-2].to_sym
@@ -283,24 +466,24 @@ class GraphvizR
283
466
 
284
467
  protected
285
468
 
286
- def properties_to_dot(hash)
469
+ def properties_to_dot(hash) #:nodoc:
287
470
  hash.to_a.sort.map{|e| indent_enter "#{e[0]} #{e[1].to_dot};"}.join()
288
471
  end
289
472
  end
290
473
 
291
- class Number
474
+ class Number #:nodoc:
292
475
  def to_dot
293
476
  to_s
294
477
  end
295
478
  end
296
479
 
297
- class String
480
+ class String #:nodoc:
298
481
  def to_dot
299
482
  inspect
300
483
  end
301
484
  end
302
485
 
303
- class Symbol
486
+ class Symbol #:nodoc:
304
487
  def to_dot
305
488
  to_s
306
489
  end
@@ -310,7 +493,7 @@ class Symbol
310
493
  end
311
494
  end
312
495
 
313
- class Hash
496
+ class Hash #:nodoc:
314
497
  def to_dot
315
498
  "[#{to_a.sort.map{|e| "#{e[0].to_dot} = #{e[1].to_dot}"}.join(', ')}]"
316
499
  end
@@ -1,7 +1,7 @@
1
1
  require 'graphviz_r'
2
2
 
3
3
  class ActionController::Base
4
- def render_with_rdot(options = nil, deprecated_status = nil, &block)
4
+ def render_with_rdot(options = nil, deprecated_status = nil, &block) #:nodoc:
5
5
  if options == :rdot
6
6
  render_rdot deprecated_status, &block
7
7
  else
@@ -11,7 +11,35 @@ class ActionController::Base
11
11
  #alias_method_chain :render, :rdot
12
12
  alias_method :render_without_rdot, :render
13
13
  alias_method :render, :render_with_rdot
14
-
14
+
15
+ # render rdot. <tt>options</tt> hash can have <tt>:format</tt> and <tt>:disposition</tt>,
16
+ # and their default values are 'png' and 'inline' respectively.
17
+ #
18
+ # rdot is directed in given block and the block can have zero or one argument.
19
+ # If any argument is not given, only local variables can be refered in the block.
20
+ # Otherwise, not only local variables but also instance one can be refered.
21
+ #
22
+ # class RdotGenController < ApplicationController
23
+ # def index
24
+ # graph_size_directed_as_local_variable = '1.5, 2.5'
25
+ #
26
+ # render :rdot do
27
+ # graph [:size => graph_size_directed_as_local_variable]
28
+ # node1 >> node2
29
+ # end
30
+ # end
31
+ # end
32
+ #
33
+ # class RdotGenController < ApplicationController
34
+ # def index
35
+ # @graph_size_directed_as_instance_variable = '1.5, 2.5'
36
+ #
37
+ # render :rdot do |gvr|
38
+ # gvr.graph [:size => @graph_size_directed_as_instance_variable]
39
+ # gvr.node1 >> gvr.node2
40
+ # end
41
+ # end
42
+ # end
15
43
  def render_rdot(options=nil, &block)
16
44
  options ||= {}
17
45
  format = options[:format] || 'png'
@@ -37,7 +65,7 @@ class ActionController::Base
37
65
 
38
66
  private
39
67
 
40
- def copy_instance_variables(from_block, to_obj)
68
+ def copy_instance_variables(from_block, to_obj) #:nodoc:
41
69
  block_instance_variables = eval 'instance_variables', from_block.binding
42
70
  block_instance_variables.each do |name|
43
71
  to_obj.instance_variable_set name.to_sym, eval(name, from_block.binding)
@@ -1,8 +1,30 @@
1
+ # Process rdot template.
2
+ #
3
+ # == Rdot Template Example
4
+ # class RdotGenController < ApplicationController
5
+ # def index
6
+ # @label1 = "<p_left> left|<p_center>center|<p_right> right"
7
+ # @label2 = "left|center|right"
8
+ # end
9
+ # end
10
+ #
11
+ # # view/rdot_gen/index.rdot
12
+ # graph [:size => '1.5, 2.5']
13
+ # node [:shape => :record]
14
+ # node1 [:label => @label1]
15
+ # node2 [:label => @label2]
16
+ # node1 >> node2
17
+ # node1(:p_left) >> node2
18
+ # node2 >> node1(:p_center)
19
+ # (node2 >> node1(:p_right)) [:label => 'record']
20
+ #
21
+ # To know more about rdot, please see the documents for GraphvizR.
1
22
  class RdotTemplate
2
23
  def initialize(view)
3
24
  @view = view
4
25
  end
5
26
 
27
+ # render rdot template
6
28
  def render(template, local_assigns)
7
29
  params = @view.assigns['params']
8
30
  format = params['format'] || 'png'
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: GraphvizR
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2007-02-19 00:00:00 +09:00
6
+ version: 0.3.2
7
+ date: 2007-02-21 00:00:00 +09:00
8
8
  summary: Graphviz wrapper for Ruby and Rails
9
9
  require_paths:
10
10
  - lib
@@ -23,20 +23,20 @@ description: "Graphviz wrapper for Ruby. This can be used as a common library, a
23
23
  gvr.gamma gvr.to_dot replies the dot code: digraph sample { graph [label =
24
24
  \"example\", size = \"1.5, 2.5\"]; beta [shape = box]; alpha -> beta; beta ->
25
25
  delta [label = \"label1\"]; delta -> gamma; } To know more detail, please see
26
- test/test_graphviz_r.rb === On Rails : *use _render :rdot_ in controller* def
27
- show_graph render :rdot do graph [:size => '1.5, 2.5'] node [:shape => :record]
28
- node1 [:label => \"<p_left> left|<p_center>center|<p_right> right\"] node2
29
- [:label => \"left|center|right\"] node1 >> node2 node1(:p_left) >> node2 node2
30
- >> node1(:p_center) (node2 >> node1(:p_right)) [:label => 'record'] end end
31
- *use rdot view template* class RdotGenController < ApplicationController def
32
- index @label1 = \"<p_left> left|<p_center>center|<p_right> right\" @label2 =
33
- \"left|center|right\" end end # view/rdot_gen/index.rdot graph [:size => '1.5,
34
- 2.5'] node [:shape => :record] node1 [:label => @label1] node2 [:label =>
35
- @label2] node1 >> node2 node1(:p_left) >> node2 node2 >> node1(:p_center) (node2
36
- >> node1(:p_right)) [:label => 'record'] == DEPENDENCIES: * Graphviz
37
- (http://www.graphviz.org/) == TODO: * cover all specification for graphviz ==
38
- INSTALL: * sudo gem install graphviz_r * if you want to use this in ruby on
39
- rails * script/plugin install
26
+ test/test_graphviz_r.rb === On Rails : <b>use _render :rdot_ in controller</b>
27
+ def show_graph render :rdot do graph [:size => '1.5, 2.5'] node [:shape =>
28
+ :record] node1 [:label => \"<p_left> left|<p_center>center|<p_right> right\"]
29
+ node2 [:label => \"left|center|right\"] node1 >> node2 node1(:p_left) >> node2
30
+ node2 >> node1(:p_center) (node2 >> node1(:p_right)) [:label => 'record'] end
31
+ end <b>use rdot view template</b> class RdotGenController <
32
+ ApplicationController def index @label1 = \"<p_left>
33
+ left|<p_center>center|<p_right> right\" @label2 = \"left|center|right\" end end
34
+ # view/rdot_gen/index.rdot graph [:size => '1.5, 2.5'] node [:shape => :record]
35
+ node1 [:label => @label1] node2 [:label => @label2] node1 >> node2
36
+ node1(:p_left) >> node2 node2 >> node1(:p_center) (node2 >> node1(:p_right))
37
+ [:label => 'record'] == DEPENDENCIES: * Graphviz (http://www.graphviz.org) ==
38
+ TODO: * cover all specification for graphviz == INSTALL: * sudo gem install
39
+ graphviz_r * if you want to use this in ruby on rails * script/plugin install
40
40
  http://technohippy.net/svn/repos/graphviz_r/trunk/vendor/plugins/rdot ==
41
41
  LICENSE: (The MIT License)"
42
42
  autorequire: