Class: Jeka::Algorithm

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

Constant Summary collapse

@@information =
Hash.new

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_information(info) ⇒ Object



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

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

.add_tests(test_dir) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jeka/algorithm.rb', line 30

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



101
102
103
104
# File 'lib/jeka/algorithm.rb', line 101

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

.build_all(&block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/jeka/algorithm.rb', line 122

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



96
97
98
99
# File 'lib/jeka/algorithm.rb', line 96

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

.resetObject



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

def self.reset
  @@algorithms = {}
end

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/jeka/algorithm.rb', line 134

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/jeka/algorithm.rb', line 106

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



52
53
54
55
56
57
58
# File 'lib/jeka/algorithm.rb', line 52

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



43
44
45
46
47
48
49
50
# File 'lib/jeka/algorithm.rb', line 43

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

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jeka/algorithm.rb', line 60

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