Class: NERA::ParameterLayerController

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

Instance Method Summary collapse

Constructor Details

#initialize(path_db_folder, sim_name) ⇒ ParameterLayerController

Returns a new instance of ParameterLayerController.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nera/nera_parameter_layer_controller.rb', line 19

def initialize( path_db_folder, sim_name)
  raise ArgumentError unless path_db_folder.is_a?(String)
  @db_folders = NERA::DbFolders.new( path_db_folder)
  sim_records = NERA::SimulatorRecords.new( @db_folders.path_to_simulators_table)
  sim_records.set_yaml_file( @db_folders.path_to_simulators_yaml )
  
  found = sim_records.list.find do |rec|
    rec[:name] == sim_name.to_s
  end
  raise "No such simulator." unless found
  sim_id = found[:id]
  @sim_class = NERA::Simulator.inherited_simulators.find do |sim|
    sim.to_s == sim_name.to_s
  end
  raise "No such simulator." unless @sim_class
  
  p_tab_path = @db_folders.path_to_parameters_table( @sim_class)
  @param_records = NERA::ParameterRecords.new( p_tab_path, sim_records, sim_id)
  @param_records.set_yaml_file( @db_folders.path_to_parameters_yaml( @sim_class) )

  convert_to_recent_format
end

Instance Method Details

#create_a_new_parameter_set(p_hash) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/nera/nera_parameter_layer_controller.rb', line 127

def create_a_new_parameter_set( p_hash)
  pid = nil
  @param_records.transaction {
    pid = @param_records.add( p_hash)
    return nil unless pid
    b = @db_folders.create_new_parameter_folder( @sim_class, pid)
    raise "Creation of a new folder for #{@sim_calss}, #{pid} failed." unless b
    path = @db_folders.path_to_runs_table( @sim_class, pid)
    c = NERA::RunRecords.create_table( path)
    raise "File #{path} already exists." unless c
    @db_folders.logger.info(self.class.to_s) {
      "created a new parameter set (#{@sim_class.to_s}/#{pid})"
    }
  }
  return pid
end

#delete_a_parameter_set(id) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/nera/nera_parameter_layer_controller.rb', line 217

def delete_a_parameter_set( id)
  @param_records.transaction {
    flag = @param_records.destroy(id)
    return nil unless flag
    r_path = @db_folders.path_to_run_layer( @sim_class, id).sub(/\/$/,'')
    FileUtils.rm("#{r_path}.tar.bz2")
    @db_folders.logger.info(self.class.to_s) {
      "deleted a parameter set in the trashbox (#{@sim_class.to_s}/#{id})"
    }
  }
  return true
end

#find_ids_by_parameter_hash(param_hash) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nera/nera_parameter_layer_controller.rb', line 93

def find_ids_by_parameter_hash( param_hash)
  found_ids = []
  @param_records.list_all.each do |rec|
    matched = true
    param_hash.each_pair do |key,value|
      unless rec[key] == value
        matched = false
        break
      end
    end
    if matched
      found_ids << rec[:id]
    end
  end
  found_ids = nil unless found_ids.size > 0
  return found_ids
end

#get_id_from_csv_string(pstr) ⇒ Object



89
90
91
# File 'lib/nera/nera_parameter_layer_controller.rb', line 89

def get_id_from_csv_string( pstr)
  return pstr.split(',').first.to_i
end

#list_of_parameters(param_id = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/nera/nera_parameter_layer_controller.rb', line 111

def list_of_parameters( param_id = nil)
  param_list = []
  @sim_class::Parameters.each do |elem| param_list << elem.dup end
  if param_id
    found = @param_records.list_all.find do |rec| rec[:id] == param_id end
    if found         # load parameters
      param_list.each do |param| param[2] = found[ param[0] ] end
    end
  end
  return param_list
end

#move_a_parameter_set_into_trashbox(id) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/nera/nera_parameter_layer_controller.rb', line 144

def move_a_parameter_set_into_trashbox( id)
  return nil unless @param_records.find_by_id(id)
  run_records = NERA::RunRecords.new( @db_folders.path_to_runs_table( @sim_class, id), @param_records, id )
  return nil if run_records.list_all_not_finished.size > 0

  job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table)
  job_records.transaction { # to lock the runs tables
    @param_records.transaction {
      flag = @param_records.update_to_state_trashbox(id)
      return nil unless flag
      r_path = @db_folders.path_to_run_layer( @sim_class, id).sub(/\/$/,'')
      cmd = "tar cjf #{File.basename(r_path)}.tar.bz2 #{File.basename(r_path)}"
      Dir.chdir( File.dirname(r_path)) { system(cmd) }
      raise "Command #{cmd} failed" unless $? == 0
      FileUtils.rm_r(r_path)
      @db_folders.logger.info(self.class.to_s) {
        "moved a parameter set into the trashbox (#{@sim_class.to_s}/#{id})"
      }
    }
  }
  return true
end

#parameters_list_in_csvObject



61
62
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
# File 'lib/nera/nera_parameter_layer_controller.rb', line 61

def parameters_list_in_csv
  l_active = @param_records.list_active
  header = @param_records.keys.dup
  header.delete( :in_trashbox?)
  csv_list = []
  l_active.each do |p_hash|
    strings = []
    header.each do |key|
      if p_hash[key].is_a?(DateTime)
        strings << p_hash[key].to_s.split('T')[0]
      elsif p_hash[key]
        strings << p_hash[key].to_s
      elsif p_hash[key] == nil
        found = @sim_class::Parameters.find do |p| p[0] == key end
        strings << found[2].to_s
      end
    end
    strings[1] = "#{strings[1]}/#{strings[2]}"
    strings.delete_at(2)
    csv_list << strings.join(", ")
  end

  header.delete( :num_finished_runs)
  header[1] = :num_runs
  
  return header.join(", "), csv_list
end

#path_to_param_layerObject



123
124
125
# File 'lib/nera/nera_parameter_layer_controller.rb', line 123

def path_to_param_layer
  return  @db_folders.path_to_parameter_layer( @sim_class)
end

#revert_a_parameter_set_in_trashbox(id) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/nera/nera_parameter_layer_controller.rb', line 198

def revert_a_parameter_set_in_trashbox( id)
  @param_records.transaction {
    flag = @param_records.update_to_state_active(id)
    return nil unless flag
    r_path = @db_folders.path_to_run_layer( @sim_class, id).sub(/\/$/,'')
    cmd = "tar xjf #{File.basename(r_path)}.tar.bz2"
    Dir.chdir( File.dirname(r_path) ) {
      system(cmd)
    }
    raise "Command #{cmd} failed" unless $? == 0
    FileUtils.rm(r_path+".tar.bz2")
    @db_folders.logger.info(self.class.to_s) {
      "reverted a parameter set in the trashbox (#{@sim_class.to_s}/#{id})"
    }
    convert_to_recent_format
  }
  return true
end

#trashbox_parameter_list_in_csvObject



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

def trashbox_parameter_list_in_csv
  lt = @param_records.list_trashbox
  header = @param_records.keys.dup
  header.delete( :in_trashbox?)
  csv_list = []
  lt.each do |p_hash|
    strings = []
    header.each do |key|
      if p_hash[key].is_a?(DateTime)
        strings << p_hash[key].to_s.split('T')[0]
      elsif p_hash[key]
        strings << p_hash[key].to_s
      elsif p_hash[key] == nil
        if key == :num_finished_runs or key == :num_all_runs
          strings << 0
        else
          found = @sim_class::Parameters.find do |param| param[0] == key end
          strings << found[2].to_s
        end
      end
    end
    strings[1] = "#{strings[1].to_i}/#{strings[2].to_i}"
    strings.delete_at(2)
    csv_list << strings.join(", ")
  end

  header.delete( :num_finished_runs)
  header[1] = :num_runs
  return header.join(", "), csv_list
end