Class: FHIR::DSTU2::Definitions

Inherits:
Object
  • Object
show all
Extended by:
Deprecate
Defined in:
lib/fhir_dstu2_models/bootstrap/definitions.rb

Constant Summary collapse

@@defns =
File.expand_path '../definitions', File.dirname(File.absolute_path(__FILE__))
@@types =
nil
@@resources =
nil
@@profiles =
nil
@@extensions =
nil
@@valuesets =
nil
@@search_params =
nil
@@cache =
{}

Class Method Summary collapse

Methods included from Deprecate

deprecate

Class Method Details

.basetype(uri) ⇒ Object

Get the basetype (String) for a given profile or extension.



117
118
119
120
121
122
123
124
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 117

def self.basetype(uri)
  return nil if uri.nil?

  defn = profiles.detect { |x| x['url'] == uri } || extensions.detect { |x| x['url'] == uri }
  return nil if defn.nil?

  defn['baseType']
end

.complex_typesObject



37
38
39
40
41
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 37

def self.complex_types
  # complex data types start with an uppercase letter
  # and we'll filter out profiles on types (for example, Age is a profile on Quantity)
  @complex_types ||= types.select { |t| t['id'].start_with?(*('A'..'Z').to_a) && (t['id'] == t['snapshot']['element'].first['path']) }
end

.extension_definition(extension_name) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 106

def self.extension_definition(extension_name)
  return nil if extension_name.nil?

  extension = extensions.find { |x| x['xmlId'] == extension_name || x['name'] == extension_name || x['url'] == extension_name }
  return nil if extension.nil?

  FHIR::DSTU2::StructureDefinition.new(extension)
end

.get_codes(uri, parent_code = nil) ⇒ Object

Get codes (Array of Strings) for a given expansion.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 207

def self.get_codes(uri, parent_code = nil)
  return nil if uri.nil?
  return @@cache[uri] if @@cache[uri]

  FHIR::DSTU2.logger.debug "Looking up codes for #{uri}"
  valueset = valuesets.select { |x| x['url'] == uri }.first
  if valueset.nil?
    # if we can't identify the valueset by id/uri, see if we can match the codeSystem
    FHIR::DSTU2.logger.debug "Could not find valueset, looking for codeSystem #{uri}"
    valueset = valuesets.select { |v| !v['codeSystem'].nil? && v['codeSystem']['system'] == uri }.first
  end
  if valueset.nil?
    FHIR::DSTU2.logger.debug 'Could not find valueset or code system.'
  else
    FHIR::DSTU2.logger.debug "Found valueset #{valueset['url']}"
    @@cache[uri] = {}
    if !valueset['codeSystem'].nil? && !valueset['codeSystem']['system'].nil?
      FHIR::DSTU2.logger.debug 'Looking at codeSystem...'
      @@cache[uri][valueset['codeSystem']['system']] = []
      valueset['codeSystem']['concept'].each do |concept|
        @@cache[uri][valueset['codeSystem']['system']] += get_codes_from_concept(concept, parent_code)
      end
    end
    if !valueset['expansion'].nil? && !valueset['expansion']['contains'].nil?
      FHIR::DSTU2.logger.debug 'Looking at expansion...'
      keys = valueset['expansion']['contains'].map { |x| x['system'] }.uniq
      keys.each { |x| @@cache[uri][x] = [] }
      valueset['expansion']['contains'].each { |x| @@cache[uri][x['system']] << x['code'] }
    end
    if !valueset['compose'].nil? && !valueset['compose']['include'].nil?
      FHIR::DSTU2.logger.debug 'Looking at compose.include...'
      valueset['compose']['include'].each do |x|
        system = x['system']
        unless @@cache[uri].keys.include?(system)
          included_codes = get_codes(system)
          @@cache[uri][system] = if included_codes
                                   included_codes[system]
                                 else
                                   []
                                 end
        end
        x['concept']&.each { |y| @@cache[uri][system] += get_codes_from_concept(y, parent_code) }
        next unless x['filter']

        x['filter'].each do |filter|
          if filter['property'] == 'concept' && filter['op'] == 'is-a'
            codes = get_codes(system, filter['value'])
            @@cache[uri][system] += codes[system] if codes && codes[system]
          end
        end
      end
      unless valueset['compose']['exclude'].nil?
        FHIR::DSTU2.logger.debug 'Looking at compose.exclude...'
        valueset['compose']['exclude'].each do |x|
          system = x['system']
          unless @@cache[uri].keys.include?(system)
            included_codes = get_codes(system)[system]
            @@cache[uri][system] = if included_codes
                                     included_codes[system]
                                   else
                                     []
                                   end
          end
          x['concept']&.each { |y| @@cache[uri][system].delete(y['code']) }
        end
      end
    end
    if !valueset['compose'].nil? && !valueset['compose']['import'].nil?
      FHIR::DSTU2.logger.debug 'Looking at compose.import...'
      imported_systems = valueset['compose']['import']
      FHIR::DSTU2.logger.debug "Searching #{imported_systems}..."
      imported_systems.each do |importsys|
        # @@cache[uri][system] = [] unless @@cache[uri].keys.include?(system)
        FHIR::DSTU2.logger.debug "Looking at compose.import #{importsys}..."
        results = get_codes(importsys)
        results.each do |sys, codes|
          if codes.empty?
            lookup = get_codes(sys)
            codes = lookup[sys] if lookup
          end
          next unless codes

          codes.each do |code|
            @@cache[uri][sys] = [] unless @@cache[uri].keys.include?(sys)
            @@cache[uri][sys] << code
          end
        end
      end
    end
    @@cache[uri].each { |_system, codes| codes.uniq! }
  end
  FHIR::DSTU2.logger.debug "Done caching codes for #{uri}"
  @@cache[uri]
end

.get_codes_from_concept(concept, filter_code = nil) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 302

def self.get_codes_from_concept(concept, filter_code = nil)
  codes = []
  codes << concept['code'] if concept['code'] == filter_code || filter_code.nil?
  if concept['concept']
    filter_code = nil if concept['code'] == filter_code
    concept['concept'].each do |item|
      codes += get_codes_from_concept(item, filter_code)
    end
  end
  codes
rescue StandardError => e
  FHIR::DSTU2.logger.debug "Unable to extract codes from concept #{concept}: #{e.message}"
end

.get_profile_class(uri) ⇒ Object

Get a dynamically generated class for a given profile.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 146

def self.get_profile_class(uri)
  return nil if uri.nil?

  load_profiles
  load_extensions

  defn = @@profiles.select { |x| x['url'] == uri }.first
  defn = @@extensions.select { |x| x['url'] == uri }.first if defn.nil?

  klass = nil
  unless defn.nil?
    generator = FHIR::DSTU2::Boot::Generator.new(false)
    type = defn['baseType']
    id = defn['id'].gsub(/-|_/, '').capitalize
    defn['id'] = type # override profile id with baseType name for generator
    template = generator.generate_class([type], defn)
    f = Tempfile.new(["profile-#{id}", '.rb'])
    f.write("module FHIR::DSTU2\n")
    f.write("module Profile\n")
    f.write("module #{id}\n")
    f.write(template.to_s)
    3.times { f.write("\nend") }
    f.close
    begin
      # load the profiled class
      load f
      # set the return class type
      klass = Object.const_get("FHIR::DSTU2::Profile::#{id}::#{type}")
    rescue StandardError
      FHIR::DSTU2.logger.error "Failed to generate class for profile #{uri}"
    end
    # unlink the file so it can be garbage collected
    f.unlink
  end
  klass
end

.primitive_typesObject



31
32
33
34
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 31

def self.primitive_types
  # primitive data types start with a lowercase letter
  @primitive_types ||= types.select { |t| t['id'].start_with?(*('a'..'z').to_a) }
end

.profile(uri) ⇒ Object

Get the StructureDefinition for a given profile.



128
129
130
131
132
133
134
135
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 128

def self.profile(uri)
  return nil if uri.nil?

  defn = profiles.detect { |x| x['url'] == uri } || extensions.detect { |x| x['url'] == uri }
  return nil if defn.nil?

  FHIR::DSTU2::StructureDefinition.new(defn)
end

.profiles_for_resource(resource_name) ⇒ Object



138
139
140
141
142
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 138

def self.profiles_for_resource(resource_name)
  return nil if resource_name.nil?

  profiles.select { |x| x['baseType'] == resource_name }.map { |x| FHIR::DSTU2::StructureDefinition.new(x) }
end

.resource_definition(resource_name) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 74

def self.resource_definition(resource_name)
  return nil if resource_name.nil?
  return @@cache[resource_name] if @@cache[resource_name]

  definition = resources.find { |x| x['xmlId'] == resource_name || x['name'] == resource_name || x['url'] == resource_name }
  @@cache[resource_name] = FHIR::DSTU2::StructureDefinition.new(definition) if definition
  @@cache[resource_name]
end

.resource_definitionsObject



69
70
71
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 69

def self.resource_definitions
  resources.select { |r| r['kind'] == 'resource' }
end

.search_parameters(type_name) ⇒ Object



356
357
358
359
360
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 356

def self.search_parameters(type_name)
  return nil if type_name.nil?

  search_params.select { |p| p['base'].include?(type_name) && p['xpath'] && !p['xpath'].include?('extension') }.map { |p| p['code'] }
end

.type_definition(type_name) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 44

def self.type_definition(type_name)
  return nil if type_name.nil?
  return @@cache[type_name] if @@cache[type_name]

  definition = types.find { |x| x['xmlId'] == type_name || x['name'] == type_name || x['url'] == type_name }
  @@cache[type_name] = FHIR::DSTU2::StructureDefinition.new(definition) if definition
  @@cache[type_name]
end

.valuesetsObject


ValueSet Code Expansions



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/fhir_dstu2_models/bootstrap/definitions.rb', line 187

def self.valuesets
  @@valuesets ||= begin
    # load the valuesets
    filename = File.join(@@defns, 'valuesets', 'valuesets.json')
    raw = File.open(filename, 'r:UTF-8', &:read)
    vs = JSON.parse(raw)['entry'].map { |e| e['resource'] }

    filename = File.join(@@defns, 'valuesets', 'v3-codesystems.json')
    raw = File.open(filename, 'r:UTF-8', &:read)
    v3 = JSON.parse(raw)['entry'].map { |e| e['resource'] }

    filename = File.join(@@defns, 'valuesets', 'v2-tables.json')
    raw = File.open(filename, 'r:UTF-8', &:read)
    v2 = JSON.parse(raw)['entry'].map { |e| e['resource'] }

    vs + v3 + v2
  end
end