Class: SegmentPicker

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

Constant Summary collapse

@@LOAD_FACTOR =

load 50 percent of optional segments

0.5
@@MSH_SEGMENTS =
['MSH', "#{BASE_INDICATOR}MSH"]
@@random =

@@MSH_SEGMENTS = [‘MSH’, “base:MSH”]

Random.new

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

#initialize(profile, encodedSegments, loadFactor = nil) ⇒ SegmentPicker

Returns a new instance of SegmentPicker.



17
18
19
20
21
22
23
24
25
# File 'lib/ez7gen/service/segment_picker.rb', line 17

def initialize(profile, encodedSegments, loadFactor=nil)
  @profile = profile
  @encodedSegments = encodedSegments
  #refactoring
  @candidates =[]

  @loadFactor = loadFactor
  @loadFactor||=@@LOAD_FACTOR # set to default if not specified or set to nil
end

Instance Attribute Details

#encodedSegmentsObject

Returns the value of attribute encodedSegments.



7
8
9
# File 'lib/ez7gen/service/segment_picker.rb', line 7

def encodedSegments
  @encodedSegments
end

#profileObject

Returns the value of attribute profile.



8
9
10
# File 'lib/ez7gen/service/segment_picker.rb', line 8

def profile
  @profile
end

Instance Method Details

#build_segments_for_indexes(idxs) ⇒ Object

Turn indexes to segments



37
38
39
40
41
42
43
44
45
46
# File 'lib/ez7gen/service/segment_picker.rb', line 37

def build_segments_for_indexes(idxs)
  idxs.map do |it|
    if(is_number?(@profile[it]))
        idx = @profile[it].to_i
        @encodedSegments[idx]
      else
        @profile[it]
    end
  end
end

#get_load_candidates_count(total) ⇒ Object

calculate number of segments based on load factor



96
97
98
# File 'lib/ez7gen/service/segment_picker.rb', line 96

def get_load_candidates_count(total)
  (total*@loadFactor).ceil     #round it up
end

#get_optional_segment_idxs(regiredIdxs) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/ez7gen/service/segment_picker.rb', line 55

def get_optional_segment_idxs(regiredIdxs)
  # range of indexes
  allIdxs = (0...@profile.size).to_a
  optIdxs = allIdxs- regiredIdxs
  count = get_load_candidates_count(optIdxs.size())
  optIdxs.sample(count)
end

#get_required_segment_idxsObject

get segments that will always be build, include z segments



64
65
66
67
68
69
70
71
# File 'lib/ez7gen/service/segment_picker.rb', line 64

def get_required_segment_idxs()
  # profile already has all required segments identified
  rs = @profile.each_index.select{|it| is_required1?(@profile[it])}
  # promote z segments to required, and add them as required, keeping their index
  zs = @profile.each_index.select{|it| is_z1?(@profile[it])}
  # return indexes
  (rs+zs).sort.uniq
end

#get_required_segmentsObject

get segments that will always be build, include z segments



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/ez7gen/service/segment_picker.rb', line 76

def get_required_segments()
  # profile already has all required segments identified
  # promote z segments to required, and add them as required, keeping their index
  zs = @encodedSegments.select{|it| is_z?(it)}

  #promote z to required, replace it's placeholder in profile with the value of the segment
  # adjust optional segments, clear the value, but do not delete to preserve the indexes
  zs.each{|it| idx = @encodedSegments.index(it); @profile[@profile.index(idx)] = it; @encodedSegments[idx] = nil}

  #reset encoded segments
  @encodedSegments.delete_if{|it| it == nil}

  # Make a copy of profile and set to nil all optional segments, indexes into encoded segments array
  # required = []
  # @profile.each{|it| required << Utils.num_to_nil(it)}
  #
  return @profile.select{|it| is_required?(it)}
end

#is_required1?(encoded) ⇒ Boolean

check is segment is required

Returns:

  • (Boolean)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ez7gen/service/segment_picker.rb', line 101

def is_required1?(encoded)
 check = false
 #segment not encoded
 if(!is_number?(encoded))
   check = true
 else
   # look at encoded segment for the index
   seg = @encodedSegments[encoded.to_i]
   # Required segments left not encoded as strings, optional and groups encoded - indexes of encoded segments
   if(seg.instance_of?(RepeatingGroup))
     check = true
   elsif(seg.instance_of?(String))
     check = (seg[0] == '{' ) # signifies repeating segment
   end
 end

 return check
end

#is_required?(encoded) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/ez7gen/service/segment_picker.rb', line 120

def is_required?(encoded)
# Required segments left not encoded as strings, optional and groups encoded as numbers
  !is_number?(encoded)
end

#is_z1?(encoded) ⇒ Boolean

refactoring

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ez7gen/service/segment_picker.rb', line 131

def is_z1?(encoded)
segment = ''
  if(is_number?(encoded))
      # look at encoded segment for the index
      segment = @encodedSegments[encoded.to_i]
      #if segment happen to be a group, flatten it into string
      if(segment.kind_of?(Array))
        segment = segment.flatten().to_s
      end
  else
    #segment was not encoded to an index, use it as is
    segment = encoded
  end

   (segment =~ /\~Z/)? true: false
end

#is_z?(segment) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/ez7gen/service/segment_picker.rb', line 126

def is_z?(segment)
  segment=~/\~Z/
end

#pick_segment_idx_to_buildObject



48
49
50
51
52
53
# File 'lib/ez7gen/service/segment_picker.rb', line 48

def pick_segment_idx_to_build
  reqIdxs= get_required_segment_idxs()
  #profile indexes - reqired = optional?
  optIdxs = get_optional_segment_idxs(reqIdxs)
  (reqIdxs+optIdxs).sort.uniq
end

#pick_segments_to_buildObject

refactoring Get list of segments for test message generation. MSH is populated with quick generation, skip it here.



30
31
32
33
34
# File 'lib/ez7gen/service/segment_picker.rb', line 30

def pick_segments_to_build()
  idxs = pick_segment_idx_to_build
  segmentCandidates = build_segments_for_indexes(idxs)
  return segmentCandidates - @@MSH_SEGMENTS
end