Class: Cauldron::Solution::Composite
- Inherits:
-
Object
- Object
- Cauldron::Solution::Composite
- Defined in:
- lib/cauldron/solution/composite.rb
Instance Attribute Summary collapse
-
#operators ⇒ Object
readonly
Returns the value of attribute operators.
Instance Method Summary collapse
- #add_first_statement(statement) ⇒ Object
- #add_statement(statement, inner) ⇒ Object
- #add_statement_at(statement, point) ⇒ Object
- #clone_solution ⇒ Object
- #end_points ⇒ Object
-
#initialize(children) ⇒ Composite
constructor
A new instance of Composite.
- #insert_tracking(params) ⇒ Object
- #record(example) ⇒ Object
- #reset_and_track(caret) ⇒ Object
- #solution?(problems) ⇒ Boolean
-
#successful?(problem) ⇒ Boolean
TODO Drop this method.
- #to_programme(value) ⇒ Object
- #to_ruby(scope) ⇒ Object
- #to_sexp(scope = Cauldron::Scope.new) ⇒ Object
Constructor Details
#initialize(children) ⇒ Composite
Returns a new instance of Composite.
7 8 9 10 |
# File 'lib/cauldron/solution/composite.rb', line 7 def initialize(children) raise StandardError.new('inital value should be an array') unless children.kind_of?(Array) @operators = children end |
Instance Attribute Details
#operators ⇒ Object (readonly)
Returns the value of attribute operators.
5 6 7 |
# File 'lib/cauldron/solution/composite.rb', line 5 def operators @operators end |
Instance Method Details
#add_first_statement(statement) ⇒ Object
168 169 170 |
# File 'lib/cauldron/solution/composite.rb', line 168 def add_first_statement(statement) [:stmts_add, [:stmts_new], statement] end |
#add_statement(statement, inner) ⇒ Object
172 173 174 |
# File 'lib/cauldron/solution/composite.rb', line 172 def add_statement(statement, inner) [:stmts_add, inner, statement] end |
#add_statement_at(statement, point) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cauldron/solution/composite.rb', line 35 def add_statement_at(statement, point) if point.length == 2 container = self.operators[0] #return self if container.length > 1 # TODO: Quick hack to get it working container << Tree::TreeNode.new('SASA', statement) elsif point.length == 1 operators << Tree::TreeNode.new('SASA', statement) else raise StandardError.new('Have not written code: '+point.inspect) end self end |
#clone_solution ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/cauldron/solution/composite.rb', line 27 def clone_solution #self.clone tree_operators = operators.collect do |node| Tree::TreeNode.new('x', node.content.clone_statement) end Composite.new(tree_operators) end |
#end_points ⇒ Object
17 18 19 20 21 22 23 24 25 |
# File 'lib/cauldron/solution/composite.rb', line 17 def end_points results = [] operators.each do |x| if x.content.branch? results << [0,x.children.length] end end results << [operators.length] end |
#insert_tracking(params) ⇒ Object
48 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 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/cauldron/solution/composite.rb', line 48 def insert_tracking(params) scope = Cauldron::Scope.new(params.clone) # TODO Might be useful # trace = TracePoint.new(:call) do |tp| # p [tp.lineno, tp.event, tp.raised_exception] # end # NEW: Implementation m = %Q{ def function(#{params.join(',')}) #{to_ruby(Cauldron::Scope.new(params.clone))} end } sexp = Ripper::SexpBuilder.new(m).parse rendered_code = Sorcerer.source(sexp, indent: true) caret = Cauldron::Caret.new rendered_code = Sorcerer.source(sexp, indent: true).gsub(/end/,"\nend").split("\n").reject(&:empty?).join("\n") # Generate tracking code with pending substitutions tracked_code = [] rendered_code.each_line do |line| #if line.match /end\s+/ if line.match /end/ tracked_code << Sorcerer.source(Ripper::SexpBuilder.new(Cauldron::Tracer.substitue_tracking).parse) #Sorcerer.source(Cauldron::Tracer.substitue_tracking) end tracked_code << line end sexp = Ripper::SexpBuilder.new(tracked_code.join("\n")).parse code_tracking = Sorcerer.source(sexp, indent: true) code_tracking.split("\n") current_line = -1 total_lines = 0 new_tracked_code = [] last_line = nil relative_line = 0 placeholder = nil point = [0,0] current_depth = 0 caret = Cauldron::Caret.new points = end_points code_tracking.split("\n").each do |line| if line.match /record/ depth = (line.match(/^(\s+)/)[0].length / 2) -1 if depth > current_depth relative_line = 0 end current_depth = depth new_tracked_code << last_line new_tracked_code << Sorcerer.source( Cauldron::Tracer.tracking(relative_line, depth, total_lines, points.shift) ) new_tracked_code << placeholder else total_lines += 1 unless line['='] placeholder = "#{'placeholder_'+rand(10000000000).to_s}" last_line = "#{placeholder} = "+line end if last_line if !last_line.match(/\s+end/).nil? || !last_line.match(/function/).nil? # || last_line.match /function/ last_line = nil placeholder = nil end end if line.match /end$/ unless line.strip == 'end' line = line.gsub(/end$/,'') end end new_tracked_code << line current_line += 1 end #total_lines += 1 end # NOTE: Keep this to debug before conversion of S-EXP sexp = Ripper::SexpBuilder.new(new_tracked_code.join("\n")).parse Cauldron::Tracer.new(sexp) end |
#record(example) ⇒ Object
12 13 14 15 |
# File 'lib/cauldron/solution/composite.rb', line 12 def record(example) # TODO params passed twice - and example not used at all insert_tracking(example.params).process(example) end |
#reset_and_track(caret) ⇒ Object
143 144 145 146 |
# File 'lib/cauldron/solution/composite.rb', line 143 def reset_and_track(caret) caret.return_depth(0) Cauldron::Tracer.tracking(caret.line, caret.current_depth, caret.total_lines) end |
#solution?(problems) ⇒ Boolean
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/cauldron/solution/composite.rb', line 176 def solution?(problems) o = Object.new m = %Q{ def function(#{problems.variables.join(',')}) #{to_ruby(problems.scope)} end } o.instance_eval(m) #o.function *problems.examples.first.arguments problems.all? do |example| o.function(*example.arguments) == example.response end # TODO: Remove this resque - it is just a temp rescue NoMethodError => e return false rescue NameError => e return false rescue TypeError => e return false end |
#successful?(problem) ⇒ Boolean
TODO Drop this method
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/cauldron/solution/composite.rb', line 200 def successful?(problem) # # TODO track the parameters of the operator # operators.trace(problem) # # TODO For now just evalute the code # return true if problem[:arguments].first == problem[:response] # false pt = PryTester.new args = problem.arguments variables = problem.params #(0...args.length).collect {|x| 'var'+x.to_s} a = [ 'def function('+variables.join(',')+');'+self.to_ruby(variables)+"; end", 'function('+problem.arguments.collect {|x| to_programme(x) }.join(',')+')' ] res = pt.eval( ['def function('+variables.join(',')+');'+self.to_ruby(variables)+"; end", 'function('+problem.arguments.collect {|x| to_programme(x) }.join(',')+')'] ) problem.response == res end |
#to_programme(value) ⇒ Object
225 226 227 228 229 230 |
# File 'lib/cauldron/solution/composite.rb', line 225 def to_programme(value) if value.kind_of?(String) return %Q{'#{value}'} end value.to_s end |
#to_ruby(scope) ⇒ Object
163 164 165 166 |
# File 'lib/cauldron/solution/composite.rb', line 163 def to_ruby(scope) return '' if operators.empty? Sorcerer.source(to_sexp(scope)) end |
#to_sexp(scope = Cauldron::Scope.new) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/cauldron/solution/composite.rb', line 148 def to_sexp(scope=Cauldron::Scope.new) res = operators.collect do |operator| #begin operator.content.to_ruby(scope, operator.children) # rescue NoMethodError => e # binding.pry # end end.join("\n") sexp = Ripper::SexpBuilder.new(res).parse return sexp end |