Skip to content
This repository
Browse code

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

  • Loading branch information...
commit abbfe4182902bb63fa5144b372891a45e6d9b7c3 1 parent b8d10d2
Gregoire Lejeune authored October 30, 2010
3  README.rdoc
Source Rendered
@@ -25,7 +25,8 @@ Interface to the GraphViz graphing tool
25 25
     g = GraphViz.digraph(:G, :strict => true)
26 26
     # or 
27 27
     g = GraphViz.strict_digraph(:G)
28  
-
  28
+* Add GraphViz#root_graph, Node#root_graph, Edge#root_graph
  29
+* The GraphML parser now accept a graphml file or string
29 30
 
30 31
 === 0.9.18 :
31 32
 * JRuby bug correction (by Nigel Thorne)
5  examples/sample57.rb
@@ -2,5 +2,6 @@
2 2
 
3 3
 require 'graphviz/graphml'
4 4
 
5  
-g = GraphViz::GraphML.new( "graphml/cluster.graphml" )
6  
-g.graph.output( :path => "/usr/local/bin", :png => "#{$0}.png" )
  5
+graphml = File.join( File.dirname( File.expand_path( __FILE__ ) ), "graphml", "cluster.graphml" )
  6
+g = GraphViz::GraphML.new( graphml )
  7
+g.graph.output( :png => "#{$0}.png" )
28  lib/graphviz.rb
@@ -600,6 +600,13 @@ def pg #:nodoc:
600 600
     @oParentGraph
601 601
   end
602 602
   
  603
+  #
  604
+  # Return the root graph
  605
+  #
  606
+  def root_graph
  607
+    return( (self.pg.nil?) ? self : self.pg.root_graph )
  608
+  end
  609
+  
603 610
   def self.commonGraph( o1, o2 ) #:nodoc:
604 611
     g1 = o1.pg
605 612
     g2 = o2.pg
@@ -686,10 +693,10 @@ def self.parse( xFile, hOpts = {}, &block )
686 693
   # 
687 694
   # Options :
688 695
   # * :output : Output format (Constants::FORMATS) (default : dot)
689  
-  # * :file : Output file name (default : none)
  696
+  # * :file : Output file name (default : nil)
690 697
   # * :use : Program to use (Constants::PROGRAMS) (default : dot)
691 698
   # * :path : Program PATH
692  
-  # * :parent : Parent graph (default : none)
  699
+  # * :parent : Parent graph (default : nil)
693 700
   # * :type : Graph type (Constants::GRAPHTYPE) (default : digraph)
694 701
   # * :errors : DOT error level (default 1)
695 702
   #   * 0 = Error + Warning
@@ -760,12 +767,29 @@ def initialize( xGraphName, hOpts = {}, &block )
760 767
     yield( self ) if( block )
761 768
   end
762 769
   
  770
+  #
  771
+  # Create a new undirected graph
  772
+  #
  773
+  # See also GraphViz::new
  774
+  #
763 775
   def self.graph( xGraphName, hOpts = {}, &block )
764 776
     new( xGraphName, hOpts.symbolize_keys.merge( {:type => "graph"} ), &block )
765 777
   end
  778
+
  779
+  #
  780
+  # Create a new directed graph
  781
+  #
  782
+  # See also GraphViz::new
  783
+  #
766 784
   def self.digraph( xGraphName, hOpts = {}, &block )
767 785
     new( xGraphName, hOpts.symbolize_keys.merge( {:type => "digraph"} ), &block )
768 786
   end
  787
+  
  788
+  #
  789
+  # Create a new strict directed graph
  790
+  #
  791
+  # See also GraphViz::new
  792
+  #
769 793
   def self.strict_digraph( xGraphName, hOpts = {}, &block )
770 794
     new( xGraphName, hOpts.symbolize_keys.merge( {:type => "digraph", :strict => true} ), &block )
771 795
   end
18  lib/graphviz/edge.rb
@@ -35,7 +35,7 @@ class Edge
35 35
     # * vNodeTwo : Second node (can be a GraphViz::Node or a node ID)
36 36
     # * oGParrent : Graph 
37 37
     #
38  
-    def initialize( vNodeOne, vNodeTwo, oGParrent = nil )
  38
+    def initialize( vNodeOne, vNodeTwo, oGParrent )
39 39
       @xNodeOne, @xNodeOnePort = getNodeNameAndPort( vNodeOne )
40 40
 	    # if vNodeOne.class == String
41 41
       #   @xNodeOne = vNodeOne
@@ -64,7 +64,8 @@ def node_one( with_port = true )
64 64
         GraphViz.escape(@xNodeOne, true) + ":#{@xNodeOnePort}"
65 65
       end
66 66
     end
67  
-
  67
+    alias :tail_node :node_one
  68
+    
68 69
     # Return the node two as string (so with port if any)
69 70
     def node_two( with_port = true )
70 71
       if @xNodeTwoPort.nil? or with_port == false
@@ -73,7 +74,7 @@ def node_two( with_port = true )
73 74
 	      GraphViz.escape(@xNodeTwo, true) + ":#{@xNodeTwoPort}"
74 75
       end
75 76
     end
76  
-    
  77
+    alias :head_node :node_two
77 78
     #
78 79
 	  # Return the index of the edge
79 80
 	  #
@@ -121,6 +122,17 @@ def <<( oNode ) #:nodoc:
121 122
     alias :>> :<< #:nodoc:
122 123
     
123 124
     #
  125
+    # Return the root graph
  126
+    #
  127
+    def root_graph
  128
+      return( (self.pg.nil?) ? nil : self.pg.root_graph )
  129
+    end
  130
+    
  131
+    def pg #:nodoc:
  132
+      @oGParrent
  133
+    end
  134
+    
  135
+    #
124 136
     # Set edge attributs
125 137
     #
126 138
     # Example :
5  lib/graphviz/graphml.rb
@@ -35,8 +35,9 @@ class GraphML
35 35
       'undirected' => :graph
36 36
     }
37 37
     
38  
-    def initialize( xFile )
39  
-      @xmlDoc = REXML::Document::new( File::new( xFile ) )
  38
+    def initialize( file_or_str )
  39
+      data = ((File.file?( file_or_str )) ? File::new(file_or_str) : file_or_str) 
  40
+      @xmlDoc = REXML::Document::new( data )
40 41
       @attributs = {
41 42
         :nodes => {},
42 43
         :edges => {},
13  lib/graphviz/node.rb
@@ -31,7 +31,7 @@ class Node
31 31
     # * xNodeName : ID of the node
32 32
     # * oGParrent : Graph 
33 33
     # 
34  
-    def initialize( xNodeName, oGParrent = nil )
  34
+    def initialize( xNodeName, oGParrent )
35 35
       @xNodeName = xNodeName
36 36
       @oGParrent = oGParrent
37 37
       @oAttrNode = GraphViz::Attrs::new( nil, "node", NODESATTRS )
@@ -54,7 +54,7 @@ def id
54 54
 	  end
55 55
 	  
56 56
 	  #
57  
-	  # Return the index of the node
  57
+	  # Return the node index
58 58
 	  #
59 59
 	  def index
60 60
 	    @index
@@ -62,7 +62,14 @@ def index
62 62
     def index=(i) #:nodoc:
63 63
       @index = i if @index == nil
64 64
     end
65  
-	
  65
+
  66
+    #
  67
+    # Return the root graph
  68
+    #
  69
+    def root_graph
  70
+      return( (self.pg.nil?) ? nil : self.pg.root_graph )
  71
+    end
  72
+    
66 73
 	  # 
67 74
     # Set value +xAttrValue+ to the node attribut +xAttrName+
68 75
     # 

0 notes on commit abbfe41

Please sign in to comment.
Something went wrong with that request. Please try again.