class Gem::Molinillo::DependencyGraph
一个用于保存命名依赖的有向无环图
常量
- Edge
{DependencyGraph} 的有向边 @attr [Vertex] origin 有向边的起点 @attr [Vertex] destination 有向边的终点 @attr [Object] requirement 有向边表示的需求
属性
@return [Log] 此图的 op 日志
@return [{String => Vertex}] 依赖图的顶点,以键值对形式存储
by {Vertex#name}
公共类方法
初始化一个空的依赖图
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 55 def initialize @vertices = {} @log = Log.new end
对给定的顶点进行拓扑排序。 @param [Enumerable<Vertex>] vertices 要排序的顶点,必须
all belong to the same graph.
@return [Array<Vertex>] 排序后的顶点。
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 34 def self.tsort(vertices) Gem::TSort.tsort( lambda { |b| vertices.each(&b) }, lambda { |v, &b| (v.successors & vertices).each(&b) } ) end
公共实例方法
@param [DependencyGraph] other @return [Boolean] 两个依赖图是否相等,由以下因素确定
by a recursive traversal of each {#root_vertices} and its {Vertex#successors}
# File rubygems/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
@param [String] name @param [Object] payload @param [Array<String>] parent_names @param [Object] requirement 需要子项的需求 @return [void]
# File rubygems/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
向依赖图中添加一个新的 {Edge} @param [Vertex] origin @param [Vertex] destination @param [Object] requirement 此边表示的需求 @return [Edge] 添加的边
# File rubygems/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
添加具有给定名称的顶点,或更新现有顶点。 @param [String] name @param [Object] payload @return [Vertex] 添加到“self”的顶点
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 161 def add_vertex(name, payload, root = false) log.add_vertex(self, name, payload, root) end
从依赖图中删除一个 {Edge} @param [Edge] edge @return [Void]
# File rubygems/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
从图中分离 {#vertex_named} “name” {Vertex},递归删除在此过程中成为孤立顶点的任何非根顶点 @param [String] name @return [Array<Vertex>] 已分离的顶点
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 169 def detach_vertex_named(name) log.detach_vertex_named(self, name) end
枚举图的顶点。 @return [Array<Vertex>] 图的顶点。
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 15 def each return vertices.values.each unless block_given? vertices.values.each { |v| yield v } end
初始化 {DependencyGraph} 的副本,确保所有 {#vertices} 都被正确复制。 @param [DependencyGraph] other 要复制的图。
# File rubygems/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
@return [String] 适合调试的字符串
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 97 def inspect "#{self.class}:#{vertices.values.inspect}" end
将图回溯到标记为“tag”的状态 @param [Object] tag 要回溯到的标签 @return [Void]
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 70 def rewind_to(tag) log.rewind_to(self, tag) end
@param [String] name @return [Vertex,nil] 具有给定名称的根顶点
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 181 def root_vertex_named(name) vertex = vertex_named(name) vertex if vertex && vertex.root? end
设置具有给定名称的顶点的有效负载 @param [String] name 顶点的名称 @param [Object] payload 有效负载 @return [Void]
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 209 def set_payload(name, payload) log.set_payload(self, name, payload) end
将依赖关系的当前状态标记为给定的标签 @param [Object] tag 图的当前状态的不透明标签 @return [Void]
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 63 def tag(tag) log.tag(self, tag) end
@param [Hash] options dot 输出的选项。 @return [String] 返回图的 dot 格式表示形式
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 103 def to_dot(options = {}) edge_label = options.delete(:edge_label) raise ArgumentError, "Unknown options: #{options.keys}" unless options.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
@!visibility private
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 26 def tsort_each_child(vertex, &block) vertex.successors.each(&block) end
@param [String] name @return [Vertex,nil] 具有给定名称的顶点
# File rubygems/vendor/molinillo/lib/molinillo/dependency_graph.rb, line 175 def vertex_named(name) vertices[name] end
私有实例方法
返回两个顶点之间的路径 @raise [ArgumentError] 如果顶点之间没有路径 @param [Vertex] from @param [Vertex] to @return [Array<Vertex>] 从“from”到“to”的最短路径
# File rubygems/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