require 'graphviz/attrs'
require 'graphviz/constants'
class GraphViz
class Node
include GraphViz::Constants
attr_reader :neighbors
attr_reader :incidents
def initialize( node_id, parent_graph )
@neighbors = []
@incidents = []
@node_id = node_id
@parent_graph = parent_graph
@node_attributes = GraphViz::Attrs::new( nil, "node", NODESATTRS )
@index = nil
end
def id
@node_id.clone
end
def index
@index
end
def index=(i) @index = i if @index == nil
end
def root_graph
return( (self.pg.nil?) ? nil : self.pg.root_graph )
end
def []=( attribute_name, attribute_value )
attribute_value = attribute_value.to_s if attribute_value.class == Symbol
@node_attributes[attribute_name.to_s] = attribute_value
end
def []( attribute_name )
if Hash === attribute_name
attribute_name.each do |key, value|
self[key] = value
end
return self
else
(@node_attributes[attribute_name.to_s].nil?)?nil:@node_attributes[attribute_name.to_s].clone
end
end
def each_attribute(global = true, &b)
attrs = @node_attributes.to_h
if global
attrs = pg.node.to_h.merge attrs
end
attrs.each do |k,v|
yield(k,v)
end
end
def each_attribut(global = true, &b)
warn "`GraphViz::Node#each_attribut` is deprecated, please use `GraphViz::Node#each_attribute`"
each_attribute(global, &b)
end
def <<( node )
if( node.class == Array )
node.each do |no|
self << no
end
else
return GraphViz::commonGraph( node, self ).add_edges( self, node )
end
end
alias :> :<<
alias :- :<<
alias :>> :<<
def set( &b )
yield( self )
end
def method_missing( idName, *args, &block ) xName = idName.id2name
self[xName.gsub( /=$/, "" )]=args[0]
end
def pg @parent_graph
end
def output reserved_names = ["node", "edge","graph", "digraph", "subgraph", "strict"]
node_id = GraphViz.escape(@node_id)
xOut = reserved_names.include?(node_id) ? "" << "_" + node_id : "" << node_id
xAttr = ""
xSeparator = ""
if @node_attributes.data.has_key?("label") and @node_attributes.data.has_key?("html")
@node_attributes.data.delete("label")
end
@node_attributes.data.each do |k, v|
xAttr << xSeparator + k + " = " + v.to_gv
xSeparator = ", "
end
if xAttr.length > 0
xOut << " [" + xAttr + "]"
end
xOut << ";"
return( xOut )
end
end
end