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.
-
#graphs ⇒ Array<Graph>
readonly
Any subgraphs.
-
#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.
- #graph_format(options) ⇒ Object
- #identifier ⇒ Object
-
#initialize(name = 'G', parent = nil, **attributes) ⇒ Graph
constructor
Initialize the graph with the specified unique name.
-
#to_dot(options = {}) ⇒ String
Output the graph using the dot format.
Methods inherited from Node
#attach, #connect, #connected?, #dump_attributes, #dump_value
Constructor Details
#initialize(name = 'G', parent = nil, **attributes) ⇒ Graph
Initialize the graph with the specified unique name.
138 139 140 141 142 143 |
# File 'lib/graphviz/graph.rb', line 138 def initialize(name = 'G', parent = nil, **attributes) super @edges = [] @nodes = {} end |
Instance Attribute Details
#attributes ⇒ Hash
Returns Any associated graphviz attributes.
155 156 157 |
# File 'lib/graphviz/graph.rb', line 155 def attributes @attributes end |
#edges ⇒ Object (readonly)
All edges in the graph
146 147 148 |
# File 'lib/graphviz/graph.rb', line 146 def edges @edges end |
#graphs ⇒ Array<Graph> (readonly)
Returns Any subgraphs.
152 153 154 |
# File 'lib/graphviz/graph.rb', line 152 def graphs @graphs end |
#nodes ⇒ Array<Node> (readonly)
Returns All nodes in the graph.
149 150 151 |
# File 'lib/graphviz/graph.rb', line 149 def nodes @nodes end |
Instance Method Details
#<<(node) ⇒ Object
176 177 178 179 180 |
# File 'lib/graphviz/graph.rb', line 176 def << node @nodes[node.name] = node node.attach(self) end |
#add_node(name = nil, **attributes) ⇒ Node
Returns Add a node to this graph.
158 159 160 161 162 |
# File 'lib/graphviz/graph.rb', line 158 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.
166 167 168 169 170 171 172 173 174 |
# File 'lib/graphviz/graph.rb', line 166 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
207 208 209 210 211 212 213 214 215 |
# File 'lib/graphviz/graph.rb', line 207 def dump_edges(buffer, indent, ) @edges.each do |edge| from_name = dump_value(edge.source.identifier) to_name = dump_value(edge.destination.identifier) edge_attributes_text = dump_attributes(edge.attributes) buffer.puts "#{indent}#{from_name} #{edge} #{to_name}#{edge_attributes_text};" end end |
#dump_graph(buffer, indent, options) ⇒ Object
Dump the entire graph and all subgraphs to dot text format.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/graphviz/graph.rb', line 218 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 |
#graph_format(options) ⇒ Object
191 192 193 194 195 196 197 |
# File 'lib/graphviz/graph.rb', line 191 def graph_format() if @graph 'subgraph' else [:format] || 'digraph' end end |
#identifier ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/graphviz/graph.rb', line 199 def identifier if @attributes[:cluster] "cluster_#{@name}" else super end end |
#to_dot(options = {}) ⇒ String
Returns Output the graph using the dot format.
183 184 185 186 187 188 189 |
# File 'lib/graphviz/graph.rb', line 183 def to_dot( = {}) buffer = StringIO.new dump_graph(buffer, "", ) return buffer.string end |