Class: Graphviz::Graph
Overview
Contains a set of nodes, edges and subgraphs.
Instance Attribute Summary collapse
-
#attributes ⇒ Hash
Any associated graphviz attributes.
-
#edges ⇒ Object
readonly
All edges in the graph.
-
#nodes ⇒ Array<Node>
readonly
All nodes in the graph.
Attributes inherited from Node
Instance Method Summary collapse
- #<<(node) ⇒ Object
-
#add_node(name = nil, **attributes) ⇒ Node
Add a node to this graph.
-
#add_subgraph(name = nil, **attributes) ⇒ Graph
Add a subgraph with a given name and attributes.
- #dump_edges(buffer, indent, **options) ⇒ Object
-
#dump_graph(buffer, indent = "", **options) ⇒ Object
Dump the entire graph and all subgraphs to dot text format.
-
#get_node(node_name) ⇒ Array<Graphviz::Node>?
Finds all nodes with a given name.
- #graph_format(options) ⇒ Object
- #identifier ⇒ Object
-
#initialize(name = 'G', parent = nil, **attributes) ⇒ Graph
constructor
Initialize the graph with the specified unique name.
-
#node_exists?(node_name) ⇒ Boolean
Determines if a node with a given name exists in the graph.
-
#to_dot(**options) ⇒ String
Output the graph using the dot format.
Methods inherited from Node
#attach, #connect, #connected?, #dump_attributes, #dump_value, #to_s
Constructor Details
#initialize(name = 'G', parent = nil, **attributes) ⇒ Graph
Initialize the graph with the specified unique name.
29 30 31 32 33 34 |
# File 'lib/graphviz/graph.rb', line 29 def initialize(name = 'G', parent = nil, **attributes) super @edges = [] @nodes = {} end |
Instance Attribute Details
#attributes ⇒ Hash
Returns Any associated graphviz attributes.
43 44 45 |
# File 'lib/graphviz/graph.rb', line 43 def attributes @attributes end |
#edges ⇒ Object (readonly)
All edges in the graph
37 38 39 |
# File 'lib/graphviz/graph.rb', line 37 def edges @edges end |
#nodes ⇒ Array<Node> (readonly)
Returns All nodes in the graph.
40 41 42 |
# File 'lib/graphviz/graph.rb', line 40 def nodes @nodes end |
Instance Method Details
#<<(node) ⇒ Object
81 82 83 84 85 |
# File 'lib/graphviz/graph.rb', line 81 def << node @nodes[node.name] = node node.attach(self) end |
#add_node(name = nil, **attributes) ⇒ Node
Returns Add a node to this graph.
46 47 48 49 50 |
# 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) ⇒ Graph
Add a subgraph with a given name and attributes.
54 55 56 57 58 59 60 61 62 |
# 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) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/graphviz/graph.rb', line 112 def dump_edges(buffer, indent, **) @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) ⇒ Object
Dump the entire graph and all subgraphs to dot text format.
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/graphviz/graph.rb', line 121 def dump_graph(buffer, indent = "", **) format = graph_format() 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", **) end dump_edges(buffer, indent + "\t", **) buffer.puts "#{indent}}" end |
#get_node(node_name) ⇒ Array<Graphviz::Node>?
Finds all nodes with a given name
68 69 70 |
# 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) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/graphviz/graph.rb', line 96 def graph_format() if @graph 'subgraph' else [:format] || 'digraph' end end |
#identifier ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/graphviz/graph.rb', line 104 def identifier if @attributes[:cluster] "cluster_#{@name}" else super end end |
#node_exists?(node_name) ⇒ Boolean
Determines if a node with a given name exists in the graph
76 77 78 |
# 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) ⇒ String
Returns Output the graph using the dot format.
88 89 90 91 92 93 94 |
# File 'lib/graphviz/graph.rb', line 88 def to_dot(**) buffer = StringIO.new dump_graph(buffer, **) return buffer.string end |