Class: Inferno::CLI::Evaluate

Inherits:
Thor::Group
  • Object
show all
Defined in:
lib/inferno/apps/cli/evaluate.rb

Instance Method Summary collapse

Instance Method Details

#check_ig_version(ig) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/inferno/apps/cli/evaluate.rb', line 47

def check_ig_version(ig)
  versions = ig.ig_resource.fhirVersion

  return unless versions.any? { |v| v > '4.0.1' }

  puts '**WARNING** The selected IG targets a FHIR version higher than 4.0.1, which is not supported by Inferno.'
end

#evaluate(ig_path, data_path, _log_level) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/inferno/apps/cli/evaluate.rb', line 11

def evaluate(ig_path, data_path, _log_level)
  # NOTE: repositories is required here rather than at the top of the file because
  # the tree of requires means that this file and its requires get required by every CLI app.
  # Sequel::Model, used in some repositories, fetches the table schema at instantiation.
  # This breaks the `migrate` task by fetching a table before the task runs/creates it.
  require_relative '../../repositories'

  validate_args(ig_path, data_path)
  ig = Inferno::Repositories::IGs.new.find_or_load(ig_path)

  check_ig_version(ig)

  data =
    if data_path
      DatasetLoader.from_path(File.join(__dir__, data_path))
    else
      ig.examples
    end

  validator = setup_validator(ig_path)

  evaluator = Inferno::DSL::FHIREvaluation::Evaluator.new(ig, validator)

  config = Inferno::DSL::FHIREvaluation::Config.new
  results = evaluator.evaluate(data, config)
  output_results(results, options[:output])
end

#output_results(results, output) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/inferno/apps/cli/evaluate.rb', line 73

def output_results(results, output)
  if output&.end_with?('json')
    oo = FhirEvaluator::EvaluationResult.to_operation_outcome(results)
    File.write(output, oo.to_json)
    puts "Results written to #{output}"
  else
    counts = results.group_by(&:severity).transform_values(&:count)
    print(counts, 'Result Count')
    puts "\n"
    puts results
  end
end

#pad(string, length) ⇒ Object



99
100
101
# File 'lib/inferno/apps/cli/evaluate.rb', line 99

def pad(string, length)
  format("%#{length}.#{length}s", string)
end


86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/inferno/apps/cli/evaluate.rb', line 86

def print(output_fields, title)
  puts("╔══════════════ #{title} ═══════════════╗")
  puts('║ ╭────────────────┬──────────────────────╮ ║')
  output_fields.each_with_index do |(key, value), i|
    field_name = pad(key, 14)
    field_value = pad(value.to_s, 20)
    puts("║ │ #{field_name}#{field_value} │ ║")
    puts('║ ├────────────────┼──────────────────────┤ ║') unless i == output_fields.length - 1
  end
  puts('║ ╰────────────────┴──────────────────────╯ ║')
  puts('╚═══════════════════════════════════════════╝')
end

#setup_validator(ig_path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/inferno/apps/cli/evaluate.rb', line 55

def setup_validator(ig_path)
  igs_directory = File.join(Dir.pwd, 'data', 'igs')
  if File.exist?(ig_path) && !File.realpath(ig_path).start_with?(igs_directory)
    puts "Copying #{File.basename(ig_path)} to data/igs so it is accessible to validator"
    destination_file_path = File.join(igs_directory, File.basename(ig_path))
    FileUtils.copy_file(ig_path, destination_file_path, true)
    ig_path = "igs/#{File.basename(ig_path)}"
  end
  Inferno::DSL::FHIRResourceValidation::Validator.new(:default, 'evaluator_cli') do
    igs(ig_path)

    cli_context do
      # For our purposes, code display mismatches should be warnings and not affect profile conformance
      displayWarnings(true)
    end
  end
end

#validate_args(ig_path, data_path) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/inferno/apps/cli/evaluate.rb', line 39

def validate_args(ig_path, data_path)
  raise 'A path to an IG is required!' unless ig_path

  return unless data_path && (!File.directory? data_path)

  raise "Provided path '#{data_path}' is not a directory"
end