ruby-graphviz 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -41,10 +41,10 @@ class GraphViz
41
41
  if @attributs.index( xKey.to_s ).nil? == true
42
42
  raise ArgumentError, "#{@name} attribut '#{xKey.to_s}' invalid"
43
43
  end
44
- @data[xKey.to_s] = xValue
44
+ @data[xKey.to_s] = xValue.to_s
45
45
 
46
46
  if @graphviz.nil? == false
47
- @graphviz.set_position( @name, xKey.to_s, xValue )
47
+ @graphviz.set_position( @name, xKey.to_s, xValue.to_s )
48
48
  end
49
49
  end
50
50
  end
@@ -15,7 +15,7 @@
15
15
  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
16
 
17
17
  module Constants
18
- RGV_VERSION = "0.9.0"
18
+ RGV_VERSION = "0.9.1"
19
19
 
20
20
  ## Const: Output formats
21
21
  FORMATS = [
@@ -0,0 +1,93 @@
1
+ grammar Dot
2
+ rule graph
3
+ space type:("graph" / "digraph") blank name:string space cluster:cluster space <GraphViz::Parser::Graph>
4
+ end
5
+
6
+ rule cluster
7
+ "{" content:(preference / node / edge / subgraph)* "}" <GraphViz::Parser::Cluster>
8
+ end
9
+
10
+ rule preference
11
+ graph_preference / named_graph_preference / node_preference / edge_preference
12
+ end
13
+
14
+ rule graph_preference
15
+ space key:name blank "=" blank value:string separator <GraphViz::Parser::GraphPreference>
16
+ end
17
+
18
+ rule named_graph_preference
19
+ space "graph" blank options:options? separator <GraphViz::Parser::NamedGraphPreference>
20
+ end
21
+
22
+ rule node_preference
23
+ space "node" blank options:options? separator <GraphViz::Parser::NodePreference>
24
+ end
25
+
26
+ rule edge_preference
27
+ space "edge" blank options:options? separator <GraphViz::Parser::EdgePreference>
28
+ end
29
+
30
+ rule node
31
+ space name:string blank options:options? separator <GraphViz::Parser::Node>
32
+ end
33
+
34
+ rule edge
35
+ direct_edge / undirect_edge
36
+ end
37
+
38
+ rule direct_edge
39
+ space node_one:string blank "->" blank node_two:string blank other_nodes:("->" blank next_node:string blank)* options:options? separator <GraphViz::Parser::Edge>
40
+ end
41
+
42
+ rule undirect_edge
43
+ space node_one:string blank "--" blank node_two:string blank other_nodes:("--" blank next_node:string blank)* options:options? separator <GraphViz::Parser::Edge>
44
+ end
45
+
46
+ rule subgraph
47
+ space "subgraph" blank name:string space cluster:cluster space <GraphViz::Parser::Subgraph>
48
+ end
49
+
50
+ rule options
51
+ "[" space (name blank "=" blank string comma space)* name blank "=" blank string space "]" <GraphViz::Parser::Options>
52
+ end
53
+
54
+ rule string
55
+ name / quoted_string
56
+ end
57
+
58
+ rule comma
59
+ space ","
60
+ end
61
+
62
+ rule separator
63
+ blank ";" space
64
+ end
65
+
66
+ rule quoted_string
67
+ '"' [^"]* '"'
68
+ end
69
+
70
+ rule name
71
+ [a-zA-Z0-9_\.]+
72
+ end
73
+
74
+ rule blank
75
+ [\ \t]*
76
+ end
77
+
78
+ rule space
79
+ [\s\n\r\t]*
80
+ end
81
+
82
+ rule newline
83
+ CR LF
84
+ end
85
+
86
+ rule CR
87
+ [\r]?
88
+ end
89
+
90
+ rule LF
91
+ [\n]?
92
+ end
93
+ end
@@ -55,6 +55,7 @@ class GraphViz
55
55
  # Set value +xAttrValue+ to the edge attribut +xAttrName+
56
56
  #
57
57
  def []=( xAttrName, xAttrValue )
58
+ xAttrValue = xAttrValue.to_s if xAttrValue.class == Symbol
58
59
  @oAttrEdge[xAttrName.to_s] = xAttrValue
59
60
  end
60
61
 
@@ -65,6 +66,11 @@ class GraphViz
65
66
  @oAttrEdge[xAttrName.to_s].clone
66
67
  end
67
68
 
69
+ def <<( oNode ) #:nodoc:
70
+ n = @oGParrent.get_node(@xNodeTwo)
71
+
72
+ GraphViz::commonGraph( oNode, n ).add_edge( n, oNode )
73
+ end
68
74
  #
69
75
  # Set edge attributs
70
76
  #
@@ -94,7 +100,13 @@ class GraphViz
94
100
  xLink = " -- "
95
101
  end
96
102
 
97
- xOut = @xNodeOne + xLink + @xNodeTwo
103
+ xNodeNameOne = @xNodeOne.clone
104
+ xNodeNameOne = '"' << xNodeNameOne << '"' if xNodeNameOne.match( /^[a-zA-Z_]+[a-zA-Z0-9_\.]*$/ ).nil?
105
+
106
+ xNodeNameTwo = @xNodeTwo.clone
107
+ xNodeNameTwo = '"' << xNodeNameTwo << '"' if xNodeNameTwo.match( /^[a-zA-Z_]+[a-zA-Z0-9_\.]*$/ ).nil?
108
+
109
+ xOut = xNodeNameOne + xLink + xNodeNameTwo
98
110
  xAttr = ""
99
111
  xSeparator = ""
100
112
  @oAttrEdge.data.each do |k, v|
@@ -48,6 +48,7 @@ class GraphViz
48
48
  # Set value +xAttrValue+ to the node attribut +xAttrName+
49
49
  #
50
50
  def []=( xAttrName, xAttrValue )
51
+ xAttrValue = xAttrValue.to_s if xAttrValue.class == Symbol
51
52
  @oAttrNode[xAttrName.to_s] = xAttrValue
52
53
  end
53
54
 
@@ -99,7 +100,10 @@ class GraphViz
99
100
  end
100
101
 
101
102
  def output #:nodoc:
102
- xOut = "" << @xNodeName.clone
103
+ xNodeName = @xNodeName.clone
104
+ xNodeName = '"' << xNodeName << '"' if xNodeName.match( /^[a-zA-Z_]+[a-zA-Z0-9_\.]*$/ ).nil?
105
+
106
+ xOut = "" << xNodeName
103
107
  xAttr = ""
104
108
  xSeparator = ""
105
109
  @oAttrNode.data.each do |k, v|
@@ -0,0 +1,244 @@
1
+ require 'rubygems'
2
+ require 'treetop'
3
+
4
+ Treetop.load File.dirname(__FILE__) + '/dot.treetop'
5
+
6
+ class GraphViz
7
+ class Parser
8
+
9
+ class Context
10
+ def initialize
11
+ @graph = nil
12
+ @nodes = {}
13
+ @edges = []
14
+ @options = {
15
+ :node => {},
16
+ :edge => {}
17
+ }
18
+ end
19
+
20
+ def graph
21
+ @graph
22
+ end
23
+
24
+ def graph=(g)
25
+ @graph = g
26
+ end
27
+
28
+ def nodes
29
+ @nodes
30
+ end
31
+
32
+ def edges
33
+ @edges
34
+ end
35
+
36
+ def options
37
+ @options
38
+ end
39
+
40
+ def options=(o)
41
+ @options = o
42
+ end
43
+ end
44
+
45
+ class Graph < Treetop::Runtime::SyntaxNode
46
+ def eval( context, hOpts )
47
+ puts "GRAPH TYPE = #{type.text_value}"
48
+ puts "GRAPH NAME = #{name.text_value}"
49
+
50
+ hOpts = hOpts[0].merge( {:type => type.text_value} )
51
+
52
+ # Create Graph
53
+ context.graph = GraphViz.new( name.text_value.gsub(/"/, ""), hOpts )
54
+
55
+ # Eval cluster
56
+ cluster.eval( context )
57
+
58
+ return context.graph
59
+ end
60
+ end
61
+
62
+ class Cluster < Treetop::Runtime::SyntaxNode
63
+ def eval( context )
64
+ content.elements.each do |e|
65
+ e.eval( context )
66
+ end
67
+ end
68
+ end
69
+
70
+ class GraphPreference < Treetop::Runtime::SyntaxNode
71
+ def eval( context )
72
+ puts "GRAPH PREFERENCE : "
73
+ puts " #{key.text_value} = #{value.text_value.gsub(/"/, "")}"
74
+ context.graph[key.text_value] = value.text_value.gsub(/"/, "")
75
+ end
76
+ end
77
+
78
+ class NamedGraphPreference < Treetop::Runtime::SyntaxNode
79
+ def eval( context )
80
+ puts "GRAPH PREFERENCES :"
81
+ options.eval().each do |k,v|
82
+ context.graph[k] = v
83
+ end
84
+ end
85
+ end
86
+
87
+ class NodePreference < Treetop::Runtime::SyntaxNode
88
+ def eval( context )
89
+ puts "NODE PREFERENCES :"
90
+ context.options[:node] = context.options[:node].merge( options.eval() )
91
+ end
92
+ end
93
+
94
+ class EdgePreference < Treetop::Runtime::SyntaxNode
95
+ def eval( context )
96
+ puts "EDGE PREFERENCES :"
97
+ context.options[:edge] = context.options[:edge].merge( options.eval() )
98
+ end
99
+ end
100
+
101
+ class Node < Treetop::Runtime::SyntaxNode
102
+ def eval( context )
103
+ node_name = name.text_value.gsub( /"/, "" )
104
+ puts "NODE NAME = #{node_name}"
105
+ puts "OPTIONS = "
106
+
107
+ # Create node
108
+ node = context.nodes[node_name] || context.graph.add_node( node_name )
109
+
110
+ # Add global options
111
+ context.options[:node].each do |k, v|
112
+ node[k] = v
113
+ end
114
+
115
+ # Add custom options
116
+ unless options.terminal?
117
+ options.eval().each do |k, v|
118
+ node[k] = v
119
+ end
120
+ end
121
+
122
+ # Save node
123
+ context.nodes[node_name] = node
124
+ end
125
+ end
126
+
127
+ class Edge < Treetop::Runtime::SyntaxNode
128
+ def create_node( name, context )
129
+ puts " NEED TO CREATE NODE : #{name}"
130
+ # Create the node
131
+ node = context.graph.add_node( name )
132
+
133
+ # Add global options
134
+ context.options[:node].each do |k, v|
135
+ node[k] = v
136
+ end
137
+
138
+ # Save node
139
+ context.nodes[name] = node
140
+ end
141
+
142
+ def create_edge( one, two, edge_options, context )
143
+ # Create edge
144
+ edge = context.graph.add_edge( one, two )
145
+
146
+ # Add global options
147
+ context.options[:edge].each do |k, v|
148
+ edge[k] = v
149
+ end
150
+
151
+ # Add custom options
152
+ edge_options.each do |k, v|
153
+ edge[k] = v
154
+ end
155
+
156
+ # Save edge
157
+ context.edges << edge
158
+ end
159
+
160
+ def eval( context )
161
+ one_name = node_one.text_value.gsub( /"/, "" )
162
+ two_name = node_two.text_value.gsub( /"/, "" )
163
+ puts "EDGE"
164
+ puts "NODE ONE = #{one_name}"
165
+ puts "NODE TWO = #{two_name}"
166
+ puts "OPTIONS = "
167
+
168
+ # Get or create node one
169
+ one = context.nodes[one_name] || create_node( one_name, context )
170
+
171
+ # Get or create node two
172
+ two = context.nodes[two_name] || create_node( two_name, context )
173
+
174
+ # Get options
175
+ edge_options = {}
176
+ edge_options = options.eval() unless options.terminal?
177
+
178
+ # Create edge
179
+ create_edge( one, two, edge_options, context )
180
+
181
+ last_node = two
182
+ other_nodes.elements.each do |e|
183
+ new_node_name = e.next_node.text_value.gsub( /"/, "" )
184
+ puts "OTHER NODE : #{new_node_name}"
185
+
186
+ new_node = context.nodes[new_node_name] || create_node( new_node_name, context )
187
+ create_edge( last_node, new_node, edge_options, context )
188
+
189
+ last_node = new_node
190
+ end
191
+ end
192
+ end
193
+
194
+ class Subgraph < Treetop::Runtime::SyntaxNode
195
+ def eval( context )
196
+ puts "CREATE SUBGRAPH : #{name.text_value}"
197
+
198
+ # Save options
199
+ saved_options = context.options.clone
200
+ # Save graph
201
+ saved_graph = context.graph
202
+
203
+ # Create Graph
204
+ context.graph = context.graph.add_graph( name.text_value.gsub(/"/, "") )
205
+ #context.options = {
206
+ # :node => {},
207
+ # :edge => {}
208
+ #}
209
+
210
+ # Eval cluster
211
+ cluster.eval( context )
212
+
213
+ # Reinitialize graph and options
214
+ context.graph = saved_graph
215
+ context.options = saved_options
216
+ end
217
+ end
218
+
219
+ class Options < Treetop::Runtime::SyntaxNode
220
+ def eval
221
+ options = {}
222
+ elements[2].elements.each do |e|
223
+ puts " #{e.elements[0].text_value} = #{e.elements[4].text_value}"
224
+ options[e.elements[0].text_value] = e.elements[4].text_value.gsub( /"/, "" )
225
+ end
226
+ puts " #{elements[3].text_value} = #{elements[7].text_value}"
227
+ options[elements[3].text_value] = elements[7].text_value.gsub( /"/, "" )
228
+
229
+ return options
230
+ end
231
+ end
232
+
233
+ def self.parse( file, *hOpts, &block )
234
+ dot = open(file).read
235
+ parser = DotParser.new()
236
+ tree = parser.parse( dot )
237
+ graph = tree.eval( GraphViz::Parser::Context.new(), hOpts )
238
+
239
+ yield( graph ) if( block and graph.nil? == false )
240
+
241
+ return graph
242
+ end
243
+ end
244
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-graphviz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregoire Lejeune
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-12 00:00:00 +02:00
12
+ date: 2009-10-10 00:00:00 +02:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: treetop
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
16
25
  description: Ruby/Graphviz provides an interface to layout and generate images of directed graphs in a variety of formats (PostScript, PNG, etc.) using GraphViz.
17
26
  email: gregoire.lejeune@free.fr
18
27
  executables:
@@ -20,18 +29,35 @@ executables:
20
29
  extensions: []
21
30
 
22
31
  extra_rdoc_files:
23
- - README
32
+ - README.rdoc
24
33
  - ChangeLog
25
34
  - COPYING
26
35
  - AUTHORS
27
36
  files:
28
37
  - ChangeLog
29
38
  - COPYING
30
- - README
39
+ - README.rdoc
31
40
  - AUTHORS
32
41
  - setup.rb
33
42
  - bin/ruby2gv
34
43
  - examples/arrowhead.rb
44
+ - examples/dot/cluster.dot
45
+ - examples/dot/fsm.dot
46
+ - examples/dot/genetic.dot
47
+ - examples/dot/hello.dot
48
+ - examples/dot/hello_test.rb
49
+ - examples/dot/lion_share.dot
50
+ - examples/dot/prof.dot
51
+ - examples/dot/psg.dot
52
+ - examples/dot/sdh.dot
53
+ - examples/dot/siblings.dot
54
+ - examples/dot/test.dot
55
+ - examples/dot/unix.dot
56
+ - examples/graphviz.org/cluster.rb
57
+ - examples/graphviz.org/hello_world.rb
58
+ - examples/graphviz.org/lion_share.rb
59
+ - examples/graphviz.org/process.rb
60
+ - examples/graphviz.org/TrafficLights.rb
35
61
  - examples/HTML-Labels.rb
36
62
  - examples/maketest.sh
37
63
  - examples/p2p.rb
@@ -40,6 +66,7 @@ files:
40
66
  - examples/sample03.rb
41
67
  - examples/sample04.rb
42
68
  - examples/sample05.rb
69
+ - examples/sample06.rb
43
70
  - examples/sample07.rb
44
71
  - examples/sample08.rb
45
72
  - examples/sample09.rb
@@ -54,25 +81,30 @@ files:
54
81
  - examples/sample18.rb
55
82
  - examples/sample19.rb
56
83
  - examples/sample20.rb
84
+ - examples/sample21.rb
85
+ - examples/sample22.rb
57
86
  - examples/shapes.rb
58
87
  - examples/test.xml
59
88
  - examples/testorder.rb
60
89
  - examples/testxml.rb
61
- - lib/graphviz
62
90
  - lib/graphviz/attrs.rb
63
91
  - lib/graphviz/constants.rb
92
+ - lib/graphviz/dot.treetop
64
93
  - lib/graphviz/edge.rb
65
94
  - lib/graphviz/node.rb
95
+ - lib/graphviz/parser.rb
66
96
  - lib/graphviz/xml.rb
67
97
  - lib/graphviz.rb
68
98
  has_rdoc: true
69
99
  homepage: http://raa.ruby-lang.org/project/ruby-graphviz/
100
+ licenses: []
101
+
70
102
  post_install_message:
71
103
  rdoc_options:
72
104
  - --title
73
105
  - Ruby/GraphViz
74
106
  - --main
75
- - README
107
+ - README.rdoc
76
108
  - --line-numbers
77
109
  require_paths:
78
110
  - lib
@@ -91,9 +123,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
123
  requirements: []
92
124
 
93
125
  rubyforge_project: ruby-asp
94
- rubygems_version: 1.1.1
126
+ rubygems_version: 1.3.5
95
127
  signing_key:
96
- specification_version: 2
128
+ specification_version: 3
97
129
  summary: Interface to the GraphViz graphing tool
98
130
  test_files: []
99
131