Class: ArrayReverseOperator

Inherits:
Object
  • Object
show all
Defined in:
lib/cauldron/operator/array_reverse_operator.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indexes) ⇒ ArrayReverseOperator

Returns a new instance of ArrayReverseOperator.

Raises:

  • (StandardError)


3
4
5
6
# 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

Class Method Details

.context_instances(contexts) ⇒ Object



150
151
152
153
154
155
156
157
158
# 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) ⇒ Object



114
115
116
117
118
119
# 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) ⇒ Object



36
37
38
# File 'lib/cauldron/operator/array_reverse_operator.rb', line 36

def self.find_constants(problems)
  []
end

.init(indexes) ⇒ Object



70
71
72
# File 'lib/cauldron/operator/array_reverse_operator.rb', line 70

def self.init(indexes)
  self.new(indexes)
end

.instances(histories, composite, examples, insert_points) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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

.process(arguments) ⇒ Object



44
45
46
# File 'lib/cauldron/operator/array_reverse_operator.rb', line 44

def self.process(arguments)
  arguments.collect {|x| x.reverse }
end

.uses_block?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/cauldron/operator/array_reverse_operator.rb', line 40

def self.uses_block?
  false
end

.uses_constants?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/cauldron/operator/array_reverse_operator.rb', line 32

def self.uses_constants?
  false
end

.viable?(arguments, output) ⇒ Boolean

  1. Only has one argument value

  2. Argument is an array value

  3. Response is an array

Returns:

  • (Boolean)


25
26
27
28
29
30
# 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

Instance Method Details

#branch?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/cauldron/operator/array_reverse_operator.rb', line 66

def branch?
  false
end

#build(operators, scope) ⇒ Object



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

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

#clone_statementObject



121
122
123
# File 'lib/cauldron/operator/array_reverse_operator.rb', line 121

def clone_statement
  self.class.init(@indexes.clone)
end

#context_realizable?(context) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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) ⇒ Boolean

Matching in

Returns:

  • (Boolean)


9
10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



48
49
50
# 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) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/cauldron/operator/array_reverse_operator.rb', line 56

def to_sexp(scope, operators)
  [:call, 
    [:vcall, 
      [:@ident, scope[@indexes[0]] ]
    ], 
    :".", 
    [:@ident, "reverse"]
  ]    
end