Class: NERA::ParameterRecords

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#keysObject (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

Raises:

  • (ArgumentError)


200
201
202
203
204
205
# File 'lib/nera/nera_parameter_records.rb', line 200

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

#list_activeObject



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_allObject



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_trashboxObject



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

Raises:

  • (ArgumentError)


190
191
192
193
194
195
196
# File 'lib/nera/nera_parameter_records.rb', line 190

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

#transactionObject



207
208
209
210
211
# File 'lib/nera/nera_parameter_records.rb', line 207

def transaction
  @db.transaction{
    yield
  }
end

#update_num_runs(id, num_finished, num_all) ⇒ Object

Raises:

  • (ArgumentError)


176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/nera/nera_parameter_records.rb', line 176

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

Raises:

  • (ArgumentError)


156
157
158
159
160
161
162
163
164
# File 'lib/nera/nera_parameter_records.rb', line 156

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

Raises:

  • (ArgumentError)


166
167
168
169
170
171
172
173
174
# File 'lib/nera/nera_parameter_records.rb', line 166

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