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, #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

#attributesHash

Returns Any associated graphviz attributes.

Returns:

  • (Hash)

    Any associated graphviz attributes.



43
44
45
# File 'lib/graphviz/graph.rb', line 43

def attributes
  @attributes
end

#edgesObject (readonly)

All edges in the graph



37
38
39
# File 'lib/graphviz/graph.rb', line 37

def edges
  @edges
end

#nodesArray<Node> (readonly)

Returns All nodes in the graph.

Returns:

  • (Array<Node>)

    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.

Returns:

  • (Node)

    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.

Returns:

  • (Graph)

    the new graph.



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



79
80
81
82
83
84
85
# File 'lib/graphviz/graph.rb', line 79

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

#identifierObject



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.

Returns:

  • (String)

    Output the graph using the dot format.



71
72
73
74
75
76
77
# File 'lib/graphviz/graph.rb', line 71

def to_dot(**options)
	buffer = StringIO.new
	
	dump_graph(buffer, **options)
	
	return buffer.string
end