Class: HQMF2::Precondition

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/hqmf-parser/2.0/precondition.rb

Overview

Represents the logic that defines grouping of criteria and actions done on it.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#attr_val, attr_val, #strip_tokens, #to_xml

Methods included from HQMF::Conversion::Utilities

#build_hash, #check_equality, #json_array, #openstruct_to_json

Constructor Details

#initialize(id, conjunction, preconditions = [], negation = false, reference = nil) ⇒ Precondition

Returns a new instance of Precondition.



57
58
59
60
61
62
63
# File 'lib/hqmf-parser/2.0/precondition.rb', line 57

def initialize(id, conjunction, preconditions = [], negation = false, reference = nil)
  @preconditions = preconditions || []
  @conjunction = conjunction
  @reference = reference
  @negation = negation
  @id = id
end

Instance Attribute Details

#conjunctionObject (readonly)

Returns the value of attribute conjunction.



6
7
8
# File 'lib/hqmf-parser/2.0/precondition.rb', line 6

def conjunction
  @conjunction
end

#idObject (readonly)

Returns the value of attribute id.



6
7
8
# File 'lib/hqmf-parser/2.0/precondition.rb', line 6

def id
  @id
end

#preconditionsObject (readonly)

Returns the value of attribute preconditions.



6
7
8
# File 'lib/hqmf-parser/2.0/precondition.rb', line 6

def preconditions
  @preconditions
end

#referenceObject (readonly)

Returns the value of attribute reference.



6
7
8
# File 'lib/hqmf-parser/2.0/precondition.rb', line 6

def reference
  @reference
end

Class Method Details

.handle_aggregation(id_generator, reference, preconditions, aggregation, conjunction = nil) ⇒ Object

“False” aggregators exist, and require special handling, so this manages that and returns the

proper precondition.


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hqmf-parser/2.0/precondition.rb', line 34

def self.handle_aggregation(id_generator, reference, preconditions, aggregation, conjunction = nil)
  negation = false
  conjunction = aggregation.name
  case conjunction
  # DeMorgan's law is used to handle negated caes: e.g. to find if all are false, negate the "at least one true"
  # check.
  when 'allFalse'
    negation = true
    conjunction = 'atLeastOneTrue'
  when 'atLeastOneFalse'
    negation = true
    conjunction = 'allTrue'
  end
  # Return the proper precondition given if a negation exists
  if negation
    # Wrap the negation in a seperate precondition which this will reference
    precondition_wrapper = new(id_generator.next_id, conjunction, preconditions, true, reference)
    new(id_generator.next_id, conjunction, [precondition_wrapper])
  else
    new(id_generator.next_id, conjunction, preconditions, false, reference)
  end
end

.parse(entry, doc, id_generator) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/hqmf-parser/2.0/precondition.rb', line 8

def self.parse(entry, doc, id_generator)
  doc = doc
  entry = entry
  aggregation = entry.at_xpath('./cda:allTrue | ./cda:atLeastOneTrue | ./cda:allFalse | ./cda:atLeastOneFalse',
                               HQMF2::Document::NAMESPACES)

  # Sets the reference criteria for the precondition (if it exists)
  reference_def = entry.at_xpath('./*/cda:id', HQMF2::Document::NAMESPACES)
  reference_def ||= entry.at_xpath('./cda:join/cda:templateId/cda:item', HQMF2::Document::NAMESPACES)
  reference = Reference.new(reference_def) if reference_def

  # Unless there is an aggregator, no further actions are necessary.
  return new(id_generator.next_id, nil, [], false, reference) unless aggregation

  precondition_entries = entry.xpath('./*/cda:precondition', HQMF2::Document::NAMESPACES)
  preconditions = precondition_entries.collect do |precondition|
    precondition = Precondition.parse(precondition, doc, id_generator)
    # There are cases where a precondition may contain no references or preconditions, and should be ignored.
    precondition.reference.nil? && precondition.preconditions.empty? ? nil : precondition
  end
  preconditions.compact!
  handle_aggregation(id_generator, reference, preconditions, aggregation)
end

Instance Method Details

#to_modelObject

Generates this classes hqmf-model equivalent



66
67
68
69
70
71
# File 'lib/hqmf-parser/2.0/precondition.rb', line 66

def to_model
  pcs = @preconditions.collect(&:to_model)
  mr = @reference ? @reference.to_model : nil
  cc = @conjunction
  HQMF::Precondition.new(@id, pcs, mr, cc, @negation)
end