Class: Bundler::Molinillo::DependencyGraph
- Inherits:
-
Object
- Object
- Bundler::Molinillo::DependencyGraph
- Includes:
- Enumerable, TSort
- Defined in:
- lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb,
lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/log.rb,
lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/tag.rb,
lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/action.rb,
lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/vertex.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/set_payload.rb,
lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/detach_vertex_named.rb,
lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph/add_edge_no_circular.rb
Overview
A directed acyclic graph that is tuned to hold named dependencies
Defined Under Namespace
Classes: Action, Edge, Log, Vertex
Instance Attribute Summary collapse
-
#log ⇒ Log
readonly
The op log for this graph.
-
#vertices ⇒ {String => Vertex}
readonly
The vertices of the dependency graph, keyed by Vertex#name.
Class Method Summary collapse
-
.tsort(vertices) ⇒ Array<Vertex>
Topologically sorts the given vertices.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Whether the two dependency graphs are equal, determined by a recursive traversal of each #root_vertices and its Vertex#successors.
- #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 ⇒ DependencyGraph
constructor
Initializes an empty dependency graph.
-
#initialize_copy(other) ⇒ Object
Initializes a copy of a DependencyGraph, ensuring that all #vertices are properly copied.
-
#inspect ⇒ String
A string suitable for debugging.
-
#rewind_to(tag) ⇒ Void
Rewinds the graph to the state tagged as ‘tag`.
-
#root_vertex_named(name) ⇒ Vertex?
The root vertex with the given name.
-
#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
Returns a dot format representation of the graph.
-
#vertex_named(name) ⇒ Vertex?
The vertex with the given name.
Constructor Details
#initialize ⇒ DependencyGraph
Initializes an empty dependency graph
56 57 58 59 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 56 def initialize @vertices = {} @log = Log.new end |
Instance Attribute Details
#log ⇒ Log (readonly)
Returns the op log for this graph.
53 54 55 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 53 def log @log end |
#vertices ⇒ {String => Vertex} (readonly)
Returns the vertices of the dependency graph, keyed by Bundler::Molinillo::DependencyGraph::Vertex#name.
50 51 52 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 50 def vertices @vertices end |
Class Method Details
.tsort(vertices) ⇒ Array<Vertex>
Topologically sorts the given vertices.
35 36 37 38 39 40 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 35 def self.tsort(vertices) TSort.tsort( lambda { |b| vertices.each(&b) }, lambda { |v, &b| (v.successors & vertices).each(&b) } ) end |
Instance Method Details
#==(other) ⇒ Boolean
Returns whether the two dependency graphs are equal, determined by a recursive traversal of each #root_vertices and its Bundler::Molinillo::DependencyGraph::Vertex#successors.
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 131 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.
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 147 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 Edge to the dependency graph
192 193 194 195 196 197 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 192 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_vertex(name, payload, root = false) ⇒ Vertex
Adds a vertex with the given name, or updates the existing one.
162 163 164 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 162 def add_vertex(name, payload, root = false) log.add_vertex(self, name, payload, root) end |
#delete_edge(edge) ⇒ Void
Deletes an Edge from the dependency graph
202 203 204 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 202 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` Vertex from the graph, recursively removing any non-root vertices that were orphaned in the process
170 171 172 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 170 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.
16 17 18 19 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 16 def each return vertices.values.each unless block_given? vertices.values.each { |v| yield v } end |
#initialize_copy(other) ⇒ Object
Initializes a copy of a Bundler::Molinillo::DependencyGraph, ensuring that all #vertices are properly copied.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 78 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
Returns a string suitable for debugging.
98 99 100 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 98 def inspect "#{self.class}:#{vertices.values.inspect}" end |
#rewind_to(tag) ⇒ Void
Rewinds the graph to the state tagged as ‘tag`
71 72 73 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 71 def rewind_to(tag) log.rewind_to(self, tag) end |
#root_vertex_named(name) ⇒ Vertex?
Returns the root vertex with the given name.
182 183 184 185 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 182 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
210 211 212 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 210 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
64 65 66 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 64 def tag(tag) log.tag(self, tag) end |
#to_dot(options = {}) ⇒ String
Returns a dot format representation of the graph
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 104 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 |
#vertex_named(name) ⇒ Vertex?
Returns the vertex with the given name.
176 177 178 |
# File 'lib/bundler/vendor/molinillo/lib/molinillo/dependency_graph.rb', line 176 def vertex_named(name) vertices[name] end |