Class: NERA::SimulatorLayerController

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

Instance Method Summary collapse

Constructor Details

#initialize(path_db_folds) ⇒ SimulatorLayerController

Returns a new instance of SimulatorLayerController.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nera/nera_simulator_layer_controller.rb', line 16

def initialize( path_db_folds)
  raise ArgumentError unless path_db_folds.is_a?(String)
  
  @db_folders = NERA::DbFolders.new( path_db_folds)
  table_path = @db_folders.path_to_simulators_table
  @sim_records = NERA::SimulatorRecords.new(table_path)
  @sim_records.set_yaml_file( @db_folders.path_to_simulators_yaml )

  NERA::Simulator.inherited_simulators.each do |sim|
    next if list.find do |rec| rec[:name] == sim.to_s end
    mapped = NERA::ParameterRecords::ATTRIBUTES_PART.map do |att| att[0] end
    found = sim::Parameters.find do |p|
      mapped.include?(p[0])
      # p[0] == :in_trashbox? or p[0] == :created_at or p[0] == :updated_at
    end
    next if found
    register_a_simulator( sim)
  end
end

Instance Method Details

#dump_simulator(sim_name, job_records = nil) ⇒ Object



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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/nera/nera_simulator_layer_controller.rb', line 138

def dump_simulator( sim_name, job_records = nil)
  flag = true
  sim_class = get_class_by_name( sim_name)
  return nil unless sim_class
  sim_id = (list.find do |rec| rec[:name] == sim_name end)[:id]
  return nil unless sim_id
  job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table ) unless job_records.is_a?(NERA::JobRecords)
  job_records.transaction {
    @sim_records.transaction {
      # check uncompleted jobs
      found = job_records.list_all.find do |rec| rec[:simulator] == sim_name end
      if found
        flag = nil
        return
      end
      # destroy the record
      # f = @sim_records.destroy( sim_id)
      # raise "Couldn't destroy the record #{sim_id} in simulators table" unless f
      # compression
      path1 = @db_folders.path_to_parameter_layer( sim_class)
      path2 = File.dirname( @db_folders.path_to_parameters_table( sim_class) )
      path3 = File.join( @db_folders.path_to_simulator_layer, "Simulator_classes", "#{sim_name}.rb")
      raise "Couldn't find #{path1}" unless File.directory?(path1)
      raise "Couldn't find #{path2}" unless File.directory?(path2)
      raise "Couldn't find #{path3}" unless File.exist?(path3)
      base_path = Pathname.new( @db_folders.path_to_simulator_layer ).realpath
      path1 = Pathname.new( path1).realpath.relative_path_from( base_path).to_s
      path2 = Pathname.new( path2).realpath.relative_path_from( base_path).to_s
      path3 = Pathname.new( path3).realpath.relative_path_from( base_path).to_s
      Dir.chdir(base_path) {
        tar_file = "#{sim_id}_#{sim_name}.tar"
        cmd = "tar cf #{tar_file} #{path1} #{path2} #{path3}"
        system cmd
        raise "command \"#{cmd}\" failed." unless $? == 0
        bz2_file = tar_file + '.bz2'
        FileUtils.rm( bz2_file) if File.exist?( bz2_file )
        cmd = "bzip2 #{tar_file}"
        system cmd
        raise "command \"#{cmd}\" failed." unless $? == 0
        # FileUtils.rm_r( path1)
        # FileUtils.rm_r( path2)
        # FileUtils.rm( path3)
      }
      # remove inherited classes
      # NERA::Simulator.remove_simulator( sim_class)
      # keep a log
      @db_folders.logger.info(self.class.to_s) {
        "dumped the simulator (#{sim_class.to_s})"
      }
    }
  }
  return flag
end

#dumpable_simulatorsObject



75
76
77
78
79
80
81
82
# File 'lib/nera/nera_simulator_layer_controller.rb', line 75

def dumpable_simulators
  job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table )
  names = @sim_records.list.map do |rec| rec[:name] end
  dumpable = names.find_all do |nam|
    found = !( job_records.list_all.find do |jrec| jrec[:simulator] == nam end )
  end
  return dumpable
end

#get_class_by_name(sim_name) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
# File 'lib/nera/nera_simulator_layer_controller.rb', line 44

def get_class_by_name( sim_name)
  raise ArgumentError unless sim_name.is_a?(String)
  found = NERA::Simulator.inherited_simulators.find do |sim|
    sim.to_s == sim_name
  end
  return found
end

#import_simulator(sim_file) ⇒ Object



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

def import_simulator( sim_file)
  flag = true
  return nil unless sim_file =~ /^([0-9]+)_([A-Z]\w*)\.tar\.bz2/
  return nil unless File.exist?( File.join(@db_folders.path_to_simulator_layer,sim_file) )
  sim_name = $2
  job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table )

  job_records.transaction {
    @sim_records.transaction {
      # check name overlap
      if @sim_records.list.find do |rec| rec[:name] == sim_name end
        flag = nil
        return
      end
      raise "must not happen" if File.directory?( File.join( @db_folders.path_to_simulator_layer, sim_name ) )
      raise "must not happen" if File.directory?( File.join( File.dirname(@db_folders.path_to_simulators_table), sim_name) )
      raise "must not happen" if File.exist?( File.join( @db_folders.path_to_simulator_layer, "Simulator_classes", "#{sim_name}.rb") )
      # decompress the file
      Dir.chdir(@db_folders.path_to_simulator_layer) {
        # cmd = "bunzip2 #{sim_file}"
        # system cmd
        # raise "command \"#{cmd}\" failed." unless $? == 0
        tar_file = sim_file.sub(/\.bz2$/,'')
        cmd = "tar xjf #{sim_file}"
        system cmd
        raise "command \"#{cmd}\" failed." unless $? == 0
      }
      # load simulator definition
      require File.join( @db_folders.path_to_simulator_layer, "Simulator_classes", "#{sim_name}.rb")
      sim_class = sim_name.split(/::/).inject(Object) {|c,name| c.const_get(name) }
      raise "invalid definition of simulator class #{sim_name}" unless sim_class.is_a?(Class)
      NERA::Simulator.add_simulator( sim_class)
      flag = @sim_records.add( sim_class)
      # keep a log
      @db_folders.logger.info(self.class.to_s) {
        "imported the simulator (#{sim_class.to_s})"
      }
    }
  }
  return flag
end

#importable_simulatorsObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/nera/nera_simulator_layer_controller.rb', line 193

def importable_simulators
  sim_names = @sim_records.list.map do |rec| rec[:name] end

  Dir.chdir( @db_folders.path_to_simulator_layer ) {
    candidates = Dir.glob( "[0-9]*_[A-Z]*.tar.bz2").find_all do |file|
      file =~ /^([0-9]+)_([A-Z]\w*)\.tar\.bz2/ and !(sim_names.include?($2))
    end
    # found = candidates.find_all do |file|
    #   file =~ /^([0-9]+)_([A-Z]\w*)\.tar\.bz2/ and
    #     !(@sim_records.list.include? do |rec| rec[:name] == $2 end)
    # end
    return candidates
  }
end

#listObject



36
37
38
# File 'lib/nera/nera_simulator_layer_controller.rb', line 36

def list
  @sim_records.list
end

#path_to_log_fileObject



71
72
73
# File 'lib/nera/nera_simulator_layer_controller.rb', line 71

def path_to_log_file
  @db_folders.path_to_log_file
end

#path_to_sim_layerObject



40
41
42
# File 'lib/nera/nera_simulator_layer_controller.rb', line 40

def path_to_sim_layer
  @db_folders.path_to_simulator_layer
end

#remove_simulator(sim_name) ⇒ Object



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
132
133
134
135
136
# File 'lib/nera/nera_simulator_layer_controller.rb', line 84

def remove_simulator( sim_name)
  flag = true
  sim_class = get_class_by_name( sim_name)
  return nil unless sim_class
  sim_id = (list.find do |rec| rec[:name] == sim_name end)[:id]
  return nil unless sim_id
  job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table )
  job_records.transaction {
    @sim_records.transaction {
      # check uncompleted jobs
      found = job_records.list_all.find do |rec| rec[:simulator] == sim_name end
      if found
        flag = nil
        return
      end
      # destroy the record
      f = @sim_records.destroy( sim_id)
      raise "Couldn't destroy the record #{sim_id} in simulators table" unless f
      # compression
      path1 = @db_folders.path_to_parameter_layer( sim_class)
      path2 = File.dirname( @db_folders.path_to_parameters_table( sim_class) )
      path3 = File.join( @db_folders.path_to_simulator_layer, "Simulator_classes", "#{sim_name}.rb")
      raise "Couldn't find #{path1}" unless File.directory?(path1)
      raise "Couldn't find #{path2}" unless File.directory?(path2)
      raise "Couldn't find #{path3}" unless File.exist?(path3)
      base_path = Pathname.new( @db_folders.path_to_simulator_layer ).realpath
      path1 = Pathname.new( path1).realpath.relative_path_from( base_path).to_s
      path2 = Pathname.new( path2).realpath.relative_path_from( base_path).to_s
      path3 = Pathname.new( path3).realpath.relative_path_from( base_path).to_s
      Dir.chdir(base_path) {
        tar_file = "#{sim_id}_#{sim_name}.tar"
        cmd = "tar cf #{tar_file} #{path1} #{path2} #{path3}"
        system cmd
        raise "command \"#{cmd}\" failed." unless $? == 0
        bz2_file = tar_file + '.bz2'
        FileUtils.rm( bz2_file) if File.exist?( bz2_file )
        cmd = "bzip2 #{tar_file}"
        system cmd
        raise "command \"#{cmd}\" failed." unless $? == 0
        FileUtils.rm_r( path1)
        FileUtils.rm_r( path2)
        FileUtils.rm( path3)
      }
      # remove inherited classes
      NERA::Simulator.remove_simulator( sim_class)
      # keep a log
      @db_folders.logger.info(self.class.to_s) {
        "removed the simulator (#{sim_class.to_s})"
      }
    }
  }
  return flag
end