Class: TraceVisualization::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/trace_visualization/mapping.rb

Constant Summary collapse

LEXEME_REGEXP =
/\{LEXEME;(?<name>[a-zA-Z0-9]+);(?<source>[^;]+);(?<value>[0-9]+)\}/
DEFAULT_TOKENS =
{
  :ID => [ 
    /(?<lexeme>\[\d{3,}\])/, 
    lambda { |source| source[1 ... -1].to_i } 
  ],
  
  :IP => [
    /(?<lexeme>(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))/,
    lambda { |source| IPAddr.new(source).to_i }
  ],
    
  :TIME => [
    /(?<lexeme>\[\d{2} [a-zA-Z]{3} \d{4} \d{2}\:\d{2}\:\d{2}\])/,
    lambda { |source| Time.parse(source[1 ... -1]).to_i }
  ]      
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMapping

Returns a new instance of Mapping.



32
33
34
35
36
37
38
# File 'lib/trace_visualization/mapping.rb', line 32

def initialize
  # hash map: lexeme-object by lexeme-string
  @lexeme_table = {}
  
  @tokens = {}
  @mapped_str = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



234
235
236
# File 'lib/trace_visualization/mapping.rb', line 234

def method_missing(name, *args, &blk)
  raise "Missing method #{name}" 
end

Instance Attribute Details

#tokensObject

Returns the value of attribute tokens.



11
12
13
# File 'lib/trace_visualization/mapping.rb', line 11

def tokens
  @tokens
end

Class Method Details

.init(&block) ⇒ Object



40
41
42
43
44
# File 'lib/trace_visualization/mapping.rb', line 40

def self.init(&block)
  mapping = Mapping.new
  mapping.instance_eval(&block)
  mapping
end

Instance Method Details

#<<(object) ⇒ Object



208
209
210
211
212
# File 'lib/trace_visualization/mapping.rb', line 208

def <<(object)
  lexeme = install_lexeme('UNKNOWN', object, object.to_i, object.to_s.length)
  lexeme.ord = 0 # fake ord because Reorder already processed
  @mapped_str << lexeme
end

#[](index) ⇒ Object



196
197
198
# File 'lib/trace_visualization/mapping.rb', line 196

def [](index)
  @mapped_str[index]
end

#default_tokensObject



51
52
53
# File 'lib/trace_visualization/mapping.rb', line 51

def default_tokens
  @tokens.merge!(DEFAULT_TOKENS)
end

#find_allObject



222
223
224
# File 'lib/trace_visualization/mapping.rb', line 222

def find_all
  @mapped_str.find_all { |item| yield(item) }
end

#from_file(path) ⇒ Object

Load data from source file. File is read line by line



68
69
70
71
72
73
74
75
76
# File 'lib/trace_visualization/mapping.rb', line 68

def from_file(path)
  validate_file_name_argument(path)

  open(path) do |fd|
    while (line = fd.gets)
      process_line(line)
    end
  end
end

#from_preprocessed_file(path, offset, limit, use_lexeme_table = true) ⇒ Object

Load data from preprocessed file. File is read line by line



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/trace_visualization/mapping.rb', line 81

def from_preprocessed_file(path, offset, limit, use_lexeme_table = true)
  validate_file_name_argument(path)

  idx = 0
  open(path) do |fd|
    while (line = fd.gets)
      process_preprocessed_line(line, use_lexeme_table) if (idx >= offset && idx < offset + limit)

      idx += 1
    end
  end
end

#from_preprocessed_string(str, use_lexeme_table = true) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/trace_visualization/mapping.rb', line 107

def from_preprocessed_string(str, use_lexeme_table = true)
  validate_string_argument(str)

  str.split("\n").each do |line|
    process_preprocessed_line(line)
  end
end

#from_string(str) ⇒ Object

new



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/trace_visualization/mapping.rb', line 95

def from_string(str)
  validate_string_argument(str)
  
  @str = str

  str.split("\n").each do |line|
    process_line(line)
  end

  @mapped_str.pop if @mapped_str[-1].value == "\n" && str[-1] != "\n"
end

#install_lexeme(name, lexeme_string, int_value, lexeme_length, use_lexeme_table = true) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/trace_visualization/mapping.rb', line 179

def install_lexeme(name, lexeme_string, int_value, lexeme_length, use_lexeme_table = true)
  lexeme = use_lexeme_table ? @lexeme_table[lexeme_string] : nil
  
  if lexeme.nil?
    lexeme = TraceVisualization::Data::Lexeme.new(name, lexeme_string, int_value)
    lexeme.lexeme_length = lexeme_length

    @lexeme_table[lexeme_string] = lexeme if use_lexeme_table
  end
  
  lexeme
end

#install_lexeme_m(m, use_lexeme_table = true) ⇒ Object



192
193
194
# File 'lib/trace_visualization/mapping.rb', line 192

def install_lexeme_m(m, use_lexeme_table = true)
  install_lexeme(m[:name], m[:source], m[:value].to_i, m.to_s.length, use_lexeme_table)
end

#lengthObject



200
201
202
# File 'lib/trace_visualization/mapping.rb', line 200

def length
  @mapped_str.length
end

#maxObject



218
219
220
# File 'lib/trace_visualization/mapping.rb', line 218

def max
  @max_value
end

#popObject



214
215
216
# File 'lib/trace_visualization/mapping.rb', line 214

def pop
  @mapped_str.pop
end

#process(&block) ⇒ Object

new



56
57
58
59
# File 'lib/trace_visualization/mapping.rb', line 56

def process(&block)
  instance_eval(&block)
  @max_value = TraceVisualization::Reorder.process(@mapped_str)
end

#process_line(line) ⇒ Object

new



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/trace_visualization/mapping.rb', line 145

def process_line(line)
  lexeme_poss = []

  @tokens.each do |name, value|
    pattern, converter_func = value
    pos = 0
    while (m = pattern.match(line, pos))
      lexeme_string, pos = m[:lexeme], m.begin(0)
      
      lexeme = install_lexeme(name, lexeme_string, converter_func.call(lexeme_string), lexeme_string.length)
      lexeme_poss << TraceVisualization::Data::LexemePos.new(lexeme, pos)
      
      pos += lexeme_string.length
    end
  end
  
  lexeme_poss = TraceVisualization::LexemeOverlapFilter.process(lexeme_poss)
  
  pos, idx = 0, 0
  while pos < line.length
    lexeme = nil
    if idx < lexeme_poss.size && lexeme_poss[idx].pos == pos
      lexeme = lexeme_poss[idx].lexeme
      idx += 1
    else
      lexeme = install_lexeme('CHAR', line[pos], line[pos].ord, 1)
    end
    pos += lexeme.length
    @mapped_str << lexeme
  end
  
  @mapped_str << install_lexeme('CHAR', "\n", "\n".ord, 1)
end

#process_preprocessed_line(line, use_lexeme_table = true) ⇒ Object



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
# File 'lib/trace_visualization/mapping.rb', line 115

def process_preprocessed_line(line, use_lexeme_table = true)
  lexeme_positions = []
  pos = 0
  while (m = LEXEME_REGEXP.match(line, pos))
    pos = m.begin(0)

    lexeme = install_lexeme_m(m, use_lexeme_table)
    lexeme_positions << TraceVisualization::Data::LexemePos.new(lexeme, pos)

    pos += lexeme.lexeme_length
  end


  pos, idx = 0, 0
  while pos < line.length
    lexeme = nil
    if idx < lexeme_positions.size && lexeme_positions[idx].pos == pos
      lexeme = lexeme_positions[idx].lexeme
      idx += 1
    else
      lexeme = install_lexeme('CHAR', line[pos], line[pos].ord, 1, use_lexeme_table)
    end
    pos += lexeme.lexeme_length
    @mapped_str << lexeme
  end

  @mapped_str << install_lexeme('CHAR', "\n", "\n".ord, 1, use_lexeme_table)
end

#process_without_reorder(&block) ⇒ Object



61
62
63
# File 'lib/trace_visualization/mapping.rb', line 61

def process_without_reorder(&block)
  instance_eval(&block)
end

#restore(pos = 0, length = @mapped_str.length) ⇒ Object



226
227
228
# File 'lib/trace_visualization/mapping.rb', line 226

def restore(pos = 0, length = @mapped_str.length)
  @mapped_str[pos ... pos + length].inject("") { |res, c| res += c.value }
end

#sizeObject



204
205
206
# File 'lib/trace_visualization/mapping.rb', line 204

def size
  length
end

#to_aryObject



230
231
232
# File 'lib/trace_visualization/mapping.rb', line 230

def to_ary
  @mapped_str.collect { |lexeme| lexeme.value }
end

#token(name, pattern, converter_func) ⇒ Object

new



47
48
49
# File 'lib/trace_visualization/mapping.rb', line 47

def token(name, pattern, converter_func)
  @tokens[name] = [pattern, converter_func]
end