class Graph
Contains a set of nodes, edges and subgraphs.
Definitions
def initialize(name = "G", parent = nil, **attributes)
Initialize the graph with the specified unique name.
Implementation
def initialize(name = "G", parent = nil, **attributes)
super
@edges = []
@nodes = {}
end
attr :edges
All edges in the graph
attr :nodes
attr_accessor :attributes
def add_node(name = nil, **attributes)
Implementation
def add_node(name = nil, **attributes)
name ||= "#{@name}N#{@nodes.count}"
Node.new(name, self, **attributes)
end
def add_subgraph(name = nil, **attributes)
Add a subgraph with a given name and attributes.
Implementation
def add_subgraph(name = nil, **attributes)
name ||= "#{@name}S#{@nodes.count}"
subgraph = Graph.new(name, self, **attributes)
self << subgraph
return subgraph
end
def get_node(node_name)
Finds all nodes with a given name
Implementation
def get_node(node_name)
@nodes.select{ |k, v| v.name == node_name}.values
end
def node_exists?(node_name)
Determines if a node with a given name exists in the graph
Implementation
def node_exists?(node_name)
@nodes.select{ |k, v| v.name == node_name}.any?
end
def to_dot(**options)
Implementation
def to_dot(**options)
buffer = StringIO.new
dump_graph(buffer, **options)
return buffer.string
end
def dump_graph(buffer, indent = "", **options)
Dump the entire graph and all subgraphs to dot text format.
Implementation
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