Class: Graphviz::Graph

Inherits:
Node
  • Object
show all
Defined in:
lib/graphviz/graph.rb

Overview

Contains a set of nodes, edges and subgraphs.

Instance Attribute Summary collapse

Attributes inherited from Node

#connections, #name

Instance Method Summary collapse

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

#attributesHash

Returns Any associated graphviz attributes.

Returns:

  • (Hash)

    Any associated graphviz attributes.



155
156
157
# File 'lib/graphviz/graph.rb', line 155

def attributes
  @attributes
end

#edgesObject (readonly)

All edges in the graph



146
147
148
# File 'lib/graphviz/graph.rb', line 146

def edges
  @edges
end

#graphsArray<Graph> (readonly)

Returns Any subgraphs.

Returns:

  • (Array<Graph>)

    Any subgraphs.



152
153
154
# File 'lib/graphviz/graph.rb', line 152

def graphs
  @graphs
end

#nodesArray<Node> (readonly)

Returns All nodes in the graph.

Returns:

  • (Array<Node>)

    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.

Returns:

  • (Node)

    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.

Returns:

  • (Graph)

    the new graph.



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, options)
	@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, 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

#graph_format(options) ⇒ Object



191
192
193
194
195
196
197
# File 'lib/graphviz/graph.rb', line 191

def graph_format(options)
	if @graph
		'subgraph'
	else
		options[:format] || 'digraph'
	end
end

#identifierObject



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.

Returns:

  • (String)

    Output the graph using the dot format.



183
184
185
186
187
188
189
# File 'lib/graphviz/graph.rb', line 183

def to_dot(options = {})
	buffer = StringIO.new
	
	dump_graph(buffer, "", options)
	
	return buffer.string
end