/vendor/plugins/ruby/1.8/gems/ruby-graphviz-0.9.21/lib/graphviz/node.rb

https://github.com/akiko74/sakanakana · Ruby · 183 lines · 99 code · 19 blank · 65 comment · 12 complexity · 09793cf32d88c84b2325bcbe5a565169 MD5 · raw file

  1. # Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Gregoire Lejeune <gregoire.lejeune@free.fr>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. require 'graphviz/attrs'
  17. require 'graphviz/constants'
  18. class GraphViz
  19. class Node
  20. include Constants
  21. @xNodeName
  22. @oAttrNode
  23. @oGParrent
  24. #
  25. # Create a new node
  26. #
  27. # In:
  28. # * xNodeName : ID of the node
  29. # * oGParrent : Graph
  30. #
  31. def initialize( xNodeName, oGParrent )
  32. @xNodeName = xNodeName
  33. @oGParrent = oGParrent
  34. @oAttrNode = GraphViz::Attrs::new( nil, "node", NODESATTRS )
  35. @index = nil
  36. end
  37. #
  38. # Get the node ID
  39. #
  40. def name
  41. # TODO : Remove in v1.0
  42. warn "GraphViz::Node#name is deprecated, please use GraphViz::Node#id! -- BE CAREFUL, it will be removed in the 1.0 version!"
  43. return self.id
  44. end
  45. #
  46. # Get the node ID
  47. #
  48. def id
  49. @xNodeName.clone
  50. end
  51. #
  52. # Return the node index
  53. #
  54. def index
  55. @index
  56. end
  57. def index=(i) #:nodoc:
  58. @index = i if @index == nil
  59. end
  60. #
  61. # Return the root graph
  62. #
  63. def root_graph
  64. return( (self.pg.nil?) ? nil : self.pg.root_graph )
  65. end
  66. #
  67. # Set value +xAttrValue+ to the node attribut +xAttrName+
  68. #
  69. def []=( xAttrName, xAttrValue )
  70. xAttrValue = xAttrValue.to_s if xAttrValue.class == Symbol
  71. @oAttrNode[xAttrName.to_s] = xAttrValue
  72. end
  73. #
  74. # Get the value of the node attribut +xAttrName+
  75. #
  76. def []( xAttrName )
  77. if Hash === xAttrName
  78. xAttrName.each do |key, value|
  79. self[key] = value
  80. end
  81. else
  82. (@oAttrNode[xAttrName.to_s].nil?)?nil:@oAttrNode[xAttrName.to_s].clone
  83. end
  84. end
  85. #
  86. # Calls block once for each attribut of the node, passing the name and value to the
  87. # block as a two-element array.
  88. #
  89. # If global is set to false, the block does not receive the attributs set globally
  90. #
  91. def each_attribut(global = true, &b)
  92. attrs = @oAttrNode.to_h
  93. if global
  94. attrs = pg.node.to_h.merge attrs
  95. end
  96. attrs.each do |k,v|
  97. yield(k,v)
  98. end
  99. end
  100. #
  101. # Create an edge between the current node and the node +oNode+
  102. #
  103. def <<( oNode )
  104. if( oNode.class == Array )
  105. oNode.each do |no|
  106. self << no
  107. end
  108. else
  109. return GraphViz::commonGraph( oNode, self ).add_edge( self, oNode )
  110. end
  111. end
  112. alias :> :<<
  113. alias :- :<<
  114. alias :>> :<<
  115. #
  116. # Set node attributs
  117. #
  118. # Example :
  119. # n = graph.add_node( ... )
  120. # ...
  121. # n.set { |_n|
  122. # _n.color = "blue"
  123. # _n.fontcolor = "red"
  124. # }
  125. #
  126. def set( &b )
  127. yield( self )
  128. end
  129. # Add node options
  130. # use node.<option>=<value> or node.<option>( <value> )
  131. def method_missing( idName, *args, &block ) #:nodoc:
  132. xName = idName.id2name
  133. self[xName.gsub( /=$/, "" )]=args[0]
  134. end
  135. def pg #:nodoc:
  136. @oGParrent
  137. end
  138. def output #:nodoc:
  139. #xNodeName = @xNodeName.clone
  140. #xNodeName = '"' << xNodeName << '"' if xNodeName.match( /^[a-zA-Z_]+[a-zA-Z0-9_\.]*$/ ).nil?
  141. xNodeName = GraphViz.escape(@xNodeName)
  142. xOut = "" << xNodeName
  143. xAttr = ""
  144. xSeparator = ""
  145. if @oAttrNode.data.has_key?("label") and @oAttrNode.data.has_key?("html")
  146. @oAttrNode.data.delete("label")
  147. end
  148. @oAttrNode.data.each do |k, v|
  149. if k == "html"
  150. warn "html attribut is deprecated, please use label : :label => '<<html />>' -- BE CAREFUL, it will be removed in the 1.0 version!"
  151. xAttr << xSeparator + "label = " + v.to_gv
  152. else
  153. xAttr << xSeparator + k + " = " + v.to_gv
  154. end
  155. xSeparator = ", "
  156. end
  157. if xAttr.length > 0
  158. xOut << " [" + xAttr + "]"
  159. end
  160. xOut << ";"
  161. return( xOut )
  162. end
  163. end
  164. end