Class: Inferno::DSL::MustSupportMetadataExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/inferno/dsl/must_support_metadata_extractor.rb

Overview

The MustSupportMetadataExtractor takes a StructureDefinition and parses it into a hash-based metadata that simplifies checking for MustSupport elements. MustSupport elements may be either plain elements, extensions, or slices. This logic was originally developed for the US Core Test Kit and has been migrated into Inferno core.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile_elements, profile, resource, ig_resources, requirement_extension_url = nil) ⇒ MustSupportMetadataExtractor

Construct a new extractor

Parameters:

  • profile_elements (Array<FHIR::ElementDefinition>)

    the elements of the profile to consider, ie, profile.snapshot.element

  • profile (FHIR::StructureDefinition)

    the profile to parse

  • resource (String)

    the resourceType that the profile applies to, ie, profile.type

  • ig_resources (Inferno::Entities::IG)
  • requirement_extension_url (String) (defaults to: nil)

    the URL of an extension to flag elements as required even if not MS



19
20
21
22
23
24
25
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 19

def initialize(profile_elements, profile, resource, ig_resources, requirement_extension_url = nil)
  self.profile_elements = profile_elements
  self.profile = profile
  self.resource = resource
  self.ig_resources = ig_resources
  self.requirement_extension_url = requirement_extension_url
end

Instance Attribute Details

#ig_resourcesObject

Returns the value of attribute ig_resources.



10
11
12
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 10

def ig_resources
  @ig_resources
end

#profileObject

Returns the value of attribute profile.



10
11
12
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 10

def profile
  @profile
end

#profile_elementsObject

Returns the value of attribute profile_elements.



10
11
12
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 10

def profile_elements
  @profile_elements
end

#requirement_extension_urlObject

Returns the value of attribute requirement_extension_url.



10
11
12
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 10

def requirement_extension_url
  @requirement_extension_url
end

#resourceObject

Returns the value of attribute resource.



10
11
12
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 10

def resource
  @resource
end

Instance Method Details

#all_must_support_elementsObject



44
45
46
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 44

def all_must_support_elements
  profile_elements.select { |element| element.mustSupport || by_requirement_extension_only?(element) }
end

#by_requirement_extension_only?(element) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 37

def by_requirement_extension_only?(element)
  requirement_extension_url && !element.mustSupport &&
    element.extension.any? do |extension|
      extension.url == requirement_extension_url && extension.valueBoolean
    end
end

#discriminator_path(discriminator) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 142

def discriminator_path(discriminator)
  if discriminator.path == '$this'
    ''
  elsif discriminator.path.start_with?('$this.')
    discriminator.path[6..]
  else
    discriminator.path
  end
end

#discriminators(slice) ⇒ Object



76
77
78
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 76

def discriminators(slice)
  slice&.slicing&.discriminator
end

#element_part_of_slice_discrimination?(element) ⇒ Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 239

def element_part_of_slice_discrimination?(element)
  must_support_slice_elements.any? { |ms_slice| element.id.include?(ms_slice.id) }
end

#extract_required_binding_values(pattern_element, metadata) ⇒ Object



129
130
131
132
133
134
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 129

def extract_required_binding_values(pattern_element, )
  value_extractor = ValueExtractor.new(ig_resources, resource, profile_elements)

  value_extractor.codings_from_value_set_binding(pattern_element).presence ||
    value_extractor.([[:path]]).presence || []
end

#extract_supported_types(current_element) ⇒ Object



355
356
357
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 355

def extract_supported_types(current_element)
  current_element.type.select { |type| save_type_code?(type) }.map(&:code)
end

#extract_target_profiles(type) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 292

def extract_target_profiles(type)
  target_profiles = []

  if type.targetProfile&.length == 1
    target_profiles << type.targetProfile.first
  else
    type.source_hash['_targetProfile']&.each_with_index do |hash, index|
      if hash.present?
        element = FHIR::Element.new(hash)
        target_profiles << type.targetProfile[index] if type_must_support_extension?(element.extension)
      end
    end
  end

  target_profiles
end

#find_element_by_discriminator_path(current_element, discriminator_path) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 80

def find_element_by_discriminator_path(current_element, discriminator_path)
  if discriminator_path.present?
    profile_elements.find { |element| element.id == "#{current_element.id}.#{discriminator_path}" } ||
      profile_elements.find { |element| element.id == "#{current_element.path}.#{discriminator_path}" }
  else
    current_element
  end
end

#get_type_must_support_metadata(current_metadata, current_element) ⇒ Object



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 268

def (, current_element)
  current_element.type.map do |type|
    next unless type_must_support_extension?(type.extension)

     =
      {
        path: "#{[:path].delete_suffix('[x]')}#{type.code.upcase_first}",
        original_path: [:path]
      }
    [:types] = [type.code] if save_type_code?(type)
    handle_type_must_support_target_profiles(type, ) if type.code == 'Reference'

    
  end.compact
end

#handle_choice_type_in_sliced_element(current_metadata, must_support_elements_metadata) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 309

def handle_choice_type_in_sliced_element(, )
   = .find do ||
    [:original_path].present? &&
      [:path].include?([:original_path])
  end

  return unless .present?

  [:original_path] = [:path]
  [:path] =
    [:path].sub([:original_path], [:path])
end

#handle_fixed_values(metadata, element) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 243

def handle_fixed_values(, element)
  if element.fixed.present?
    [:fixed_value] = element.fixed
  elsif element.patternCodeableConcept.present? && !element_part_of_slice_discrimination?(element)
    [:fixed_value] = element.patternCodeableConcept.coding.first.code
    [:path] += '.coding.code'
  elsif element.fixedCode.present?
    [:fixed_value] = element.fixedCode
  elsif element.patternIdentifier.present? && !element_part_of_slice_discrimination?(element)
    [:fixed_value] = element.patternIdentifier.system
    [:path] += '.system'
  end
end

#handle_type_must_support_target_profiles(type, metadata) ⇒ Object



284
285
286
287
288
289
290
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 284

def handle_type_must_support_target_profiles(type, )
  target_profiles = extract_target_profiles(type)

  # remove target_profile for FHIR Base resource type.
  target_profiles.delete_if { |reference| reference.start_with?('http://hl7.org/fhir/StructureDefinition') }
  [:target_profiles] = target_profiles if target_profiles.present?
end

#must_support_elementsObject



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 322

def must_support_elements
   = []
  plain_must_support_elements.each do |current_element|
     = {
      path: current_element.id.gsub("#{resource}.", '')
    }
    [:by_requirement_extension_only] = true if by_requirement_extension_only?(current_element)

     = (, current_element)

    if .any?
      .concat()
    else
      handle_choice_type_in_sliced_element(, )

      supported_types = extract_supported_types(current_element)
      [:types] = supported_types if supported_types.present?

      if current_element.type.first&.code == 'Reference'
        handle_type_must_support_target_profiles(current_element.type.first,
                                                 )
      end

      handle_fixed_values(, current_element)

      (, )

       << 
    end
  end
  .uniq
end

#must_support_extension_elementsObject



48
49
50
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 48

def must_support_extension_elements
  all_must_support_elements.select { |element| element.path.end_with? 'extension' }
end

#must_support_extensionsObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 52

def must_support_extensions
  must_support_extension_elements.map do |element|
    {
      id: element.id,
      path: element.path.gsub("#{resource}.", ''),
      url: element.type.first.profile.first
    }.tap do ||
      [:by_requirement_extension_only] = true if by_requirement_extension_only?(element)
    end
  end
end

#must_support_slice_elementsObject



64
65
66
67
68
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 64

def must_support_slice_elements
  all_must_support_elements.select do |element|
    !element.path.end_with?('extension') && element.sliceName.present?
  end
end

#must_support_slicesObject



231
232
233
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 231

def must_support_slices
  type_slices + value_slices
end

#must_support_type_slice_elementsObject



136
137
138
139
140
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 136

def must_support_type_slice_elements
  must_support_slice_elements.select do |element|
    discriminators(sliced_element(element))&.first&.type == 'type'
  end
end

#must_support_value_slice_elementsObject



174
175
176
177
178
179
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 174

def must_support_value_slice_elements
  must_support_slice_elements.select do |element|
    # discriminator type 'pattern' is deprecated in FHIR R5 and made equivalent to 'value'
    ['value', 'pattern'].include?(discriminators(sliced_element(element))&.first&.type)
  end
end

#must_supportsHash

Retrieval method for the must support metadata

Returns:

  • (Hash)


29
30
31
32
33
34
35
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 29

def must_supports
  @must_supports ||= {
    extensions: must_support_extensions,
    slices: must_support_slices,
    elements: must_support_elements
  }
end

#plain_must_support_elementsObject



235
236
237
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 235

def plain_must_support_elements
  all_must_support_elements - must_support_extension_elements - must_support_slice_elements
end

#remove_conflicting_metadata_without_fixed_value(must_support_elements_metadata, current_metadata) ⇒ Object



359
360
361
362
363
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 359

def (, )
  .delete_if do ||
    [:path] == [:path] && [:fixed_value].blank?
  end
end

#required_binding_pattern?(pattern_element) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 125

def required_binding_pattern?(pattern_element)
  pattern_element.binding&.strength == 'required' && pattern_element.binding&.valueSet
end

#save_pattern_slice(pattern_element, discriminator_path, metadata) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 89

def save_pattern_slice(pattern_element, discriminator_path, )
  if pattern_element.patternCodeableConcept
    {
      type: 'patternCodeableConcept',
      path: discriminator_path,
      code: pattern_element.patternCodeableConcept.coding.first.code,
      system: pattern_element.patternCodeableConcept.coding.first.system
    }
  elsif pattern_element.patternCoding
    {
      type: 'patternCoding',
      path: discriminator_path,
      code: pattern_element.patternCoding.code,
      system: pattern_element.patternCoding.system
    }
  elsif pattern_element.patternIdentifier
    {
      type: 'patternIdentifier',
      path: discriminator_path,
      system: pattern_element.patternIdentifier.system
    }
  elsif required_binding_pattern?(pattern_element)
    {
      type: 'requiredBinding',
      path: discriminator_path,
      values: extract_required_binding_values(pattern_element, )
    }
  else
    # prevent errors in case an IG does something different
    {
      type: 'unsupported',
      path: discriminator_path
    }
  end
end

#save_type_code?(type) ⇒ Boolean

Returns:

  • (Boolean)


264
265
266
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 264

def save_type_code?(type)
  type.code == 'Reference'
end

#sliced_element(slice) ⇒ Object



70
71
72
73
74
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 70

def sliced_element(slice)
  profile_elements.find do |element|
    element.id == slice.path || element.id == slice.id.sub(":#{slice.sliceName}", '')
  end
end

#type_must_support_extension?(extensions) ⇒ Boolean

Returns:

  • (Boolean)


257
258
259
260
261
262
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 257

def type_must_support_extension?(extensions)
  extensions&.any? do |extension|
    extension.url == 'http://hl7.org/fhir/StructureDefinition/elementdefinition-type-must-support' &&
      extension.valueBoolean
  end
end

#type_slicesObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 152

def type_slices
  must_support_type_slice_elements.map do |current_element|
    discriminator = discriminators(sliced_element(current_element)).first
    type_path = discriminator_path(discriminator)
    type_element = find_element_by_discriminator_path(current_element, type_path)

    type_code = type_element.type.first.code

    {
      slice_id: current_element.id,
      slice_name: current_element.sliceName,
      path: current_element.path.gsub("#{resource}.", ''),
      discriminator: {
        type: 'type',
        code: type_code.upcase_first
      }
    }.tap do ||
      [:by_requirement_extension_only] = true if by_requirement_extension_only?(current_element)
    end
  end
end

#value_slicesObject

rubocop:disable Metrics/CyclomaticComplexity



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 181

def value_slices # rubocop:disable Metrics/CyclomaticComplexity
  must_support_value_slice_elements.map do |current_element|
    {
      slice_id: current_element.id,
      slice_name: current_element.sliceName,
      path: current_element.path.gsub("#{resource}.", '')
    }.tap do ||
      fixed_values = []
      pattern_value = {}

      element_discriminators = discriminators(sliced_element(current_element))

      element_discriminators.each do |discriminator|
        discriminator_path = discriminator_path(discriminator)
        pattern_element = find_element_by_discriminator_path(current_element, discriminator_path)

        # This is a workaround for a known version of a profile that has a bad discriminator:
        # the discriminator refers to a nested field within a CodeableConcept,
        # but the profile doesn't contain an element definition for it, so there's no way to
        # define a fixed value on the element to define the slice.
        # In this instance the element has a second (good) discriminator on the CodeableConcept field itself,
        # and in subsequent versions of the profile, the bad discriminator was removed.
        next if pattern_element.nil? && element_discriminators.length > 1

        if pattern_element.fixed.present?
          fixed_values << {
            path: discriminator_path,
            value: pattern_element.fixed
          }
        elsif pattern_value.present?
          raise StandardError, "Found more than one pattern slices for the same element #{pattern_element}."
        else
          pattern_value = save_pattern_slice(pattern_element, discriminator_path, )
        end
      end

      if fixed_values.present?
        [:discriminator] = {
          type: 'value',
          values: fixed_values
        }
      elsif pattern_value.present?
        [:discriminator] = pattern_value
      end

      [:by_requirement_extension_only] = true if by_requirement_extension_only?(current_element)
    end
  end
end