Class: TraceVisualization::Mapping
- Inherits:
-
Object
- Object
- TraceVisualization::Mapping
- 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
-
#tokens ⇒ Object
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(object) ⇒ Object
- #[](index) ⇒ Object
- #default_tokens ⇒ Object
- #find_all ⇒ Object
-
#from_file(path) ⇒ Object
Load data from source file.
-
#from_preprocessed_file(path) ⇒ Object
Load data from preprocessed file.
- #from_preprocessed_string(str) ⇒ Object
-
#from_string(str) ⇒ Object
new.
-
#initialize ⇒ Mapping
constructor
A new instance of Mapping.
- #install_lexeme(name, lexeme_string, int_value, lexeme_length) ⇒ Object
- #install_lexeme_m(m) ⇒ Object
- #length ⇒ Object
- #max ⇒ Object
- #method_missing(name, *args, &blk) ⇒ Object
- #pop ⇒ Object
-
#process(&block) ⇒ Object
new.
-
#process_line(line) ⇒ Object
new.
- #process_preprocessed_line(line) ⇒ Object
- #restore(pos = 0, length = @mapped_str.length) ⇒ Object
- #size ⇒ Object
- #to_ary ⇒ Object
-
#token(name, pattern, converter_func) ⇒ Object
new.
Constructor Details
#initialize ⇒ Mapping
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
227 228 229 |
# File 'lib/trace_visualization/mapping.rb', line 227 def method_missing(name, *args, &blk) raise "Missing method #{name}" end |
Instance Attribute Details
#tokens ⇒ Object
Returns the value of attribute tokens.
11 12 13 |
# File 'lib/trace_visualization/mapping.rb', line 11 def tokens @tokens end |
Class Method Details
Instance Method Details
#<<(object) ⇒ Object
203 204 205 |
# File 'lib/trace_visualization/mapping.rb', line 203 def <<(object) @mapped_str << Item.new(object, "unknown") end |
#[](index) ⇒ Object
191 192 193 |
# File 'lib/trace_visualization/mapping.rb', line 191 def [](index) @mapped_str[index] end |
#default_tokens ⇒ Object
51 52 53 |
# File 'lib/trace_visualization/mapping.rb', line 51 def default_tokens @tokens.merge!(DEFAULT_TOKENS) end |
#find_all ⇒ Object
215 216 217 |
# File 'lib/trace_visualization/mapping.rb', line 215 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
62 63 64 65 66 67 68 69 70 |
# File 'lib/trace_visualization/mapping.rb', line 62 def from_file(path) raise ArgumentError, 'Argument must be a string' unless path.instance_of? String raise ArgumentError, 'File path is not defined' unless path.empty? raise RuntimeError, 'File doesn\'t exists' unless File.exists?(path) fd = open(path) process_line(line) while line = fd.gets fd.close end |
#from_preprocessed_file(path) ⇒ Object
Load data from preprocessed file. File is read line by line
73 74 75 76 77 78 79 80 81 |
# File 'lib/trace_visualization/mapping.rb', line 73 def from_preprocessed_file(path) raise ArgumentError, 'Argument must be a string' unless path.instance_of? String raise ArgumentError, 'File path is not defined' unless path.empty? raise RuntimeError, 'File doesn\'t exists' unless File.exists?(path) fd = open(path) process_preprocessed_line line while line = fd.gets fd.close end |
#from_preprocessed_string(str) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/trace_visualization/mapping.rb', line 95 def from_preprocessed_string(str) raise ArgumentError, 'Argument must be a string' if not str.instance_of? String raise ArgumentError, 'String is not defined' if str.empty? str.split("\n").each do |line| process_preprocessed_line(line) end end |
#from_string(str) ⇒ Object
new
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/trace_visualization/mapping.rb', line 84 def from_string(str) raise ArgumentError, 'Argument must be a string' unless str.instance_of? String raise ArgumentError, 'String is not defined' if str.empty? @str = str str.split("\n").each do |line| process_line(line) end end |
#install_lexeme(name, lexeme_string, int_value, lexeme_length) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/trace_visualization/mapping.rb', line 167 def install_lexeme(name, lexeme_string, int_value, lexeme_length) lexeme = @lexeme_table[lexeme_string] if lexeme == nil lexeme = TraceVisualization::Data::Lexeme.new(name, lexeme_string, int_value) lexeme.lexeme_length = lexeme_length @lexeme_table[lexeme_string] = lexeme end lexeme end |
#install_lexeme_m(m) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/trace_visualization/mapping.rb', line 179 def install_lexeme_m(m) lexeme = @lexeme_table[m[:source]] if lexeme == nil lexeme = TraceVisualization::Data::Lexeme.new(m[:name], m[:source], m[:value].to_i) lexeme.lexeme_length = m.to_s.length @lexeme_table[m[:source]] = lexeme end lexeme end |
#length ⇒ Object
195 196 197 |
# File 'lib/trace_visualization/mapping.rb', line 195 def length @mapped_str.length end |
#max ⇒ Object
211 212 213 |
# File 'lib/trace_visualization/mapping.rb', line 211 def max @max_value end |
#pop ⇒ Object
207 208 209 |
# File 'lib/trace_visualization/mapping.rb', line 207 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
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/trace_visualization/mapping.rb', line 133 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) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/trace_visualization/mapping.rb', line 104 def process_preprocessed_line(line) lexeme_positions = [] pos = 0 while (m = LEXEME_REGEXP.match(line, pos)) pos = m.begin(0) lexeme = install_lexeme_m(m) 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) end pos += lexeme.lexeme_length @mapped_str << lexeme end @mapped_str << install_lexeme('CHAR', "\n", "\n".ord, 1) end |
#restore(pos = 0, length = @mapped_str.length) ⇒ Object
219 220 221 |
# File 'lib/trace_visualization/mapping.rb', line 219 def restore(pos = 0, length = @mapped_str.length) @mapped_str[pos ... pos + length].inject("") { |res, c| res += c.value } end |
#size ⇒ Object
199 200 201 |
# File 'lib/trace_visualization/mapping.rb', line 199 def size length end |
#to_ary ⇒ Object
223 224 225 |
# File 'lib/trace_visualization/mapping.rb', line 223 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 |