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.
- #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, #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
64 65 66 67 68 |
# File 'lib/graphviz/graph.rb', line 64 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
95 96 97 98 99 100 101 |
# File 'lib/graphviz/graph.rb', line 95 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/graphviz/graph.rb', line 104 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
79 80 81 82 83 84 85 |
# File 'lib/graphviz/graph.rb', line 79 def graph_format() if @graph 'subgraph' else [:format] || 'digraph' end end |
#identifier ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/graphviz/graph.rb', line 87 def identifier if @attributes[:cluster] "cluster_#{@name}" else super end end |
#to_dot(**options) ⇒ String
Returns Output the graph using the dot format.
71 72 73 74 75 76 77 |
# File 'lib/graphviz/graph.rb', line 71 def to_dot(**) buffer = StringIO.new dump_graph(buffer, **) return buffer.string end |