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



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.

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



112
113
114
115
116
117
118
# File 'lib/graphviz/graph.rb', line 112

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.



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

#get_node(node_name) ⇒ Array<Graphviz::Node>?

Finds all nodes with a given name

Parameters:

  • node_name (String)

    the name to look for

Returns:



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(options)
	if @graph
		'subgraph'
	else
		options[:format] || 'digraph'
	end
end

#identifierObject



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

Parameters:

  • node_name (String)

    the name to look for

Returns:

  • (Boolean)

    if node exists in 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.

Returns:

  • (String)

    Output the graph using the dot format.



88
89
90
91
92
93
94
# File 'lib/graphviz/graph.rb', line 88

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