require 'yaml' module Jeka class Algorithm attr_reader :database def implementations methods.select {|m| m =~ /^implementation_/}.map {|m| send(m)} end def self.implementation(name) imp = Implementation.new(name) yield imp define_method("implementation_#{name}".to_sym) do return imp end end @@information = Hash.new def self.add_information(info) if info.kind_of? Hash @@information = info else yaml = YAML::load(File.open(info)) yaml.each_key {|key| @@information[key.to_sym] = yaml[key]} end end def self.add_tests(test_dir) Jeka::TestCase.reset Dir.glob(test_dir).each do |d| eval(File.open(d).readlines.join) end test_suites = Jeka::TestCase.test_suites define_method(:test_suites) do return test_suites end end def jekafy @database = Jeka::Analysis::Algorithm.create!( name: self.class.to_s, implementations: implementations.collect {|imp| imp.jekafy}, test_cases: test_suites.collect {|tc| tc.jekafy}, algorithm_information: Jeka::Analysis::AlgorithmInformation.convert(@@information) ) end def build(&block) implementations.each do |imp| b = imp.compiler.build block.call(nil, :output, b[0]) if block_given? block.call(nil, :error, b[1]) if block_given? and (not b[2] == 0) end end def run(n=1, check=false, &block) implementations.each do |imp| test_suites.each do |ts| ts.tests.each do |test| n.times do test_result = imp.compiler.run("#{test.input}\n") block.call(nil, :output, test_result[0]) if block_given? block.call(nil, :error, test_result[1]) if block_given? and (not test_result[2] == 0) if block_given? and check txt = "#{imp.name} -> #{ts.class}::#{test.name}" unless test_result[0].join == test.output block.call(0, :test, "#{txt}...failures") end end Jeka::Analysis::Result.create!( stdout: test_result[0].join, stderr: test_result[1].join, exit_status: test_result[2], user_cpu_time: test_result[3][:user_cpu_time], system_cpu_time: test_result[3][:system_cpu_time], childrens_use_cpu_time: test_result[3][:childrens_use_cpu_time], childrens_system_cpu_time: test_result[3][:childrens_system_cpu_time], elapsed_real_time: test_result[3][:elapsed_real_time], implementation: imp.database, test: test.database ) end end end end end def self.reset @@algorithms = {} end def self.inherited(klass) @@algorithms ||= {} @@algorithms[klass] = true end def self.algorithms @@algorithms ||= {} @@algorithms.keys.sort_by { |ts| ts.name }.collect{|ts| ts.new} end def self.test_all(&block) block.call(0, :step, "Testing algorithms...") if block_given? s = self.algorithms.length + 1 i = 0 self.algorithms.each do |alg| i += 1 block.call(100*i/s, :step, "Testing #{alg.class.to_s}...") if block_given? if block_given? alg.run(1, true, &block) else alg.run(1, true) end end block.call(100, :done, "Done") if block_given? end def self.build_all(&block) block.call(0, :step, "Building algorithms...") if block_given? n = self.algorithms.length + 1 i = 0 self.algorithms.each do |alg| i += 1 block.call(100*i/n, :step, "Building #{alg.class.to_s}") if block_given? block_given? ? alg.build(&block) : alg.build end block.call(100, :done, "Done") if block_given? end def self.run_all(n=1, output="analysis.jeka", &block) Jeka::Analysis::Database.create(File.absolute_path(output)) block.call(0, :step, "Running algorithms...") if block_given? s = self.algorithms.length + 1 i = 0 self.algorithms.each do |alg| alg.jekafy i += 1 block.call(100*i/s, :step, "Running #{alg.class.to_s}...") if block_given? if block_given? alg.run(n, &block) else alg.run(n) end end block.call(100, :done, "Done") if block_given? end end end