Class: NumericOperator

Inherits:
Object
  • Object
show all
Includes:
Cauldron::Operator
Defined in:
lib/cauldron/operator/numeric_operator.rb

Constant Summary collapse

ADDITION =

Maybe NumericOperation

4

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Cauldron::Operator

included

Constructor Details

#initialize(indexes) ⇒ NumericOperator

Returns a new instance of NumericOperator.



8
9
10
# File 'lib/cauldron/operator/numeric_operator.rb', line 8

def initialize(indexes)
  @indexes = indexes
end

Class Method Details

.find_constants(problems) ⇒ Object



73
74
75
# File 'lib/cauldron/operator/numeric_operator.rb', line 73

def self.find_constants(problems)
  problems.collect {|x| x.response - x.arguments.first }.uniq
end

.uses_block?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/cauldron/operator/numeric_operator.rb', line 81

def self.uses_block?
  false
end

.uses_constants?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/cauldron/operator/numeric_operator.rb', line 77

def self.uses_constants?
  true
end

.viable?(arguments, output) ⇒ Boolean

Is the problem suitable for a numeric operatio? e.g. can the .find_contants call be called without error

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cauldron/operator/numeric_operator.rb', line 14

def self.viable?(arguments,output)

  # 1. Only has one argument value
  # 2. Argument is a numeric value
  # 3. Response is numeric

  # TODO  Need to save these viablility tests in shared (easily comparable) state.
  #       e.g. so all viable operations can be found in one go. 

  return false unless arguments.all? { |x| x.kind_of?(Numeric) }
  return false unless output.kind_of?(Numeric)
  true

end

Instance Method Details

#branch?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/cauldron/operator/numeric_operator.rb', line 85

def branch?
  false
end

#build(operators, scope) ⇒ Object



60
61
62
# File 'lib/cauldron/operator/numeric_operator.rb', line 60

def build(operators, scope)
  to_sexp(scope)    
end

#context_realizable?(context) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/cauldron/operator/numeric_operator.rb', line 89

def context_realizable?(context)
  vars = context.keys.select {|x| x.match(/var\d/) }
  var_names = vars.collect(&:to_s)
  
  first_variable = 'var'+@indexes[0].to_s

  a = %Q{
  def function(#{first_variable})
    #{Sorcerer.source(to_sexp(Cauldron::Scope.new(var_names), []), indent: true)}
  end
  }       
  
  o = Object.new
  o.instance_eval(a)

  begin
    #o.function(*vars.collect {|x| context[x] })  
    o.function context[first_variable.to_sym]
  rescue NoMethodError => e
    return false
  rescue StandardError => e
    puts e
  end
  return true    
end

#realizable?(histories) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/cauldron/operator/numeric_operator.rb', line 29

def realizable?(histories)
  parameters = histories.variable_permutations(@indexes.length)
  parameters.each do |params|
    begin
      realize(params)
    rescue TypeError
      return false
    end
  end
  true
end

#realize(params) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/cauldron/operator/numeric_operator.rb', line 41

def realize(params)
  o = Object.new
  a = %Q{
    def function(var0)
      #{Sorcerer.source(to_sexp(Cauldron::Scope.new(['var0']),[]), indent: true)}
    end
  }
  o.instance_eval(a)
  o.function(*params.values)
end

#successful?(problem) ⇒ Boolean

Operator for “x + n” e.g. x + 1 Does the input match the answer

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/cauldron/operator/numeric_operator.rb', line 66

def successful?(problem)
  if (problem[:arguments].first + ADDITION) == problem[:response]
    return true
  end
  return false
end

#to_ruby(scope, operators) ⇒ Object



56
57
58
# File 'lib/cauldron/operator/numeric_operator.rb', line 56

def to_ruby(scope, operators)
  Sorcerer.source self.to_sexp(scope, operators)
end

#to_sexp(scope, operators) ⇒ Object



52
53
54
# File 'lib/cauldron/operator/numeric_operator.rb', line 52

def to_sexp(scope, operators)
  [:binary, [:@ident, scope[@indexes[0]] ] , :+, [:@int, ADDITION.to_s]]
end