class Graphviz::Graph
Contains a set of nodes, edges and subgraphs.
Attributes
attributes[RW]
@return [Hash] Any associated graphviz attributes.
edges[R]
All edges in the graph
nodes[R]
@return [Array<Node>] All nodes in the graph.
Public Class Methods
new(name = 'G', parent = nil, **attributes)
click to toggle source
Initialize the graph with the specified unique name.
Calls superclass method
# File lib/graphviz/graph.rb, line 29 def initialize(name = 'G', parent = nil, **attributes) super @edges = [] @nodes = {} end
Public Instance Methods
<<(node)
click to toggle source
# File lib/graphviz/graph.rb, line 81 def << node @nodes[node.name] = node node.attach(self) end
add_node(name = nil, **attributes)
click to toggle source
@return [Node] Add a node to this graph.
# File lib/graphviz/graph.rb, line 46 def add_node(name = nil, **attributes) name ||= "#{@name}N#{@nodes.count}" Node.new(name, self, **attributes) end
add_subgraph(name = nil, **attributes)
click to toggle source
Add a subgraph with a given name and attributes. @return [Graph] the new graph.
# File lib/graphviz/graph.rb, line 54 def add_subgraph(name = nil, **attributes) name ||= "#{@name}S#{@nodes.count}" subgraph = Graph.new(name, self, attributes) self << subgraph return subgraph end
dump_edges(buffer, indent, **options)
click to toggle source
# File lib/graphviz/graph.rb, line 112 def dump_edges(buffer, indent, **options) @edges.each do |edge| edge_attributes_text = dump_attributes(edge.attributes) buffer.puts "#{indent}#{edge}#{edge_attributes_text};" end end
dump_graph(buffer, indent = "", **options)
click to toggle source
Dump the entire graph and all subgraphs to dot text format.
# File lib/graphviz/graph.rb, line 121 def dump_graph(buffer, indent = "", **options) format = graph_format(options) buffer.puts "#{indent}#{format} #{dump_value(self.identifier)} {" @attributes.each do |name, value| buffer.puts "#{indent}\t#{name}=#{dump_value(value)};" end @nodes.each do |_name, node| node.dump_graph(buffer, indent + "\t", **options) end dump_edges(buffer, indent + "\t", **options) buffer.puts "#{indent}}" end
get_node(node_name)
click to toggle source
Finds all nodes with a given name
@param [String] node_name the name to look for @return [Array<Graphviz::Node>, nil] list of all found nodes or nil
# File lib/graphviz/graph.rb, line 68 def get_node(node_name) @nodes.select{ |k, v| v.name == node_name}.values end
graph_format(options)
click to toggle source
# File lib/graphviz/graph.rb, line 96 def graph_format(options) if @graph 'subgraph' else options[:format] || 'digraph' end end
identifier()
click to toggle source
Calls superclass method
# File lib/graphviz/graph.rb, line 104 def identifier if @attributes[:cluster] "cluster_#{@name}" else super end end
node_exists?(node_name)
click to toggle source
Determines if a node with a given name exists in the graph
@param [String] node_name the name to look for @return [Boolean] if node exists in graph
# File lib/graphviz/graph.rb, line 76 def node_exists?(node_name) @nodes.select{ |k, v| v.name == node_name}.any? end
to_dot(**options)
click to toggle source
@return [String] Output the graph using the dot format.
# File lib/graphviz/graph.rb, line 88 def to_dot(**options) buffer = StringIO.new dump_graph(buffer, **options) return buffer.string end