cqm-parsers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +29 -0
  3. data/README.md +21 -0
  4. data/Rakefile +19 -0
  5. data/lib/ext/code.rb +10 -0
  6. data/lib/ext/data_element.rb +24 -0
  7. data/lib/hqmf-model/attribute.rb +63 -0
  8. data/lib/hqmf-model/data_criteria.rb +467 -0
  9. data/lib/hqmf-model/document.rb +253 -0
  10. data/lib/hqmf-model/population_criteria.rb +102 -0
  11. data/lib/hqmf-model/precondition.rb +94 -0
  12. data/lib/hqmf-model/types.rb +457 -0
  13. data/lib/hqmf-model/utilities.rb +52 -0
  14. data/lib/hqmf-parser.rb +116 -0
  15. data/lib/hqmf-parser/1.0/attribute.rb +121 -0
  16. data/lib/hqmf-parser/1.0/comparison.rb +34 -0
  17. data/lib/hqmf-parser/1.0/data_criteria.rb +92 -0
  18. data/lib/hqmf-parser/1.0/document.rb +195 -0
  19. data/lib/hqmf-parser/1.0/expression.rb +60 -0
  20. data/lib/hqmf-parser/1.0/observation.rb +61 -0
  21. data/lib/hqmf-parser/1.0/population_criteria.rb +75 -0
  22. data/lib/hqmf-parser/1.0/precondition.rb +90 -0
  23. data/lib/hqmf-parser/1.0/range.rb +76 -0
  24. data/lib/hqmf-parser/1.0/restriction.rb +162 -0
  25. data/lib/hqmf-parser/1.0/utilities.rb +55 -0
  26. data/lib/hqmf-parser/2.0/data_criteria.rb +372 -0
  27. data/lib/hqmf-parser/2.0/data_criteria_helpers/dc_base_extract.rb +80 -0
  28. data/lib/hqmf-parser/2.0/data_criteria_helpers/dc_definition_from_template_or_type_extract.rb +201 -0
  29. data/lib/hqmf-parser/2.0/data_criteria_helpers/dc_post_processing.rb +85 -0
  30. data/lib/hqmf-parser/2.0/data_criteria_helpers/dc_specific_occurrences_and_source_data_criteria_extract.rb +117 -0
  31. data/lib/hqmf-parser/2.0/document.rb +304 -0
  32. data/lib/hqmf-parser/2.0/document_helpers/doc_population_helper.rb +173 -0
  33. data/lib/hqmf-parser/2.0/document_helpers/doc_utilities.rb +131 -0
  34. data/lib/hqmf-parser/2.0/field_value_helper.rb +251 -0
  35. data/lib/hqmf-parser/2.0/population_criteria.rb +134 -0
  36. data/lib/hqmf-parser/2.0/precondition.rb +73 -0
  37. data/lib/hqmf-parser/2.0/source_data_criteria_helper.rb +112 -0
  38. data/lib/hqmf-parser/2.0/types.rb +448 -0
  39. data/lib/hqmf-parser/2.0/utilities.rb +45 -0
  40. data/lib/hqmf-parser/2.0/value_set_helper.rb +104 -0
  41. data/lib/hqmf-parser/converter/pass1/data_criteria_converter.rb +257 -0
  42. data/lib/hqmf-parser/converter/pass1/document_converter.rb +133 -0
  43. data/lib/hqmf-parser/converter/pass1/population_criteria_converter.rb +185 -0
  44. data/lib/hqmf-parser/converter/pass1/precondition_converter.rb +173 -0
  45. data/lib/hqmf-parser/converter/pass1/precondition_extractor.rb +201 -0
  46. data/lib/hqmf-parser/converter/pass1/simple_data_criteria.rb +26 -0
  47. data/lib/hqmf-parser/converter/pass1/simple_operator.rb +89 -0
  48. data/lib/hqmf-parser/converter/pass1/simple_population_criteria.rb +10 -0
  49. data/lib/hqmf-parser/converter/pass1/simple_precondition.rb +51 -0
  50. data/lib/hqmf-parser/converter/pass1/simple_restriction.rb +64 -0
  51. data/lib/hqmf-parser/converter/pass2/comparison_converter.rb +112 -0
  52. data/lib/hqmf-parser/converter/pass2/operator_converter.rb +102 -0
  53. data/lib/hqmf-parser/cql/data_criteria.rb +57 -0
  54. data/lib/hqmf-parser/cql/data_criteria_helpers/dc_definition_from_template_or_type_extract.rb +79 -0
  55. data/lib/hqmf-parser/cql/data_criteria_helpers/dc_post_processing.rb +43 -0
  56. data/lib/hqmf-parser/cql/document.rb +78 -0
  57. data/lib/hqmf-parser/cql/document_helpers/doc_population_helper.rb +124 -0
  58. data/lib/hqmf-parser/cql/value_set_helper.rb +103 -0
  59. data/lib/hqmf-parser/parser.rb +100 -0
  60. data/lib/qrda-export/catI-r5/qrda1_r5.rb +125 -0
  61. data/lib/qrda-export/helper/cat_1_view_helper.rb +142 -0
  62. data/lib/qrda-export/helper/code_system_helper.rb +77 -0
  63. data/lib/qrda-export/helper/date_helper.rb +81 -0
  64. data/lib/qrda-import/base-importers/demographics_importer.rb +47 -0
  65. data/lib/qrda-import/base-importers/medication_importer.rb +22 -0
  66. data/lib/qrda-import/base-importers/section_importer.rb +196 -0
  67. data/lib/qrda-import/cda_identifier.rb +19 -0
  68. data/lib/qrda-import/data-element-importers/adverse_event_importer.rb +23 -0
  69. data/lib/qrda-import/data-element-importers/allergy_intolerance_importer.rb +21 -0
  70. data/lib/qrda-import/data-element-importers/assessment_performed_importer.rb +23 -0
  71. data/lib/qrda-import/data-element-importers/communication_from_patient_to_provider_importer.rb +18 -0
  72. data/lib/qrda-import/data-element-importers/communication_from_provider_to_patient_importer.rb +18 -0
  73. data/lib/qrda-import/data-element-importers/communication_from_provider_to_provider_importer.rb +20 -0
  74. data/lib/qrda-import/data-element-importers/device_applied_importer.rb +23 -0
  75. data/lib/qrda-import/data-element-importers/device_order_importer.rb +18 -0
  76. data/lib/qrda-import/data-element-importers/diagnosis_importer.rb +23 -0
  77. data/lib/qrda-import/data-element-importers/diagnostic_study_order_importer.rb +20 -0
  78. data/lib/qrda-import/data-element-importers/diagnostic_study_performed_importer.rb +30 -0
  79. data/lib/qrda-import/data-element-importers/encounter_order_importer.rb +20 -0
  80. data/lib/qrda-import/data-element-importers/encounter_performed_importer.rb +41 -0
  81. data/lib/qrda-import/data-element-importers/immunization_administered_importer.rb +18 -0
  82. data/lib/qrda-import/data-element-importers/intervention_order_importer.rb +18 -0
  83. data/lib/qrda-import/data-element-importers/intervention_performed_importer.rb +22 -0
  84. data/lib/qrda-import/data-element-importers/laboratory_test_order_importer.rb +20 -0
  85. data/lib/qrda-import/data-element-importers/laboratory_test_performed_importer.rb +28 -0
  86. data/lib/qrda-import/data-element-importers/medication_active_importer.rb +17 -0
  87. data/lib/qrda-import/data-element-importers/medication_administered_importer.rb +17 -0
  88. data/lib/qrda-import/data-element-importers/medication_discharge_importer.rb +19 -0
  89. data/lib/qrda-import/data-element-importers/medication_dispensed_importer.rb +19 -0
  90. data/lib/qrda-import/data-element-importers/medication_order_importer.rb +16 -0
  91. data/lib/qrda-import/data-element-importers/patient_characteristic_expired.rb +21 -0
  92. data/lib/qrda-import/data-element-importers/physical_exam_performed_importer.rb +26 -0
  93. data/lib/qrda-import/data-element-importers/procedure_order_importer.rb +26 -0
  94. data/lib/qrda-import/data-element-importers/procedure_performed_importer.rb +34 -0
  95. data/lib/qrda-import/data-element-importers/substance_administered_importer.rb +16 -0
  96. data/lib/qrda-import/entry_finder.rb +20 -0
  97. data/lib/qrda-import/entry_package.rb +16 -0
  98. data/lib/qrda-import/narrative_reference_handler.rb +33 -0
  99. data/lib/qrda-import/patient_importer.rb +105 -0
  100. data/lib/util/code_system_helper.rb +76 -0
  101. data/lib/util/counter.rb +20 -0
  102. data/lib/util/hqmf_template_helper.rb +39 -0
  103. metadata +340 -0
@@ -0,0 +1,201 @@
1
+ module HQMF2
2
+ # Extracts the type, and modifies the data criteria, based on the template id or definition
3
+ module DataCriteriaTypeAndDefinitionExtraction
4
+ VARIABLE_TEMPLATE = '0.1.2.3.4.5.6.7.8.9.1'
5
+ SATISFIES_ANY_TEMPLATE = '2.16.840.1.113883.10.20.28.3.108'
6
+ SATISFIES_ALL_TEMPLATE = '2.16.840.1.113883.10.20.28.3.109'
7
+ def extract_definition_from_template_or_type
8
+ # Try to determine what kind of data criteria we are dealing with
9
+ # First we look for a template id and if we find one just use the definition
10
+ # status and negation associated with that
11
+ # If no template id or not one we recognize then try to determine type from
12
+ # the definition element
13
+ extract_definition_from_type unless extract_definition_from_template_id
14
+ end
15
+
16
+ # Given a template id, derive (if available) the definition for the template.
17
+ # The definitions are stored in hqmf-model/data_criteria.json.
18
+ def extract_definition_from_template_id
19
+ found = false
20
+
21
+ @template_ids.each do |template_id|
22
+ defs = HQMF::DataCriteria.definition_for_template_id(template_id, 'r2')
23
+ if defs
24
+ @definition = defs['definition']
25
+ @status = defs['status'].length > 0 ? defs['status'] : nil
26
+ found ||= true
27
+ else
28
+ found ||= handle_known_template_id(template_id)
29
+ end
30
+ end
31
+
32
+ found
33
+ end
34
+
35
+ # Given a template id, modify the variables inside this data criteria to reflect the template
36
+ def handle_known_template_id(template_id)
37
+ case template_id
38
+ when VARIABLE_TEMPLATE
39
+ @derivation_operator = HQMF::DataCriteria::INTERSECT if @derivation_operator == HQMF::DataCriteria::XPRODUCT
40
+ @definition ||= 'derived'
41
+ @variable = true
42
+ @negation = false
43
+ when SATISFIES_ANY_TEMPLATE
44
+ @definition = HQMF::DataCriteria::SATISFIES_ANY
45
+ @negation = false
46
+ when SATISFIES_ALL_TEMPLATE
47
+ @definition = HQMF::DataCriteria::SATISFIES_ALL
48
+ @derivation_operator = HQMF::DataCriteria::INTERSECT
49
+ @negation = false
50
+ else
51
+ return false
52
+ end
53
+ true
54
+ end
55
+
56
+ # Extract the definition (sometimes status, sometimes other elements) of the data criteria based on the type
57
+ def extract_definition_from_type
58
+ # If we have a specific occurrence of a variable, pull attributes from the reference.
59
+ # IDEA set this up to be called from dc_specific_and_source_extract, the number of
60
+ # fields changed by handle_specific_variable_ref may pose an issue.
61
+ extract_information_for_specific_variable if @variable && @specific_occurrence
62
+
63
+ if @entry.at_xpath('./cda:grouperCriteria')
64
+ @definition ||= 'derived'
65
+ return
66
+ end
67
+ # See if we can find a match for the entry definition value and status.
68
+ entry_type = attr_val('./*/cda:definition/*/cda:id/@extension')
69
+ handle_entry_type(entry_type)
70
+ end
71
+
72
+ # Extracts information from a reference for a specific
73
+ def extract_information_for_specific_variable
74
+ reference = @entry.at_xpath('./*/cda:outboundRelationship/cda:criteriaReference',
75
+ HQMF2::Document::NAMESPACES)
76
+ if reference
77
+ ref_id = strip_tokens(
78
+ "#{HQMF2::Utilities.attr_val(reference, 'cda:id/@extension')}_#{HQMF2::Utilities.attr_val(reference, 'cda:id/@root')}")
79
+ end
80
+ reference_criteria = @data_criteria_references[ref_id] if ref_id
81
+ # if the reference is derived, pull from the original variable
82
+ if reference_criteria && reference_criteria.definition == 'derived'
83
+ reference_criteria = @data_criteria_references["GROUP_#{ref_id}"]
84
+ end
85
+ return unless reference_criteria
86
+ handle_specific_variable_ref(reference_criteria)
87
+ end
88
+
89
+ # Apply additional information to a specific occurrence's elements from the criteria it references.
90
+ def handle_specific_variable_ref(reference_criteria)
91
+ # if there are no referenced children, then it's a variable representing
92
+ # a single data criteria, so just reference it
93
+ if reference_criteria.children_criteria.empty?
94
+ @children_criteria = [reference_criteria.id]
95
+ # otherwise pull all the data criteria info from the reference
96
+ else
97
+ @field_values = reference_criteria.field_values
98
+ @temporal_references = reference_criteria.temporal_references
99
+ @subset_operators = reference_criteria.subset_operators
100
+ @derivation_operator = reference_criteria.derivation_operator
101
+ @definition = reference_criteria.definition
102
+ @description = reference_criteria.description
103
+ @status = reference_criteria.status
104
+ @children_criteria = reference_criteria.children_criteria
105
+ end
106
+ end
107
+
108
+ # Generate the definition and/or status from the entry type in most cases.
109
+ # If the entry type is nil, and the value is a specific occurrence, more parsing may be necessary.
110
+ def handle_entry_type(entry_type)
111
+ # settings is required to trigger exceptions, which set the definition
112
+ HQMF::DataCriteria.get_settings_for_definition(entry_type, @status)
113
+ @definition = entry_type
114
+ rescue
115
+ # if no exact match then try a string match just using entry definition value
116
+ case entry_type
117
+ when 'Medication', 'Medications'
118
+ @definition = 'medication'
119
+ @status = 'active' unless @status
120
+ when 'RX'
121
+ @definition = 'medication'
122
+ @status = 'dispensed' unless @status
123
+ when nil
124
+ definition_for_nil_entry
125
+ else
126
+ @definition = extract_definition_from_entry_type(entry_type)
127
+ end
128
+ end
129
+
130
+ # If there is no entry type, extract the entry type from what it references, and extract additional information for
131
+ # specific occurrences. If there are no outbound references, print an error and mark it as variable.
132
+ def definition_for_nil_entry
133
+ reference = @entry.at_xpath('./*/cda:outboundRelationship/cda:criteriaReference', HQMF2::Document::NAMESPACES)
134
+ ref_id = nil
135
+ unless reference.nil?
136
+ ref_id = "#{HQMF2::Utilities.attr_val(reference, 'cda:id/@extension')}_#{HQMF2::Utilities.attr_val(reference, 'cda:id/@root')}"
137
+ end
138
+ reference_criteria = @data_criteria_references[strip_tokens(ref_id)] unless ref_id.nil?
139
+ if reference_criteria
140
+ # we only want to copy the reference criteria definition, status, and code_list_id if this is this is not a grouping criteria (i.e., there are no children)
141
+ if @children_criteria.blank?
142
+ @definition = reference_criteria.definition
143
+ @status = reference_criteria.status
144
+ if @specific_occurrence
145
+ @title = reference_criteria.title
146
+ @description = reference_criteria.description
147
+ @code_list_id = reference_criteria.code_list_id
148
+ end
149
+ else
150
+ # if this is a grouping data criteria (has children) mark it as derived and only pull title and description from the reference criteria
151
+ @definition = 'derived'
152
+ if @specific_occurrence
153
+ @title = reference_criteria.title
154
+ @description = reference_criteria.description
155
+ end
156
+ end
157
+ else
158
+ puts "MISSING_DC_REF: #{ref_id}" unless @variable
159
+ @definition = 'variable'
160
+ end
161
+ end
162
+
163
+ # Given an entry type (which describes the criteria's purpose) return the appropriate defintino
164
+ def extract_definition_from_entry_type(entry_type)
165
+ case entry_type
166
+ when 'Problem', 'Problems'
167
+ 'diagnosis'
168
+ when 'Encounter', 'Encounters'
169
+ 'encounter'
170
+ when 'LabResults', 'Results'
171
+ 'laboratory_test'
172
+ when 'Procedure', 'Procedures'
173
+ 'procedure'
174
+ when 'Demographics'
175
+ definition_for_demographic
176
+ when 'Derived'
177
+ 'derived'
178
+ else
179
+ fail "Unknown data criteria template identifier [#{entry_type}]"
180
+ end
181
+ end
182
+
183
+ # Return the definition for a known subset of patient characteristics
184
+ def definition_for_demographic
185
+ demographic_type = attr_val('./cda:observationCriteria/cda:code/@code')
186
+ demographic_translation = {
187
+ '21112-8' => 'patient_characteristic_birthdate',
188
+ '424144002' => 'patient_characteristic_age',
189
+ '263495000' => 'patient_characteristic_gender',
190
+ '102902016' => 'patient_characteristic_languages',
191
+ '125680007' => 'patient_characteristic_marital_status',
192
+ '103579009' => 'patient_characteristic_race'
193
+ }
194
+ if demographic_translation[demographic_type]
195
+ demographic_translation[demographic_type]
196
+ else
197
+ fail "Unknown demographic identifier [#{demographic_type}]"
198
+ end
199
+ end
200
+ end
201
+ end
@@ -0,0 +1,85 @@
1
+ module HQMF2
2
+ # Processing on data criteria after the initial extractions have taken place
3
+ module DataCriteriaPostProcessing
4
+ # Handles settings values after (most) values have been setup
5
+ def post_processing
6
+ extract_code_list_path_and_result_value
7
+
8
+ # prefix ids that start with numerical values, and strip tokens from others
9
+ @id = strip_tokens(@id)
10
+ @children_criteria.map! { |cc| strip_tokens(cc) }
11
+
12
+ # append "_source" to the criteria since all the source criteria are separated from the non-source with the "_source" identifier
13
+ # "_source" is added to the SDC ids so that we are not duplicating ids between source and non source data criteria lists
14
+ # the derived source data criteria maintain their original ids since they are duplicated in the data criteria and source data criteria lists from the simple xml
15
+ @source_data_criteria = "#{@id}_source" unless (@definition == 'derived' || @definition == 'satisfies_all' || @definition == 'satisfies_any')
16
+ @source_data_criteria = strip_tokens(@source_data_criteria) unless @source_data_criteria.nil?
17
+ @specific_occurrence_const = strip_tokens(@specific_occurrence_const) unless @specific_occurrence_const.nil?
18
+ change_xproduct_to_intersection
19
+ handle_derived_specific_occurrences
20
+ end
21
+
22
+ # Extract the code_list_xpath and the criteria's value from either the location related to the specific occurrence,
23
+ # or from any of the template ids (if multiple exist)
24
+ def extract_code_list_path_and_result_value
25
+ if @template_ids.empty? && @specific_occurrence
26
+ template = @entry.document.at_xpath(
27
+ "//cda:id[@root='#{@source_data_criteria_root}' and @extension='#{@source_data_criteria_extension}']/../cda:templateId/cda:item/@root")
28
+ if template
29
+ mapping = ValueSetHelper.get_mapping_for_template(template.to_s)
30
+ handle_mapping_template(mapping)
31
+ end
32
+ end
33
+ @template_ids.each do |t|
34
+ mapping = ValueSetHelper.get_mapping_for_template(t)
35
+ handle_mapping_template(mapping)
36
+ break if mapping # quit if one template id with a mapping has set these values
37
+ end
38
+ end
39
+
40
+ # Set the value and code_list_xpath using the template mapping held in the ValueSetHelper class
41
+ def handle_mapping_template(mapping)
42
+ if mapping
43
+ if mapping[:valueset_path] && @entry.at_xpath(mapping[:valueset_path])
44
+ @code_list_xpath = mapping[:valueset_path]
45
+ end
46
+ @value = DataCriteriaMethods.parse_value(@entry, mapping[:result_path]) if mapping[:result_path]
47
+ end
48
+ end
49
+
50
+ # Changes XPRODUCT data criteria that has an associated tempalte(s) to an INTERSETION criteria.
51
+ # UNION is used for all other cases.
52
+ def change_xproduct_to_intersection
53
+ # Need to handle grouper criteria that do not have template ids -- these will be union of and intersection
54
+ # criteria
55
+ return unless @template_ids.empty?
56
+ # Change the XPRODUCT to an INTERSECT otherwise leave it as a UNION
57
+ @derivation_operator = HQMF::DataCriteria::INTERSECT if @derivation_operator == HQMF::DataCriteria::XPRODUCT
58
+ @description ||= (@derivation_operator == HQMF::DataCriteria::INTERSECT) ? 'Intersect' : 'Union'
59
+ end
60
+
61
+ # Apply some elements from the reference_criteria to the derived specific occurrence
62
+ def handle_derived_specific_occurrences
63
+ return unless @definition == 'derived'
64
+
65
+ # remove "_source" from source data critera. It gets added in in SpecificOccurrenceAndSource but
66
+ # when it gets added we have not yet determined the definition of the data criteria so we cannot
67
+ # skip adding it. Determining the definition before SpecificOccurrenceAndSource processes doesn't
68
+ # work because we need to know if it is a specific occurrence to be able to figure out the definition
69
+ @source_data_criteria = @source_data_criteria.gsub("_source",'') if @source_data_criteria
70
+
71
+ # Adds a child if none exists (specifically the source criteria)
72
+ @children_criteria << @source_data_criteria if @children_criteria.empty?
73
+ return if @children_criteria.length != 1 ||
74
+ (@source_data_criteria.present? && @children_criteria.first != @source_data_criteria)
75
+ # if child.first is nil, it will be caught in the second statement
76
+ reference_criteria = @data_criteria_references[@children_criteria.first]
77
+ return if reference_criteria.nil?
78
+ @is_derived_specific_occurrence_variable = true # easier to track than all testing all features of these cases
79
+ @subset_operators ||= reference_criteria.subset_operators
80
+ @derivation_operator ||= reference_criteria.derivation_operator
81
+ @description = reference_criteria.description
82
+ @variable = reference_criteria.variable
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,117 @@
1
+ module HQMF2
2
+ # Handles various tasks that the Data Criteria needs performed to obtain and
3
+ # modify secific occurrences
4
+ class SpecificOccurrenceAndSource
5
+ include HQMF2::Utilities
6
+
7
+ def initialize(entry, id, local_variable_name, data_criteria_references, occurrences_map)
8
+ @entry = entry
9
+ @id = id
10
+ @local_variable_name = local_variable_name
11
+ @occurrences_map = occurrences_map
12
+ @is_variable = DataCriteriaMethods.extract_variable(@local_variable_name, @id)
13
+ @data_criteria_references = data_criteria_references
14
+ end
15
+
16
+ # Retrieve the specific occurrence and source data criteria information (or just source if there is no specific)
17
+ def extract_specific_occurrences_and_source_data_criteria
18
+ specific_def = @entry.at_xpath('./*/cda:outboundRelationship[@typeCode="OCCR"]', HQMF2::Document::NAMESPACES)
19
+ source_def = @entry.at_xpath('./*/cda:outboundRelationship[cda:subsetCode/@code="SOURCE"]',
20
+ HQMF2::Document::NAMESPACES)
21
+ if specific_def
22
+ source_data_criteria_extension = HQMF2::Utilities.attr_val(specific_def,
23
+ './cda:criteriaReference/cda:id/@extension')
24
+ source_data_criteria_root = HQMF2::Utilities.attr_val(specific_def, './cda:criteriaReference/cda:id/@root')
25
+
26
+ occurrence_criteria = @data_criteria_references[strip_tokens("#{source_data_criteria_extension}_#{source_data_criteria_root}")]
27
+
28
+ return if occurrence_criteria.nil?
29
+ specific_occurrence_const = HQMF2::Utilities.attr_val(specific_def,
30
+ './cda:localVariableName/@controlInformationRoot')
31
+ specific_occurrence = HQMF2::Utilities.attr_val(specific_def,
32
+ './cda:localVariableName/@controlInformationExtension')
33
+
34
+ # FIXME: Remove debug statements after cleaning up occurrence handling
35
+ # build regex for extracting alpha-index of specific occurrences
36
+ occurrence_identifier = obtain_occurrence_identifier(strip_tokens(@id),
37
+ strip_tokens(@local_variable_name) || '',
38
+ strip_tokens(source_data_criteria_extension),
39
+ @is_variable)
40
+
41
+ handle_specific_and_source(occurrence_identifier, source_data_criteria_extension, source_data_criteria_root,
42
+ specific_occurrence_const, specific_occurrence)
43
+
44
+ elsif source_def
45
+ extension = HQMF2::Utilities.attr_val(source_def, './cda:criteriaReference/cda:id/@extension')
46
+ root = HQMF2::Utilities.attr_val(source_def, './cda:criteriaReference/cda:id/@root')
47
+ ["#{extension}_#{root}_source", root, extension] # return the soruce data criteria itself, the rest will be blank
48
+ end
49
+ end
50
+
51
+ # Handle setting the specific and source instance variables with a given occurrence identifier
52
+ def handle_specific_and_source(occurrence_identifier, source_data_criteria_extension, source_data_criteria_root,
53
+ specific_occurrence_const, specific_occurrence)
54
+ source_data_criteria = "#{source_data_criteria_extension}_#{source_data_criteria_root}_source"
55
+ if !occurrence_identifier.blank?
56
+ # if it doesn't exist, add extracted occurrence to the map
57
+ # puts "\tSetting #{@source_data_criteria}-#{@source_data_criteria_root} to #{occurrence_identifier}"
58
+ @occurrences_map[strip_tokens(source_data_criteria)] ||= occurrence_identifier
59
+ specific_occurrence ||= occurrence_identifier
60
+ specific_occurrence_const = "#{source_data_criteria}".upcase
61
+ else
62
+ # create variable occurrences that do not already exist
63
+ if @is_variable
64
+ # puts "\tSetting #{@source_data_criteria}-#{@source_data_criteria_root} to #{occurrence_identifier}"
65
+ @occurrences_map[strip_tokens(source_data_criteria)] ||= occurrence_identifier
66
+ end
67
+ occurrence = @occurrences_map.try(:[], strip_tokens(source_data_criteria))
68
+ unless occurrence
69
+ fail "Could not find occurrence mapping for #{source_data_criteria}, #{source_data_criteria_root}"
70
+ end
71
+ # puts "\tUsing #{occurrence} for #{@id}"
72
+ specific_occurrence ||= occurrence
73
+ end
74
+
75
+ specific_occurrence = 'A' unless specific_occurrence
76
+ specific_occurrence_const = source_data_criteria.upcase unless specific_occurrence_const
77
+ [source_data_criteria, source_data_criteria_root, source_data_criteria_extension,
78
+ specific_occurrence, specific_occurrence_const]
79
+ end
80
+
81
+ # Using the id, source data criteria id, and local variable name (and whether or not it's a variable),
82
+ # extract the occurrence identifiter (if one exists).
83
+ def obtain_occurrence_identifier(stripped_id, stripped_lvn, stripped_sdc, is_variable)
84
+ if is_variable || (stripped_sdc.include? 'qdm_var')
85
+ occurrence_lvn_regex = 'occ[A-Z]of_qdm_var'
86
+ occurrence_id_regex = 'occ[A-Z]of_qdm_var'
87
+ occ_index = 3
88
+ return handle_occurrence_var(stripped_id, stripped_lvn, stripped_sdc, occurrence_id_regex, occurrence_lvn_regex, occ_index)
89
+ else
90
+ occurrence_lvn_regex = 'Occurrence[A-Z]of'
91
+ occurrence_id_regex = 'Occurrence[A-Z]_'
92
+ occ_index = 10
93
+
94
+ occurrence_identifier = handle_occurrence_var(
95
+ stripped_id, stripped_lvn, stripped_sdc,
96
+ "#{occurrence_id_regex}#{stripped_sdc}", "#{occurrence_lvn_regex}#{stripped_sdc}",
97
+ occ_index)
98
+ return occurrence_identifier if occurrence_identifier.present?
99
+
100
+ stripped_sdc[occ_index] if stripped_sdc.match(
101
+ /(^#{occurrence_id_regex}| ^#{occurrence_id_regex}qdm_var_| ^#{occurrence_lvn_regex})| ^#{occurrence_lvn_regex}qdm_var_/)
102
+ end
103
+ end
104
+
105
+ # If the occurrence is a variable, extract the occurrence identifier (if present)
106
+ def handle_occurrence_var(stripped_id, stripped_lvn, stripped_sdc, occurrence_id_compare, occurrence_lvn_compare, occ_index)
107
+ # TODO: Handle specific occurrences of variables that don't self-reference?
108
+ if stripped_id.match(/^#{occurrence_id_compare}/)
109
+ return stripped_id[occ_index]
110
+ elsif stripped_lvn.match(/^#{occurrence_lvn_compare}/)
111
+ return stripped_lvn[occ_index]
112
+ elsif stripped_sdc.match(/^#{occurrence_id_compare}/)
113
+ return stripped_sdc[occ_index]
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,304 @@
1
+ module HQMF2
2
+ # Class representing an HQMF document
3
+ class Document
4
+ include HQMF2::Utilities, HQMF2::DocumentUtilities
5
+ NAMESPACES = { 'cda' => 'urn:hl7-org:v3',
6
+ 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
7
+ 'qdm' => 'urn:hhs-qdm:hqmf-r2-extensions:v1' }
8
+
9
+ attr_reader :measure_period, :id, :hqmf_set_id, :hqmf_version_number, :populations, :attributes,
10
+ :source_data_criteria
11
+
12
+ # Create a new HQMF2::Document instance by parsing the given HQMF contents
13
+ # @param [String] containing the HQMF contents to be parsed
14
+ def initialize(hqmf_contents, use_default_measure_period = true)
15
+ setup_default_values(hqmf_contents, use_default_measure_period)
16
+
17
+ extract_criteria
18
+
19
+ # Extract the population criteria and population collections
20
+ pop_helper = HQMF2::DocumentPopulationHelper.new(@entry, @doc, self, @id_generator, @reference_ids)
21
+ @populations, @population_criteria = pop_helper.extract_populations_and_criteria
22
+
23
+ # Remove any data criteria from the main data criteria list that already has an equivalent member
24
+ # and no references to it. The goal of this is to remove any data criteria that should not
25
+ # be purely a source.
26
+ @data_criteria.reject! do |dc|
27
+ criteria_covered_by_criteria?(dc)
28
+ end
29
+ end
30
+
31
+ # Get the title of the measure
32
+ # @return [String] the title
33
+ def title
34
+ @doc.at_xpath('cda:QualityMeasureDocument/cda:title/@value', NAMESPACES).inner_text
35
+ end
36
+
37
+ # Get the description of the measure
38
+ # @return [String] the description
39
+ def description
40
+ description = @doc.at_xpath('cda:QualityMeasureDocument/cda:text/@value', NAMESPACES)
41
+ description.nil? ? '' : description.inner_text
42
+ end
43
+
44
+ # Get all the population criteria defined by the measure
45
+ # @return [Array] an array of HQMF2::PopulationCriteria
46
+ def all_population_criteria
47
+ @population_criteria
48
+ end
49
+
50
+ # Get a specific population criteria by id.
51
+ # @param [String] id the population identifier
52
+ # @return [HQMF2::PopulationCriteria] the matching criteria, raises an Exception if not found
53
+ def population_criteria(id)
54
+ find(@population_criteria, :id, id)
55
+ end
56
+
57
+ # Get all the data criteria defined by the measure
58
+ # @return [Array] an array of HQMF2::DataCriteria describing the data elements used by the measure
59
+ def all_data_criteria
60
+ @data_criteria
61
+ end
62
+
63
+ # Get a specific data criteria by id.
64
+ # @param [String] id the data criteria identifier
65
+ # @return [HQMF2::DataCriteria] the matching data criteria, raises an Exception if not found
66
+ def data_criteria(id)
67
+ find(@data_criteria, :id, id)
68
+ end
69
+
70
+ # Adds data criteria to the Document's criteria list
71
+ # needed so data criteria can be added to a document from other objects
72
+ def add_data_criteria(dc)
73
+ @data_criteria << dc
74
+ end
75
+
76
+ # Finds a data criteria by it's local variable name
77
+ def find_criteria_by_lvn(local_variable_name)
78
+ find(@data_criteria, :local_variable_name, local_variable_name)
79
+ end
80
+
81
+ # Get ids of data criteria directly referenced by others
82
+ # @return [Array] an array of ids of directly referenced data criteria
83
+ def all_reference_ids
84
+ @reference_ids
85
+ end
86
+
87
+ # Adds id of a data criteria to the list of reference ids
88
+ def add_reference_id(id)
89
+ @reference_ids << id
90
+ end
91
+
92
+ # Parse an XML document from the supplied contents
93
+ # @return [Nokogiri::XML::Document]
94
+ def self.parse(hqmf_contents)
95
+ doc = hqmf_contents.is_a?(Nokogiri::XML::Document) ? hqmf_contents : Nokogiri::XML(hqmf_contents)
96
+ doc.root.add_namespace_definition('cda', 'urn:hl7-org:v3')
97
+ doc
98
+ end
99
+
100
+ # Generates this classes hqmf-model equivalent
101
+ def to_model
102
+ dcs = all_data_criteria.collect(&:to_model)
103
+ pcs = all_population_criteria.collect(&:to_model)
104
+ sdc = source_data_criteria.collect(&:to_model)
105
+ HQMF::Document.new(@id, @id, @hqmf_set_id, @hqmf_version_number, @cms_id,
106
+ title, description, pcs, dcs, sdc,
107
+ @attributes, @measure_period, @populations)
108
+ end
109
+
110
+ # Finds an element within the collection given that has an instance variable or method of "attribute" with a value
111
+ # of "value"
112
+ def find(collection, attribute, value)
113
+ collection.find { |e| e.send(attribute) == value }
114
+ end
115
+
116
+ private
117
+
118
+ # Handles setup of the base values of the document, defined here as ones that are either
119
+ # obtained from the xml directly or with limited parsing
120
+ def setup_default_values(hqmf_contents, use_default_measure_period)
121
+ @id_generator = IdGenerator.new
122
+ @doc = @entry = Document.parse(hqmf_contents)
123
+
124
+ @id = attr_val('cda:QualityMeasureDocument/cda:id/@extension') ||
125
+ attr_val('cda:QualityMeasureDocument/cda:id/@root').upcase
126
+ @hqmf_set_id = attr_val('cda:QualityMeasureDocument/cda:setId/@extension') ||
127
+ attr_val('cda:QualityMeasureDocument/cda:setId/@root').upcase
128
+ @hqmf_version_number = attr_val('cda:QualityMeasureDocument/cda:versionNumber/@value')
129
+
130
+ # TODO: -- figure out if this is the correct thing to do -- probably not, but is
131
+ # necessary to get the bonnie comparison to work. Currently
132
+ # defaulting measure period to a period of 1 year from 2012 to 2013 this is overriden during
133
+ # calculation with correct year information . Need to investigate parsing mp from meaures.
134
+ @measure_period = extract_measure_period_or_default(use_default_measure_period)
135
+
136
+ # Extract measure attributes
137
+ # TODO: Review
138
+ @attributes = @doc.xpath('/cda:QualityMeasureDocument/cda:subjectOf/cda:measureAttribute', NAMESPACES)
139
+ .collect do |attribute|
140
+ read_attribute(attribute)
141
+ end
142
+
143
+ @data_criteria = []
144
+ @source_data_criteria = []
145
+ @data_criteria_references = {}
146
+ @occurrences_map = {}
147
+
148
+ # Used to keep track of referenced data criteria ids
149
+ @reference_ids = []
150
+ end
151
+
152
+ # Extracts a measure period from the document or returns the default measure period
153
+ # (if the default value is set to true).
154
+ def extract_measure_period_or_default(default)
155
+ if default
156
+ mp_low = HQMF::Value.new('TS', nil, '201201010000', nil, nil, nil)
157
+ mp_high = HQMF::Value.new('TS', nil, '201212312359', nil, nil, nil)
158
+ mp_width = HQMF::Value.new('PQ', 'a', '1', nil, nil, nil)
159
+ HQMF::EffectiveTime.new(mp_low, mp_high, mp_width)
160
+ else
161
+ measure_period_def = @doc.at_xpath('cda:QualityMeasureDocument/cda:controlVariable/cda:measurePeriod/cda:value',
162
+ NAMESPACES)
163
+ EffectiveTime.new(measure_period_def).to_model if measure_period_def
164
+ end
165
+ end
166
+
167
+ # Handles parsing the attributes of the document
168
+ def read_attribute(attribute)
169
+ id = attribute.at_xpath('./cda:id/@root', NAMESPACES).try(:value)
170
+ code = attribute.at_xpath('./cda:code/@code', NAMESPACES).try(:value)
171
+ name = attribute.at_xpath('./cda:code/cda:displayName/@value', NAMESPACES).try(:value)
172
+ value = attribute.at_xpath('./cda:value/@value', NAMESPACES).try(:value)
173
+
174
+ id_obj = nil
175
+ if attribute.at_xpath('./cda:id', NAMESPACES)
176
+ id_obj = HQMF::Identifier.new(attribute.at_xpath('./cda:id/@xsi:type', NAMESPACES).try(:value),
177
+ id,
178
+ attribute.at_xpath('./cda:id/@extension', NAMESPACES).try(:value))
179
+ end
180
+
181
+ code_obj = nil
182
+ if attribute.at_xpath('./cda:code', NAMESPACES)
183
+ code_obj, null_flavor, o_text = handle_attribute_code(attribute, code, name)
184
+
185
+ # Mapping for nil values to align with 1.0 parsing
186
+ code = null_flavor if code.nil?
187
+ name = o_text if name.nil?
188
+
189
+ end
190
+
191
+ value_obj = nil
192
+ value_obj = handle_attribute_value(attribute, value) if attribute.at_xpath('./cda:value', NAMESPACES)
193
+
194
+ # Handle the cms_id - changed to eCQM in MAT 5.4 (QDM 5.3)
195
+ @cms_id = "CMS#{value}v#{@hqmf_version_number.to_i}" if (name.include? 'eMeasure Identifier') || (name.include? 'eCQM Identifier')
196
+
197
+ HQMF::Attribute.new(id, code, value, nil, name, id_obj, code_obj, value_obj)
198
+ end
199
+
200
+ # Extracts the code used by a particular attribute
201
+ def handle_attribute_code(attribute, code, name)
202
+ null_flavor = attribute.at_xpath('./cda:code/@nullFlavor', NAMESPACES).try(:value)
203
+ o_text = attribute.at_xpath('./cda:code/cda:originalText/@value', NAMESPACES).try(:value)
204
+ code_obj = HQMF::Coded.new(attribute.at_xpath('./cda:code/@xsi:type', NAMESPACES).try(:value) || 'CD',
205
+ attribute.at_xpath('./cda:code/@codeSystem', NAMESPACES).try(:value),
206
+ code,
207
+ attribute.at_xpath('./cda:code/@valueSet', NAMESPACES).try(:value),
208
+ name,
209
+ null_flavor,
210
+ o_text)
211
+ [code_obj, null_flavor, o_text]
212
+ end
213
+
214
+ # Extracts the value used by a particular attribute
215
+ def handle_attribute_value(attribute, value)
216
+ type = attribute.at_xpath('./cda:value/@xsi:type', NAMESPACES).try(:value)
217
+ case type
218
+ when 'II'
219
+ if value.nil?
220
+ value = attribute.at_xpath('./cda:value/@extension', NAMESPACES).try(:value)
221
+ end
222
+ HQMF::Identifier.new(type,
223
+ attribute.at_xpath('./cda:value/@root', NAMESPACES).try(:value),
224
+ attribute.at_xpath('./cda:value/@extension', NAMESPACES).try(:value))
225
+ when 'ED'
226
+ HQMF::ED.new(type, value, attribute.at_xpath('./cda:value/@mediaType', NAMESPACES).try(:value))
227
+ when 'CD'
228
+ HQMF::Coded.new('CD',
229
+ attribute.at_xpath('./cda:value/@codeSystem', NAMESPACES).try(:value),
230
+ attribute.at_xpath('./cda:value/@code', NAMESPACES).try(:value),
231
+ attribute.at_xpath('./cda:value/@valueSet', NAMESPACES).try(:value),
232
+ attribute.at_xpath('./cda:value/cda:displayName/@value', NAMESPACES).try(:value))
233
+ else
234
+ value.present? ? HQMF::GenericValueContainer.new(type, value) : HQMF::AnyValue.new(type)
235
+ end
236
+ end
237
+
238
+ def extract_criteria
239
+ # Extract the data criteria
240
+ extracted_criteria = []
241
+ @doc.xpath('cda:QualityMeasureDocument/cda:component/cda:dataCriteriaSection/cda:entry', NAMESPACES)
242
+ .each do |entry|
243
+ extracted_criteria << entry
244
+ end
245
+
246
+ # Extract the source data criteria from data criteria
247
+ @source_data_criteria, collapsed_source_data_criteria = SourceDataCriteriaHelper.get_source_data_criteria_list(
248
+ extracted_criteria, @data_criteria_references, @occurrences_map)
249
+
250
+ extracted_criteria.each do |entry|
251
+ criteria = DataCriteria.new(entry, @data_criteria_references, @occurrences_map)
252
+ handle_data_criteria(criteria, collapsed_source_data_criteria)
253
+ @data_criteria << criteria
254
+ end
255
+ end
256
+
257
+ def handle_data_criteria(criteria, collapsed_source_data_criteria)
258
+ # Sometimes there are multiple criteria with the same ID, even though they're different; in the HQMF
259
+ # criteria refer to parent criteria via outboundRelationship, using an extension (aka ID) and a root;
260
+ # we use just the extension to follow the reference, and build the lookup hash using that; since they
261
+ # can repeat, we wind up overwriting some content. This becomes important when we want, for example,
262
+ # the code_list_id and we overwrite the parent with the code_list_id with a child with the same ID
263
+ # without the code_list_id. As a temporary approach, we only overwrite a data criteria reference if
264
+ # it doesn't have a code_list_id. As a longer term approach we may want to use the root for lookups.
265
+ if criteria && (@data_criteria_references[criteria.id].try(:code_list_id).nil?)
266
+ @data_criteria_references[criteria.id] = criteria
267
+ end
268
+ if collapsed_source_data_criteria.key?(criteria.id)
269
+ candidate = find(all_data_criteria, :id, collapsed_source_data_criteria[criteria.id])
270
+ # derived criteria should not be collapsed... they do not have enough info to be collapsed and may cross into the wrong criteria
271
+ # only add the collapsed as a source for derived if it is stripped of any temporal references, fields, etc. to make sure we don't cross into an incorrect source
272
+ if ((criteria.definition != 'derived') || (!candidate.nil? && SourceDataCriteriaHelper.already_stripped?(candidate)))
273
+ criteria.instance_variable_set(:@source_data_criteria, collapsed_source_data_criteria[criteria.id])
274
+ end
275
+ end
276
+
277
+ handle_variable(criteria, collapsed_source_data_criteria) if criteria.variable
278
+ handle_specific_source_data_criteria_reference(criteria)
279
+ @reference_ids.concat(criteria.children_criteria)
280
+ if criteria.temporal_references
281
+ criteria.temporal_references.each do |tr|
282
+ @reference_ids << tr.reference.id if tr.reference.id != HQMF::Document::MEASURE_PERIOD_ID
283
+ end
284
+ end
285
+ end
286
+
287
+ # For specific occurrence data criteria, make sure the source data criteria reference points
288
+ # to the correct source data criteria.
289
+ def handle_specific_source_data_criteria_reference(criteria)
290
+ original_sdc = find(@source_data_criteria, :id, criteria.source_data_criteria)
291
+ updated_sdc = find(@source_data_criteria, :id, criteria.id)
292
+ if !updated_sdc.nil? && !criteria.specific_occurrence.nil? && (original_sdc.nil? || original_sdc.specific_occurrence.nil?)
293
+ criteria.instance_variable_set(:@source_data_criteria, criteria.id)
294
+ end
295
+ return if original_sdc.nil?
296
+ if (criteria.specific_occurrence && !original_sdc.specific_occurrence)
297
+ original_sdc.instance_variable_set(:@specific_occurrence, criteria.specific_occurrence)
298
+ original_sdc.instance_variable_set(:@specific_occurrence_const, criteria.specific_occurrence_const)
299
+ original_sdc.instance_variable_set(:@code_list_id, criteria.code_list_id)
300
+ end
301
+ end
302
+
303
+ end
304
+ end