Class: NERA::ParameterRecords
- Inherits:
-
Object
- Object
- NERA::ParameterRecords
- Defined in:
- lib/nera/nera_parameter_records.rb
Overview
The parameter tables are accessed from this class
Constant Summary collapse
- ATTRIBUTES_PART =
keys of the parameter table
[ [:id, Integer], [:num_finished_runs, Integer], [:num_all_runs, Integer], [:created_at, DateTime], [:updated_at, DateTime], [:in_trashbox?, Symbol] ]
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Class Method Summary collapse
-
.create_table(db_file, sim_class) ⇒ Object
if file already exists, return false.
Instance Method Summary collapse
-
#add(param_hash) ⇒ Object
add one record.
- #destroy(id) ⇒ Object
- #find_by_id(id) ⇒ Object
-
#find_similar_parameter_sets(param_id, param_key) ⇒ Object
method to find similar parameter sets.
-
#initialize(db_file, sim_records, sim_id) ⇒ ParameterRecords
constructor
argument is the path to database file.
- #list_active ⇒ Object
- #list_all ⇒ Object
- #list_trashbox ⇒ Object
-
#set_yaml_file(yaml_path) ⇒ Object
argument is the path to parameters.yml.
- #touch(id) ⇒ Object (also: #update)
- #transaction ⇒ Object
- #update_num_runs(id, num_finished, num_all) ⇒ Object
- #update_to_state_active(id) ⇒ Object
- #update_to_state_trashbox(id) ⇒ Object
Constructor Details
#initialize(db_file, sim_records, sim_id) ⇒ ParameterRecords
argument is the path to database file
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/nera/nera_parameter_records.rb', line 28 def initialize( db_file, sim_records, sim_id) unless sim_records.is_a?( NERA::SimulatorRecords) raise ArgumentError, "The second argument must be an instance of NERA::SimualtorRecords. #{sim_records.inspect}" end unless sim_id.is_a?(Integer) raise ArgumentError, "sim_id must be an Integer. : #{sim_id.inspect}" end @sim_class = sim_records.get_class( sim_id) @keys = ATTRIBUTES_PART.map do |k| k[0] end sim_keys = @sim_class::Parameters.map do |x| x[0] end found = @keys.find do |a| sim_keys.include?(a) end if found raise ArgumentError, "Invalid class. This class has a parameter : #{found}" end @keys += sim_keys @db = NERA::Database.new( db_file, sim_id) @db.add_observer( sim_records) end |
Instance Attribute Details
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
22 23 24 |
# File 'lib/nera/nera_parameter_records.rb', line 22 def keys @keys end |
Class Method Details
.create_table(db_file, sim_class) ⇒ Object
if file already exists, return false
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/nera/nera_parameter_records.rb', line 60 def self.create_table( db_file, sim_class) unless sim_class.superclass == NERA::Simulator raise ArgumentError, "Given simulator class is not a subclass of NERA::Simulator : #{sim_class}" end ks = ATTRIBUTES_PART.map do |k| k[0] end params = sim_class::Parameters.map do |x| x[0] end found = ks.find do |a| params.include?(a) end if found raise ArgumentError, "Invalid class. This class has a parameter : #{found}" end NERA::Database.create_table( db_file) end |
Instance Method Details
#add(param_hash) ⇒ Object
add one record
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 |
# File 'lib/nera/nera_parameter_records.rb', line 80 def add( param_hash) # check the argument unless param_hash.is_a?(Hash) raise ArgumentError, "Argument must be a Hash." end # check the argument sim_keys = @sim_class::Parameters.map do |x| x[0] end param_hash.each_pair do |param,value| unless sim_keys.include?(param) raise ArgumentError, "The given parameter set has unknown key." end a = @sim_class::Parameters.find do |p| p[0] == param end unless value.is_a?( a[1] ) raise ArgumentError, "The given parameter set has invalid type." end end # check duplication found = @db.find_all do |rec| f = true @sim_class::Parameters.each do |p| # set default values rec[ p[0] ] = p[2] if rec[ p[0] ] == nil param_hash[ p[0] ] = p[2] if param_hash[ p[0]] == nil unless rec[p[0]] == param_hash[p[0]] f = false break end end f end if found # the given parameter set already exists. return nil end # add a parameter d = DateTime.now h = {:num_finished_runs => 0, :num_all_runs => 0, :created_at => d, :updated_at => d, :in_trashbox? => :active} ps = param_hash.dup @sim_class::Parameters.each do |p| ps[ p[0]] = p[3] if ps[p[0]] == nil end h.merge!( ps) @db.add(h) end |
#destroy(id) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/nera/nera_parameter_records.rb', line 236 def destroy( id) raise ArgumentError unless id.is_a?(Integer) found = @db.find_by_id( id) return nil if found == nil or found[:in_trashbox?] == :active @db.destroy(id) end |
#find_by_id(id) ⇒ Object
152 153 154 |
# File 'lib/nera/nera_parameter_records.rb', line 152 def find_by_id( id) @db.find_by_id( id) end |
#find_similar_parameter_sets(param_id, param_key) ⇒ Object
method to find similar parameter sets. argument ( parameter_id to compare, parameter name) return the array having arrays of [ found_id, values of the parameter]. example:
param_recs.find_similar_parameter_sets( 3, :L)
# => [ [1,18],[2,22],[10,128] ]
For this case, param_id 1,2,3,10 have the same parameters except for :L. :L for param_id 1, 2, 10 are 18, 22, and 128, respectively.
If the parameter set specified by the first argument <param_id> is not found, nil is returned.
If the simulator does not have the parameter specified by the second argument <param>, nil is returned.
If a similar parameter is not found, [] is returned.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/nera/nera_parameter_records.rb', line 172 def find_similar_parameter_sets( param_id, param_key) return nil unless @sim_class::Parameters.any? do |prm| prm[0] == param_key end found = find_by_id( param_id.to_i) return nil unless found match = {} @sim_class::Parameters.each do |prm| next if prm[0] == param_key match[ prm[0] ] = prm[2] match[ prm[0] ] = found[ prm[0] ] if found.has_key?( prm[0] ) end list = @db.find_all do |r| next if r[:id] == param_id match.all? do |k, val| r[k] == val end end a = list.to_a.map do |rec| [ rec[:id], rec[param_key] ] end sorted = a.sort_by do |elem| elem[1] end return sorted end |
#list_active ⇒ Object
138 139 140 141 142 143 |
# File 'lib/nera/nera_parameter_records.rb', line 138 def list_active list = @db.find_all do |r| r[:in_trashbox?] == :active end return list.to_a end |
#list_all ⇒ Object
131 132 133 134 135 136 |
# File 'lib/nera/nera_parameter_records.rb', line 131 def list_all list = @db.find_all do |r| true end return list.to_a end |
#list_trashbox ⇒ Object
145 146 147 148 149 150 |
# File 'lib/nera/nera_parameter_records.rb', line 145 def list_trashbox list = @db.find_all do |r| r[:in_trashbox?] == :trashbox end return list.to_a end |
#set_yaml_file(yaml_path) ⇒ Object
argument is the path to parameters.yml
55 56 57 |
# File 'lib/nera/nera_parameter_records.rb', line 55 def set_yaml_file( yaml_path) @db.set_yaml_file( yaml_path) end |
#touch(id) ⇒ Object Also known as: update
226 227 228 229 230 231 232 |
# File 'lib/nera/nera_parameter_records.rb', line 226 def touch( id) raise ArgumentError unless id.is_a?(Integer) found = @db.find_by_id( id) return nil unless found found[:updated_at] = DateTime.now @db.update( found) end |
#transaction ⇒ Object
243 244 245 246 247 |
# File 'lib/nera/nera_parameter_records.rb', line 243 def transaction @db.transaction{ yield } end |
#update_num_runs(id, num_finished, num_all) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/nera/nera_parameter_records.rb', line 212 def update_num_runs( id, num_finished, num_all) raise ArgumentError unless id.is_a?(Integer) and num_finished.is_a?(Integer) and num_all.is_a?(Integer) return nil if num_finished < 0 or num_all < 0 return nil if num_finished > num_all found = @db.find_by_id(id) return nil unless found return nil if found[:in_trashbox?] == :trashbox found[:num_finished_runs] = num_finished found[:num_all_runs] = num_all @db.update( found) end |
#update_to_state_active(id) ⇒ Object
192 193 194 195 196 197 198 199 200 |
# File 'lib/nera/nera_parameter_records.rb', line 192 def update_to_state_active( id) raise ArgumentError unless id.is_a?(Integer) found = @db.find_by_id( id) return nil unless found return nil if found[:in_trashbox?] == :active found[:in_trashbox?] = :active @db.update( found) end |
#update_to_state_trashbox(id) ⇒ Object
202 203 204 205 206 207 208 209 210 |
# File 'lib/nera/nera_parameter_records.rb', line 202 def update_to_state_trashbox( id) raise ArgumentError unless id.is_a?(Integer) found = @db.find_by_id( id) return nil unless found return nil if found[:in_trashbox?] == :trashbox found[:in_trashbox?] = :trashbox @db.update( found) end |