Class: GraphViz
- Inherits:
-
Object
- Object
- GraphViz
- Includes:
- Constants
- Defined in:
- lib/graphviz/attrs.rb,
lib/graphviz.rb,
lib/graphviz/xml.rb,
lib/graphviz/edge.rb,
lib/graphviz/node.rb,
lib/graphviz/parser.rb
Overview
Copyright © 2004, 2005, 2006, 2007, 2008 Gregoire Lejeune <[email protected]>
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Defined Under Namespace
Classes: Attrs, Edge, Node, Parser, XML
Constant Summary collapse
- @@format =
Var: Output format (dot, png, jpeg, …)
nil
- @@prog =
Var: program to use (dot|twopi)
"dot"
- @@path =
Var: program path
nil
- @@errors =
Var: Error level
1
Constants included from Constants
Constants::EDGESATTRS, Constants::FORMATS, Constants::GENCS_ATTRS, Constants::GRAPHSATTRS, Constants::GRAPHTYPE, Constants::NODESATTRS, Constants::PROGRAMS, Constants::RGV_VERSION
Instance Attribute Summary collapse
-
#edge ⇒ Object
This accessor allow you to set global edges attributs.
-
#node ⇒ Object
This accessor allow you to set global nodes attributs.
Class Method Summary collapse
-
.commonGraph(o1, o2) ⇒ Object
:nodoc:.
-
.default(hOpts) ⇒ Object
Change default options (:use, :path, :errors and :output).
- .options(hOpts) ⇒ Object
-
.parse(xFile, *hOpts, &block) ⇒ Object
Create a new graph from a GraphViz File.
Instance Method Summary collapse
-
#<<(oNode) ⇒ Object
(also: #>, #-, #>>)
Create an edge between the current cluster and the node or cluster
oNode
. -
#[](xAttrName) ⇒ Object
Get the value of the graph attribut
xAttrName
. -
#[]=(xAttrName, xValue) ⇒ Object
Set value
xValue
to the graph attributxAttrName
. -
#add_edge(oNodeOne, oNodeTwo, *hOpt) ⇒ Object
Create a new edge.
-
#add_graph(xGraphName, *hOpt) ⇒ Object
Create a new graph.
-
#add_node(xNodeName, *hOpt) ⇒ Object
Create a new node.
-
#edge_count ⇒ Object
Get the number of edges.
-
#get_graph(xGraphName) {|graph| ... } ⇒ Object
Return the graph object for the given name (or nil).
-
#get_node(xNodeName) {|node| ... } ⇒ Object
Return the node object for the given name (or nil).
-
#method_missing(idName, *args, &block) ⇒ Object
:nodoc:.
-
#name ⇒ Object
Get the graph name.
-
#node_count ⇒ Object
Get the number of nodes.
-
#output(*hOpt) ⇒ Object
(also: #save)
Generate the graph.
-
#output_and_errors_from_command(cmd) ⇒ Object
:nodoc:.
-
#output_from_command(cmd) ⇒ Object
:nodoc:.
-
#pg ⇒ Object
:nodoc:.
-
#set_position(xType, xKey, xValue) ⇒ Object
:nodoc:.
Methods included from Constants
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(idName, *args, &block) ⇒ Object
:nodoc:
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/graphviz.rb', line 192 def method_missing( idName, *args, &block ) #:nodoc: xName = idName.id2name rCod = nil if block # Creating a cluster named '#{xName}' rCod = add_graph( xName, args[0] ) yield( rCod ) else # Create a node named '#{xName}' or search for a node, edge or cluster if @hoNodes.keys.include?( xName ) if( args[0] ) return "#{xName}:#{args[0].to_s}" else return( @hoNodes[xName] ) end end return( @hoGraphs[xName] ) if @hoGraphs.keys.include?( xName ) rCod = add_node( xName, args[0] ) end return rCod end |
Instance Attribute Details
#edge ⇒ Object
This accessor allow you to set global edges attributs
60 61 62 |
# File 'lib/graphviz.rb', line 60 def edge @edge end |
#node ⇒ Object
This accessor allow you to set global nodes attributs
57 58 59 |
# File 'lib/graphviz.rb', line 57 def node @node end |
Class Method Details
.commonGraph(o1, o2) ⇒ Object
:nodoc:
481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/graphviz.rb', line 481 def self.commonGraph( o1, o2 ) #:nodoc: g1 = o1.pg g2 = o2.pg return o1 if g1.nil? return o2 if g2.nil? return g1 if g1.object_id == g2.object_id return GraphViz::commonGraph( g1, g2 ) end |
.default(hOpts) ⇒ Object
Change default options (:use, :path, :errors and :output)
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
# File 'lib/graphviz.rb', line 506 def self.default( hOpts ) hOpts.each do |k, v| case k.to_s when "use" @@prog = v when "path" @@path = v when "errors" @@errors = v when "output" warn ":output option is deprecated!" @@format = v else warn "Invalide option #{k}!" end end end |
.options(hOpts) ⇒ Object
524 525 526 |
# File 'lib/graphviz.rb', line 524 def self.( hOpts ) GraphViz::default( hOpts ) end |
.parse(xFile, *hOpts, &block) ⇒ Object
Create a new graph from a GraphViz File
Options :
-
:output : Output format (Constants::FORMATS) (default : dot)
-
:file : Output file name (default : none)
-
:use : Program to use (Constants::PROGRAMS) (default : dot)
-
:path : Program PATH
-
:parent : Parent graph (default : none)
-
:type : Graph type (Constants::GRAPHTYPE) (default : digraph)
541 542 543 544 |
# File 'lib/graphviz.rb', line 541 def self.parse( xFile, *hOpts, &block ) g = GraphViz::Parser.parse( xFile, hOpts[0], &block ) return g end |
Instance Method Details
#<<(oNode) ⇒ Object Also known as: >, -, >>
Create an edge between the current cluster and the node or cluster oNode
462 463 464 465 466 467 468 469 470 471 472 |
# File 'lib/graphviz.rb', line 462 def <<( oNode ) raise( ArgumentError, "Edge between root graph and node or cluster not allowed!" ) if self.pg.nil? if( oNode.class == Array ) oNode.each do |no| self << no end else return GraphViz::commonGraph( oNode, self ).add_edge( self, oNode ) end end |
#[](xAttrName) ⇒ Object
Get the value of the graph attribut xAttrName
229 230 231 |
# File 'lib/graphviz.rb', line 229 def []( xAttrName ) return( @graph[xAttrName].clone ) end |
#[]=(xAttrName, xValue) ⇒ Object
Set value xValue
to the graph attribut xAttrName
221 222 223 224 |
# File 'lib/graphviz.rb', line 221 def []=( xAttrName, xValue ) xValue = xValue.to_s if xValue.class == Symbol @graph[xAttrName] = xValue end |
#add_edge(oNodeOne, oNodeTwo, *hOpt) ⇒ Object
Create a new edge
In:
-
oNodeOne : First node (or node list)
-
oNodeTwo : Second Node (or node list)
-
*hOpt : Edge attributs
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/graphviz.rb', line 110 def add_edge( oNodeOne, oNodeTwo, *hOpt ) if( oNodeOne.class == Array ) oNodeOne.each do |no| add_edge( no, oNodeTwo, *hOpt ) end else if( oNodeTwo.class == Array ) oNodeTwo.each do |nt| add_edge( oNodeOne, nt, *hOpt ) end else oEdge = GraphViz::Edge::new( oNodeOne, oNodeTwo, self ) if hOpt.nil? == false and hOpt[0].nil? == false hOpt[0].each do |xKey, xValue| oEdge[xKey.to_s] = xValue end end @elements_order.push( { "type" => "edge", "value" => oEdge } ) @loEdges.push( oEdge ) return( oEdge ) end end end |
#add_graph(xGraphName, *hOpt) ⇒ Object
Create a new graph
In:
-
xGraphName : Graph name
-
*hOpt : Graph attributs
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/graphviz.rb', line 149 def add_graph( xGraphName, *hOpt ) @hoGraphs[xGraphName] = GraphViz::new( xGraphName, :parent => self, :type => @oGraphType ) if hOpt.nil? == false and hOpt[0].nil? == false hOpt[0].each do |xKey, xValue| @hoGraphs[xGraphName][xKey.to_s] = xValue end end @elements_order.push( { "type" => "graph", "name" => xGraphName, "value" => @hoGraphs[xGraphName] } ) return( @hoGraphs[xGraphName] ) end |
#add_node(xNodeName, *hOpt) ⇒ Object
Create a new node
In:
-
xNodeName : Name of the new node
-
*hOpt : Node attributs
Return the GraphViz::Node object created
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/graphviz.rb', line 73 def add_node( xNodeName, *hOpt ) @hoNodes[xNodeName] = GraphViz::Node::new( xNodeName, self ) if hOpt.nil? == false and hOpt[0].nil? == false hOpt[0].each do |xKey, xValue| @hoNodes[xNodeName][xKey.to_s] = xValue end end @elements_order.push( { "type" => "node", "name" => xNodeName, "value" => @hoNodes[xNodeName] } ) return( @hoNodes[xNodeName] ) end |
#edge_count ⇒ Object
Get the number of edges
188 189 190 |
# File 'lib/graphviz.rb', line 188 def edge_count @loEdges.size end |
#get_graph(xGraphName) {|graph| ... } ⇒ Object
Return the graph object for the given name (or nil)
170 171 172 173 174 175 176 |
# File 'lib/graphviz.rb', line 170 def get_graph( xGraphName, &block ) graph = @hoGraphs[xGraphName] || nil yield( graph ) if( block and graph.nil? == false ) return graph end |
#get_node(xNodeName) {|node| ... } ⇒ Object
Return the node object for the given name (or nil)
94 95 96 97 98 99 100 |
# File 'lib/graphviz.rb', line 94 def get_node( xNodeName, &block ) node = @hoNodes[xNodeName] || nil yield( node ) if( block and node.nil? == false ) return node end |
#name ⇒ Object
Get the graph name
455 456 457 |
# File 'lib/graphviz.rb', line 455 def name @name.clone end |
#node_count ⇒ Object
Get the number of nodes
181 182 183 |
# File 'lib/graphviz.rb', line 181 def node_count @hoNodes.size end |
#output(*hOpt) ⇒ Object Also known as: save
Generate the graph
Options :
-
:output : Output format (Constants::FORMATS)
-
:file : Output file name
-
:use : Program to use (Constants::PROGRAMS)
-
:path : Program PATH
-
:<format> => <file> : <file> can be
-
a file name
-
nil, then the output will be printed to STDOUT
-
String, then the output will be returned as a String
-
-
:errors : DOT error level (default 1)
-
0 = Error + Warning
-
1 = Error
-
2 = none
-
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
# File 'lib/graphviz.rb', line 250 def output( *hOpt ) xDOTScript = "" xLastType = nil xSeparator = "" xData = "" @elements_order.each { |kElement| if xLastType.nil? == true or xLastType != kElement["type"] if xData.length > 0 case xLastType when "graph_attr" xDOTScript << " " + xData + ";\n" when "node_attr" xDOTScript << " node [" + xData + "];\n" when "edge_attr" xDOTScript << " edge [" + xData + "];\n" end end xSeparator = "" xData = "" end xLastType = kElement["type"] #Modified by #Brandon Coleman #verify value is NOT NULL if kElement["value"] == nil then raise ArgumentError, "#{kElement["name"]} has a nil value!" end case kElement["type"] when "graph_attr" xData << xSeparator + kElement["name"] + " = \"" + kElement["value"] + "\"" xSeparator = "; " when "node_attr" xData << xSeparator + kElement["name"] + " = \"" + kElement["value"] + "\"" xSeparator = ", " when "edge_attr" xData << xSeparator + kElement["name"] + " = \"" + kElement["value"] + "\"" xSeparator = ", " when "node" xDOTScript << " " + kElement["value"].output() + "\n" when "edge" xDOTScript << " " + kElement["value"].output( @oGraphType ) + "\n" when "graph" xDOTScript << kElement["value"].output() + "\n" else raise ArgumentError, "Don't know what to do with element type '#{kElement['type']}'" end } if xData.length > 0 case xLastType when "graph_attr" xDOTScript << " " + xData + ";\n" when "node_attr" xDOTScript << " node [" + xData + "];\n" when "edge_attr" xDOTScript << " edge [" + xData + "];\n" end end xDOTScript << "}" if @oParentGraph.nil? == false xDOTScript = "subgraph #{@name} {\n" << xDOTScript return( xDOTScript ) else if hOpt.nil? == false and hOpt[0].nil? == false hOpt[0].each do |xKey, xValue| xValue = xValue.to_s unless xValue.nil? or [Class, TrueClass, FalseClass].include?(xValue.class) case xKey.to_s when "output" warn ":output option is deprecated, please use :<format> => :<file>" if FORMATS.index( xValue ).nil? == true raise ArgumentError, "output format '#{xValue}' invalid" end @format = xValue when "file" warn ":file option is deprecated, please use :<format> => :<file>" @filename = xValue when "use" if PROGRAMS.index( xValue ).nil? == true raise ArgumentError, "can't use '#{xValue}'" end @prog = xValue when "path" @path = xValue when "errors" @errors = xValue else if FORMATS.index( xKey.to_s ).nil? == true raise ArgumentError, "output format '#{xValue}' invalid" end @output[xKey.to_s] = xValue end end end xDOTScript = "#{@oGraphType} #{@name} {\n" << xDOTScript xOutputString = false xOutput = if @format != "none" ## Act: Save script and send it to dot t = if /Windows/.match( ENV['OS'] ) Tempfile::open( File.basename($0), "." ) else Tempfile::open( File.basename($0) ) end t.print( xDOTScript ) t.close cmd = find_executable( ) if cmd == nil raise StandardError, "GraphViz not installed or #{@prog} not in PATH. Install GraphViz or use the 'path' option" end xOutputWithFile = "" xOutputWithoutFile = "" unless @format.nil? if @filename.nil? xOutputWithoutFile = "-T#{@format} " elsif @filename == String xOutputWithoutFile = "-T#{@format} " xOutputString = true else xOutputWithFile = "-T#{@format} -o#{@filename} " end end @output.each do |format, file| if file.nil? xOutputWithoutFile << "-T#{format} " elsif file == String xOutputWithoutFile << "-T#{format} " xOutputString = true else xOutputWithFile << "-T#{format} -o#{file} " end end #xCmd = "#{cmd} #{xOutputWithFile} #{xOutputWithoutFile} #{t.path}" #if /Windows/.match( ENV['OS'] ) xCmd = "\"#{cmd}\" -q#{@errors} #{xOutputWithFile} #{xOutputWithoutFile} #{t.path}" #end output_from_command( xCmd ) else xDOTScript end if xOutputString xOutput else print xOutput end end end |
#output_and_errors_from_command(cmd) ⇒ Object
:nodoc:
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
# File 'lib/graphviz.rb', line 423 def output_and_errors_from_command(cmd) #:nodoc: unless defined? Open3 begin require 'open3' require 'win32/open3' rescue LoadError end end begin Open3.popen3( cmd ) do |stdin, stdout, stderr| stdin.close [stdout.read, stderr.read] end rescue NotImplementedError, NoMethodError IO.popen( cmd ) do |stdout| [stdout.read, nil] end end end |
#output_from_command(cmd) ⇒ Object
:nodoc:
443 444 445 446 447 448 449 450 |
# File 'lib/graphviz.rb', line 443 def output_from_command(cmd) #:nodoc: output, errors = output_and_errors_from_command(cmd) if errors.nil? || errors.strip.empty? output else raise "Error from #{cmd}:\n#{errors}" end end |
#pg ⇒ Object
:nodoc:
477 478 479 |
# File 'lib/graphviz.rb', line 477 def pg #:nodoc: @oParentGraph end |
#set_position(xType, xKey, xValue) ⇒ Object
:nodoc:
493 494 495 496 497 498 499 |
# File 'lib/graphviz.rb', line 493 def set_position( xType, xKey, xValue ) #:nodoc: @elements_order.push( { "type" => "#{xType}_attr", "name" => xKey, "value" => xValue } ) end |