Class: HQMF2::PopulationCriteria

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/hqmf-parser/2.0/population_criteria.rb

Overview

Represents an HQMF population criteria, also supports all the same methods as HQMF2::Precondition

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#attr_val, attr_val, #strip_tokens, #to_xml

Methods included from HQMF::Conversion::Utilities

#build_hash, #check_equality, #json_array, #openstruct_to_json

Constructor Details

#initialize(entry, doc, id_generator) ⇒ PopulationCriteria

Create a new population criteria from the supplied HQMF entry

Parameters:

  • the (Nokogiri::XML::Element)

    HQMF entry



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 12

def initialize(entry, doc, id_generator)
  @id_generator = id_generator
  @doc = doc
  @entry = entry
  setup_derived_entry_elements(id_generator)
  # modify type to meet current expected population names
  @type = 'IPP' if @type == 'IPOP' || @type == 'IPPOP'
  @comments = nil if comments.empty?
  # MEAN is handled in current code. Changed since it should have the same effect
  @aggregator = 'MEAN' if @aggregator == 'AVERAGE'
  @hqmf_id = @type unless @hqmf_id # The id extension is not required, if it's not provided use the code
  handle_type(id_generator)
end

Instance Attribute Details

#aggregatorObject (readonly)

Returns the value of attribute aggregator.



7
8
9
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 7

def aggregator
  @aggregator
end

#commentsObject (readonly)

Returns the value of attribute comments.



7
8
9
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 7

def comments
  @comments
end

#hqmf_idObject (readonly)

Returns the value of attribute hqmf_id.



7
8
9
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 7

def hqmf_id
  @hqmf_id
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 7

def id
  @id
end

#preconditionsObject (readonly)

Returns the value of attribute preconditions.



7
8
9
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 7

def preconditions
  @preconditions
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 7

def title
  @title
end

#typeObject

need to do this to allow for setting the type to OBSERV for



9
10
11
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 9

def type
  @type
end

Instance Method Details

#conjunction_codeString

Get the conjunction code, ALL_TRUE or AT_LEAST_ONE_TRUE

Returns:

  • (String)

    conjunction code



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 115

def conjunction_code
  case @type
  when HQMF::PopulationCriteria::IPP, HQMF::PopulationCriteria::DENOM, HQMF::PopulationCriteria::NUMER,
       HQMF::PopulationCriteria::MSRPOPL, HQMF::PopulationCriteria::STRAT
    HQMF::Precondition::ALL_TRUE
  when HQMF::PopulationCriteria::DENEXCEP, HQMF::PopulationCriteria::DENEX, HQMF::PopulationCriteria::MSRPOPLEX,
       HQMF::PopulationCriteria::NUMEX
    HQMF::Precondition::AT_LEAST_ONE_TRUE
  else
    fail "Unknown population type [#{@type}]"
  end
end

#create_human_readable_id(id) ⇒ Object



109
110
111
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 109

def create_human_readable_id(id)
  @id = id
end

#handle_observation_criteriaObject

extracts out any measure observation definitons, creating from them the proper criteria to generate a precondition



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 69

def handle_observation_criteria
  exp = @entry.at_xpath('./cda:measureObservationDefinition/cda:value/cda:expression/@value',
                        HQMF2::Document::NAMESPACES)
  # Measure Observations criteria rely on computed expressions. If it doesn't have one,
  #  then it is likely formatted improperly.
  fail 'Measure Observations criteria is missing computed expression(s) ' if exp.nil?
  parts = exp.to_s.split('-')
  dc = parse_parts_to_dc(parts)
  @doc.add_data_criteria(dc)
  # Update reference_ids with any newly referenced data criteria
  dc.children_criteria.each { |cc| @doc.add_reference_id(cc) } unless dc&.children_criteria.nil?
  dc
end

#handle_preconditions(id_generator) ⇒ Object

specifically handles extracting the preconditions for the population criteria



57
58
59
60
61
62
63
64
65
66
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 57

def handle_preconditions(id_generator)
  # Nest multiple preconditions under a single root precondition
  @preconditions = @entry.xpath('./*/cda:precondition[not(@nullFlavor)]', HQMF2::Document::NAMESPACES)
                   .collect do |pre|
    precondition = Precondition.parse(pre, @doc, id_generator)
    precondition.reference.nil? && precondition.preconditions.empty? ? nil : precondition
  end
  # Remove uneeded nils from the array
  @preconditions.compact!
end

#handle_type(id_generator) ⇒ Object

Handles how the code should deal with the type definition (aggregate vs non-aggregate)



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 27

def handle_type(id_generator)
  if @type != 'AGGREGATE'
    # Generate the precondition for this population
    if @preconditions.length > 1 ||
       (@preconditions.length == 1 && @preconditions[0].conjunction != conjunction_code)
      @preconditions = [Precondition.new(id_generator.next_id, conjunction_code, @preconditions)]
    end
  else
    # Extract the data criteria this population references
    dc = handle_observation_criteria
    @preconditions = [Precondition.new(id_generator.next_id, nil, nil, false, HQMF2::Reference.new(dc.id))] if dc
  end
end

#parse_parts_to_dc(parts) ⇒ Object

generates the value given in an expression based on the number of criteria it references.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 84

def parse_parts_to_dc(parts)
  case parts.length
  when 1
    # If there is only one part, it is a reference to an existing data criteria's value
    @doc.find_criteria_by_lvn(parts.first.strip.split('.')[0])
  when 2
    # If there are two parts, there is a computation performed, specifically time difference, on the two criteria
    children = parts.collect { |p| @doc.find_criteria_by_lvn(p.strip.split('.')[0]).id }
    id = "GROUP_TIMEDIFF_#{@id_generator.next_id}"
    HQMF2::DataCriteriaWrapper.new(id: id,
                                   title: id,
                                   subset_operators: [HQMF::SubsetOperator.new('DATETIMEDIFF', nil)],
                                   children_criteria: children,
                                   derivation_operator: HQMF::DataCriteria::XPRODUCT,
                                   type: 'derived',
                                   definition: 'derived',
                                   negation: false,
                                   source_data_criteria: id
                                  )
  else
    # If there are neither one or 2 parts, the code should fail
    fail "No defined extraction method to handle #{parts.length} parts"
  end
end

#setup_derived_entry_elements(id_generator) ⇒ Object

Handles extracting elements from the entry



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 42

def setup_derived_entry_elements(id_generator)
  @hqmf_id = attr_val('./*/cda:id/@root') || attr_val('./*/cda:typeId/@extension')
  @title = attr_val('./*/cda:code/cda:displayName/@value').try(:titleize)
  @type = attr_val('./*/cda:code/@code')
  @comments = @entry.xpath('./*/cda:text/cda:xml/cda:qdmUserComments/cda:item/text()', HQMF2::Document::NAMESPACES)
              .map(&:content)
  handle_preconditions(id_generator)
  obs_test = attr_val('./cda:measureObservationDefinition/@classCode')
  # If there are no measure observations, or there is a title, then there are no aggregations to extract
  return unless !@title && obs_test.to_s == 'OBS'
  @title = attr_val('../cda:code/cda:displayName/@value')
  @aggregator = attr_val('./cda:measureObservationDefinition/cda:methodCode/cda:item/@code')
end

#to_modelObject

Generates this classes hqmf-model equivalent



129
130
131
132
# File 'lib/hqmf-parser/2.0/population_criteria.rb', line 129

def to_model
  mps = preconditions.collect(&:to_model)
  HQMF::PopulationCriteria.new(id, hqmf_id, type, mps, title, aggregator, comments)
end