class StructureParser
Constants
- PRNTHS_OP
- PRNTHS_REP
- REGEX_OP
attr_reader :regExOp, :regExRep
- REGEX_REP
Attributes
encodedSegments[RW]
idx[RW]
Public Class Methods
new()
click to toggle source
@encodedSegments =[] idx = 0
# File lib/ez7gen/structure_parser.rb, line 15 def initialize @encodedSegments = [] @idx = 0 end
Public Instance Methods
handle_groups(segments)
click to toggle source
handle groups in the array of ecnoded segments groups and subgroups organized as Arrays with specific Markers
# 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)
click to toggle source
checks a segment for subgroups
# 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)
click to toggle source
check if encoded segment is a group
# 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)
click to toggle source
check if there are inside elements of sub-groups defined by the same parenthesis
# 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)
click to toggle source
wrap a match in parantesis used to brake structure into sub structures
# File lib/ez7gen/structure_parser.rb, line 162 def parenthesis_wrap(m) @prnths.clone.insert(1, m) end
process(structure, regEx, prnths)
click to toggle source
process group using regular expression and specific pharenthesis as markers
# 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)
click to toggle source
process groups with optional markers - []
# File lib/ez7gen/structure_parser.rb, line 102 def process_opt_groups(struct) process(struct, REGEX_OP, PRNTHS_OP) end
process_rep_groups(struct)
click to toggle source
process groups with repeating markers - {}
# File lib/ez7gen/structure_parser.rb, line 107 def process_rep_groups(struct) process(struct, REGEX_REP, PRNTHS_REP) end
process_segments(struct)
click to toggle source
takes a message structure and converts it into array of processed segments, ready for building hl7
# File lib/ez7gen/structure_parser.rb, line 21 def process_segments(struct) process_struct(struct) handle_groups(@encodedSegments) end
process_struct(struct)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/ez7gen/structure_parser.rb, line 157 def replace(str, m) str.sub!(m, @idx.to_s) end