Class: Pallets::Graph
Instance Method Summary collapse
- #add(node, dependencies) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize ⇒ Graph
constructor
A new instance of Graph.
- #parents(node) ⇒ Object
-
#sorted_with_order ⇒ Object
Returns nodes topologically sorted, together with their order (number of nodes that have to be executed prior).
Constructor Details
#initialize ⇒ Graph
Returns a new instance of Graph.
7 8 9 |
# File 'lib/pallets/graph.rb', line 7 def initialize @nodes = {} end |
Instance Method Details
#add(node, dependencies) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/pallets/graph.rb', line 11 def add(node, dependencies) raise WorkflowError, "Task #{node} is already defined in this workflow. "\ "Use `task '#{node}', as: 'FooBar'` to define an "\ "alias and reuse task" if nodes.key?(node) nodes[node] = dependencies end |
#empty? ⇒ Boolean
23 24 25 |
# File 'lib/pallets/graph.rb', line 23 def empty? nodes.empty? end |
#parents(node) ⇒ Object
19 20 21 |
# File 'lib/pallets/graph.rb', line 19 def parents(node) nodes[node] end |
#sorted_with_order ⇒ Object
Returns nodes topologically sorted, together with their order (number of nodes that have to be executed prior)
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/pallets/graph.rb', line 29 def sorted_with_order # Identify groups of nodes that can be executed concurrently groups = tsort_each.slice_when { |a, b| parents(a) != parents(b) } # Assign order to each node i = 0 groups.flat_map do |group| group_with_order = group.product([i]) i += group.size group_with_order end end |