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



90
91
92
93
# File 'lib/jeka/algorithm.rb', line 90

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

.build_all(&block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/jeka/algorithm.rb', line 111

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



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

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

.resetObject



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

def self.reset
  @@algorithms = {}
end

.run_all(n = 1, output = "analysis.jeka", &block) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/jeka/algorithm.rb', line 123

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

.test_all(&block) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/jeka/algorithm.rb', line 95

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

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, check = false, &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
74
75
76
77
78
79
# File 'lib/jeka/algorithm.rb', line 49

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