module HQMF # Class for cleaning up references to specific occurrences in the tree class SpecificOccurrenceConverter def initialize(data_criteria_converter) @data_criteria_converter = data_criteria_converter end def convert_specific_occurrences(population_criteria) create_precondition_map(population_criteria) @specific_occurrence_key_mapper = {} specific_occurrences = {} @data_criteria_converter.v2_data_criteria.each do |data_criteria| if (is_specific_occurrence(data_criteria)) if (data_criteria.respond_to? :precondition_id and @precondition_map[data_criteria.precondition_id]) specific_occurrences[data_criteria.source_data_criteria] ||= [] specific_occurrences[data_criteria.source_data_criteria] << data_criteria elsif (data_criteria.children_criteria or data_criteria.subset_operators or data_criteria.temporal_references) binding.pry end end end specific_occurrences.each do |key, occurrences| occurrences.sort! do |left, right| @precondition_map[right.precondition_id].depth <=> @precondition_map[left.precondition_id].depth end occurrences.each_cons(2) do |pair| parent = get_common_parent(@precondition_map[pair[0].precondition_id], @precondition_map[pair[1].precondition_id]) binding.pry end end end def get_common_parent(left, right) return left if left == right get_common_parent(@precondition_parent_map[left.id], @precondition_parent_map[right.id]) end def create_precondition_map(population_criteria) @precondition_map = {} @precondition_parent_map = {} depth = 0 population_criteria.each do |population| recurse_precondition_map(population, depth) end end def recurse_precondition_map(node, depth) node.preconditions.each do |precondition| precondition.depth = depth @precondition_map[precondition.id] = precondition @precondition_parent_map[precondition.id] = node if precondition.has_preconditions? depth += 1 recurse_precondition_map(precondition, depth) end end if node.preconditions end def is_specific_occurrence(data_criteria) !data_criteria.specific_occurrence.nil? end end end