Class: StructureParser

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/ez7gen/structure_parser.rb

Constant Summary collapse

REGEX_OP =

attr_reader :regExOp, :regExRep

/(?=\[((?:[^\[\]]*|\[\g<1>\])*)\])/
PRNTHS_OP =
'[]'
REGEX_REP =
/(?=\{((?:[^{}]*|\{\g<1>\})*)\})/
PRNTHS_REP =
'{}'

Constants included from Utils

Utils::BASE, Utils::BASE_INDICATOR, Utils::DATA_LOOKUP_MIS, Utils::PRIMARY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#blank?, #get_name_without_base, #get_segment_name, #get_type_by_name, #has_html_encoded_ch?, #is_number?, #num_to_nil, #safe_len, #sample_index

Constructor Details

#initializeStructureParser

idx = 0



15
16
17
18
# File 'lib/ez7gen/structure_parser.rb', line 15

def initialize
  @encodedSegments = []
  @idx = 0
end

Instance Attribute Details

#encodedSegmentsObject

Returns the value of attribute encodedSegments.



5
6
7
# File 'lib/ez7gen/structure_parser.rb', line 5

def encodedSegments
  @encodedSegments
end

#idxObject

Returns the value of attribute idx.



5
6
7
# File 'lib/ez7gen/structure_parser.rb', line 5

def idx
  @idx
end

Instance Method Details

#handle_groups(segments) ⇒ Object

handle groups in the array of ecnoded segments groups and subgroups organized as Arrays with specific Markers



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ez7gen/structure_parser.rb', line 52

def handle_groups(segments)
  #find groups and decode the group elements and put them in array
  segments.map!{ |seg|
    if(is_complex_group?(seg))

      # Generate marker for the segment to preserve specifics of type of the group - opt. vs repeating.
      if(!seg.kind_of? Array )
        seg = Marker.gen(seg)
      end

      if(seg.kind_of? Array)# TODO: Refactor, check not needed?
        seg.resolve(@encodedSegments)
      else
        is_number?(seg)? @encodedSegments[it.to_i] : seg
      end
      handle_groups(seg)
      # seg = handle_groups(seg)
    else
      seg
    end
  }
  return segments
end

#has_subgroups?(seg) ⇒ Boolean

checks a segment for subgroups

Returns:

  • (Boolean)


77
78
79
80
81
# File 'lib/ez7gen/structure_parser.rb', line 77

def has_subgroups?(seg)
  inner = remove_outer_parenthesis(seg)
  # inner segment should have no groups identified by {} or []
  inner =~ /[\[{]/
end

#is_complex_group?(encoded) ⇒ Boolean

check if encoded segment is a group

Returns:

  • (Boolean)


167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/ez7gen/structure_parser.rb', line 167

def is_complex_group?(encoded)

  # most of arrays have already been resolved
  # look inside if any of complex group left unresolved
  # return  false if(encoded.kind_of?(Array))
  if(encoded.kind_of?(Array))
    is_complex = false
    encoded.each{|it|
      is_complex = is_complex_group?(it) ||is_complex
    }
    return is_complex
  end

  # group has an index of encoded optional element
  # isGroupWithEncodedElements = ((encoded =~ /\~\d+\~/) || is_number?(encoded)) ? true: false
  isGroupWithEncodedElements = (encoded =~ /\~\d+\~/) ? true: false
  return isGroupWithEncodedElements if(isGroupWithEncodedElements)

  inner = remove_outer_parenthesis(encoded)
  subGroups = inner.split('~')
  (subGroups.size > 1)? true: false

  # # group consists of all required elements {~MRG~PV1~}, so look ahead for that
  # subGroups = encoded.split(/[~\{\[\}\]]/).delete_if{|it| blank?(it)}
  # isGroupOfRequiredElements = (subGroups.size > 1)? true: false
  #
  # return (isGroupWithEncodedElements || isGroupOfRequiredElements), subGroups
end

#is_group?(str, prnths) ⇒ Boolean

check if there are inside elements of sub-groups defined by the same parenthesis

Returns:

  • (Boolean)


151
152
153
154
155
# File 'lib/ez7gen/structure_parser.rb', line 151

def is_group?(str, prnths)
  # prnth_mtch = @prnths[0]
  # prnth_mtch = prnths[0]
  (str.scan(prnths[0]).size > 1)# outside paranthesis expected
end

#parenthesis_wrap(m) ⇒ Object

wrap a match in parantesis used to brake structure into sub structures



162
163
164
# File 'lib/ez7gen/structure_parser.rb', line 162

def parenthesis_wrap(m)
  @prnths.clone.insert(1, m)
end

#process(structure, regEx, prnths) ⇒ Object

process group using regular expression and specific pharenthesis as markers



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/ez7gen/structure_parser.rb', line 112

def process(structure, regEx, prnths)
  groups = []

  # brake up the structure into array of subgroups
  # using recursive regEx
  subGroups = structure.scan(regEx)

  subGroups.each {|subGroup|

    # m = parenthesis_wrap(a.first())
    # group boundaries - parenthesis, stripped by regEx
    # put the parenthesis back for processing
    groupElement = Marker.mark(subGroup.first(), prnths)

    # process an array of matches and substitute subgroups
    if(groups.empty?)
      replace(structure, groupElement)
    else
      if(groups.last().include?(groupElement))
        replace(groups.last, groupElement)
        if(!is_group?(groups.last, prnths)) #done resolving a group
          groups.pop()
        end
      else
        replace(structure, groupElement)
      end
    end

    if (is_group?(groupElement, prnths))
      groups << groupElement
    end

    # resolved group added to encoded segments
    @encodedSegments << groupElement
    @idx +=1
}
end

#process_opt_groups(struct) ⇒ Object

process groups with optional markers - []



102
103
104
# File 'lib/ez7gen/structure_parser.rb', line 102

def process_opt_groups(struct)
    process(struct, REGEX_OP, PRNTHS_OP)
end

#process_rep_groups(struct) ⇒ Object

process groups with repeating markers - {}



107
108
109
# File 'lib/ez7gen/structure_parser.rb', line 107

def process_rep_groups(struct)
  process(struct, REGEX_REP, PRNTHS_REP)
end

#process_segments(struct) ⇒ Object

takes a message structure and converts it into array of processed segments, ready for building hl7



21
22
23
24
# File 'lib/ez7gen/structure_parser.rb', line 21

def process_segments(struct)
  process_struct(struct)
  handle_groups(@encodedSegments)
end

#process_struct(struct) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ez7gen/structure_parser.rb', line 26

def process_struct(struct)
  #process original segments and build encoded segmets
  process_opt_groups(struct)
  process_rep_groups(struct)

  #check encoded segments to find and process segments which have subgroups
  @encodedSegments.map!{|seg|
   #has more subgroups
   if(has_subgroups?(seg))
     groupMarker = Marker.whatGroup?(seg)

     #unwrap for recursive processing
     process_struct(groupMarker.unwrap(seg))

     # wrap group back to restore the original segment
     groupMarker.mark(seg)
     # (groupMarker)?groupMarker.mark(seg):seg

   else
      seg
   end
  }
end

#remove_outer_parenthesis(seg) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ez7gen/structure_parser.rb', line 83

def remove_outer_parenthesis(seg)
  regexOptOuter = /^\[~(.*?)~\]$/
  regExRepOuter = /^{~(.*?)~}$/
  # (seg.scan(REGEX_OP).size()>1 || seg.scan(REGEX_REP).size()>1)
  # look for the outer parenthesis [] - opt
  # innerSeg =  seg.scan(/\[~{~(.*)~}~\]|\[~(.*)~\]|{~(.*)~}/)
  # innerOpt=  seg.scan(/^\[~(.*?)~\]/)
  innerOpt= seg.scan(regexOptOuter)
  innerOpt = (innerOpt.empty?) ? nil : innerOpt.flatten.first

  # look for outer {} - rep
  innerRep = (innerOpt) ? innerOpt.scan(regExRepOuter) : seg.scan(regExRepOuter)
  innerRep = (innerRep.empty?) ? nil : innerRep.flatten.first


  inner = innerRep || innerOpt || seg
end

#replace(str, m) ⇒ Object



157
158
159
# File 'lib/ez7gen/structure_parser.rb', line 157

def replace(str, m)
  str.sub!(m, @idx.to_s)
end