Class: TspRunner::Runner
- Inherits:
-
Object
- Object
- TspRunner::Runner
- Defined in:
- lib/tsp_runner/runner.rb
Defined Under Namespace
Classes: InvalidError
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#time_limit ⇒ Object
readonly
Returns the value of attribute time_limit.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(time_limit, filename, *cmd_and_opts) ⇒ Runner
constructor
A new instance of Runner.
- #run ⇒ Object
- #validate!(output, initial_location_name = nil) ⇒ Object
- #validate_input ⇒ Object
Constructor Details
#initialize(time_limit, filename, *cmd_and_opts) ⇒ Runner
Returns a new instance of Runner.
18 19 20 21 22 |
# File 'lib/tsp_runner/runner.rb', line 18 def initialize(time_limit, filename, *cmd_and_opts) @time_limit = time_limit @filename = filename @cmd = cmd_and_opts.join(' ') end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
7 8 9 |
# File 'lib/tsp_runner/runner.rb', line 7 def cmd @cmd end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
7 8 9 |
# File 'lib/tsp_runner/runner.rb', line 7 def filename @filename end |
#time_limit ⇒ Object (readonly)
Returns the value of attribute time_limit.
7 8 9 |
# File 'lib/tsp_runner/runner.rb', line 7 def time_limit @time_limit end |
Class Method Details
.usage ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/tsp_runner/runner.rb', line 9 def self.usage puts "Usage: #{$0} <time limit> <input file> <command> [command arg 1]..." puts '' puts ' time limit - maximum duration the executable can run in seconds' puts ' input file - input file with location names and lat/lons' puts ' command - command line executable that calculates the path' puts ' command arg n - optional args for the command line executable' end |
Instance Method Details
#run ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tsp_runner/runner.rb', line 37 def run unless validate_input self.class.usage return end output = Timeout.timeout(time_limit.to_i) { `#{cmd} #{filename}` } distance = validate!(output, 'San Francisco') puts("Success: #{distance} km") rescue InvalidError => error $stderr.puts error exit(1) rescue Timeout::Error $stderr.puts 'Failed: timeout' exit(1) end |
#validate!(output, initial_location_name = nil) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/tsp_runner/runner.rb', line 54 def validate!(output, initial_location_name = nil) solution = TspRunner::Solution.from_string(output, location_hash) if solution.valid?(initial_location_name) solution.total_distance / 1000.0 else raise InvalidError, ['Failed: invalid, program output:', output].join("\n") end end |
#validate_input ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tsp_runner/runner.rb', line 24 def validate_input return false if time_limit.nil? || filename.nil? || cmd.to_s.length == 0 Integer(time_limit.to_s, 10) unless File.exists?(filename.to_s) puts "Input file does not exist: #{filename}" return false end true rescue ArgumentError puts "Invalid time limit: #{time_limit}" false end |