############################################################################### ### CMSC330 Project 6: Multi-threaded Train Simulation ### ### Source code: metro.rb ### ### Description: A multi-threaded Ruby program that simulates ### ### the Washington Metro by creating Train and Person threads ### ############################################################################### require "monitor" Thread.abort_on_exception = true # to avoid hiding errors in threads #---------------------------------------------------------------- # Metro Simulator #---------------------------------------------------------------- def simulate(lines,numTrains,passengers,simMonitor) # puts lines.inspect # puts numTrains.inspect # puts passengers.inspect # puts simMonitor.inspect end #---------------------------------------------------------------- # Simulation Display #---------------------------------------------------------------- # line = hash of line names to array of stops # stations = hash of station names => # hashes for each line => hash of trains at station # OR hash for "passenger" => hash of passengers at station # trains = hash of train names => hash of passengers def displayState(lines,stations,trains) lines.keys.sort.each { |color| stops = lines[color] puts color stops.each { |stop| pStr = "" tStr = "" stations[stop]["passenger"].keys.sort.each { |passenger| pStr << passenger << " " } stations[stop][color].keys.sort.each { |trainNum| tr = color+" "+trainNum tStr << "[" << tr if trains[tr] != nil trains[tr].keys.sort.each { |p| tStr << " " << p } end tStr << "]" } printf(" %25s %10s %-10s\n", stop, pStr, tStr) } } puts end def display(lines,passengers,output) # puts lines.inspect # puts passengers.inspect # puts output.inspect stations = {} trains = {} output.each {|o| puts o displayState(lines,stations,trains) } end #---------------------------------------------------------------- # Simulation Verifier #---------------------------------------------------------------- def verify(lines,numTrains,passengers,output) # puts lines.inspect # puts numTrains.inspect # puts passengers.inspect # puts output.inspect # return false return true end