Class: NERA::CUI_Simulator_Adder

Inherits:
Object
  • Object
show all
Defined in:
lib/nera_addsim/make_simulator.rb

Instance Method Summary collapse

Constructor Details

#initialize(db_folder) ⇒ CUI_Simulator_Adder

————– output file ——————–



316
317
318
319
320
321
322
323
324
# File 'lib/nera_addsim/make_simulator.rb', line 316

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

Instance Method Details

#main_process(db_folder) ⇒ Object

Main process



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/nera_addsim/make_simulator.rb', line 158

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