DFS is working

This commit is contained in:
Senad Uka
2018-08-13 12:48:47 +02:00
parent de58457ef9
commit 92cadb76ed
3 changed files with 29 additions and 14 deletions

View File

@@ -2,25 +2,35 @@ module Pruning
module Processing
class Pruner
def initialize(tree, indicator_ids)
def initialize(tree)
@tree = tree
end
def prune_tree(nodes, indicator_ids)
nodes.delete_if do |node|
unwanted_indicator = indicator_node?(node) && !indicator_ids.include?(node['id'])
has_no_wanted_indicators_in_children = prune_tree(children(node), indicator_ids)
unwanted_indicator && has_no_wanted_indicators_in_children
end
nodes.empty?
def prune_tree(indicator_ids)
pruned_tree = @tree.dup
prune_subtree(pruned_tree, indicator_ids)
pruned_tree
end
private
def prune_subtree(nodes, indicator_ids)
nodes_to_examine = nodes.dup
nodes_to_examine.each do |node|
if indicator_node?(node)
unwanted_indicator = !indicator_ids.include?(node['id'])
nodes.delete(node) if unwanted_indicator
else
has_no_wanted_indicators_in_children = prune_subtree(children(node), indicator_ids)
nodes.delete(node) if has_no_wanted_indicators_in_children
end
end
nodes.empty?
end
def children(node)
node.get('sub-themes', false) ||
node.get('categories', false) ||
node.get('indicators', false) ||
node.fetch('sub_themes', false) ||
node.fetch('categories', false) ||
node.fetch('indicators', false) ||
[]
end