Class: NERA::CUI_exec

Inherits:
Object
  • Object
show all
Defined in:
lib/nera/nera_cui.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_db_folder) ⇒ CUI_exec

— main method



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nera/nera_cui.rb', line 37

def initialize( path_db_folder)
  @state = :simulator_layer
  @path_db_folder = path_db_folder
  unless FileTest.directory?(@path_db_folder)
    NERA::DbFolders.create_db(@path_db_folder)
  end
  
  while true
    case @state
    when :simulator_layer
      @state = sim_layer_action
    when :parameter_layer
      @state = param_layer_action
    when :run_layer
      @state = run_layer_action
    when :job_layer
      @state = job_layer_action
    when :exit
      return 0
    else
      raise "Must not happen"
    end
  end
end

Instance Method Details

#job_layer_actionObject

— action at job layer



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'lib/nera/nera_cui.rb', line 381

def job_layer_action
  $stdout.puts <<"HEADER"
===============================================
--- in job layer ------------------------------
  #{@path_db_folder} / Jobs
===============================================
HEADER

  action_list = ["Back to simulator layer",
                 "Show job infos",
                 "Cancel jobs",
                 "Execute jobs now",
                 "Show host infos",
                 "Upload jobs",
                 "Download data",
                 "Include data",
                 "Show recently included jobs",
                 "Exit"]
  jlc = NERA::JobLayerController.new( @path_db_folder)
  rc = NERA::RemoteConnector.new( @path_db_folder)

  selected = 0
  Dir.chdir( jlc.path_to_job_layer) {
    selected = Dialog.select_one_or_return_string( action_list, "Select one")
    if selected.is_a?(String)
      system( selected)
      return :job_layer
    end
  }
  
  case action_list[selected]
  when "Back to simulator layer"
    return :simulator_layer
  when "Show job infos"
    Dir.chdir( jlc.path_to_job_layer) {
      system("less jobs.yml")
    }
    return :job_layer
  when "Cancel jobs"
    header,list = jlc.not_finished_list_in_csv
    list.unshift("Cancel")
    selected_jobs = Dialog::select_many( list, header).map do |num|
      list[num].split(',')[0].to_i
    end
    unless selected_jobs.include?(0)
      stat = jlc.cancel_jobs( selected_jobs)
      if stat
        str = selected_jobs.join(", ")
        Dialog::message("The following jobs are successfully cancelled.\n#{str}")
      else
        Dialog::message("Cancellation failed.")
      end
    end
    return :job_layer
  when "Execute jobs now"
    header,list = jlc.not_finished_list_in_csv
    list.unshift("Cancel")
    selected_jobs = Dialog::select_many( list, header).map do |num|
      list[num].split(',')[0].to_i
    end
    return :job_layer if selected_jobs.include?(0)

    system "top"
    if Dialog::ask("Will you execute now?", true)
      flag = jlc.execute_jobs_now( selected_jobs)
      Dialog::message("The following jobs are successfully submitted.\n#{flag.join(', ')}") if flag
       = selected_jobs - flag.to_a
      Dialog::message("Warning! The following jobs are not submitted.\n#{.join(', ')}") if .size > 0
    end
    
    return :job_layer
  when "Show host infos"
    h_list = ["Cancel", "All"] + rc.hostnames
    sel = Dialog::select_many( h_list)
    if sel.include?(0)
      return :job_layer
    elsif sel.include?(1)
      rc.hostnames.each do |hname|
        Dialog::message("Information of the host #{hname} =========================")
        Dialog::message( rc.show_status( hname) )
      end
    else
      sel.each do |num|
        Dialog::message("Information of the host #{h_list[num]} =========================")
        Dialog::message( rc.show_status( h_list[num]) )
      end
    end
    return :job_layer
  when "Upload jobs"
    header,list = jlc.not_finished_list_in_csv
    list.unshift("Cancel")
    selected_jobs = Dialog::select_many( list, header).map do |num|
      list[num].split(',')[0].to_i
    end
    return :job_layer if selected_jobs.include?(0)

    h_list = ["Cancel"] + rc.hostnames
    sel = Dialog::select_one( h_list, "Select the host")
    return :job_layer if sel == 0
    selected_host = h_list[sel]
    
    Dialog::message("Information of the host #{selected_host} =========================")
    Dialog::message( rc.show_status( selected_host) + "\n" )
    if Dialog::ask("Will you upload the script?", true)
      if rc.submittable_hostnames.include?(selected_host) and Dialog::ask("Will you submit the jobs?", true)
        v = rc.submit( selected_jobs, selected_host)
        Dialog::message(v.to_s)
      else
        v = rc.transfer( selected_jobs, selected_host)
        Dialog::message(v.to_s)
      end
    end
    return :job_layer
  when "Download data"
    d_hash = rc.check_completion_all
    list = ["Cancel", "All"]
    d_hash.each_pair do |host, d_files|
      if d_files.is_a?(Array)
        d_files.each do |f|
          list << "#{f} @ #{host}"
        end
      end
    end
    sel = Dialog::select_many( list)
    if sel.include?(0)
      return :job_layer
    elsif sel.include?(1)
      d_hash.each_pair do |host, d_files|
        res = rc.download( d_files, host)
        if res.is_a?(Array)
          Dialog::message("#{res.join(', ')} have been downloaded.")
        else
          Dialog::message(res.to_s)
        end
      end
    else
      downloaded = []
      sel.each do |num|
        a = list[num].split('@')
        res = rc.download( [ a[0].strip ], a[1].strip)
        downloaded += res if res.is_a?(Array)
      end
      Dialog::message("#{downloaded.join(', ')} have been downloaded.")
    end
    return :job_layer
  when "Include data"
    list = jlc.include_list
    list = ["Cancel","All"] + list
    sel = Dialog::select_many( list)
    status = nil
    if sel.include?(0)
      return :job_layer
    elsif sel.include?(1)
      status = jlc.include_all
    else
      sel.each do |num|
        status = jlc.include( list[num])
      end
    end
    if status
      Dialog::message("Inclusion successfully completed.")
    else
      Dialog::message("Inclusion failed.")
    end
    return :job_layer
  when "Show recently included jobs"
    list = jlc.recently_included_list
    Dialog::message("  * id, simulator, parameter_id, run_id, included_at")
    list.each do |rec|
      Dialog::message("  - #{rec[:id]}, #{rec[:simulator]}, #{rec[:parameter_id]}, #{rec[:run_id]}, #{rec[:included_at].to_s}")
    end
    return :job_layer
  when "Exit"
    return :exit
  end
end

#param_layer_actionObject

— selection of a parameter set



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/nera/nera_cui.rb', line 134

def param_layer_action

  plc = NERA::ParameterLayerController.new( @path_db_folder, @simulator_class)
  unless plc
    Dialog::message("Specified simulator does not exist #{@path_db_folder}/#{@simulator_class}")
    return :simulator_layer
  end

  $stdout.puts <<"HEADER"
===============================================
--- in parameter layer ------------------------
  #{@path_db_folder} / #{@simulator_class}
===============================================
HEADER

  header,list = plc.parameters_list_in_csv
  list.unshift( "Menu")
  selected = 0
  Dir.chdir( plc.path_to_param_layer) {
    selected = Dialog::select_one_or_return_string( list, header)
    if selected.is_a?(String)
      system(selected)
      return :parameter_layer
    end
  }
  if selected == 0
    layer = parameter_layer_menu_select
    return layer
  else
    id = plc.get_id_from_csv_string( list[selected])
    @parameter_id = id
    return :run_layer
  end
end

#parameter_layer_menu_selectObject

— menu at a parameter layer



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
# File 'lib/nera/nera_cui.rb', line 170

def parameter_layer_menu_select
  menu = ["return to simulator layer",
          "create a new parameter set",
          "move a parameter set into the trashbox",
          "revert a parameter set in the trashbox",
          "delete a parameter set completely",
          "exit"]
  s = Dialog::select_one( menu, "Select one from the following")
  plc = NERA::ParameterLayerController.new( @path_db_folder, @simulator_class)
  case menu[s]
  when "return to simulator layer"
    return :simulator_layer
  when "create a new parameter set"
    plist = plc.get_parameter( @parameter_id)
    new_param_hash = Dialog::set_multiple_values( plist, "Input the new parameter set")
    f = plc.create_a_new_parameter_set( new_param_hash)
    if f
      Dialog::message("A new parameter is successfully added to the table.")
      @parameter_id = f
    else
      Dialog::message("This parameter already exists in the database.")
    end
    return :parameter_layer
  when "move a parameter set into the trashbox"
    header,list = plc.parameters_list_in_csv
    list.unshift("Cancel")
    s = Dialog::select_one( list, header)
    if s == 0
      return :parameter_layer
    else
      id = plc.get_id_from_csv_string( list[s])
      f = plc.move_a_parameter_set_into_trashbox(id)
      if f
        Dialog::message("Parameter #{id} was successfully moved into the trashbox")
      else
        Dialog::message("Couldn't move the parameter #{id} to the trashbox.\nYou have to cancel all the jobs that are not finished.")
      end
      return :parameter_layer
    end
  when "revert a parameter set in the trashbox"
    header,list = plc.trashbox_parameter_list_in_csv
    list.unshift("Cancel")
    s = Dialog::select_one( list, header)
    if s == 0
      return :parameter_layer
    else
      id = plc.get_id_from_csv_string( list[s])
      f = plc.revert_a_parameter_set_in_trashbox(id)
      if f
        Dialog::message("Parameter #{id} was successfully reverted.")
      else
        Dialog::message("Couldn't revert the parameter #{id}")
      end
      return :parameter_layer
    end
  when "delete a parameter set completely"
    header,list = plc.trashbox_parameter_list_in_csv
    list.unshift("Cancel")
    s = Dialog::select_one( list, header)
    if s == 0
      return :parameter_layer
    else
      id = plc.get_id_from_csv_string( list[s])
      f = plc.delete_a_parameter_set( id)
      if f
        Dialog::message("Parameter #{id} was successfully deleted.")
      else
        Dialog::message("Couldn't delete the parameter #{id}")
      end
      return :parameter_layer
    end
  when "exit"
    return :exit
  else
    raise "must not happen"
  end
end

#run_layer_actionObject

— action at run layer



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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/nera/nera_cui.rb', line 249

def run_layer_action

  rlc = NERA::RunLayerController.new( @path_db_folder, @simulator_class, @parameter_id)
  unless rlc
    Dialog::message("Table #{@path_db_folder}/#{@simulator_class}/#{@parameter_id} is not found.")
    return :parameter_layer
  end

  $stdout.puts <<"HEADER"
===============================================
--- in run layer ------------------------------
  #{@path_db_folder} / #{@simulator_class} / #{@parameter_id}
===============================================
HEADER

  action_list = ["Back to parameter layer",
                 "Show run infos",
                 "Create jobs",
                 #"Execute created jobs",
                 #"Submit created jobs to remote host",
                 "Cancel jobs",
                 "Analyze data",
                 "Exit"]
  selected = 0
  Dir.chdir( rlc.path_to_run_layer) {
    selected = Dialog::select_one_or_return_string( action_list, "Select one")
    if selected.is_a?(String)
      system( selected)
      return :run_layer
    end
  }
  
  case action_list[selected]
  when "Back to parameter layer"
    return :parameter_layer
  when "Show run infos"
    Dir.chdir( rlc.path_to_run_layer) {
      system("less runs.yml")
    }
    return :run_layer
  when "Create jobs"
    num_job = Dialog::get_integer( "Input the number of jobs (must be equal or larger than zero)") { |i| i >= 0 }
    return :run_layer if num_job == 0
    num_run_per_job = Dialog::get_integer( "Input the number of runs per job (must be larger than zero)") { |i| i > 0 }
    jobids = rlc.create_jobs( num_job, num_run_per_job)
    str = jobids.join(", ")
    Dialog::message( "The following jobs are newly created.\n#{str}")
    return :run_layer
  when "Execute created jobs"
=begin        
    header,list = rlc.not_finished_runs_list_in_csv
  list = RunLayerController::get_created_runs_list( @db_folder, @model_id, @parameter_id)
  selected = select_one( list, "Select a job to execute")
  job_id = list[selected][:job_id]
  stat = JobLayerController::execute_one( @db_folder, job_id)
  if stat
    $stderr.puts "Simulation finished successfully"
  else
    $stderr.puts "Simulation failed"
  end
=end
    return :run_layer
  when "Submit created jobs to remote host"
=begin        
    header,list = rlc.not_finished_runs_list_in_csv
    list.unshift("Cancel")
    selected_jobs = Dialog::select_many( list, header)
    unless selected_jobs.include?(0)
      rc = RemoteConnector.new( @db_folder)
      hosts = rc.hostnames
      hosts.unshift("Cancel")
      s = Dialog::select_one( hosts, "Select a host")
      return :run_layer if s == 0
      if rc.connect( hosts[s])
        Dialog::message( rc.get_status)
        action = Dialog::select_one( ["Transfer", "Transfer and Submit", "Cancel"], "Select action")
        stat = nil
        case action
        when 0
          stat = rc.transfer( selected_jobs)
          Dialog::message("File transfer to #{hosts[s]} failed") unless stat
        when 1
          stat = rc.submit( selected_jobs)
          Dialog::message("Submission to #{hosts[s]} failed") unless stat
        when 2
        end
        Dialog::message("Completed successfully.") if stat
      else
        Dialog::message("Connection to #{hosts[s]} failed.")
      end
    end
=end
    return :run_layer
  when "Cancel jobs"
    header,list = rlc.not_finished_jobs_list_in_csv
    list.unshift("Cancel")
    selected_jobs = Dialog::select_many( list, header).map do |num|
      list[num].split(',')[0].to_i
    end
    unless selected_jobs.include?(0)
      stat = rlc.cancel_jobs( selected_jobs)
      if stat
        str = selected_jobs.join(", ")
        Dialog::message("The following jobs are successfully cancelled.\n#{str}")
      else
        Dialog::message("Cancellation failed.")
      end
    end
    return :run_layer
  when "Analyze data"
    list = rlc.analysis_methods
    list = [ "Cancel", "All"] + list
    sel = Dialog.select_many( list, "Select numbers.")
    if sel.include?(0)
      return :run_layer
    elsif sel.include?(1)
      rlc.analyze_all
      return :run_layer
    else
      sel.each do |i|
        rlc.analyze( list[i])
      end
      return :run_layer
    end
  when "Exit"
    return :exit
  else
    raise "Must not happen"
  end
end

#sim_layer_actionObject

— action at simulator layer



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/nera/nera_cui.rb', line 63

def sim_layer_action
  $stdout.puts <<"HEADER"
===============================================
--- in simulator layer ------------------------
  #{@path_db_folder} 
===============================================
HEADER
  
  slc = NERA::SimulatorLayerController.new(@path_db_folder)
  list = slc.list
  names = list.map do |r|
    r[:name]
  end
  names.unshift("go to Jobs layer")
  names += ["show logs", "remove a simulator", "dump a simulator", "import a simulator", "exit"]
  selected = 0
  Dir.chdir( slc.path_to_sim_layer) {
    selected = Dialog::select_one_or_return_string( names, "Select a simulator.")
    if selected.is_a?(String)
      system( selected)
      return :simulator_layer
    end
  }
  case names[selected]
  when "go to Jobs layer"
    return :job_layer
  when "show logs"
    system "less #{slc.path_to_log_file}"
    return :simulator_layer
  when "remove a simulator"
    sim_names = ["Cancel"] + slc.dumpable_simulators
    selected = Dialog::select_one( sim_names, "Select a simulator to be removed.")
    return :simulator_layer if selected == 0
    flag = slc.remove_simulator( sim_names[selected])
    if flag
      Dialog::message("Simulator \"#{sim_names[selected]}\" have been successfully removed from the database.")
    else
      Dialog::message("Failed to remove the simulator \"#{sim_names[selected]}\". There are uncompleted jobs.")
    end
    return :simulator_layer
  when "dump a simulator"
    sim_names = ["Cancel"] + slc.dumpable_simulators
    selected = Dialog::select_one( sim_names, "Select a simulator to be dumped.")
    return :simulator_layer if selected == 0
    flag = slc.dump_simulator( sim_names[selected])
    if flag
      Dialog::message("Data for the simulator \"#{sim_names[selected]}\" have been successfully dumped.")
    else
      Dialog::message("Failed to dump the simulator \"#{sim_names[selected]}\". There are uncompleted jobs.")
    end
    return :simulator_layer
  when "import a simulator"
    list = ["Cancel"] + slc.importable_simulators
    selected = Dialog::select_one( list, "Select a simulator to be dumped.")
    return :simulator_layer if selected == 0
    flag = slc.import_simulator( list[selected])
    if flag
      Dialog::message("Simulator \"#{list[selected]}\" have been successfully imported.")
    else
      Dialog::message("Failed to import the simulator \"#{sim_names[selected]}\".")
    end
    return :simulator_layer
  when "exit"
    return :exit
  else
    @simulator_class = names[selected]
    return :parameter_layer
  end
end