nera 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/History.txt +4 -0
  2. data/Manifest.txt +37 -0
  3. data/README.rdoc +47 -0
  4. data/Rakefile +27 -0
  5. data/bin/nera +29 -0
  6. data/bin/nera_addsim +30 -0
  7. data/lib/nera/nera_cui.rb +417 -0
  8. data/lib/nera/nera_database.rb +281 -0
  9. data/lib/nera/nera_db_folders.rb +226 -0
  10. data/lib/nera/nera_dialog.rb +205 -0
  11. data/lib/nera/nera_job_layer_controller.rb +237 -0
  12. data/lib/nera/nera_job_records.rb +111 -0
  13. data/lib/nera/nera_job_script.rb +202 -0
  14. data/lib/nera/nera_parameter_layer_controller.rb +157 -0
  15. data/lib/nera/nera_parameter_records.rb +186 -0
  16. data/lib/nera/nera_run_layer_controller.rb +192 -0
  17. data/lib/nera/nera_run_records.rb +184 -0
  18. data/lib/nera/nera_simulator.rb +26 -0
  19. data/lib/nera/nera_simulator_layer_controller.rb +66 -0
  20. data/lib/nera/nera_simulator_records.rb +84 -0
  21. data/lib/nera.rb +25 -0
  22. data/lib/nera_addsim/make_simulator.rb +307 -0
  23. data/scripts/make_manifest.rb +21 -0
  24. data/test/runner.rb +3 -0
  25. data/test/test_helper.rb +52 -0
  26. data/test/test_nera_database.rb +221 -0
  27. data/test/test_nera_db_folders.rb +209 -0
  28. data/test/test_nera_dialog.rb +134 -0
  29. data/test/test_nera_job_layer_controller.rb +132 -0
  30. data/test/test_nera_job_records.rb +260 -0
  31. data/test/test_nera_parameter_layer_controller.rb +188 -0
  32. data/test/test_nera_parameter_records.rb +285 -0
  33. data/test/test_nera_run_layer_controller.rb +171 -0
  34. data/test/test_nera_run_records.rb +290 -0
  35. data/test/test_nera_simulator.rb +26 -0
  36. data/test/test_nera_simulator_layer_controller.rb +54 -0
  37. data/test/test_nera_simulator_records.rb +140 -0
  38. metadata +125 -0
@@ -0,0 +1,84 @@
1
+ require 'nera_database'
2
+ require 'nera_simulator'
3
+
4
+ module NERA
5
+
6
+ # This class access the simulator table.
7
+ class SimulatorRecords
8
+
9
+ # keys of the simulator table
10
+ ATTRIBUTES = [ [:id, Integer], [:name, String], [:created_at, DateTime], [:updated_at, DateTime] ]
11
+
12
+ # NERA::Database object
13
+ @db
14
+
15
+ # argument is the path to database file
16
+ def initialize( db_file)
17
+ @db = NERA::Database.new( db_file)
18
+ end
19
+
20
+ # if file already exists, returns false
21
+ def self.create_table( db_file)
22
+ NERA::Database.create_table( db_file)
23
+ end
24
+
25
+ # return keys of the simulator table
26
+ def keys
27
+ keys = ATTRIBUTES.map do |x|
28
+ x[0]
29
+ end
30
+ end
31
+
32
+ # sim_class must be a subclass of NERA::Simulator
33
+ def add( sim_class)
34
+ unless sim_class.superclass == NERA::Simulator
35
+ raise ArgumentError, "must be a subclass of NERA::Simulator"
36
+ end
37
+ name = sim_class.to_s
38
+ found = @db.find_all do |rec|
39
+ rec[:name] == name
40
+ end
41
+ if found
42
+ $stderr.puts "Same simulator already exists."
43
+ return nil
44
+ end
45
+ d = DateTime.now
46
+ h = { :name => name, :created_at => d, :updated_at => d}
47
+ id = @db.add( h)
48
+ return id
49
+ end
50
+
51
+ def get_class( id)
52
+ raise ArgumentError unless id.is_a?(Integer)
53
+ rec = @db.find_by_id(id)
54
+ return nil unless rec
55
+ klass = eval(rec[:name])
56
+ return klass
57
+ end
58
+
59
+ def list
60
+ l = @db.find_all do |r| true end
61
+ return l.to_a
62
+ end
63
+
64
+ def touch( id)
65
+ raise ArgumentError unless id.is_a?( Integer)
66
+ rec = @db.find_by_id( id)
67
+ return nil unless rec
68
+ rec[:updated_at] = DateTime.now
69
+ @db.update( rec)
70
+ end
71
+
72
+ def destroy( id)
73
+ raise ArgumentError unless id.is_a?(Integer)
74
+ @db.destroy(id)
75
+ end
76
+
77
+ def transaction
78
+ @db.transaction {
79
+ yield
80
+ }
81
+ end
82
+
83
+ end
84
+ end
data/lib/nera.rb ADDED
@@ -0,0 +1,25 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ $:.unshift(File.dirname(__FILE__)+'/nera/') unless
5
+ $:.include?(File.dirname(__FILE__)+'/nera/') || $:.include?(File.expand_path(File.dirname(__FILE__))+'/nera/')
6
+
7
+ require 'yaml'
8
+ require 'nera_cui'
9
+ require 'nera_database'
10
+ require 'nera_db_folders'
11
+ require 'nera_dialog'
12
+ require 'nera_job_layer_controller'
13
+ require 'nera_job_records'
14
+ require 'nera_job_script'
15
+ require 'nera_parameter_layer_controller'
16
+ require 'nera_parameter_records'
17
+ require 'nera_run_layer_controller'
18
+ require 'nera_run_records'
19
+ require 'nera_simulator'
20
+ require 'nera_simulator_layer_controller'
21
+ require 'nera_simulator_records'
22
+
23
+ module NERA
24
+ VERSION = '0.0.1'
25
+ end
@@ -0,0 +1,307 @@
1
+ require 'nera_dialog'
2
+
3
+ module NERA
4
+
5
+ class CUI_Simulator_Adder
6
+ #-------------- create simulator script --------------------
7
+ def script_body(sim_info)
8
+ params=[]
9
+ sim_info[:params].each do | param |
10
+ params << " [#{param[0]},#{param[1]},#{param[2]}]"
11
+ end
12
+ params_str = params.join(",\n\t\t")
13
+ str = <<"BODY"
14
+ class #{sim_info[:name]} < NERA::Simulator
15
+
16
+ Parameters = [#{params_str}\n\t\t]
17
+
18
+ def simulate_command(seed)
19
+ return \"#{sim_info[:exec]}\"
20
+ end
21
+
22
+ end
23
+ BODY
24
+ return str
25
+ end
26
+ private :script_body
27
+
28
+ #-------------- add new parameter --------------------
29
+ def add_param
30
+ array = []
31
+ type = [Integer, Float, String]
32
+ while true
33
+ $stdout.puts
34
+ $stdout.puts "+++Input parameter name+++"
35
+ name = Readline.readline(">").chomp.strip
36
+ break if name =~ /^[a-z]/ or name =~ /^[A-Z]/
37
+ $stdout.puts "Input is not valid"
38
+ $stdout.puts
39
+ $stdout.puts "Input the simulator name again."
40
+ end
41
+ array << ":#{name}"
42
+ $stdout.puts
43
+ num = Dialog.select_one(type, "++Select parameter type+++")
44
+ array << type[num]
45
+ default = 0
46
+ while true
47
+ $stdout.puts
48
+ $stdout.puts "+++Input default value+++"
49
+ default = Readline.readline(">").chomp.strip
50
+ if default == ""
51
+ $stdout.puts "Input is empty"
52
+ next
53
+ end
54
+ if type[num] == Integer or type[num] == Float
55
+ if /[-+]?(?:[0-9]+(\.[0-9]*)?|(\.[0-9]+))([eE][-+]?[0-9]+)?/ =~ default
56
+ break
57
+ end
58
+ else
59
+ default = "\"#{default}\""
60
+ break
61
+ end
62
+ $stdout.puts "! Input value must be #{type[num]}"
63
+ end
64
+ array << default
65
+ $stdout.puts
66
+
67
+ return array
68
+ end
69
+ private :add_param
70
+
71
+
72
+ #-------------- modify parameter --------------------
73
+ def mod_param(param)
74
+ type = [Integer, Float, String]
75
+
76
+ while true
77
+ $stdout.puts
78
+ menu = ["return"]
79
+ menu += ["Name -> #{param[0].sub(':','')}"]
80
+ menu += ["Type -> #{param[1]}"]
81
+ menu += ["Default -> #{param[2]}"]
82
+ menu += ["delete"]
83
+ num = Dialog.select_one(menu,"Select modifying parameter")
84
+
85
+ case num
86
+ when 0
87
+ if param[1] == Integer or param[1] == Float
88
+ unless /[-+]?(?:[0-9]+(\.[0-9]*)?|(\.[0-9]+))([eE][-+]?[0-9]+)?/ =~ param[2]
89
+ $stdout.puts "! Default value must be #{param[1]}"
90
+ next
91
+ end
92
+ end
93
+ return param
94
+ when 1
95
+ name = nil
96
+ while true
97
+ $stdout.puts
98
+ $stdout.puts "+++Input parameter name+++"
99
+ name = Readline.readline(">").chomp.strip
100
+ break if name =~ /^[a-z]/ or name =~ /^[A-Z]/
101
+ $stdout.puts "Input is not valid"
102
+ $stdout.puts
103
+ $stdout.puts "Input the simulator name again."
104
+ end
105
+ param[0] = ":#{name}"
106
+ when 2
107
+ $stdout.puts
108
+ num = Dialog.select_one(type, "+++Select new parameter type+++ \n #{param[1]} ->")
109
+ param[1] = type[num]
110
+ when 3
111
+ while true
112
+ $stdout.puts
113
+ $stdout.puts "+++Input new default value+++"
114
+ default = Readline.readline("#{param[2]} -> ").chomp.strip
115
+ # p param[1]
116
+ if param[1] == Integer or param[1] == Float
117
+ if /[-+]?(?:[0-9]+(\.[0-9]*)?|(\.[0-9]+))([eE][-+]?[0-9]+)?/ =~ default
118
+ param[2] = default
119
+ break
120
+ end
121
+ else
122
+ param[2] = "\"#{default}\""
123
+ break
124
+ end
125
+ $stdout.puts "! Input value must be #{param[1]}"
126
+ end
127
+ when 4
128
+ if param[1] == Integer or param[1] == Float
129
+ unless /[-+]?(?:[0-9]+(\.[0-9]*)?|(\.[0-9]+))([eE][-+]?[0-9]+)?/ =~ param[2]
130
+ $stdout.puts "! Default value must be #{param[1]}"
131
+ next
132
+ end
133
+ end
134
+ return []
135
+ end
136
+ end
137
+ end
138
+ private :mod_param
139
+
140
+
141
+ ########################################
142
+ # Main process
143
+ ########################################
144
+ def main_process( db_folder )
145
+ sim_info = {}
146
+ #-------------- set simulator name --------------------
147
+ $stdout.puts "=================================================="
148
+ $stdout.puts "(1/4) Simulator Name "
149
+ $stdout.puts "=================================================="
150
+ $stdout.puts "Input the simulator name."
151
+ while true
152
+ name = Readline.readline(">",true).chomp.strip
153
+ if name == ""
154
+ $stdout.puts "Input is empty"
155
+ $stdout.puts
156
+ $stdout.puts "Input the simulator name again."
157
+ next
158
+ end
159
+ if /\W/ =~ name
160
+ $stdout.puts "You can use only alphameric characters; 0-9, A-Z, a-z, and _(underscore) ."
161
+ $stdout.puts
162
+ next
163
+ elsif /^[_0-9]/ =~ name
164
+ $stdout.puts "Initial character must be capital Roman-character."
165
+ $stdout.puts
166
+ next
167
+ elsif /^[a-z]/ =~ name
168
+ name[0,1] = name[0,1].upcase
169
+ end
170
+
171
+ if File.exists?(db_folder+"Simulator_classes/"+name+".rb") or File.exists?(db_folder + name )
172
+ $stdout.puts "The simulator \"#{name}\" already exists. "
173
+ return nil
174
+ end
175
+
176
+ $stdout.print "Would you create a new simulator \"#{name}\" ?(y/n): "
177
+ if Readline.readline("").chomp.strip == "y"
178
+ sim_info[:name]=name
179
+ break
180
+ end
181
+ $stdout.puts
182
+ $stdout.puts "Input the simulator name again."
183
+ end
184
+
185
+ #-------------- set executable file --------------------
186
+ $stdout.puts
187
+ $stdout.puts "=================================================="
188
+ $stdout.puts "(2/4) Path of Executable File "
189
+ $stdout.puts "=================================================="
190
+ $stdout.puts "Input the path of the executable file."
191
+ home = File.expand_path("~/")+"/"
192
+ Dir.chdir(home) do
193
+ while true
194
+ path_file = Readline.readline("> ~/").chomp.strip
195
+ unless File.file?(home+path_file)
196
+ $stdout.puts "File \"~/#{path_file}\" is not found."
197
+ $stdout.print "Would you abort this registration\" ?(y/n): "
198
+ return nil if Readline.readline("").chomp.strip == "y"
199
+ else
200
+ if (File.stat(home+path_file).mode & 2**6 ) != 2**6
201
+ $stdout.puts "File ~/#{path_file} is not executable."
202
+ $stdout.print "Would you abort this registration\" ?(y/n): "
203
+ return nil if Readline.readline("").chomp.strip == "y"
204
+ end
205
+ end
206
+
207
+ $stdout.print "Would you use \"~/#{path_file}\" ?(y/n): "
208
+ if Readline.readline("").chomp.strip == "y"
209
+ sim_info[:path]="~/"+path_file
210
+ break
211
+ end
212
+ $stdout.puts
213
+ $stdout.puts "Input the path of the executable file again."
214
+ end
215
+ end
216
+
217
+ #-------------- set parameters --------------------
218
+ $stdout.puts
219
+ $stdout.puts "=================================================="
220
+ $stdout.puts "(3/4) Parameters "
221
+ $stdout.puts "=================================================="
222
+ $stdout.puts "Set parameters"
223
+
224
+ exec = ""
225
+ path = sim_info[:path]
226
+ params = []
227
+ while true
228
+ $stdout.puts "------------------------------"
229
+ $stdout.puts "Execute command :"
230
+ params_str = ""
231
+ params.each do | name, type, default |
232
+ params_str += " (#{name.sub(':','')})"
233
+ end
234
+ exec = "#{path}#{params_str} (seed)"
235
+ $stdout.puts " $ #{exec}"
236
+ $stdout.puts "------------------------------"
237
+ menu =["add"]
238
+ params.each do | name, type, default |
239
+ menu += ["#{name.sub(':','')} : #{type} (default #{default})"]
240
+ end
241
+ menu += ["exit"]
242
+ num = Dialog.select_one(menu)
243
+ # p menu[num]
244
+ case menu[num]
245
+ when "add"
246
+ params << add_param()
247
+ next
248
+ when "exit"
249
+ if params.length==0
250
+ $stdout.print "Any parameters don't exist. Would you continue this registration?(y/n): "
251
+ next if Readline.readline("").chomp.strip != "y"
252
+ end
253
+ break
254
+ else
255
+ case tmp = mod_param(params[num-1])
256
+ when []
257
+ params.delete_at(num-1)
258
+ when params[num-1]
259
+ next
260
+ else
261
+ params[num-1] = tmp
262
+ end
263
+
264
+ end
265
+ end
266
+
267
+ sim_info[:exec] = exec.gsub("(seed)", "\#\{seed\}").gsub("(", "\#\{@param\[:").gsub(")","\]\}")
268
+ sim_info[:params] = params
269
+
270
+
271
+ #-------------- finish --------------------
272
+ $stdout.puts
273
+ $stdout.puts "=================================================="
274
+ $stdout.puts "(4/4) Completed successfully "
275
+ $stdout.puts "=================================================="
276
+ $stdout.puts "+ Simulator Name : #{sim_info[:name]}"
277
+ $stdout.puts "+ Executable File : #{sim_info[:path]}"
278
+ $stdout.puts "+ Parameters : "
279
+ if sim_info[:params].length == 0
280
+ $stdout.puts " No parameter"
281
+ else
282
+ index = 1
283
+ cmd = []
284
+ sim_info[:params].each do |name, type, default|
285
+ cmd << name.sub(':','')
286
+ $stdout.puts " #{index}-> #{name.sub(':','')} : #{type} (default #{default}) "
287
+ index += 1
288
+ end
289
+ end
290
+ $stdout.puts "+ Command : \"#{sim_info[:exec].gsub( "\#\{seed\}","(seed)").gsub("\#\{@param\[:","(").gsub("\]\}",")")}\""
291
+
292
+ return sim_info
293
+ end
294
+
295
+ #-------------- output file --------------------
296
+ def initialize(db_folder)
297
+ sim_info = main_process( db_folder + '/' )
298
+ return nil if sim_info == nil
299
+ $stdout.puts "----------------------------------------"
300
+ filename = sim_info[:name]+".rb"
301
+ $stdout.puts "This simulator is saved as \"#{filename}\""
302
+ $stdout.puts "----------------------------------------"
303
+ File.open(db_folder+"/Simulator_classes/"+filename,"w").puts script_body(sim_info)
304
+ end
305
+
306
+ end
307
+ end
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/ruby
2
+
3
+ base_dir = File.join(File.dirname(__FILE__), "..")
4
+ glob_pattern = File.join("**", "*")
5
+ exclude_patterns = [
6
+ /^pkg/,/^doc/,
7
+ ]
8
+
9
+ Dir.chdir(base_dir)
10
+ files = Dir.glob(glob_pattern).delete_if do |fname|
11
+ File.directory?(fname) or
12
+ exclude_patterns.find do |pattern|
13
+ pattern =~ fname
14
+ end
15
+ end
16
+ manifest = File.new("Manifest.txt", "w")
17
+ manifest.puts files.sort.join("\n")
18
+ manifest.close
19
+
20
+ puts "Manifest.txt updated"
21
+
data/test/runner.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+
3
+ Test::Unit::AutoRunner.run(true, './', ['--exclude=test_nera_dialog'])
@@ -0,0 +1,52 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/nera'
4
+
5
+ class Ising < NERA::Simulator
6
+ Parameters = [ [:L, Integer, 32],
7
+ [:K, Float, 0.223],
8
+ [:tmax, Integer, 512]
9
+ ]
10
+
11
+ def simulate_command( seed)
12
+ return "echo Ising!"
13
+ end
14
+
15
+ public
16
+ def analyze_test1
17
+ FileUtils.touch("test1")
18
+ end
19
+
20
+ def analyze_test2
21
+ FileUtils.touch("test2")
22
+ end
23
+
24
+ end
25
+
26
+ class Heisenberg < NERA::Simulator
27
+ Parameters = [ [:lx, Integer, 255],
28
+ [:ly, Integer, 255],
29
+ [:lz, Integer, 256],
30
+ [:k, Float, 0.223],
31
+ [:mag_field, Float, 0.85],
32
+ [:tmax, Integer, 512]
33
+ ]
34
+
35
+ def simulate_command( seed)
36
+ return "echo Heisenberg!"
37
+ end
38
+
39
+ end
40
+
41
+ class XY < NERA::Simulator
42
+ Parameters = [ [:L,Integer,32], [:K,Float,0.22], [:in_trashbox? ,Integer,128] ]
43
+ end
44
+
45
+
46
+ class Temp
47
+ Parameters = [ [:hoge, Float, 0.5]]
48
+
49
+ def self.parameter_array
50
+ return [:L,:tmax]
51
+ end
52
+ end