class ArrayReverseOperator

Public Class Methods

context_instances(contexts) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 150
def self.context_instances(contexts)
  results = []
  contexts.each do |context|
    results << context.keys.collect(&:to_s).select {|x| x.match(/var\d/) }
  end
  results = results.flatten.uniq
  variable_numbers = results.collect { |x| x.match(/var(\d+)/)[1] }
  variable_numbers.collect { |x| init([x.to_i])}
end
extend_actualized_composite(x, container, examples, point) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 114
def self.extend_actualized_composite(x, container, examples, point)
  cloned_container = container.clone_solution
  cloned_container.add_statement_at(x, point)
  cloned_container
  Cauldron::ActualizedComposite.new(cloned_container, examples)
end
find_constants(problems) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 36
def self.find_constants(problems)
  []
end
init(indexes) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 70
def self.init(indexes)
  self.new(indexes)
end
instances(histories, composite, examples, insert_points) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 74
def self.instances(histories, composite, examples, insert_points)

  # TEMP
  unless examples.class == Cauldron::ExampleSet
    raise StandardError.new('Examples should be an example')
  end

  # Print out each insertable statements
  scope = examples.scope

  # self.init([0]).to_ruby(scope)
  # - this will print out "var0.chop"

  # Get the variables available at each point
  results = []

  insert_points.each do |point|

    # Find the variables at a particular point
    # TODO Change to test
    contexts = histories.contexts_at(point)

    composites = context_instances(contexts)

    # scopes = scopes_at_point(point)

    composites.each do |x|
      if contexts.all? do |context|
        x.context_realizable?(context)
      end
        
      results << extend_actualized_composite(x, composite, examples, point)
    end
  end

  end
  
  results
end
new(indexes) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 3
def initialize(indexes)
  raise StandardError.new('Need at least one item') if indexes.empty?
  @indexes = indexes
end
process(arguments) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 44
def self.process(arguments)
  arguments.collect {|x| x.reverse }
end
uses_block?() click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 40
def self.uses_block?
  false
end
uses_constants?() click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 32
def self.uses_constants?
  false
end
viable?(arguments,output) click to toggle source
  1. Only has one argument value

  2. Argument is an array value

  3. Response is an array

# File lib/cauldron/operator/array_reverse_operator.rb, line 25
def self.viable?(arguments,output)
  return false unless arguments.length == 1
  return false unless arguments.all? { |x| x.kind_of?(Array) }
  return false unless output.kind_of?(Array)
  true
end

Public Instance Methods

branch?() click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 66
def branch?
  false
end
build(operators, scope) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 52
def build(operators, scope)
  to_sexp(operators, scope)
end
clone_statement() click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 121
def clone_statement
  self.class.init(@indexes.clone)
end
context_realizable?(context) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 125
def context_realizable?(context)
  
  vars = context.keys.select {|x| x.match(/var\d/) }
  var_names = vars.collect(&:to_s)

  a = %Q{
  def function(var0)
    #{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] })  
  rescue NoMethodError => e
    return false
  rescue StandardError => e
    puts e
  end
  return true
  
end
successful?(problem) click to toggle source

Matching in

# File lib/cauldron/operator/array_reverse_operator.rb, line 9
def successful?(problem)
  # NOTE - for the future - like the idea of not actually calling the method
  # input.length.each do |i|
  # does input[0] == output[input.length-0]
  # does input[1] == output[input.length-1]
  # does input[3] == output[input.length-3]
  # end
  
  # in this case x.reverse will work
  return true if problem[:arguments].first.reverse == problem[:response]
  false
end
to_ruby(operators, scope) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 48
def to_ruby(operators, scope)
  Sorcerer.source build(operators, scope)
end
to_sexp(scope, operators) click to toggle source
# File lib/cauldron/operator/array_reverse_operator.rb, line 56
def to_sexp(scope, operators)
  [:call, 
    [:vcall, 
      [:@ident, scope[@indexes[0]] ]
    ], 
    :".", 
    [:@ident, "reverse"]
  ]    
end