class KnapsackSolver::OutputPrinter

This class provides support for printing results and statistics of a dataset solving either to stdout or to a text file.

Public Class Methods

new(dataset_filenames, suffix, results) click to toggle source

Initializes printer for output log (results, statistics).

@param dataset_filenames [Array<String>] dataset filenames @param suffix [String] suffix of the created files @param results [Hash] results of solving or statistics to print

# File lib/knapsack_solver/output_printer.rb, line 10
def initialize(dataset_filenames, suffix, results)
  @dataset_basenames = file_basenames(dataset_filenames)
  @suffix = suffix
  @results = results
end

Public Instance Methods

print(out_dir = nil) click to toggle source

Prints results or statistics to stdout or to files in output directory.

@param out_dir [String] path to output directory

Protected Instance Methods

file_basenames(paths) click to toggle source

Gets basenames of supplied file paths.

@param paths [Array<String>] path to files @return [Array<String>] basenames of the paths

# File lib/knapsack_solver/output_printer.rb, line 79
def file_basenames(paths)
  paths.each_with_object([]) do |path, basenames|
    basenames << File.basename(path, File.extname(path))
  end
end
open_output_file(fname) click to toggle source

Opens output file and turns on synchronized writes (this is neede for testing with Rspec).

@param fname [String] path to the output file @return [#puts] output stream

# File lib/knapsack_solver/output_printer.rb, line 50
def open_output_file(fname)
  f = File.new(fname, 'w')
  f.sync = true
  f
end
output_filename(output_dir, basename, solving_method) click to toggle source

Construct filename for output log.

@param output_dir [String] output directory @param basename [String] basename of the output file @param solving_method [String] name of solving method @return [String] filename for output log

# File lib/knapsack_solver/output_printer.rb, line 91
def output_filename(output_dir, basename, solving_method)
  filename = basename + '_' + solving_method + @suffix
  return filename if output_dir.nil?
  File.join(output_dir, filename)
end
output_stream(out_dir, out_file) click to toggle source

Sets output stream to stdout or to a file if path to it was provided.

@param out_dir [String] directory for output files @param out_file [String] output file

# File lib/knapsack_solver/output_printer.rb, line 60
def output_stream(out_dir, out_file)
  return $stdout if out_dir.nil?
  open_output_file(out_file)
end
print_header(out_stream, out_file, results) click to toggle source

Prints header of output log file.

@param out_stream [#puts] stream to which output will be printed @param out_file [String] name of output file @param results [Hash] results of solving or statistics

print_solving_method_results(method, res, out_dir, basename) click to toggle source

Prints results of solving or statistics.

@param method [Symbol] symbol for solving method @param res [Hash] results of the solving method @param out_dir [String] path to output directory @param basename [String] basename of dataset input file corresponding to the results