require 'nera_dialog' module NERA class CUI_Simulator_Adder #-------------- create simulator script -------------------- def script_body(sim_info) params=[] sim_info[:params].each do | param | next if param[0] == "seed" params << " [#{param[0]},#{param[1]},#{param[2]}]" end params_str = params.join(",\n\t\t") str = <<"BODY" class #{sim_info[:name]} < NERA::Simulator Parameters = [#{params_str}\n\t\t] def simulate_command(seed) return \"#{sim_info[:exec]}\" end end BODY return str end private :script_body #-------------- add new parameter -------------------- def add_param array = [] type = [Integer, Float, String] while true $stdout.puts $stdout.puts "+++Input parameter name+++" $stdout.puts " If you need a seed of random number, please just input \"seed\"" name = Readline.readline(">").chomp.strip break if name =~ /^[a-z]/ or name =~ /^[A-Z]/ $stdout.puts "Input is not valid" $stdout.puts $stdout.puts "Input the simulator name again." end return ["seed",nil,nil] if name == "seed" array << name.to_sym.inspect $stdout.puts num = Dialog.select_one(type, "++Select parameter type+++") array << type[num] default = 0 while true $stdout.puts $stdout.puts "+++Input default value+++" default = Readline.readline(">").chomp.strip if default == "" $stdout.puts "Input is empty" next end if type[num] == Integer or type[num] == Float if /[-+]?(?:[0-9]+(\.[0-9]*)?|(\.[0-9]+))([eE][-+]?[0-9]+)?/ =~ default default = "#{default.to_i}" if type[num] == Integer default = "#{default.to_f}" if type[num] == Float break end else default = "\"#{default}\"" break end $stdout.puts "! Input value must be #{type[num]}" end array << default $stdout.puts return array end private :add_param #-------------- modify parameter -------------------- def mod_param(param) type = [Integer, Float, String] while true $stdout.puts menu = ["return"] menu += ["Name -> #{param[0].sub(':','')}"] menu += ["Type -> #{param[1]}"] menu += ["Default -> #{param[2]}"] menu += ["delete"] num = Dialog.select_one(menu,"Select modifying parameter") case num when 0 if param[0] == "seed" return ["seed",nil,nil] end if param[1] == Integer or param[1] == Float unless /[-+]?(?:[0-9]+(\.[0-9]*)?|(\.[0-9]+))([eE][-+]?[0-9]+)?/ =~ param[2] $stdout.puts "! Default value must be #{param[1]}" next end param[2] = "#{param[2].to_i}" if param[1] == Integer param[2] = "#{param[2].to_f}" if param[1] == Float end return param when 1 name = nil while true $stdout.puts $stdout.puts "+++Input parameter name+++" $stdout.puts " If you need a seed of random number, please just input \"seed\"" name = Readline.readline(">").chomp.strip break if name =~ /^[a-z]/ or name =~ /^[A-Z]/ $stdout.puts "Input is not valid" $stdout.puts $stdout.puts "Input the simulator name again." end return ["seed",nil,nil] if name == "seed" # param[0] = ":#{name}" param[0] = name.to_sym.inspect when 2 $stdout.puts num = Dialog.select_one(type, "+++Select new parameter type+++ \n #{param[1]} ->") param[1] = type[num] when 3 while true $stdout.puts $stdout.puts "+++Input new default value+++" default = Readline.readline("#{param[2]} -> ").chomp.strip # p param[1] if param[1] == Integer or param[1] == Float if /[-+]?(?:[0-9]+(\.[0-9]*)?|(\.[0-9]+))([eE][-+]?[0-9]+)?/ =~ default param[2] = "#{default.to_i}" if param[1] == Integer param[2] = "#{default.to_f}" if param[1] == Float break end else param[2] = "\"#{default}\"" break end $stdout.puts "! Input value must be #{param[1]}" end when 4 if param[1] == Integer or param[1] == Float unless /[-+]?(?:[0-9]+(\.[0-9]*)?|(\.[0-9]+))([eE][-+]?[0-9]+)?/ =~ param[2] $stdout.puts "! Default value must be #{param[1]}" next end end return [] end end end private :mod_param ######################################## # Main process ######################################## def main_process( db_folder ) sim_info = {} #-------------- set simulator name -------------------- $stdout.puts "==================================================" $stdout.puts "(1/4) Simulator Name " $stdout.puts "==================================================" $stdout.puts "Input the simulator name." while true name = Readline.readline(">",true).chomp.strip if name == "" $stdout.puts "Input is empty" $stdout.puts $stdout.puts "Input the simulator name again." next end if /\W/ =~ name $stdout.puts "You can use only alphameric characters; 0-9, A-Z, a-z, and _(underscore) ." $stdout.puts next elsif /^[_0-9]/ =~ name $stdout.puts "Initial character must be capital Roman-character." $stdout.puts next elsif /^[a-z]/ =~ name name[0,1] = name[0,1].upcase end if File.exists?(db_folder+"Simulator_classes/"+name+".rb") or File.exists?(db_folder + name ) $stdout.puts "The simulator \"#{name}\" already exists. " return nil end $stdout.print "Will you create a new simulator \"#{name}\" ?(y/n): " if Readline.readline("").chomp.strip == "y" sim_info[:name]=name break end $stdout.puts $stdout.puts "Input the simulator name again." end #-------------- set executable file -------------------- $stdout.puts $stdout.puts "==================================================" $stdout.puts "(2/4) Path of Executable File " $stdout.puts "==================================================" $stdout.puts "Input the path of the executable file." home = File.expand_path("~/")+"/" Dir.chdir(home) do while true path_file = Readline.readline("> ~/").chomp.strip unless File.file?(home+path_file) $stdout.puts "File \"~/#{path_file}\" is not found." $stdout.print "Will you abort this registration\" ?(y/n): " return nil if Readline.readline("").chomp.strip == "y" else if (File.stat(home+path_file).mode & 2**6 ) != 2**6 $stdout.puts "File ~/#{path_file} is not executable." $stdout.print "Will you abort this registration\" ?(y/n): " return nil if Readline.readline("").chomp.strip == "y" end end $stdout.print "Will you use \"~/#{path_file}\" ?(y/n): " if Readline.readline("").chomp.strip == "y" sim_info[:path]="~/"+path_file break end $stdout.puts $stdout.puts "Input the path of the executable file again." end end #-------------- set parameters -------------------- $stdout.puts $stdout.puts "==================================================" $stdout.puts "(3/4) Parameters " $stdout.puts "==================================================" $stdout.puts "Set parameters" exec = "" path = sim_info[:path] params = [] while true $stdout.puts "------------------------------" $stdout.puts "Execute command :" params_str = "" params.each do | name, type, default | if name == "seed" params_str += " (seed)" else params_str += " (#{name.sub(':','')})" end end exec = "#{path}#{params_str}" $stdout.puts " $ #{exec}" $stdout.puts "------------------------------" menu =["add"] params.each do | name, type, default | if name == "seed" menu += ["seed"] else menu += ["#{name.sub(':','')} : #{type} (default #{default})"] end end menu += ["exit"] num = Dialog.select_one(menu) case menu[num] when "add" params << add_param() next when "exit" if params.length==0 $stdout.print "Any parameters don't exist. Will you continue this registration?(y/n): " next if Readline.readline("").chomp.strip != "y" end break else case tmp = mod_param(params[num-1]) when [] params.delete_at(num-1) when params[num-1] next else params[num-1] = tmp end end end sim_info[:exec] = exec.gsub("(seed)", "\#\{seed\}").gsub("(", "\#\{@param\[:").gsub(")","\]\}") sim_info[:params] = params #-------------- finish -------------------- $stdout.puts $stdout.puts "==================================================" $stdout.puts "(4/4) Completed successfully " $stdout.puts "==================================================" $stdout.puts "+ Simulator Name : #{sim_info[:name]}" $stdout.puts "+ Executable File : #{sim_info[:path]}" $stdout.puts "+ Parameters : " if sim_info[:params].length == 0 $stdout.puts " No parameter" else index = 1 sim_info[:params].each do |name, type, default| $stdout.puts " #{index}-> #{name.sub(':','')} : #{type} (default #{default}) " index += 1 end end $stdout.puts "+ Command : \"#{sim_info[:exec].gsub( "\#\{seed\}","(seed)").gsub("\#\{@param\[:","(").gsub("\]\}",")")}\"" return sim_info end #-------------- output file -------------------- def initialize(db_folder) sim_info = main_process( db_folder + '/' ) return nil if sim_info == nil $stdout.puts "----------------------------------------" filename = sim_info[:name]+".rb" $stdout.puts "This simulator is saved as \"#{filename}\"" $stdout.puts "----------------------------------------" File.open(db_folder+"/Simulator_classes/"+filename,"w").puts script_body(sim_info) end end end