Module: BatchExperiment::Extractor

Included in:
FirstLineExtractor, PyaExtractor, UKP5Extractor
Defined in:
lib/batch_experiment/extractor.rb

Overview

Module that defines the interface used for extracting info from other programs output. You don’t need to include it in your object, will suffice that the object (that you will use to extract info from the output) has the #names and #extract methods defined.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_field(lines, field) ⇒ String

Find a line in the following format: “field: value”, return value.

Parameters:

  • lines (Array<String>)

    Program output, broken in lines.

  • field (String)

    String to be found at the lines in the following pattern: ‘field: value’.

Returns:

  • (String)

    The ‘value’ as a string or, if ‘field’ isn’t found, an empty string.



15
16
17
18
19
20
# File 'lib/batch_experiment/extractor.rb', line 15

def self.get_field(lines, field)
  lines.grep(/^#{field}: .*/).each do | l |
    return l.match(/:[\t ]+(.*)/)[1]
  end
  ''
end

.get_hfield(lines, field) ⇒ String

Find a line in the following format: “field<linebreak>value”, return value.

Parameters:

  • lines (Array<String>)

    Program output, broken in lines.

  • field (String)

    String to be found at the lines in the following pattern: ‘field<lineabreak>value’. Only include the linebreak at the field, if the lines array have linebreaks at the end of every string element.

Returns:

  • (String)

    The ‘value’ as a string or, if ‘field’ isn’t found, an empty string.



33
34
35
# File 'lib/batch_experiment/extractor.rb', line 33

def self.get_hfield(lines, field)
  if ix = lines.find_index(field) then lines[ix + 1] else '' end
end

Instance Method Details

#extract(content) ⇒ Array<String>

Extract N values of some program output, where N is equal to #names.size. If some value isn’t present, the array should remain the same size, and the value at the corresponding position should be nil.

Parameters:

  • content (String)

    Program output.

Returns:

  • (Array<String>)

    The N extracted values, as strings.



56
57
58
# File 'lib/batch_experiment/extractor.rb', line 56

def extract(content)
  extract_from_lines(content.lines.map! { | l | l.chomp! })
end

#extract_from_lines(lines) ⇒ Array<String>

Optionally, you can define this method instead of #extract. The #extract method will call this method if not overrided.

Parameters:

  • lines (Array<String>)

    Program output, broken in lines, and the line string elements don’t end in linebreak.

Returns:

  • (Array<String>)

    The N extracted values, as strings.



66
67
68
# File 'lib/batch_experiment/extractor.rb', line 66

def extract_from_lines(lines)
  raise 'This method should have been overwritten by a subclass.'
end

#namesArray<String>

Note:

To be on the safe side you should create a new array at each call. If you always return a reference to the same array the array can be modified.

Return the field names for each of the elements returned by extract. Ex.:

['Time', 'Max Mem Use', 'opt', ... ]

Returns:

  • (Array<String>)

    The strings that will be used to make the column names at the BatchExperiment.experiment method.



46
47
48
# File 'lib/batch_experiment/extractor.rb', line 46

def names
  raise 'This method should have been overwritten by a subclass.'
end