Class: Bundler::Molinillo::DependencyGraph
Relationships & Source Files | |
Namespace Children | |
Classes:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
::Bundler::TSort ,
Enumerable
|
|
Inherits: | Object |
Defined in: | lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/action.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_vertex.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/delete_edge.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/detach_vertex_named.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/log.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/set_payload.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/tag.rb, lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/vertex.rb |
Overview
A directed acyclic graph that is tuned to hold named dependencies
Class Method Summary
-
.new ⇒ DependencyGraph
constructor
Initializes an empty dependency graph.
-
.tsort(vertices) ⇒ Array<Vertex>
Topologically sorts the given vertices.
Instance Attribute Summary
- #log ⇒ Log readonly
- #vertices ⇒ {String => Vertex} readonly
Instance Method Summary
- #==(other) ⇒ Boolean
- #add_child_vertex(name, payload, parent_names, requirement) ⇒ void
-
#add_edge(origin, destination, requirement) ⇒ Edge
Adds a new
Edge
to the dependency graph. -
#add_vertex(name, payload, root = false) ⇒ Vertex
Adds a vertex with the given name, or updates the existing one.
-
#delete_edge(edge) ⇒ Void
Deletes an
Edge
from the dependency graph. -
#detach_vertex_named(name) ⇒ Array<Vertex>
Detaches the #vertex_named
name
Vertex
from the graph, recursively removing any non-root vertices that were orphaned in the process. -
#each ⇒ Array<Vertex>
(also: #tsort_each_node)
Enumerates through the vertices of the graph.
-
#initialize_copy(other)
Initializes a copy of a
DependencyGraph
, ensuring that all #vertices are properly copied. - #inspect ⇒ String
-
#rewind_to(tag) ⇒ Void
Rewinds the graph to the state tagged as #tag
- #root_vertex_named(name) ⇒ Vertex?
-
#set_payload(name, payload) ⇒ Void
Sets the payload of the vertex with the given name.
-
#tag(tag) ⇒ Void
Tags the current state of the dependency as the given tag.
- #to_dot(options = {}) ⇒ String
- #vertex_named(name) ⇒ Vertex?
-
#add_edge_no_circular(origin, destination, requirement)
private
Adds a new
Edge
to the dependency graph without checking for circularity. -
#path(from, to) ⇒ Array<Vertex>
private
Returns the path between two vertices.
- #tsort_each_child(vertex, &block) private
-
#tsort_each_node
private
Alias for #each.
::Bundler::TSort
- Included
#each_strongly_connected_component | The iterator version of the |
#each_strongly_connected_component_from | Iterates over strongly connected component in the subgraph reachable from node. |
#strongly_connected_components | Returns strongly connected components as an array of arrays of nodes. |
#tsort | Returns a topologically sorted array of nodes. |
#tsort_each | The iterator version of the |
#tsort_each_child | Should be implemented by a extended class. |
#tsort_each_node | Should be implemented by a extended class. |
Constructor Details
.new ⇒ DependencyGraph
Initializes an empty dependency graph
Class Method Details
.tsort(vertices) ⇒ Array
<Vertex>
Topologically sorts the given vertices.
Instance Attribute Details
#log ⇒ Log (readonly)
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 52
attr_reader :log
#vertices ⇒ {String
=> Vertex} (readonly)
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 49
attr_reader :vertices
Instance Method Details
#==(other) ⇒ Boolean
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 130
def ==(other) return false unless other return true if equal?(other) vertices.each do |name, vertex| other_vertex = other.vertex_named(name) return false unless other_vertex return false unless vertex.payload == other_vertex.payload return false unless other_vertex.successors.to_set == vertex.successors.to_set end end
#add_child_vertex(name, payload, parent_names, requirement) ⇒ void
This method returns an undefined value.
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 146
def add_child_vertex(name, payload, parent_names, requirement) root = !parent_names.delete(nil) { true } vertex = add_vertex(name, payload, root) vertex.explicit_requirements << requirement if root parent_names.each do |parent_name| parent_vertex = vertex_named(parent_name) add_edge(parent_vertex, vertex, requirement) end vertex end
#add_edge(origin, destination, requirement) ⇒ Edge
Adds a new DependencyGraph::Edge
to the dependency graph
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 191
def add_edge(origin, destination, requirement) if destination.path_to?(origin) raise CircularDependencyError.new(path(destination, origin)) end add_edge_no_circular(origin, destination, requirement) end
#add_edge_no_circular(origin, destination, requirement) (private)
Adds a new DependencyGraph::Edge
to the dependency graph without checking for circularity.
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 219
def add_edge_no_circular(origin, destination, requirement) log.add_edge_no_circular(self, origin.name, destination.name, requirement) end
#add_vertex(name, payload, root = false) ⇒ Vertex
Adds a vertex with the given name, or updates the existing one.
#delete_edge(edge) ⇒ Void
Deletes an DependencyGraph::Edge
from the dependency graph
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 201
def delete_edge(edge) log.delete_edge(self, edge.origin.name, edge.destination.name, edge.requirement) end
#detach_vertex_named(name) ⇒ Array
<Vertex>
Detaches the #vertex_named name
DependencyGraph::Vertex
from the graph, recursively removing any non-root vertices that were orphaned in the process
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 169
def detach_vertex_named(name) log.detach_vertex_named(self, name) end
#each ⇒ Array
<Vertex>
Also known as: #tsort_each_node
Enumerates through the vertices of the graph.
#initialize_copy(other)
Initializes a copy of a DependencyGraph
, ensuring that all #vertices are properly copied.
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 77
def initialize_copy(other) super @vertices = {} @log = other.log.dup traverse = lambda do |new_v, old_v| return if new_v.outgoing_edges.size == old_v.outgoing_edges.size old_v.outgoing_edges.each do |edge| destination = add_vertex(edge.destination.name, edge.destination.payload) add_edge_no_circular(new_v, destination, edge.requirement) traverse.call(destination, edge.destination) end end other.vertices.each do |name, vertex| new_vertex = add_vertex(name, vertex.payload, vertex.root?) new_vertex.explicit_requirements.replace(vertex.explicit_requirements) traverse.call(new_vertex, vertex) end end
#inspect ⇒ String
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 97
def inspect "#{self.class}:#{vertices.values.inspect}" end
#path(from, to) ⇒ Array
<Vertex> (private)
Returns the path between two vertices
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 228
def path(from, to) distances = Hash.new(vertices.size + 1) distances[from.name] = 0 predecessors = {} each do |vertex| vertex.successors.each do |successor| if distances[successor.name] > distances[vertex.name] + 1 distances[successor.name] = distances[vertex.name] + 1 predecessors[successor] = vertex end end end path = [to] while before = predecessors[to] path << before to = before break if to == from end unless path.last.equal?(from) raise ArgumentError, "There is no path from #{from.name} to #{to.name}" end path.reverse end
#rewind_to(tag) ⇒ Void
Rewinds the graph to the state tagged as #tag
#root_vertex_named(name) ⇒ Vertex?
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 181
def root_vertex_named(name) vertex = vertex_named(name) vertex if vertex && vertex.root? end
#set_payload(name, payload) ⇒ Void
Sets the payload of the vertex with the given name
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 209
def set_payload(name, payload) log.set_payload(self, name, payload) end
#tag(tag) ⇒ Void
Tags the current state of the dependency as the given tag
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 63
def tag(tag) log.tag(self, tag) end
#to_dot(options = {}) ⇒ String
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 103
def to_dot( = {}) edge_label = .delete(:edge_label) raise ArgumentError, "Unknown options: #{ .keys}" unless .empty? dot_vertices = [] dot_edges = [] vertices.each do |n, v| dot_vertices << " #{n} [label=\"{#{n}|#{v.payload}}\"]" v.outgoing_edges.each do |e| label = edge_label ? edge_label.call(e) : e.requirement dot_edges << " #{e.origin.name} -> #{e.destination.name} [label=#{label.to_s.dump}]" end end dot_vertices.uniq! dot_vertices.sort! dot_edges.uniq! dot_edges.sort! dot = dot_vertices.unshift('digraph G {').push('') + dot_edges.push('}') dot.join("\n") end
#tsort_each_child(vertex, &block) (private)
[ GitHub ]# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 26
def tsort_each_child(vertex, &block) vertex.successors.each(&block) end
#tsort_each_node (private)
Alias for #each.
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 23
alias tsort_each_node each
#vertex_named(name) ⇒ Vertex?
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 175
def vertex_named(name) vertices[name] end