class BatchExperiment::Comm2FnameConverter

Converts a command to a filename using a given sanitizer, gives different names to different calls with the same arguments. Example: if a call with “sleep 1” yields “sleep_1”, the second call with the same argument yields “sleep_1.2”, and so on. Note that this is done by remembering previous calls, the object don't inspect the filesystem to check if that name was or wasn't used.

Attributes

num_times_seen[R]

Needed by the initialize_clone implementation.

Public Class Methods

new(sanitizer = FnameSanitizer) click to toggle source

Creates a new Comm2FnameConverter, with no memory of any previous calls.

@param sanitizer [#call] Callable object used to create a filename from

the arguments passed to Comm2FnameConverter.call. This class expects
that sanitizer has no internal state, so when an instance of this class
is cloned, there's no problem with sharing the sanitizer between the
clones. Default: BatchExperiment::FnameSanitizer.
# File lib/batch_experiment.rb, line 57
def initialize(sanitizer = FnameSanitizer)
  @num_times_seen = {}
  @sanitizer = sanitizer
end

Public Instance Methods

call(comm) click to toggle source

Takes a command, creates a fname for it, if this fname was already seen before, returns the fname + “.N”, where N is the number of times fname was already seen.

@param comm [String] A system command. @return [String] The sanitized filename created from that command. @note Note that different arguments can be reduced to the same

sanitized filename and, if this happens, they will NOT overwrite
each other. Example: 'echo "abc"' -> 'echo_abc'; 'echo abc' ->
'echo_abc.2'.
# File lib/batch_experiment.rb, line 72
def call(comm)
  fname = @sanitizer.call(comm)
  if @num_times_seen.include? fname
    @num_times_seen[fname] += 1
    fname << ".#{@num_times_seen[fname]}"
  else
    @num_times_seen[fname] = 1
  end

  fname.clone
end
initialize_clone(old) click to toggle source

Used to guarantee that a clone of Comm2FnameConverter will not share relevant state with the original. So calls to call on a clone don't affect the state of original (and vice versa).

# File lib/batch_experiment.rb, line 87
def initialize_clone(old)
  @num_times_seen = old.num_times_seen.clone
end