Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add GraphViz#root_graph, Node#root_graph, Edge#root_graph

  • Loading branch information...
commit abbfe4182902bb63fa5144b372891a45e6d9b7c3 1 parent b8d10d2
@glejeune authored
View
3  README.rdoc
@@ -25,7 +25,8 @@ Interface to the GraphViz graphing tool
g = GraphViz.digraph(:G, :strict => true)
# or
g = GraphViz.strict_digraph(:G)
-
+* Add GraphViz#root_graph, Node#root_graph, Edge#root_graph
+* The GraphML parser now accept a graphml file or string
=== 0.9.18 :
* JRuby bug correction (by Nigel Thorne)
View
5 examples/sample57.rb
@@ -2,5 +2,6 @@
require 'graphviz/graphml'
-g = GraphViz::GraphML.new( "graphml/cluster.graphml" )
-g.graph.output( :path => "/usr/local/bin", :png => "#{$0}.png" )
+graphml = File.join( File.dirname( File.expand_path( __FILE__ ) ), "graphml", "cluster.graphml" )
+g = GraphViz::GraphML.new( graphml )
+g.graph.output( :png => "#{$0}.png" )
View
28 lib/graphviz.rb
@@ -600,6 +600,13 @@ def pg #:nodoc:
@oParentGraph
end
+ #
+ # Return the root graph
+ #
+ def root_graph
+ return( (self.pg.nil?) ? self : self.pg.root_graph )
+ end
+
def self.commonGraph( o1, o2 ) #:nodoc:
g1 = o1.pg
g2 = o2.pg
@@ -686,10 +693,10 @@ def self.parse( xFile, hOpts = {}, &block )
#
# Options :
# * :output : Output format (Constants::FORMATS) (default : dot)
- # * :file : Output file name (default : none)
+ # * :file : Output file name (default : nil)
# * :use : Program to use (Constants::PROGRAMS) (default : dot)
# * :path : Program PATH
- # * :parent : Parent graph (default : none)
+ # * :parent : Parent graph (default : nil)
# * :type : Graph type (Constants::GRAPHTYPE) (default : digraph)
# * :errors : DOT error level (default 1)
# * 0 = Error + Warning
@@ -760,12 +767,29 @@ def initialize( xGraphName, hOpts = {}, &block )
yield( self ) if( block )
end
+ #
+ # Create a new undirected graph
+ #
+ # See also GraphViz::new
+ #
def self.graph( xGraphName, hOpts = {}, &block )
new( xGraphName, hOpts.symbolize_keys.merge( {:type => "graph"} ), &block )
end
+
+ #
+ # Create a new directed graph
+ #
+ # See also GraphViz::new
+ #
def self.digraph( xGraphName, hOpts = {}, &block )
new( xGraphName, hOpts.symbolize_keys.merge( {:type => "digraph"} ), &block )
end
+
+ #
+ # Create a new strict directed graph
+ #
+ # See also GraphViz::new
+ #
def self.strict_digraph( xGraphName, hOpts = {}, &block )
new( xGraphName, hOpts.symbolize_keys.merge( {:type => "digraph", :strict => true} ), &block )
end
View
18 lib/graphviz/edge.rb
@@ -35,7 +35,7 @@ class Edge
# * vNodeTwo : Second node (can be a GraphViz::Node or a node ID)
# * oGParrent : Graph
#
- def initialize( vNodeOne, vNodeTwo, oGParrent = nil )
+ def initialize( vNodeOne, vNodeTwo, oGParrent )
@xNodeOne, @xNodeOnePort = getNodeNameAndPort( vNodeOne )
# if vNodeOne.class == String
# @xNodeOne = vNodeOne
@@ -64,7 +64,8 @@ def node_one( with_port = true )
GraphViz.escape(@xNodeOne, true) + ":#{@xNodeOnePort}"
end
end
-
+ alias :tail_node :node_one
+
# Return the node two as string (so with port if any)
def node_two( with_port = true )
if @xNodeTwoPort.nil? or with_port == false
@@ -73,7 +74,7 @@ def node_two( with_port = true )
GraphViz.escape(@xNodeTwo, true) + ":#{@xNodeTwoPort}"
end
end
-
+ alias :head_node :node_two
#
# Return the index of the edge
#
@@ -121,6 +122,17 @@ def <<( oNode ) #:nodoc:
alias :>> :<< #:nodoc:
#
+ # Return the root graph
+ #
+ def root_graph
+ return( (self.pg.nil?) ? nil : self.pg.root_graph )
+ end
+
+ def pg #:nodoc:
+ @oGParrent
+ end
+
+ #
# Set edge attributs
#
# Example :
View
5 lib/graphviz/graphml.rb
@@ -35,8 +35,9 @@ class GraphML
'undirected' => :graph
}
- def initialize( xFile )
- @xmlDoc = REXML::Document::new( File::new( xFile ) )
+ def initialize( file_or_str )
+ data = ((File.file?( file_or_str )) ? File::new(file_or_str) : file_or_str)
+ @xmlDoc = REXML::Document::new( data )
@attributs = {
:nodes => {},
:edges => {},
View
13 lib/graphviz/node.rb
@@ -31,7 +31,7 @@ class Node
# * xNodeName : ID of the node
# * oGParrent : Graph
#
- def initialize( xNodeName, oGParrent = nil )
+ def initialize( xNodeName, oGParrent )
@xNodeName = xNodeName
@oGParrent = oGParrent
@oAttrNode = GraphViz::Attrs::new( nil, "node", NODESATTRS )
@@ -54,7 +54,7 @@ def id
end
#
- # Return the index of the node
+ # Return the node index
#
def index
@index
@@ -62,7 +62,14 @@ def index
def index=(i) #:nodoc:
@index = i if @index == nil
end
-
+
+ #
+ # Return the root graph
+ #
+ def root_graph
+ return( (self.pg.nil?) ? nil : self.pg.root_graph )
+ end
+
#
# Set value +xAttrValue+ to the node attribut +xAttrName+
#
Please sign in to comment.
Something went wrong with that request. Please try again.