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

#canonical_url_without_version(url) ⇒ Object



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

def canonical_url_without_version(url)
  url&.split('|')&.first
end

#discriminator_path(discriminator) ⇒ Object



248
249
250
251
252
253
254
255
256
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 248

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



83
84
85
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 83

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

#element_matches_choice_type?(element, target_path, target_type) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
181
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 178

def element_matches_choice_type?(element, target_path, target_type)
  [element.id, element.path].include?(target_path) &&
    element.type.any? { |type| type.code.casecmp?(target_type) }
end

#element_part_of_slice_discrimination?(element) ⇒ Boolean

Returns:

  • (Boolean)


353
354
355
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 353

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

#extension_discriminator_step?(path) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 111

def extension_discriminator_step?(path)
  path.start_with?('extension(') || path.start_with?('modifierExtension(')
end

#extension_profile_matches?(type, ext_url) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 127

def extension_profile_matches?(type, ext_url)
  type.code == 'Extension' && type.profile.any? { |profile| profile.start_with?(ext_url) }
end

#extract_required_binding_values(pattern_element, metadata) ⇒ Object



225
226
227
228
229
230
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 225

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



473
474
475
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 473

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

#extract_target_profiles(type) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 410

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_choice_element(current_element, target_element, target_type) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 170

def find_choice_element(current_element, target_element, target_type)
  [current_element.id, current_element.path].filter_map do |base_path|
    profile_elements.find do |element|
      element_matches_choice_type?(element, "#{base_path}.#{target_element}", target_type)
    end
  end.first
end

#find_element_by_discriminator_path(current_element, discriminator_path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 93

def find_element_by_discriminator_path(current_element, discriminator_path)
  target_element = current_element
  remaining_path = discriminator_path

  while remaining_path.present?
    target_element, remaining_path = take_discriminator_step(target_element, remaining_path)
    return nil if target_element.nil?
  end

  target_element
end

#get_type_must_support_metadata(current_metadata, current_element) ⇒ Object



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 386

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



427
428
429
430
431
432
433
434
435
436
437
438
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 427

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



361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 361

def handle_fixed_values(, element)
  if value_not_empty?(element.fixed)
    [: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



402
403
404
405
406
407
408
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 402

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

#legacy_choice_discriminator_step?(path) ⇒ Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 147

def legacy_choice_discriminator_step?(path)
  path.match?(/\A.+\s+as\s+[[:word:]]+\z/)
end

#must_support_elementsObject



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 440

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? 'xtension' }
end

#must_support_extensionsObject



52
53
54
55
56
57
58
59
60
61
62
63
# 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: canonical_url_without_version(element.type.first.profile.first),
      modifier_extension: element.path.end_with?('modifierExtension')
    }.tap do ||
      [:by_requirement_extension_only] = true if by_requirement_extension_only?(element)
    end
  end
end

#must_support_slice_elementsObject



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

def must_support_slice_elements
  profile_elements.select do |element|
    next false if element.sliceName.blank? || element.path.end_with?('xtension')

    all_must_support_elements.include?(element) || slice_has_must_support_descendants?(element)
  end
end

#must_support_slicesObject



345
346
347
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 345

def must_support_slices
  type_slices + value_slices
end

#must_support_type_slice_elementsObject



232
233
234
235
236
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 232

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



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

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


238
239
240
241
242
243
244
245
246
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 238

def navigation_compatible_discriminator_path(discriminator_path)
  normalized_path = discriminator_path&.gsub(/(modifierExtension|extension)\('([^']+)'\)/, "\\1.where(url='\\2')")
  normalized_path = normalized_path&.gsub(/([[:word:]\[\]]+)\.ofType\(([^)]+)\)/) do
    "#{Regexp.last_match(1).delete_suffix('[x]')}#{Regexp.last_match(2).upcase_first}"
  end
  normalized_path&.gsub(/([[:word:]\[\]]+)\s+as\s+([[:word:]]+)/) do
    "#{Regexp.last_match(1).delete_suffix('[x]')}#{Regexp.last_match(2).upcase_first}"
  end
end

#plain_must_support_elementsObject



349
350
351
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 349

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



477
478
479
480
481
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 477

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

#required_binding_pattern?(pattern_element) ⇒ Boolean

Returns:

  • (Boolean)


221
222
223
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 221

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



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
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 183

def save_pattern_slice(pattern_element, discriminator_path, )
  runtime_path = navigation_compatible_discriminator_path(discriminator_path)

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

#save_type_code?(type) ⇒ Boolean

Returns:

  • (Boolean)


382
383
384
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 382

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

#slice_has_must_support_descendants?(slice) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 87

def slice_has_must_support_descendants?(slice)
  all_must_support_elements.any? do |element|
    element.id.start_with?("#{slice.id}.")
  end
end

#sliced_element(slice) ⇒ Object



77
78
79
80
81
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 77

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

#take_choice_discriminator_step(current_element, step_path, remaining_path) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 151

def take_choice_discriminator_step(current_element, step_path, remaining_path)
  target_element = "#{step_path}[x]"
  target_type, remaining_path = remaining_path.delete_prefix('ofType(').split(')', 2)
  next_element = profile_elements.find do |element|
    element.id == "#{current_element.id}.#{target_element}" &&
      element.type.any? { |type| type.code.casecmp?(target_type) }
  end

  [next_element, remaining_path&.delete_prefix('.')]
end

#take_discriminator_step(current_element, path) ⇒ Object



105
106
107
108
109
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 105

def take_discriminator_step(current_element, path)
  return take_extension_discriminator_step(current_element, path) if extension_discriminator_step?(path)

  take_standard_discriminator_step(current_element, path)
end

#take_extension_discriminator_step(current_element, path) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 115

def take_extension_discriminator_step(current_element, path)
  extension_type = path.start_with?('modifierExtension(') ? 'modifierExtension' : 'extension'
  ext_url, remaining_path = path.delete_prefix("#{extension_type}('").split("')", 2)
  next_element = profile_elements.find do |element|
    element.path == "#{current_element.path}.#{extension_type}" &&
      element.id.start_with?("#{current_element.id}.#{extension_type}:") &&
      element.type.any? { |type| extension_profile_matches?(type, ext_url) }
  end

  [next_element, remaining_path&.delete_prefix('.')]
end

#take_legacy_choice_discriminator_step(current_element, step_path, remaining_path) ⇒ Object



162
163
164
165
166
167
168
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 162

def take_legacy_choice_discriminator_step(current_element, step_path, remaining_path)
  target_element, target_type = step_path.split(/\s+as\s+/, 2)
  target_element = "#{target_element}[x]" unless target_element.end_with?('[x]')
  next_element = find_choice_element(current_element, target_element, target_type)

  [next_element, remaining_path&.delete_prefix('.')]
end

#take_standard_discriminator_step(current_element, path) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 131

def take_standard_discriminator_step(current_element, path)
  step_path, remaining_path = path.split('.', 2)
  if remaining_path&.start_with?('ofType(')
    return take_choice_discriminator_step(current_element, step_path, remaining_path)
  end
  if legacy_choice_discriminator_step?(step_path)
    return take_legacy_choice_discriminator_step(current_element, step_path, remaining_path)
  end

  next_element =
    profile_elements.find { |element| element.id == "#{current_element.id}.#{step_path}" } ||
    profile_elements.find { |element| element.id == "#{current_element.path}.#{step_path}" }

  [next_element, remaining_path&.delete_prefix('.')]
end

#type_must_support_extension?(extensions) ⇒ Boolean

Returns:

  • (Boolean)


375
376
377
378
379
380
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 375

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



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 258

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
     = {
      type: 'type',
      code: type_code.upcase_first
    }
    runtime_path = navigation_compatible_discriminator_path(type_path)
    [:path] = runtime_path if runtime_path.present?

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

#value_not_empty?(value) ⇒ Boolean

Returns:

  • (Boolean)


357
358
359
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 357

def value_not_empty?(value)
  value.present? || value == false
end

#value_slicesObject

rubocop:disable Metrics/CyclomaticComplexity



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/inferno/dsl/must_support_metadata_extractor.rb', line 290

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.nil?
          pattern_value = {
            type: 'unsupported',
            path: navigation_compatible_discriminator_path(discriminator_path)
          }
        elsif value_not_empty?(pattern_element.fixed)
          fixed_values << {
            path: navigation_compatible_discriminator_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