Class: Jeka::Algorithm

Inherits:
Object
  • Object
show all
Defined in:
lib/jeka/algorithm.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



6
7
8
# File 'lib/jeka/algorithm.rb', line 6

def database
  @database
end

Class Method Details

.add_tests(test_dir) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jeka/algorithm.rb', line 20

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

.algorithmsObject



84
85
86
87
# File 'lib/jeka/algorithm.rb', line 84

def self.algorithms
  @@algorithms ||= {}
  @@algorithms.keys.sort_by { |ts| ts.name }.collect{|ts| ts.new}
end

.build_all(&block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jeka/algorithm.rb', line 89

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

.implementation(name) {|imp| ... } ⇒ Object

Yields:

  • (imp)


12
13
14
15
16
17
18
# File 'lib/jeka/algorithm.rb', line 12

def self.implementation(name)
  imp = Implementation.new(name)
  yield imp
  define_method("implementation_#{name}".to_sym) do
    return imp
  end
end

.inherited(klass) ⇒ Object



79
80
81
82
# File 'lib/jeka/algorithm.rb', line 79

def self.inherited(klass)
  @@algorithms ||= {}
  @@algorithms[klass] = true
end

.resetObject



75
76
77
# File 'lib/jeka/algorithm.rb', line 75

def self.reset
  @@algorithms = {}
end

.run_all(n = 1, &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jeka/algorithm.rb', line 101

def self.run_all(n=1, &block)
  Jeka::Analysis::Database.create(File.join(Dir.getwd, "analysis.jeka"))
  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

Instance Method Details

#build(&block) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/jeka/algorithm.rb', line 41

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

#implementationsObject



8
9
10
# File 'lib/jeka/algorithm.rb', line 8

def implementations
  methods.select {|m| m =~ /^implementation_/}.map {|m| send(m)}
end

#jekafyObject



33
34
35
36
37
38
39
# File 'lib/jeka/algorithm.rb', line 33

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}
  )
end

#run(n = 1, &block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jeka/algorithm.rb', line 49

def run(n=1, &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)
          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