diff --git a/Hype.rb b/Hype.rb index 58ec9bd..a1695a8 100644 --- a/Hype.rb +++ b/Hype.rb @@ -56,7 +56,7 @@ def subtasks_to_s(tasks, indent, ordered = true) else operators = @parser.operators output = "#{indent}#{'un' unless ordered}ordered" - tasks.each {|t| output << indent << (operators.assoc(t.first) ? 'operator' : 'method ') << " (#{t.join(' ')})"} + tasks.each {|t| output << indent << (operators.assoc(t[0]) ? 'operator' : 'method ') << " (#{t.join(' ')})"} output end end @@ -69,7 +69,7 @@ def operators_to_s output = '' indent = "\n " @parser.operators.each {|op| - output << "\n #{op.first}(#{op[1].join(' ')})" + output << "\n #{op[0]}(#{op[1].join(' ')})" output << "\n Precond positive:#{predicates_to_s(op[2], indent)}" unless op[2].empty? output << "\n Precond negative:#{predicates_to_s(op[3], indent)}" unless op[3].empty? output << "\n Effect positive:#{predicates_to_s(op[4], indent)}" unless op[4].empty? @@ -89,7 +89,7 @@ def methods_to_s @parser.methods.each {|name,param,*decompositions| output << "\n #{name}(#{param.join(' ')})" decompositions.each {|dec| - output << "\n Label: #{dec.first}" + output << "\n Label: #{dec[0]}" output << "\n Free variables:\n #{dec[1].join(indent)}" unless dec[1].empty? output << "\n Precond positive:#{predicates_to_s(dec[2], indent)}" unless dec[2].empty? output << "\n Precond negative:#{predicates_to_s(dec[3], indent)}" unless dec[3].empty? @@ -111,7 +111,7 @@ def to_s Problem #{@parser.problem_name} State:#{predicates_to_s(@parser.state.flat_map {|k,v| [k].product(v)}, "\n ")}\n Goal: - Tasks:#{subtasks_to_s(@parser.tasks.drop(1), "\n ", @parser.tasks.first)} + Tasks:#{subtasks_to_s(@parser.tasks.drop(1), "\n ", @parser.tasks[0])} Positive:#{@parser.goal_pos.empty? ? "\n empty" : predicates_to_s(@parser.goal_pos, "\n ")} Negative:#{@parser.goal_not.empty? ? "\n empty" : predicates_to_s(@parser.goal_not, "\n ")}" end @@ -217,7 +217,7 @@ def execute #----------------------------------------------- if $0 == __FILE__ begin - if ARGV.size < 2 or ARGV.first == '-h' + if ARGV.size < 2 or ARGV[0] == '-h' puts Hype::HELP elsif not File.exist?(domain = ARGV.shift) abort("Domain not found: #{domain}") diff --git a/Hypertension.rb b/Hypertension.rb index 9c932cf..a89fc75 100644 --- a/Hypertension.rb +++ b/Hypertension.rb @@ -19,10 +19,10 @@ module Hypertension def planning(tasks, level = 0) return tasks if tasks.empty? - case decomposition = @domain[(current_task = tasks.shift).first] + case decomposition = @domain[(current_task = tasks.shift)[0]] # Operator (true: visible, false: invisible) when true, false - puts "#{' ' * level}#{current_task.first}(#{current_task.drop(1).join(' ')})" if @debug + puts "#{' ' * level}#{current_task[0]}(#{current_task.drop(1).join(' ')})" if @debug old_state = @state begin # Keep decomposing the hierarchy if operator applied @@ -48,7 +48,7 @@ def planning(tasks, level = 0) end current_task.unshift(task_name) # Error - else raise "Domain defines no decomposition for #{current_task.first}" + else raise "Domain defines no decomposition for #{current_task[0]}" end nil end @@ -58,10 +58,10 @@ def planning(tasks, level = 0) def planning(tasks, level = 0) return tasks if tasks.empty? index, current_task = tasks.shift - case decomposition = @domain[current_task.first] + case decomposition = @domain[current_task[0]] # Operator (true: visible, false: invisible) when true, false - puts "#{' ' * level}#{current_task.first}(#{current_task.drop(1).join(' ')})" if @debug + puts "#{' ' * level}#{current_task[0]}(#{current_task.drop(1).join(' ')})" if @debug old_state = @state begin # Keep decomposing the hierarchy if operator applied @@ -83,7 +83,7 @@ def planning(tasks, level = 0) puts "#{' ' * level.pred}#{method}(#{current_task.join(' ')})" if @debug # Every unification is tested __send__(method, *current_task) {|subtasks| - subtasks.map! {|t| [(@index += 1 if @domain[t.first]), t]} + subtasks.each {|t| [(@index += 1 if @domain[t[0]]), t]} new_index = @index if plan = planning(subtasks.concat(tasks), level) @decomposition.unshift("#{index} #{task_name} #{current_task.join(' ')} -> #{method[task_name.size+1..-1]} #{(old_index+1..new_index).to_a.join(' ')}") @@ -98,7 +98,7 @@ def planning(tasks, level = 0) end current_task.unshift(task_name) # Error - else raise "Domain defines no decomposition for #{current_task.first}" + else raise "Domain defines no decomposition for #{current_task[0]}" end nil end @@ -154,7 +154,7 @@ def generate(precond_pos, precond_not, *free) # Free variable if t.instance_of?(Array) # Not unified - if t.first.empty? + if t[0].empty? match_objects.push(t, o) # No match with previous unification elsif not t.include?(o) @@ -171,7 +171,7 @@ def generate(precond_pos, precond_not, *free) match_objects.shift << match_objects.shift until match_objects.empty? } # Unification closed - terms.each {|i| i.first << 0 if i.instance_of?(Array) and i.first.empty?} + terms.each {|i| i[0] << 0 if i.instance_of?(Array) and i[0].empty?} } # Remove pointer and duplicates objects.each {|i| @@ -224,7 +224,7 @@ def problem(state, tasks, debug = false, ordered = true) print_data(tasks) puts 'Planning'.center(50,'-') t = Time.now.to_f - plan = ordered ? planning(tasks) : task_permutations(state, tasks, (tasks.pop if tasks[-1]&.first == :invisible_goal)) + plan = ordered ? planning(tasks) : task_permutations(state, tasks, (tasks.pop if tasks[-1]&.[](0) == :invisible_goal)) puts "Time: #{Time.now.to_f - t}s", 'Plan'.center(50,'-') if plan if plan.empty? then puts 'Empty plan' diff --git a/compilers/Cyber_Compiler.rb b/compilers/Cyber_Compiler.rb index b1be9cf..2e7cebc 100644 --- a/compilers/Cyber_Compiler.rb +++ b/compilers/Cyber_Compiler.rb @@ -52,8 +52,8 @@ def apply(modifier, effects, define_operators, duplicated, arity) def tasks_to_hyper(output, tasks, indentation, next_task = 'task->next') tasks.each_with_index {|s,i| - output << "#{indentation}subtask#{i}->value = #{term(s.first)};" - output << "#{indentation}tindex(subtask#{i});" unless s.first.start_with?('invisible_') + output << "#{indentation}subtask#{i}->value = #{term(s[0])};" + output << "#{indentation}tindex(subtask#{i});" unless s[0].start_with?('invisible_') output << "#{indentation}subtask#{i}->parameters[0] = #{s.size - 1};" 1.upto(s.size - 1) {|j| output << "#{indentation}subtask#{i}->parameters[#{j}] = #{term(s[j])};"} output << "#{indentation}subtask#{i}->next = #{i != tasks.size - 1 ? "subtask#{i + 1}" : next_task};" @@ -147,7 +147,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st visit_param = nil unless dec[4].empty? dec[4].each {|s| - if s.size > 1 and s.first.start_with?('invisible_visit_') + if s.size > 1 and s[0].start_with?('invisible_visit_') if ((visit_param = s.drop(1)) & f).empty? define_methods << "\n if(applicable_const(visit#{visit_param.size}, #{terms_to_hyper(visit_param)})) return false;" visit_param = nil @@ -202,7 +202,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st # close_method_str.prepend('}') and no indentation change for compact output close_method_str.prepend("#{indentation}}") indentation << ' ' - if terms2.size == 1 then define_methods << "#{indentation}VALUE #{terms2.first} = *it#{counter};" + if terms2.size == 1 then define_methods << "#{indentation}VALUE #{terms2[0]} = *it#{counter};" else terms2.each_with_index {|term,i| define_methods << "#{indentation}VALUE #{term} = std::get<#{i}>(*it#{counter});"} end elsif pre == '=' then equality << "#{terms2[0]} != #{terms2[1]}" @@ -289,9 +289,9 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st end comparison << pre elsif k - if k.first.empty? then define_state_const << "\nstatic VALUE0 #{pre}_ = true;" + if k[0].empty? then define_state_const << "\nstatic VALUE0 #{pre}_ = true;" else - define_state_const << "\nstatic VALUE#{arity[pre] ||= k.first.size} #{pre == '=' ? 'equal' : pre}_\n{\n #{k.map {|terms| terms_to_hyper(terms)}.join(",\n ")}\n};" + define_state_const << "\nstatic VALUE#{arity[pre] ||= k[0].size} #{pre == '=' ? 'equal' : pre}_\n{\n #{k.map {|terms| terms_to_hyper(terms)}.join(",\n ")}\n};" tokens.concat(k.flatten(1)) end end diff --git a/compilers/Dot_Compiler.rb b/compilers/Dot_Compiler.rb index 395cd4c..3132f1a 100644 --- a/compilers/Dot_Compiler.rb +++ b/compilers/Dot_Compiler.rb @@ -19,7 +19,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st # Operators operators.each {|op| # Header - domain_str << " \"#{op.first}\" [\n shape=record\n label=\"{{#{op.first}|#{op[1].join(' ')}}|{" + domain_str << " \"#{op[0]}\" [\n shape=record\n label=\"{{#{op[0]}|#{op[1].join(' ')}}|{" # Preconditions predicates_to_dot(domain_str, op[2], op[3]) # Effects @@ -32,21 +32,21 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st method_str = '' decompositions = [] met.drop(2).each_with_index {|dec,i| - decompositions << "#{met.first}_#{dec.first}" + decompositions << "#{met[0]}_#{dec[0]}" # Label - method_str << " \"label_#{met.first}_#{dec.first}\" [\n shape=Mrecord\n label=\"{{#{met.first}_#{dec.first}|#{dec[1].join(' ')}}|" + method_str << " \"label_#{met[0]}_#{dec[0]}\" [\n shape=Mrecord\n label=\"{{#{met[0]}_#{dec[0]}|#{dec[1].join(' ')}}|" # Preconditions predicates_to_dot(method_str, dec[2], dec[3]) # Subtasks connections = '' dec[4].each_with_index {|subtask,j| method_str << "|#{subtask.join(' ')}" - connections << " \"label_#{met.first}_#{dec.first}\":n#{j} -> \"#{subtask.first}\"\n" if all_connections or operators.assoc(subtask.first) + connections << " \"label_#{met[0]}_#{dec[0]}\":n#{j} -> \"#{subtask[0]}\"\n" if all_connections or operators.assoc(subtask[0]) } # Connections - method_str << "}\"\n ]\n \"#{met.first}\":n#{i} -> \"label_#{met.first}_#{dec.first}\" [style=dotted]\n#{connections}" + method_str << "}\"\n ]\n \"#{met[0]}\":n#{i} -> \"label_#{met[0]}_#{dec[0]}\" [style=dotted]\n#{connections}" } - domain_str << " \"#{met.first}\" [\n shape=Mrecord\n style=bold\n label=\"{{#{met.first}|#{met[1].join(' ')}}|{#{decompositions.join('|')}}}\"\n ]\n#{method_str}" + domain_str << " \"#{met[0]}\" [\n shape=Mrecord\n style=bold\n label=\"{{#{met[0]}|#{met[1].join(' ')}}|{#{decompositions.join('|')}}}\"\n ]\n#{method_str}" } domain_str << '}' end diff --git a/compilers/Hyper_Compiler.rb b/compilers/Hyper_Compiler.rb index e5ddad1..9f6ea68 100644 --- a/compilers/Hyper_Compiler.rb +++ b/compilers/Hyper_Compiler.rb @@ -111,7 +111,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st methods.each_with_index {|(name,param,*decompositions),mi| variables = "(#{param.join(', ').tr!('?','_')})" unless param.empty? decompositions.map! {|dec| - define_methods << "\n def #{name}_#{dec.first}#{variables}" + define_methods << "\n def #{name}_#{dec[0]}#{variables}" equality = [] define_methods_comparison = '' f = dec[1] @@ -143,7 +143,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st define_methods << define_methods_comparison visit_param = nil dec[4].each {|s| - if s.size > 1 and s.first.start_with?('invisible_visit_') + if s.size > 1 and s[0].start_with?('invisible_visit_') if ((visit_param = s.drop(1)) & f).empty? define_methods << "\n return if @visit.include?(#{terms_to_hyper(visit_param)})" visit_param = nil @@ -236,7 +236,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st define_methods << define_methods_comparison end define_methods << indentation << (dec[4].empty? ? 'yield []' : "yield [#{indentation} [" << dec[4].map {|g| g.map {|i| term(i)}.join(', ')}.join("],#{indentation} [") << "]#{indentation}]") << close_method_str - "\n :#{name}_#{dec.first}" + "\n :#{name}_#{dec[0]}" } domain_str << "\n :#{name} => [" << decompositions.join(',') << (methods.size.pred == mi ? "\n ]" : "\n ],") } @@ -287,7 +287,7 @@ def compile_problem(domain_name, problem_name, operators, methods, predicates, s } # Tasks ordered = tasks.shift - problem_str << start_str << " ],\n # Tasks\n [" << tasks.map {|g| "\n [:#{g.join(', :')}]"}.join(',') << "\n ],\n # Debug\n ARGV.first == 'debug'#{",\n # Ordered\n false" if ordered == false}\n)" + problem_str << start_str << " ],\n # Tasks\n [" << tasks.map {|g| "\n [:#{g.join(', :')}]"}.join(',') << "\n ],\n # Debug\n ARGV[0] == 'debug'#{",\n # Ordered\n false" if ordered == false}\n)" unless tasks.empty? tasks.unshift(ordered) tasks.pop if tasks[-1][0] == 'invisible_goal' diff --git a/compilers/JSHOP_Compiler.rb b/compilers/JSHOP_Compiler.rb index d8482d9..fd5ef0a 100644 --- a/compilers/JSHOP_Compiler.rb +++ b/compilers/JSHOP_Compiler.rb @@ -28,7 +28,7 @@ def subtasks_to_jshop(output, tasks, operators, indentation, order = true) else output << "#{indentation}(#{':unordered' unless order}\n" tasks.each {|t| - name = t.first + name = t[0] t[0] = "!#{name.sub(/^invisible_/,'!')}" if operators.assoc(name) output << "#{indentation} (#{t.join(' ')})\n" t[0] = name @@ -46,7 +46,7 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st # Operators operators.each {|op| # Header - domain_str << "\n\n (:operator (!#{op.first.sub(/^invisible_/,'!')} #{op[1].join(' ')})\n" + domain_str << "\n\n (:operator (!#{op[0].sub(/^invisible_/,'!')} #{op[1].join(' ')})\n" # Preconditions predicates_to_jshop(domain_str, op[2], op[3]) # Delete effects @@ -64,10 +64,10 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st # Methods domain_str << "\n\n ;#{SPACER}\n ; Methods\n ;#{SPACER}" methods.each {|met| - header = "\n\n (:method (#{met.first} #{met[1].join(' ')})\n " + header = "\n\n (:method (#{met[0]} #{met[1].join(' ')})\n " met.drop(2).each {|dec| # Header and label - domain_str << header << "#{dec.first}\n" + domain_str << header << "#{dec[0]}\n" # Preconditions predicates_to_jshop(domain_str, dec[2], dec[3]) # Subtasks @@ -96,7 +96,7 @@ def compile_problem(domain_name, problem_name, operators, methods, predicates, s end # Tasks problem_str << ")\n\n ;#{SPACER}\n ; Tasks\n ;#{SPACER}\n\n" - subtasks_to_jshop(problem_str, goal_pos.empty? && goal_not.empty? ? tasks.drop(1) : tasks.drop(1) << ['!!goal'], operators, ' ', tasks.first) + subtasks_to_jshop(problem_str, goal_pos.empty? && goal_not.empty? ? tasks.drop(1) : tasks.drop(1) << ['!!goal'], operators, ' ', tasks[0]) problem_str << ')' end end \ No newline at end of file diff --git a/compilers/PDDL_Compiler.rb b/compilers/PDDL_Compiler.rb index 959c75f..6d17183 100644 --- a/compilers/PDDL_Compiler.rb +++ b/compilers/PDDL_Compiler.rb @@ -12,19 +12,19 @@ def compile_domain(domain_name, problem_name, operators, methods, predicates, st # Operators operators.each {|op| # Header - action_str << "\n (:action #{op.first}\n :parameters (#{op[1].join(' ')})\n :precondition (and\n" + action_str << "\n (:action #{op[0]}\n :parameters (#{op[1].join(' ')})\n :precondition (and\n" # Preconditions - op[2].each {|pre| action_str << " (#{pre.join(' ')})\n"; declared[pre.first] ||= pre} - op[3].each {|pre| action_str << " (not (#{pre.join(' ')}))\n"; declared[pre.first] ||= pre} + op[2].each {|pre| action_str << " (#{pre.join(' ')})\n"; declared[pre[0]] ||= pre} + op[3].each {|pre| action_str << " (not (#{pre.join(' ')}))\n"; declared[pre[0]] ||= pre} negative_preconditions = true unless op[3].empty? # Effects action_str << " )\n :effect (and\n" - op[4].each {|pre| action_str << " (#{pre.join(' ')})\n"; declared[pre.first] ||= pre} - op[5].each {|pre| action_str << " (not (#{pre.join(' ')}))\n"; declared[pre.first] ||= pre} + op[4].each {|pre| action_str << " (#{pre.join(' ')})\n"; declared[pre[0]] ||= pre} + op[5].each {|pre| action_str << " (not (#{pre.join(' ')}))\n"; declared[pre[0]] ||= pre} action_str << " )\n )\n" } - goal_pos.each {|pre| declared[pre.first] ||= pre} - goal_not.each {|pre| declared[pre.first] ||= pre} + goal_pos.each {|pre| declared[pre[0]] ||= pre} + goal_not.each {|pre| declared[pre[0]] ||= pre} domain_str = "; Generated by Hype\n(define (domain #{domain_name}) (:requirements :strips#{' :negative-preconditions' if negative_preconditions}#{' :equality' if declared.delete('=')})\n\n (:predicates\n" declared.each_value {|pre| diff --git a/examples/experiments/Debug.rb b/examples/experiments/Debug.rb index 3e25064..a92e1ef 100644 --- a/examples/experiments/Debug.rb +++ b/examples/experiments/Debug.rb @@ -16,10 +16,10 @@ def input end def assert(pre) - if pre.first == 'not' - pre = pre.last - raise "Unexpected (#{pre.join(' ')}) in state" if @state[pre.first].include?(pre.drop(1)) - elsif not @state[pre.first].include?(pre.drop(1)) + if pre[0] == 'not' + pre = pre[1] + raise "Unexpected (#{pre.join(' ')}) in state" if @state[pre[0]].include?(pre.drop(1)) + elsif not @state[pre[0]].include?(pre.drop(1)) raise "Expected (#{pre.join(' ')}) in state" end end diff --git a/examples/experiments/Expression.rb b/examples/experiments/Expression.rb index f0c1f72..bf79407 100644 --- a/examples/experiments/Expression.rb +++ b/examples/experiments/Expression.rb @@ -21,7 +21,7 @@ def evaluate(expression, &block) def call(expression) f = expression.shift - expression.map! {|i| i.instance_of?(Array) && i.first == :call ? (i.shift; call(i)) : i}.inject(f) + expression.map! {|i| i.instance_of?(Array) && i[0] == :call ? (i.shift; call(i)) : i}.inject(f) end #----------------------------------------------- diff --git a/examples/experiments/Stochastic.rb b/examples/experiments/Stochastic.rb index a2aed59..8f77069 100644 --- a/examples/experiments/Stochastic.rb +++ b/examples/experiments/Stochastic.rb @@ -1,7 +1,7 @@ module Stochastic def planning(tasks, level = 0) - if not tasks.empty? and (d = @domain[tasks.first.first]).instance_of?(Array) + if not tasks.empty? and (d = @domain[tasks[0][0]]).instance_of?(Array) d.shuffle! end super diff --git a/examples/goldminer/pb1.rb b/examples/goldminer/pb1.rb index 5600958..f1407b6 100644 --- a/examples/goldminer/pb1.rb +++ b/examples/goldminer/pb1.rb @@ -61,7 +61,7 @@ [:get_gold] ], # Debug - ARGV.first == 'debug' + ARGV[0] == 'debug' ) # Test diff --git a/examples/hanoi/hanoi.rb b/examples/hanoi/hanoi.rb index fcf2b53..73f90d1 100644 --- a/examples/hanoi/hanoi.rb +++ b/examples/hanoi/hanoi.rb @@ -10,4 +10,4 @@ def generate(discs) end end -puts Hanoi.generate(ARGV.first.to_i) if $0 == __FILE__ \ No newline at end of file +puts Hanoi.generate(ARGV[0].to_i) if $0 == __FILE__ \ No newline at end of file diff --git a/examples/n_queens/N_Queens.rb b/examples/n_queens/N_Queens.rb index b8c7872..069fc00 100644 --- a/examples/n_queens/N_Queens.rb +++ b/examples/n_queens/N_Queens.rb @@ -70,8 +70,8 @@ def try_next(y) #----------------------------------------------- if $0 == __FILE__ # Size input - size = ARGV.first ? ARGV.first.to_i : 8 - N_Queens.solve(size, ARGV.last == 'debug', true) + size = ARGV[0] ? ARGV[0].to_i : 8 + N_Queens.solve(size, ARGV[1] == 'debug', true) # Draw from row size - 1 to 0 N_Queens.state[:queen].reverse_each {|i,| row = '[ ]' * size diff --git a/examples/robby/pb1.rb b/examples/robby/pb1.rb index c344033..8dde67e 100644 --- a/examples/robby/pb1.rb +++ b/examples/robby/pb1.rb @@ -70,7 +70,7 @@ [:swap_at, robby, right] ], # Debug - ARGV.first == 'debug' + ARGV[0] == 'debug' ) # Test diff --git a/examples/sudoku/Sudoku.rb b/examples/sudoku/Sudoku.rb index 6602963..fa51044 100644 --- a/examples/sudoku/Sudoku.rb +++ b/examples/sudoku/Sudoku.rb @@ -91,7 +91,7 @@ def try_next(counter) if symbols.empty? return elsif symbols.size == 1 - singles << [:put_symbol, x, y, b, s = symbols.first] + singles << [:put_symbol, x, y, b, s = symbols[0]] col.delete(s) row.delete(s) box.delete(s) @@ -117,7 +117,7 @@ def try_next(counter) # Main #----------------------------------------------- if $0 == __FILE__ - debug = ARGV.first == 'debug' + debug = ARGV[0] == 'debug' # Easy board = ' 3 4 | 1 2 diff --git a/examples/travel/pb1.rb b/examples/travel/pb1.rb index d04f32a..95312df 100644 --- a/examples/travel/pb1.rb +++ b/examples/travel/pb1.rb @@ -40,7 +40,7 @@ [:travel, 'me', 'park'] ], # Debug - ARGV.first == 'debug' + ARGV[0] == 'debug' ) # Test diff --git a/examples/travel/pb2.rb b/examples/travel/pb2.rb index 3bfe283..7356e5b 100644 --- a/examples/travel/pb2.rb +++ b/examples/travel/pb2.rb @@ -40,7 +40,7 @@ [:travel, 'me', 'friend'] ], # Debug - ARGV.first == 'debug' + ARGV[0] == 'debug' ) # Test diff --git a/examples/travel/pb3.rb b/examples/travel/pb3.rb index 7bd02e3..cbb6a84 100644 --- a/examples/travel/pb3.rb +++ b/examples/travel/pb3.rb @@ -40,7 +40,7 @@ [:travel, 'me', 'home'] ], # Debug - ARGV.first == 'debug' + ARGV[0] == 'debug' ) # Test diff --git a/examples/travel/pb4.rb b/examples/travel/pb4.rb index 9ff81f0..5108c85 100644 --- a/examples/travel/pb4.rb +++ b/examples/travel/pb4.rb @@ -41,7 +41,7 @@ [:travel, 'me', 'park'] ], # Debug - ARGV.first == 'debug' + ARGV[0] == 'debug' ) # Test diff --git a/examples/travel/pb5.rb b/examples/travel/pb5.rb index a771228..7b2ad62 100644 --- a/examples/travel/pb5.rb +++ b/examples/travel/pb5.rb @@ -44,7 +44,7 @@ [:travel, 'me', 'park'] ], # Debug - ARGV.first == 'debug' + ARGV[0] == 'debug' ) # Test diff --git a/extensions/Complexity.rb b/extensions/Complexity.rb index bc18616..733f2af 100644 --- a/extensions/Complexity.rb +++ b/extensions/Complexity.rb @@ -13,13 +13,13 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not) op_complexity = op[1].size 2.upto(5) {|i| op[i].each {|pre| op_complexity += pre.uniq.size}} domain_complexity += op_complexity - puts " #{op.first}: #{op_complexity}" + puts " #{op[0]}: #{op_complexity}" } methods.each {|met| met_complexity = met[1].size met.drop(2).each {|dec| 2.upto(4) {|i| dec[i].each {|pre| met_complexity += pre.uniq.size}}} domain_complexity += met_complexity - puts " #{met.first}: #{met_complexity}" + puts " #{met[0]}: #{met_complexity}" } # Problem state.each_value {|k| k.each {|terms| problem_complexity += terms.uniq.size + 1}} diff --git a/extensions/Dejavu.rb b/extensions/Dejavu.rb index 3e89fe1..2569b8d 100644 --- a/extensions/Dejavu.rb +++ b/extensions/Dejavu.rb @@ -8,24 +8,24 @@ module Dejavu def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debug = false) return if tasks.empty? methods_h = {} - methods.each {|m| methods_h[m.first] = m} + methods.each {|m| methods_h[m[0]] = m} knots = [] ordered = tasks.shift tasks.uniq(&:first).each {|t,| visit(t, methods_h, knots)} tasks.unshift(ordered) knots.uniq! {|t,| t.object_id} knots.each {|task,method,decomposition| - name = method.first + name = method[0] terms = [] - index = decomposition.last.index {|t| + index = decomposition[-1].index {|t| terms |= t.drop(1) t.equal?(task) } - if name == task.first && decomposition.last.size > 1 or decomposition[1].empty? or task.size == 1 or task.drop(1).sort! != terms.sort - name = "#{name}_#{decomposition.first}_#{index}" + if name == task[0] && decomposition[-1].size > 1 or decomposition[1].empty? or task.size == 1 or task.drop(1).sort! != terms.sort + name = "#{name}_#{decomposition[0]}_#{index}" decomposition[3] << [visited = "visited_#{name}".freeze, *terms] - decomposition.last.insert(index, [visit = "invisible_visit_#{name}", *terms]) - decomposition.last.insert(index + 2, [unvisit = "invisible_unvisit_#{name}", *terms]) + decomposition[-1].insert(index, [visit = "invisible_visit_#{name}", *terms]) + decomposition[-1].insert(index + 2, [unvisit = "invisible_unvisit_#{name}", *terms]) unless operators.assoc(visit) predicates[visited] = true operators.push( @@ -35,7 +35,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu end method.drop(2).each {|dec| break if dec.equal?(decomposition) - dec.last.each {|subtask,*sterms| subtask.sub!('visit','mark') if subtask.start_with?('invisible_visit_','invisible_unvisit_') and sterms == terms} + dec[-1].each {|subtask,*sterms| subtask.sub!('visit','mark') if subtask.start_with?('invisible_visit_','invisible_unvisit_') and sterms == terms} } end } @@ -44,7 +44,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu def visit(name, methods, knots, visited = {}) if method = methods[name] (visited = visited.dup)[name] = nil - method.drop(2).each {|decomposition| decomposition.last.each {|task| visited.include?(name = task.first) ? knots << [task, method, decomposition] : visit(name, methods, knots, visited)}} + method.drop(2).each {|decomposition| decomposition[-1].each {|task| visited.include?(name = task[0]) ? knots << [task, method, decomposition] : visit(name, methods, knots, visited)}} end end end \ No newline at end of file diff --git a/extensions/Grammar.rb b/extensions/Grammar.rb index 9625d0d..220b1a0 100644 --- a/extensions/Grammar.rb +++ b/extensions/Grammar.rb @@ -11,10 +11,10 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not) parameters = met[1].join(' ') production_or = met.drop(2).map! {|dec| production_and = dec[4].map {|subtask,*terms| "#{subtask}(#{terms.join(' ')})"} - puts "#{met.first}_#{dec.first}(#{parameters}) ->\n #{production_and.empty? ? 'empty' : production_and.join(" &\n ")}" - "#{met.first}_#{dec.first}(#{parameters})" + puts "#{met[0]}_#{dec[0]}(#{parameters}) ->\n #{production_and.empty? ? 'empty' : production_and.join(" &\n ")}" + "#{met[0]}_#{dec[0]}(#{parameters})" } - puts "#{met.first}(#{parameters}) ->\n #{production_or.empty? ? 'empty' : production_or.join(" |\n ")}" + puts "#{met[0]}(#{parameters}) ->\n #{production_or.empty? ? 'empty' : production_or.join(" |\n ")}" } end end \ No newline at end of file diff --git a/extensions/Knoblock.rb b/extensions/Knoblock.rb index b6e5782..9cf65cb 100644 --- a/extensions/Knoblock.rb +++ b/extensions/Knoblock.rb @@ -28,8 +28,8 @@ def create_hierarchy(operators, predicates, goals = nil, verbose = false) def map(positive, negative, predicates) m = [] - positive.each {|pre| m << [true, pre] if predicates[pre.first]} - negative.each {|pre| m << [false, pre] if predicates[pre.first]} + positive.each {|pre| m << [true, pre] if predicates[pre[0]]} + negative.each {|pre| m << [false, pre] if predicates[pre[0]]} m end @@ -91,7 +91,7 @@ def dot(graph) end def dot_str(i) - i.first.instance_of?(Array) ? i.map {|j| dot_str(j)}.join(' ∧ ') : i.first ? i.last.join(' ') : "not #{i.last.join(' ')}" + i[0].instance_of?(Array) ? i.map {|j| dot_str(j)}.join(' ∧ ') : i[0] ? i[1].join(' ') : "not #{i[1].join(' ')}" end end @@ -101,7 +101,7 @@ def dot_str(i) if $0 == __FILE__ require_relative '../parsers/PDDL_Parser' begin - PDDL_Parser.parse_domain(ARGV.first) + PDDL_Parser.parse_domain(ARGV[0]) if ARGV[1] PDDL_Parser.parse_problem(ARGV[1]) goals = Knoblock.map(PDDL_Parser.goal_pos, PDDL_Parser.goal_not, PDDL_Parser.predicates) diff --git a/extensions/Macro.rb b/extensions/Macro.rb index 1a00c8b..b4f264d 100644 --- a/extensions/Macro.rb +++ b/extensions/Macro.rb @@ -9,14 +9,14 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu puts 'Macro'.center(50,'-') if debug # Task counter counter = Hash.new(0) - methods.each {|met| met.drop(2).each {|dec| dec.last.each {|t,| counter[t] += 1}}} + methods.each {|met| met.drop(2).each {|dec| dec[-1].each {|t,| counter[t] += 1}}} tasks.each {|t,| counter[t] += 1} # Macro sequential operators macro = [] clear_ops = {} methods.each {|met| met.drop(2).each {|dec| - macro_sequential_operators(operators, macro, dec.last, new_subtasks = [], counter, clear_ops, debug) + macro_sequential_operators(operators, macro, dec[-1], new_subtasks = [], counter, clear_ops, debug) dec[4] = new_subtasks } } @@ -55,7 +55,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu def macro_sequential_operators(operators, macro, subtasks, new_subtasks, counter, clear_ops, debug) subtasks.each {|subtask| # Add operators to macro and skip methods - if op = operators.assoc(subtask.first) + if op = operators.assoc(subtask[0]) macro << [op, subtask] else add_macro_to_subtasks(operators, macro, new_subtasks, counter, clear_ops, debug) @@ -79,26 +79,26 @@ def add_macro_to_subtasks(operators, macro, new_subtasks, counter, clear_ops, de effect_del = [] index = new_subtasks.size macro.each {|op,subtask| - unless op.first.start_with?('invisible_') + unless op[0].start_with?('invisible_') clear_ops[op] = nil new_subtasks << subtask end # Header - (name ? name << '_and_' : name = 'invisible_macro_') << op.first + (name ? name << '_and_' : name = 'invisible_macro_') << op[0] parameters.concat(param = subtask.drop(1)) variables = op[1] # Preconditions op[2].each {|pre| pre = pre.map {|p| p.start_with?('?') ? param[variables.index(p)] : p} if precond_not.include?(pre) and not effect_add.include?(pre) or effect_del.include?(pre) - raise "#{op.first} precondition (#{pre.join(' ')}) will never be satisfied" + raise "#{op[0]} precondition (#{pre.join(' ')}) will never be satisfied" else precond_pos << pre unless precond_pos.include?(pre) or effect_add.include?(pre) end } op[3].each {|pre| pre = pre.map {|p| p.start_with?('?') ? param[variables.index(p)] : p} if precond_pos.include?(pre) and not effect_del.include?(pre) or effect_add.include?(pre) - raise "#{op.first} precondition (not (#{pre.join(' ')})) will never be satisfied" + raise "#{op[0]} precondition (not (#{pre.join(' ')})) will never be satisfied" else precond_not << pre unless precond_not.include?(pre) or effect_del.include?(pre) end } @@ -120,8 +120,8 @@ def add_macro_to_subtasks(operators, macro, new_subtasks, counter, clear_ops, de new_subtasks.insert(index, [name, *parameters]) elsif macro.size == 1 op, subtask = macro.shift - if counter[op.first] != 1 and not op.first.start_with?('invisible_') - unless operators.assoc(name = "invisible_macro_#{op.first}") + if counter[op[0]] != 1 and not op[0].start_with?('invisible_') + unless operators.assoc(name = "invisible_macro_#{op[0]}") clear_ops[op] = nil operators << [name, op[1], op[2], op[3], op[4], op[5]] puts "Macro operator #{name}" if debug diff --git a/extensions/Patterns.rb b/extensions/Patterns.rb index db7e064..0c8fa93 100644 --- a/extensions/Patterns.rb +++ b/extensions/Patterns.rb @@ -36,7 +36,7 @@ def match_patterns(swaps, dependencies, operators, predicates, debug) effect_del = op[5] # Swap (+- => -+) or (-+ => +-) or (+? => -+) or (?- => -+) (precond_pos & effect_del).each {|pre| - if pre2 = effect_add.assoc(pre.first) + if pre2 = effect_add.assoc(pre[0]) cparam = pre - pre2 | pre2 - pre # At least one constraint must exist unless (pre_constraints = constraints.select {|i| (cparam - i).empty?}).empty? @@ -58,7 +58,7 @@ def match_patterns(swaps, dependencies, operators, predicates, debug) (swap_op and swap_op2 = swaps[op2] and swap_op.any? {|i,| swap_op2.assoc(i)}) or ((effect_add - op2[2]).empty? and (effect_del - op2[3]).empty?) pos = precond_pos.select {|pre,| op2[4].assoc(pre)} - neg = [] #precond_not.select {|pre| op2[5].assoc(pre.first)} + neg = [] #precond_not.select {|pre| op2[5].assoc(pre[0])} (dependencies[op] ||= []) << [op2, pos, neg] unless pos.empty? and neg.empty? } } @@ -68,7 +68,7 @@ def match_patterns(swaps, dependencies, operators, predicates, debug) underscore = '_' edges = [] swaps.each {|op,pre_constraints| - namesub = (name = op.first).tr(hyphen, underscore) + namesub = (name = op[0]).tr(hyphen, underscore) pre_constraints.each {|pre,constraints| edges << "\n #{namesub} -> \"(#{pre_join = pre.join(sep)})\" [dir=both style=dashed]" puts " #{name} swaps (#{pre_join}) with constraint (#{constraints.join(sep)})" @@ -76,10 +76,10 @@ def match_patterns(swaps, dependencies, operators, predicates, debug) } dependency_counter = 0 dependencies.each {|op,op_dependencies| - namesub = (name = op.first).tr(hyphen, underscore) + namesub = (name = op[0]).tr(hyphen, underscore) op_dependencies.each {|op2,pos,neg| dependency_counter += pos.size + neg.size - op2_namesub = (name2 = op2.first).tr(hyphen, underscore) + op2_namesub = (name2 = op2[0]).tr(hyphen, underscore) pos.each {|pre| puts " #{name2} before #{name}, dependency (#{pre_join = pre.join(sep)})" edges.push("\n #{op2_namesub} -> \"(#{pre_join})\"", "\n \"(#{pre_join})\" -> #{namesub}") @@ -93,7 +93,7 @@ def match_patterns(swaps, dependencies, operators, predicates, debug) puts 'Counter', " Swaps: #{swaps.size}", " Dependencies: #{dependency_counter}" edges.uniq! graph = 'digraph Patterns {' - operators.each {|op| graph << "\n #{op.first.tr(hyphen, underscore)} [label=\"#{op.first}(#{op[1].join(sep)})\" shape=box]"} + operators.each {|op| graph << "\n #{op[0].tr(hyphen, underscore)} [label=\"#{op[0]}(#{op[1].join(sep)})\" shape=box]"} puts 'DOT output', graph << edges.join << "\n}" end @@ -103,11 +103,11 @@ def match_patterns(swaps, dependencies, operators, predicates, debug) def fill_goal_methods(goal_methods, goal_pos, goal_not, source_parameters, source_pos, source_not, met, parameters) goal_pos.each {|goal| - group = source_pos.assoc(goal.first) + group = source_pos.assoc(goal[0]) goal_methods[[true, goal]] << [met, group.map {|var| (i = source_parameters.index(var)) ? parameters[i] : var}] if group } goal_not.each {|goal| - group = source_not.assoc(goal.first) + group = source_not.assoc(goal[0]) goal_methods[[false, goal]] << [met, group.map {|var| (i = source_parameters.index(var)) ? parameters[i] : var}] if group } end @@ -121,12 +121,12 @@ def find_goal_methods(operators, methods, predicates, goal_pos, goal_not, debug) # Avoid methods for simple goals goal_pos_complex = goal_pos.reject {|goal,| operators.any? {|op| - op[4].assoc(goal) and op[2].none? {|pre,| predicates[pre]} and ((precond_not = op[3].select {|pre,| predicates[pre]}).empty? or (precond_not.size == 1 and precond_not.first.first == goal)) + op[4].assoc(goal) and op[2].none? {|pre,| predicates[pre]} and ((precond_not = op[3].select {|pre,| predicates[pre]}).empty? or (precond_not.size == 1 and precond_not[0][0] == goal)) } } goal_not_complex = goal_not.reject {|goal,| operators.any? {|op| - op[5].assoc(goal) and op[3].none? {|pre,| predicates[pre]} and ((precond_pos = op[2].select {|pre,| predicates[pre]}).empty? or (precond_pos.size == 1 and precond_pos.first.first == goal)) + op[5].assoc(goal) and op[3].none? {|pre,| predicates[pre]} and ((precond_pos = op[2].select {|pre,| predicates[pre]}).empty? or (precond_pos.size == 1 and precond_pos[0][0] == goal)) } } # Find every method that contains a relevant action in the subtasks @@ -138,7 +138,7 @@ def find_goal_methods(operators, methods, predicates, goal_pos, goal_not, debug) fill_goal_methods(goal_methods, goal_pos_complex, goal_not_complex, dec[1], dec[2], dec[3], met, dec) else dec[4].each {|subtask| - op = operators.assoc(subtask.first) + op = operators.assoc(subtask[0]) fill_goal_methods(goal_methods, goal_pos_complex, goal_not_complex, dec[1], op[4], op[5], met, subtask.drop(1)) if op } end @@ -146,16 +146,16 @@ def find_goal_methods(operators, methods, predicates, goal_pos, goal_not, debug) } goal_methods.each {|(type,goal),v| v.uniq! - for_goal = "_#{goal.first}" + for_goal = "_#{goal[0]}" # Give priority based on operator relevance to goal v.sort_by! {|mets,| # Prefer to match goal - val = mets.first.end_with?(for_goal) ? -1 : 0 - val - mets.drop(2).count {|dec| dec[4][0] and op = operators.assoc(dec[4].last.first) and op[type ? 4 : 5].assoc(goal.first)} + val = mets[0].end_with?(for_goal) ? -1 : 0 + val - mets.drop(2).count {|dec| dec[4][0] and op = operators.assoc(dec[4][-1][0]) and op[type ? 4 : 5].assoc(goal[0])} } if debug puts " #{'not ' unless type}(#{goal.join(' ')})" - v.each {|met,pred| puts " #{met.first}(#{met[1].join(' ')}) achieves (#{pred.join(' ')})"} + v.each {|met,pred| puts " #{met[0]}(#{met[1].join(' ')}) achieves (#{pred.join(' ')})"} end } end @@ -168,35 +168,35 @@ def inject_method_dependencies(swaps, methods, predicates, debug) # Inject dependencies puts 'Inject dependencies' if debug methods.each {|met| - if met.first =~ /^dependency_([\w-]+)_before_([\w-]+)_for_([\w-]+)$/ + if met[0] =~ /^dependency_([\w-]+)_before_([\w-]+)_for_([\w-]+)$/ dependency = $1 dependent = $2 pred = $3 # Prefer dependency with same predicate goal sub = nil methods.each {|met2| - if met2.first =~ /^dependency_(?!#{dependent})[\w-]+_before_#{dependency}_for_([\w-]+)$/ + if met2[0] =~ /^dependency_(?!#{dependent})[\w-]+_before_#{dependency}_for_([\w-]+)$/ sub = met2 break if $1 == pred end } if sub - puts " #{dependency} to #{sub.first} in #{met.first}" if debug - met.drop(3).each {|dec| dec[4][0] = [sub[0], *sub[1]] if dec[4][0].first == dependency} + puts " #{dependency} to #{sub[0]} in #{met[0]}" if debug + met.drop(3).each {|dec| dec[4][0] = [sub[0], *sub[1]] if dec[4][0][0] == dependency} sub = nil end # Prefer dependency with same predicate goal methods.each {|met2| - if not met.equal?(met2) and met2.first =~ /^dependency_swap_[\w-]+_until_[\w-]+_before_#{dependent}_for_([\w-]+)$/ + if not met.equal?(met2) and met2[0] =~ /^dependency_swap_[\w-]+_until_[\w-]+_before_#{dependent}_for_([\w-]+)$/ sub = met2 break if $1 == pred end } if sub - puts " #{dependent} to #{sub.first} in #{met.first}" if debug + puts " #{dependent} to #{sub[0]} in #{met[0]}" if debug dependent_split = dependent.split('_or_') met.drop(3).each {|dec| - dec[4].map! {|subtask| dependent_split.include?(subtask.first) ? [sub[0], *sub[1]] : subtask} + dec[4].map! {|subtask| dependent_split.include?(subtask[0]) ? [sub[0], *sub[1]] : subtask} } end end @@ -218,41 +218,41 @@ def add_tasks(goal_methods, operators, methods, predicates, tasks, goal_pos, goa # TODO check free variable names in pred ground = met[1].map {|var| (i = pred.index(var)) ? goal[i] : var} if ground.none? {|var| var.start_with?('?')} - puts " Ground task #{met.first}(#{ground.join(' ')})" if debug - tasks_goals.unshift([ground.unshift(met.first), type, goal]) + puts " Ground task #{met[0]}(#{ground.join(' ')})" if debug + tasks_goals.unshift([ground.unshift(met[0]), type, goal]) end } # Lifted - met, pred = v.first + met, pred = v[0] ground = met[1].map {|var| (i = pred.index(var)) ? goal[i] : var} - puts " Lifted task #{met.first}(#{ground.join(' ')})" if debug + puts " Lifted task #{met[0]}(#{ground.join(' ')})" if debug tasks_goals.unshift([compose_unification_method(operators, methods, predicates, met, ground), type, goal]) end } # Goal primitives goal_pos.each {|goal| operators.each {|op| - if group = op[4].assoc(goal.first) + if group = op[4].assoc(goal[0]) # TODO add unification method when required - tasks_goals.unshift([op[1].map {|var| (i = group.index(var)) ? goal[i] : var}.unshift(op.first), true, goal]) + tasks_goals.unshift([op[1].map {|var| (i = group.index(var)) ? goal[i] : var}.unshift(op[0]), true, goal]) break end } unless goal_methods.include?([true, goal]) } goal_not.each {|goal| operators.each {|op| - if group = op[5].assoc(goal.first) + if group = op[5].assoc(goal[0]) # TODO add unification method when required - tasks_goals.unshift([op[1].map {|var| (i = group.index(var)) ? goal[i] : var}.unshift(op.first), false, goal]) + tasks_goals.unshift([op[1].map {|var| (i = group.index(var)) ? goal[i] : var}.unshift(op[0]), false, goal]) break end } unless goal_methods.include?([false, goal]) } ordered_tasks = [] Knoblock.create_hierarchy(operators, predicates).each {|order| - order = [order] unless order.first.instance_of?(Array) - order.each {|i| i[1] = i[1].first} - tasks_goals.reject! {|met,type,goal| ordered_tasks.unshift(met) if order.include?([type, goal.first])} + order = [order] unless order[0].instance_of?(Array) + order.each {|i| i[1] = i[1][0]} + tasks_goals.reject! {|met,type,goal| ordered_tasks.unshift(met) if order.include?([type, goal[0]])} break if tasks_goals.empty? } # Add tasks as unordered @@ -272,8 +272,8 @@ def compose_swap_methods(swaps, operators, methods, predicates, debug) swaps.each {|op,pre_constraints| pre_constraints.each {|pre,constraints| swap_predicates[pre] << [op, constraints]}} swap_predicates.each {|(predicate_name,*predicate_terms),swap_ops| # Explicit or implicit agent - agent = predicate_terms.first if predicate_terms.size != 1 - original_current = predicate_terms.last + agent = predicate_terms[0] if predicate_terms.size != 1 + original_current = predicate_terms[-1] # Add visit and unvisit operators and predicate visited = "visited_#{predicate_name}" visit = "invisible_#{VISIT}_#{predicate_name}" @@ -290,7 +290,7 @@ def compose_swap_methods(swaps, operators, methods, predicates, debug) swap_ops.each {|op,| effects.concat(op[4])} effects.uniq! swap_ops.each {|op,constraints| - original_intermediate = (constraints.first - [original_current]).last + original_intermediate = (constraints[0] - [original_current])[-1] predicate_terms2 = predicate_terms.map {|i| i == original_current ? original_intermediate : i} # Add swap recursion free_variables = Hash.new {|h,k| h[k] = k} @@ -302,11 +302,11 @@ def compose_swap_methods(swaps, operators, methods, predicates, debug) agent ? [visited, agent, intermediate] : [visited, intermediate] ] # Replace signature with free variables - new_op = op[1].map {|i| i == agent ? i : free_variables[i]}.unshift(op.first) + new_op = op[1].map {|i| i == agent ? i : free_variables[i]}.unshift(op[0]) free_variables = free_variables.values effects.each {|eff| # Swap method - unless swap_method = methods.assoc(method_name = "swap_#{predicate_name}_until_#{eff.first}") + unless swap_method = methods.assoc(method_name = "swap_#{predicate_name}_until_#{eff[0]}") puts " swap method composed: #{method_name}" if debug methods << swap_method = [method_name, predicate_terms2] if (predicate_terms2 - eff).empty? @@ -317,7 +317,7 @@ def compose_swap_methods(swaps, operators, methods, predicates, debug) end end # Label and free variables - swap_method << ["using_#{op.first}", free_variables, + swap_method << ["using_#{op[0]}", free_variables, # Positive preconditions precond_pos, # Negative preconditions @@ -356,29 +356,29 @@ def compose_dependency_methods(swaps, dependencies, operators, methods, predicat op_dependencies.each {|first,pos,neg| # TODO consider all dependencies type = true - pre = pos.first + pre = pos[0] # Dependency of dependency first_terms = first[1] swaps[first]&.each {|p,| - if m = methods.assoc("swap_#{p.first}_until_#{p.first}") + if m = methods.assoc("swap_#{p[0]}_until_#{p[0]}") first = m - if p.first == pre.first + if p[0] == pre[0] first_terms = pre.drop(1) break else first_terms = p.drop(1) end end } or swaps[op]&.each {|p,| - if m = methods.assoc("swap_#{p.first}_until_#{p.first}") + if m = methods.assoc("swap_#{p[0]}_until_#{p[0]}") seconds = [m] - if p.first == pre.first + if p[0] == pre[0] second_terms = pre.drop(1) break else second_terms = p.drop(1) end end } - name = "dependency_#{first.first}_before_#{seconds.map(&:first).join('_or_')}" + name = "dependency_#{first[0]}_before_#{seconds.map(&:first).join('_or_')}" next if methods.any? {|met,| met.start_with?(name)} precond_pos = [] precond_not = [] @@ -386,7 +386,7 @@ def compose_dependency_methods(swaps, dependencies, operators, methods, predicat if operators.include?(first) fill_preconditions(first, predicates, precond_pos, precond_not, first_terms) # Replace different variables between first and second - pre.zip(first[type ? 4 : 5].assoc(pre.first)) {|a,b| + pre.zip(first[type ? 4 : 5].assoc(pre[0])) {|a,b| if a != b replacements[a] = b replacements[b] ||= a @@ -403,31 +403,31 @@ def compose_dependency_methods(swaps, dependencies, operators, methods, predicat precond_pos_first = precond_pos_second | precond_pos precond_not_first = precond_not_second | precond_not # Label and free variables - satisfied << [seconds.size == 1 ? 'satisfied' : "satisfied_#{second.first}", [], + satisfied << [seconds.size == 1 ? 'satisfied' : "satisfied_#{second[0]}", [], # Positive preconditions type ? precond_pos_second << pre : precond_pos_second, # Negative preconditions type ? precond_not_second : precond_not_second << pre, # Subtasks - [[second.first, *second_terms]] - ] unless first.first.start_with?(SWAP) + [[second[0], *second_terms]] + ] unless first[0].start_with?(SWAP) # Label and free variables - unsatisfied << [seconds.size == 1 ? 'unsatisfied' : "unsatisfied_#{second.first}", [], + unsatisfied << [seconds.size == 1 ? 'unsatisfied' : "unsatisfied_#{second[0]}", [], # Positive preconditions type ? precond_pos_first : precond_pos_first << pre, # Negative preconditions type ? precond_not_first << pre : precond_not_first, # Subtasks [ - first_terms.map {|i| replacements[i] || i}.unshift(first.first), - [second.first, *second_terms] + first_terms.map {|i| replacements[i] || i}.unshift(first[0]), + [second[0], *second_terms] ] ] } # Disjunctions share effects op[4].each {|effect| - puts " dependency method composed: #{name}_for_#{effect.first}" if debug - methods << ["#{name}_for_#{effect.first}", first_terms | second_terms, + puts " dependency method composed: #{name}_for_#{effect[0]}" if debug + methods << ["#{name}_for_#{effect[0]}", first_terms | second_terms, # Label and free variables ['goal-satisfied', [], # Positive preconditions @@ -462,12 +462,12 @@ def compose_unification_method(operators, methods, predicates, met, substitution ground_var << m end } - unless methods.assoc(name = "unify#{free.join.tr!('?','_')}_before_#{met.first}") + unless methods.assoc(name = "unify#{free.join.tr!('?','_')}_before_#{met[0]}") # Find rigid predicates shared across decompositions to act as preconditions precond_pos = [] precond_not = [] met.drop(3).each {|dec| - if dec.first.start_with?('unsatisfied', 'using_') + if dec[0].start_with?('unsatisfied', 'using_') fill_preconditions(dec, predicates, precond_pos_local = [], precond_not_local = [], met[1]) precond_pos.empty? ? precond_pos = precond_pos_local : precond_pos &= precond_pos_local precond_not.empty? ? precond_not = precond_not_local : precond_not &= precond_not_local @@ -503,17 +503,17 @@ def bind_variables(free, root, ground_var, precond_pos, operators, methods) fringe = [root] visited.clear while node = fringe.shift - visited[node.first] = true - if operators.assoc(node.first) + visited[node[0]] = true + if operators.assoc(node[0]) break if node[2].any? {|pre| - if pre.include?(f) and not precond_pos.assoc(pre.first) + if pre.include?(f) and not precond_pos.assoc(pre[0]) precond_pos << pre new_free.concat(pre.drop(1).reject! {|i| ground_var.include?(i) or free.include?(i)}) end } - elsif node.first.start_with?(DEPENDENCY, SWAP) + elsif node[0].start_with?(DEPENDENCY, SWAP) # TODO fix new multiple unsatisfied dependencies and multiple swap operators over same predicate - node.last[4].reverse_each {|i,| fringe.unshift(operators.assoc(i) || methods.assoc(i)) unless visited.include?(i)} + node[-1][4].reverse_each {|i,| fringe.unshift(operators.assoc(i) || methods.assoc(i)) unless visited.include?(i)} # TODO else support user provided methods end end @@ -528,7 +528,7 @@ def bind_variables(free, root, ground_var, precond_pos, operators, methods) #----------------------------------------------- def fill_preconditions(operator, predicates, precond_pos, precond_not, variables) - operator[2].each {|pre| precond_pos << pre if not predicates[pre.first] and pre.size == 1 || variables.any? {|i| pre.include?(i)}} - operator[3].each {|pre| precond_not << pre if not predicates[pre.first] and pre.size == 1 || variables.any? {|i| pre.include?(i)}} + operator[2].each {|pre| precond_pos << pre if not predicates[pre[0]] and pre.size == 1 || variables.any? {|i| pre.include?(i)}} + operator[3].each {|pre| precond_not << pre if not predicates[pre[0]] and pre.size == 1 || variables.any? {|i| pre.include?(i)}} end end \ No newline at end of file diff --git a/extensions/Pullup.rb b/extensions/Pullup.rb index 3683bf6..c0f92c8 100644 --- a/extensions/Pullup.rb +++ b/extensions/Pullup.rb @@ -19,7 +19,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu if precond_pos.each {|pre,*terms| unless predicates[pre] if not s = state[pre] or (not terms.all? {|i| i.start_with?('?')} and (s = s.select {|i| not i.zip(terms) {|a,b| break true unless a == b or b.start_with?('?')}}).empty?) then break - elsif s.size == 1 and not (terms & free).empty? then terms.zip(s.first) {|a,b| substitutions[a] = b if a != b} + elsif s.size == 1 and not (terms & free).empty? then terms.zip(s[0]) {|a,b| substitutions[a] = b if a != b} end end } @@ -29,7 +29,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu free.reject! {|i| substitutions.include?(i)} precond_pos.each {|pre| pre.map! {|i| substitutions[i] || i}} precond_not.each {|pre| pre.map! {|i| substitutions[i] || i}} - subtasks.each {|t| counter[t.map! {|i| substitutions[i] || i}.first] += 1} + subtasks.each {|t| counter[t.map! {|i| substitutions[i] || i}[0]] += 1} end end } @@ -39,7 +39,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu else decompositions.unshift(name, param) end } - operators.reject! {|op| impossible << op.first unless counter.include?(op.first) and op[2].all? {|pre,| predicates[pre] || state.include?(pre)}} + operators.reject! {|op| impossible << op[0] unless counter.include?(op[0]) and op[2].all? {|pre,| predicates[pre] || state.include?(pre)}} # Move current or rigid predicates from leaves to root/entry tasks clear_ops = [] clear_met = [] @@ -53,33 +53,33 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu old_precond_pos_size = precond_pos.size old_precond_not_size = precond_not.size subtasks.each {|s| - if impossible.include?(s.first) + if impossible.include?(s[0]) repeat = true subtasks.each {|i,| operators.reject! {|op,| op == i} if (counter[i] -= 1) == 0} break - elsif op = operators.assoc(s.first) - op[2].each {|pre| precond_pos << pre.map {|t| (j = op[1].index(t)) ? s[j + 1] : t} if effects[pre.first].even?} - op[3].each {|pre| precond_not << pre.map {|t| (j = op[1].index(t)) ? s[j + 1] : t} if effects[pre.first] < 2} + elsif op = operators.assoc(s[0]) + op[2].each {|pre| precond_pos << pre.map {|t| (j = op[1].index(t)) ? s[j + 1] : t} if effects[pre[0]].even?} + op[3].each {|pre| precond_not << pre.map {|t| (j = op[1].index(t)) ? s[j + 1] : t} if effects[pre[0]] < 2} op[4].each {|pre,| effects[pre] |= 1} op[5].each {|pre,| effects[pre] |= 2} - if first_task and counter[s.first] == 1 + if first_task and counter[s[0]] == 1 op[2].clear op[3].clear - elsif first_pass and not tasks.assoc(s.first) then clear_ops << op + elsif first_pass and not tasks.assoc(s[0]) then clear_ops << op end else all_pos = all_neg = nil - metdecompositions = (met = methods.assoc(s.first)).drop(2).each {|m| + metdecompositions = (met = methods.assoc(s[0])).drop(2).each {|m| pos = [] neg = [] m[2].each {|pre| - if effects[pre.first].even? + if effects[pre[0]].even? pre = pre.map {|t| (j = met[1].index(t)) ? s[j + 1] : t} pos << pre if (pre & m[1]).empty? end } m[3].each {|pre| - if effects[pre.first] < 2 + if effects[pre[0]] < 2 pre = pre.map {|t| (j = met[1].index(t)) ? s[j + 1] : t} neg << pre if (pre & m[1]).empty? end @@ -93,7 +93,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu end } mark_effects(operators, methods, metdecompositions, effects) - clear_met << [metdecompositions, all_pos, all_neg] unless tasks.assoc(s.first) + clear_met << [metdecompositions, all_pos, all_neg] unless tasks.assoc(s[0]) precond_pos.concat(all_pos) precond_not.concat(all_neg) end @@ -102,7 +102,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu # Add equality duplicates equalities = Hash.new {|h,k| h[k] = []} precond_pos.each {|pre| - if pre.first == '=' + if pre[0] == '=' if pre[1].start_with?('?') equalities[pre[2]] << pre[1] unless pre[2].start_with?('?') elsif pre[2].start_with?('?') @@ -113,7 +113,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu unless equalities.empty? new_precond_pos = [] precond_pos.each {|pre| - if pre.first != '=' + if pre[0] != '=' modified = false npre = pre.drop(1).map! {|i| if equalities.include?(i) @@ -122,7 +122,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu else [i] end } - new_precond_pos.concat([pre.first].product(*npre)) if modified + new_precond_pos.concat([pre[0]].product(*npre)) if modified end }.concat(new_precond_pos).uniq! end @@ -159,8 +159,8 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu subtasks.each {|t| t.map! {|i| substitutions[i] || i}} end precond_pos.reject! {|pre| - if not predicates[pre.first] and pre.none? {|i| i.start_with?('?')} - unless state[pre.first]&.include?(pre.drop(1)) + if not predicates[pre[0]] and pre.none? {|i| i.start_with?('?')} + unless state[pre[0]]&.include?(pre.drop(1)) possible_decomposition = false break end @@ -169,8 +169,8 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu } if possible_decomposition precond_not.reject! {|pre| - if not predicates[pre.first] and pre.none? {|i| i.start_with?('?')} - if state[pre.first]&.include?(pre.drop(1)) + if not predicates[pre[0]] and pre.none? {|i| i.start_with?('?')} + if state[pre[0]]&.include?(pre.drop(1)) possible_decomposition = false break end @@ -203,7 +203,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu def mark_effects(operators, methods, decompositions, effects, visited = {}) decompositions.each {|dec| - dec.last.each {|s,| + dec[-1].each {|s,| unless visited.include?(s) visited[s] = nil if op = operators.assoc(s) diff --git a/extensions/Typredicate.rb b/extensions/Typredicate.rb index a841988..2e0839c 100644 --- a/extensions/Typredicate.rb +++ b/extensions/Typredicate.rb @@ -11,7 +11,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu operator_types = {} operators.each {|name,_,precond_pos,precond_not,effect_add,effect_del| operator_types[name] = types = {} - precond_pos.each {|terms| types[terms.last] ||= terms.first if terms.size == 2 and not predicates[terms.first]} + precond_pos.each {|terms| types[terms[1]] ||= terms[0] if terms.size == 2 and not predicates[terms[0]]} find_types(precond_pos, types, new_predicates) find_types(precond_not, types, new_predicates) find_types(effect_add, types, new_predicates) @@ -34,7 +34,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu methods.each {|met| met.drop(2).each {|_,_,precond_pos,precond_not| types = {} - precond_pos.each {|terms| types[terms.last] ||= terms.first if terms.size == 2 and not predicates[terms.first]} + precond_pos.each {|terms| types[terms[1]] ||= terms[0] if terms.size == 2 and not predicates[terms[0]]} next if types.empty? precond_pos.each {|terms| pre = terms.shift; terms.unshift(transformations[types.values_at(*terms).unshift(pre)] || pre)} precond_not.each {|terms| pre = terms.shift; terms.unshift(transformations[types.values_at(*terms).unshift(pre)] || pre)} diff --git a/extensions/Wise.rb b/extensions/Wise.rb index a5192c1..2f69157 100644 --- a/extensions/Wise.rb +++ b/extensions/Wise.rb @@ -13,7 +13,7 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu # Duplicates puts "Initial state contains duplicate predicates (#{pre} ...): removed" if k.uniq! and debug # Arity check - puts "Initial state contains (#{pre} ...) with different arity" if k.any? {|i| i.size != k.first.size} and debug + puts "Initial state contains (#{pre} ...) with different arity" if k.any? {|i| i.size != k[0].size} and debug else # Unused predicate puts "Initial state contains unused predicates (#{pre} ...): removed" if debug @@ -32,8 +32,8 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu puts "#{opname} contains duplicate add effect: removed" if effect_add.uniq! and debug puts "#{opname} contains duplicate del effect: removed" if effect_del.uniq! and debug # Equality - precond_pos.each {|pre| raise "#{opname} precondition contains unnecessary (#{pre.join(' ')}), use same variable instead" if pre.first == '='} - precond_not.each {|pre| raise "#{opname} precondition contains contradiction (not (#{pre.join(' ')}))" if pre.first == '=' and pre[1] == pre[2]} + precond_pos.each {|pre| raise "#{opname} precondition contains unnecessary (#{pre.join(' ')}), use same variable instead" if pre[0] == '='} + precond_not.each {|pre| raise "#{opname} precondition contains contradiction (not (#{pre.join(' ')}))" if pre[0] == '=' and pre[1] == pre[2]} raise "#{opname} effect contains equality" if effect_add.assoc('=') or effect_del.assoc('=') # Precondition contradiction (precond_pos & precond_not).each {|pre| raise "#{opname} precondition contains contradiction (#{pre.join(' ')}) and (not (#{pre.join(' ')}))"} @@ -75,7 +75,6 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu decompositions.each {|label,free,precond_pos,precond_not,subtasks| prefix_variables(label = "#{name} #{label}", free, debug) define_variables(label, param + free, [precond_pos, precond_not, subtasks], debug) - # Duplicates puts "#{label} contains duplicate free variable: removed" if free.uniq! and debug puts "#{label} contains duplicate positive precondition: removed" if precond_pos.uniq! and debug puts "#{label} contains duplicate negative precondition: removed" if precond_not.uniq! and debug @@ -86,8 +85,8 @@ def apply(operators, methods, predicates, state, tasks, goal_pos, goal_not, debu end } # Equality - precond_pos.each {|pre| raise "#{label} precondition contains unnecessary (#{pre.join(' ')}), use same variable instead" if pre.first == '='} - precond_not.each {|pre| raise "#{label} precondition contains contradiction (not (#{pre.join(' ')}))" if pre.first == '=' and pre[1] == pre[2]} + precond_pos.each {|pre| raise "#{label} precondition contains unnecessary (#{pre.join(' ')}), use same variable instead" if pre[0] == '='} + precond_not.each {|pre| raise "#{label} precondition contains contradiction (not (#{pre.join(' ')}))" if pre[0] == '=' and pre[1] == pre[2]} # Precondition contradiction (precond_pos & precond_not).each {|pre| raise "#{label} precondition contains contradiction (#{pre.join(' ')}) and (not (#{pre.join(' ')}))"} verify_tasks("#{label} subtask", subtasks, noops, operators, methods, debug) @@ -145,14 +144,14 @@ def define_variables(name, param, group, debug) def verify_tasks(name, tasks, noops, operators, methods, debug) # Task arity check and noops removal tasks.reject! {|task| - if noops.include?(task.first) - puts "#{name} #{task.first}: removed" if debug + if noops.include?(task[0]) + puts "#{name} #{task[0]}: removed" if debug true - elsif t = operators.assoc(task.first) || methods.assoc(task.first) - raise "#{name} #{task.first} expected #{t[1].size} terms instead of #{task.size.pred}" if t[1].size != task.size.pred - elsif task.first.start_with?('?') - puts "#{name} #{task.first} is variable" if debug - else raise "#{name} #{task.first} is unknown" + elsif t = operators.assoc(task[0]) || methods.assoc(task[0]) + raise "#{name} #{task[0]} expected #{t[1].size} terms instead of #{task.size.pred}" if t[1].size != task.size.pred + elsif task[0].start_with?('?') + puts "#{name} #{task[0]} is variable" if debug + else raise "#{name} #{task[0]} is unknown" end } end diff --git a/parsers/HDDL_Parser.rb b/parsers/HDDL_Parser.rb index d813cfe..59540eb 100644 --- a/parsers/HDDL_Parser.rb +++ b/parsers/HDDL_Parser.rb @@ -32,7 +32,7 @@ def scan_tokens(filename) } raise 'Missing close parentheses' unless stack.empty? raise 'Malformed expression' if list.size != 1 - list.first + list[0] end #----------------------------------------------- @@ -98,15 +98,15 @@ def parse_action(op) raise "Error with #{name} precondition" unless (group = op.shift).instance_of?(Array) unless group.empty? # Conjunction or atom - group.first == AND ? group.shift : group = [group] + group[0] == AND ? group.shift : group = [group] group.each {|pre| raise "Error with #{name} precondition" unless pre.instance_of?(Array) - if pre.first == 'forall' - pre[2].first == AND ? pre[2].shift : pre[2] = [pre[2]] + if pre[0] == 'forall' + pre[2][0] == AND ? pre[2].shift : pre[2] = [pre[2]] @foralls << [pos, neg, pre, false] else - pre.first != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre.last : raise("Unexpected not in #{name} precondition") - @predicates[pre.first.freeze] ||= false + pre[0] != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre[1] : raise("Unexpected not in #{name} precondition") + @predicates[pre[0].freeze] ||= false end } end @@ -114,15 +114,15 @@ def parse_action(op) raise "Error with #{name} effect" unless (group = op.shift).instance_of?(Array) unless group.empty? # Conjunction or atom - group.first == AND ? group.shift : group = [group] + group[0] == AND ? group.shift : group = [group] group.each {|pre| raise "Error with #{name} effect" unless pre.instance_of?(Array) - if pre.first == 'forall' - pre[2].first == AND ? pre[2].shift : pre[2] = [pre[2]] + if pre[0] == 'forall' + pre[2][0] == AND ? pre[2].shift : pre[2] = [pre[2]] @foralls << [add, del, pre, true] else - pre.first != NOT ? add << pre : pre.size == 2 ? del << pre = pre.last : raise("Unexpected not in #{name} effect") - @predicates[pre.first.freeze] = true + pre[0] != NOT ? add << pre : pre.size == 2 ? del << pre = pre[1] : raise("Unexpected not in #{name} effect") + @predicates[pre[0].freeze] = true end } end @@ -144,7 +144,7 @@ def parse_method(met) when ':task' then name = (variables = met.shift).shift when ':precondition' then precondition.concat(met.shift) when ':constraints' - met.first.shift if not precondition.empty? and met.first.first == AND + met[0].shift if not precondition.empty? and met[0][0] == AND precondition.concat(met.shift) when ':subtasks', ':tasks', ':ordered-subtasks', ':ordered-tasks' then subtasks = met.shift when ':ordering' then ordering = met.shift @@ -174,28 +174,28 @@ def parse_method(met) end unless precondition.empty? # Conjunction or atom - precondition.first == AND ? precondition.shift : precondition = [precondition] + precondition[0] == AND ? precondition.shift : precondition = [precondition] precondition.each {|pre| - if pre.first == 'forall' - pre[2].first == AND ? pre[2].shift : pre[2] = [pre[2]] - pre[2].each {|g| (g.first != NOT ? g : g[1]).map! {|i| variables.find {|j| j == i} || free_variables.find {|j| j == i} || i}} + if pre[0] == 'forall' + pre[2][0] == AND ? pre[2].shift : pre[2] = [pre[2]] + pre[2].each {|g| (g[0] != NOT ? g : g[1]).map! {|i| variables.find {|j| j == i} || free_variables.find {|j| j == i} || i}} @foralls << [pos, neg, pre, false] else - pre.first != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre.last : raise("Error with #{name} negative precondition") + pre[0] != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre[1] : raise("Error with #{name} negative precondition") pre.map! {|i| variables.find {|j| j == i} || free_variables.find {|j| j == i} || i} - @predicates[pre.first.freeze] ||= false + @predicates[pre[0].freeze] ||= false end } end # Subtasks raise "Error with #{name} subtasks" unless (subtasks ||= []).instance_of?(Array) - if subtasks.empty? then method.last << subtasks + if subtasks.empty? then method[-1] << subtasks else # Conjunction or atom - subtasks.first == AND ? subtasks.shift : subtasks = [subtasks] + subtasks[0] == AND ? subtasks.shift : subtasks = [subtasks] # Ordering parse_ordering(name, ordering, subtasks) if ordering - method.last << subtasks.map! {|t| (t[1].instance_of?(Array) ? t[1] : t).map! {|i| variables.find {|j| j == i} || free_variables.find {|j| j == i} || i}} + method[-1] << subtasks.map! {|t| (t[1].instance_of?(Array) ? t[1] : t).map! {|i| variables.find {|j| j == i} || free_variables.find {|j| j == i} || i}} end free_variables.each {|i| i.insert(1,'free_') if method[1].include?(i)} variables.zip(method[1]) {|i,j| i.replace(j)} @@ -217,11 +217,11 @@ def parse_domain(domain_filename) @requirements = [] @foralls = [] while group = tokens.shift - case group.first + case group[0] when ':action' then parse_action(group) when ':method' then parse_method(group) when ':task' then @methods << [group[1], group[2] == ':parameters' ? group[3].keep_if {|i| i.start_with?('?')} : []] - when 'domain' then @domain_name = group.last + when 'domain' then @domain_name = group[1] when ':requirements' then (@requirements = group).shift when ':predicates' when ':types' @@ -234,7 +234,7 @@ def parse_domain(domain_filename) group.shift(i+2) end when ':constants' then parse_objects(group) - else raise "#{group.first} is not recognized in domain" + else raise "#{group[0]} is not recognized in domain" end end else raise "File #{domain_filename} does not match domain pattern" @@ -252,17 +252,17 @@ def parse_problem(problem_filename) @goal_not = [] @tasks = [] while group = tokens.shift - case group.first - when 'problem' then @problem_name = group.last - when ':domain' then raise 'Different domain specified in problem file' if @domain_name != group.last + case group[0] + when 'problem' then @problem_name = group[1] + when ':domain' then raise 'Different domain specified in problem file' if @domain_name != group[1] when ':objects' parse_objects(group) # Expand foralls @foralls.each {|pos,neg,(_,(fv,_,fvtype),g),mutable| @state[fvtype]&.each {|obj,| g.each {|pre| - pre.first != NOT ? pos << pre.map {|j| j == fv ? obj : j} : pre.size == 2 ? neg << pre = pre.last.map {|j| j == fv ? obj : j} : raise('Unexpected not in forall') - @predicates[pre.first.freeze] ||= mutable + pre[0] != NOT ? pos << pre.map {|j| j == fv ? obj : j} : pre.size == 2 ? neg << pre = pre[1].map {|j| j == fv ? obj : j} : raise('Unexpected not in forall') + @predicates[pre[0].freeze] ||= mutable } } } @@ -274,23 +274,23 @@ def parse_problem(problem_filename) when ':goal' if group = group[1] # Conjunction or atom - group.first == AND ? group.shift : group = [group] + group[0] == AND ? group.shift : group = [group] group.each {|pre| - pre.first != NOT ? @goal_pos << pre : pre.size == 2 ? @goal_not << pre = pre.last : raise('Unexpected not in goal') - @predicates[pre.first.freeze] ||= false + pre[0] != NOT ? @goal_pos << pre : pre.size == 2 ? @goal_not << pre = pre[1] : raise('Unexpected not in goal') + @predicates[pre[0].freeze] ||= false } @methods.map! {|name,param,*decompositions| decompositions.sort_by! {|d| d[4].assoc(name) ? 0 : 1}.unshift(name, param)} end when ':htn' group.shift # TODO loop group elements to improve support - if group.first == ':parameters' + if group[0] == ':parameters' group.shift - group.first.empty? ? group.shift : parameters = group.shift + group[0].empty? ? group.shift : parameters = group.shift end if (g = group.shift) == ':subtasks' or g == ':tasks' or g == ':ordered-subtasks' or g == ':ordered-tasks' @tasks = group.shift - @tasks.first == AND ? @tasks.shift : @tasks = [@tasks] + @tasks[0] == AND ? @tasks.shift : @tasks = [@tasks] # Ordering parse_ordering('problem', group.shift, @tasks) if group.shift == ':ordering' @tasks.map! {|t| t[1].instance_of?(Array) ? t[1] : t} @@ -312,7 +312,7 @@ def parse_problem(problem_filename) elsif not @tasks.empty? then @tasks.unshift(true) end end - else raise "#{group.first} is not recognized in problem" + else raise "#{group[0]} is not recognized in problem" end end else raise "File #{problem_filename} does not match problem pattern" diff --git a/parsers/JSHOP_Parser.rb b/parsers/JSHOP_Parser.rb index 7794f92..e654eac 100644 --- a/parsers/JSHOP_Parser.rb +++ b/parsers/JSHOP_Parser.rb @@ -27,7 +27,7 @@ def scan_tokens(filename) } raise 'Missing close parentheses' unless stack.empty? raise 'Malformed expression' if list.size != 1 - list.first + list[0] end #----------------------------------------------- @@ -52,8 +52,8 @@ def parse_operator(op) # Preconditions raise "Error with #{name} precondition" unless (group = op[2]).instance_of?(Array) group.each {|pre| - pre.first != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre.last : raise("Error with #{name} negative precondition") - @predicates[pre.first.freeze] ||= false + pre[0] != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre[1] : raise("Error with #{name} negative precondition") + @predicates[pre[0].freeze] ||= false } # Effects define_effects(name, op[3]) @@ -73,7 +73,7 @@ def parse_method(met) end until met.empty? # Optional label, add index for the unlabeled decompositions - if met.first.instance_of?(String) + if met[0].instance_of?(String) label = met.shift raise "#{name} redefined #{label} decomposition" if method.drop(2).assoc(label) else label = "case_#{method.size - 2}" @@ -82,8 +82,8 @@ def parse_method(met) raise "Error with #{name} precondition" unless (group = met.shift).instance_of?(Array) method << [label, free_variables = [], pos = [], neg = [], subtasks = met.shift] group.each {|pre| - pre.first != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre.last : raise("Error with #{name} negative precondition") - @predicates[pre.first.freeze] ||= false + pre[0] != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre[1] : raise("Error with #{name} negative precondition") + @predicates[pre[0].freeze] ||= false pre.each {|i| free_variables << i if i.start_with?('?') and not method[1].include?(i)} } free_variables.uniq! @@ -105,10 +105,10 @@ def parse_domain(domain_filename) @predicates = {} tokens = tokens[2] while group = tokens.shift - case group.first + case group[0] when ':operator' then parse_operator(group) when ':method' then parse_method(group) - else raise "#{group.first} is not recognized in domain" + else raise "#{group[0]} is not recognized in domain" end end else raise "File #{domain_filename} does not match domain pattern" @@ -127,7 +127,7 @@ def parse_problem(problem_filename) tokens[3].each {|pre| (@state[pre.shift.freeze] ||= []) << pre} @tasks = tokens[4] # Tasks may be ordered or unordered - @tasks.shift unless ordered = (@tasks.first != ':unordered') + @tasks.shift unless ordered = (@tasks[0] != ':unordered') @tasks.each {|pre,| pre.sub!(/^!!/,'invisible_') or pre.delete_prefix!('!')}.unshift(ordered) unless @tasks.empty? @goal_pos = [] @goal_not = [] diff --git a/parsers/PDDL_Parser.rb b/parsers/PDDL_Parser.rb index aed141f..2e52b74 100644 --- a/parsers/PDDL_Parser.rb +++ b/parsers/PDDL_Parser.rb @@ -30,7 +30,7 @@ def scan_tokens(filename) } raise 'Missing close parentheses' unless stack.empty? raise 'Malformed expression' if list.size != 1 - list.first + list[0] end #----------------------------------------------- @@ -61,24 +61,24 @@ def parse_action(op) raise "Error with #{name} precondition" unless (group = op.shift).instance_of?(Array) unless group.empty? # Conjunction or atom - group.first == AND ? group.shift : group = [group] + group[0] == AND ? group.shift : group = [group] group.each {|pre| raise "Error with #{name} precondition" unless pre.instance_of?(Array) - pre.first != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre.last : raise("Unexpected not in #{name} precondition") + pre[0] != NOT ? pos << pre : pre.size == 2 ? neg << pre = pre[1] : raise("Unexpected not in #{name} precondition") pre.map! {|i| free_variables.find {|j| j == i} || i} - @predicates[pre.first.freeze] ||= false + @predicates[pre[0].freeze] ||= false } end when ':effect' raise "Error with #{name} effect" unless (group = op.shift).instance_of?(Array) unless group.empty? # Conjunction or atom - group.first == AND ? group.shift : group = [group] + group[0] == AND ? group.shift : group = [group] group.each {|pre| raise "Error with #{name} effect" unless pre.instance_of?(Array) - pre.first != NOT ? add << pre : pre.size == 2 ? del << pre = pre.last : raise("Unexpected not in #{name} effect") + pre[0] != NOT ? add << pre : pre.size == 2 ? del << pre = pre[1] : raise("Unexpected not in #{name} effect") pre.map! {|i| free_variables.find {|j| j == i} || i} - @predicates[pre.first.freeze] = true + @predicates[pre[0].freeze] = true } end else raise "#{group} is not recognized in action" @@ -99,9 +99,9 @@ def parse_domain(domain_filename) @types = [] @requirements = [] while group = tokens.shift - case group.first + case group[0] when ':action' then parse_action(group) - when 'domain' then @domain_name = group.last + when 'domain' then @domain_name = group[1] when ':requirements' then (@requirements = group).shift when ':predicates' when ':types' @@ -113,7 +113,7 @@ def parse_domain(domain_filename) @types << [group[j], type] while (j += 1) != i group.shift(i+2) end - else raise "#{group.first} is not recognized in domain" + else raise "#{group[0]} is not recognized in domain" end end else raise "File #{domain_filename} does not match domain pattern" @@ -133,9 +133,9 @@ def parse_problem(problem_filename) @goal_not = [] @tasks = [] while group = tokens.shift - case group.first - when 'problem' then @problem_name = group.last - when ':domain' then raise 'Different domain specified in problem file' if @domain_name != group.last + case group[0] + when 'problem' then @problem_name = group[1] + when ':domain' then raise 'Different domain specified in problem file' if @domain_name != group[1] when ':objects' # Move types to initial state group.shift @@ -144,7 +144,7 @@ def parse_problem(problem_filename) group.shift types = [type = group.shift] while type = @types.assoc(type) - raise 'Circular typing' if types.include?(type = type.last) + raise 'Circular typing' if types.include?(type = type[1]) types << type end types.each {|t| (@state[t] ||= []).concat(o.zip)} @@ -157,13 +157,13 @@ def parse_problem(problem_filename) when ':goal' if group = group[1] # Conjunction or atom - group.first == AND ? group.shift : group = [group] + group[0] == AND ? group.shift : group = [group] group.each {|pre| - pre.first != NOT ? @goal_pos << pre : pre.size == 2 ? @goal_not << pre = pre.last : raise('Unexpected not in goal') - @predicates[pre.first.freeze] ||= false + pre[0] != NOT ? @goal_pos << pre : pre.size == 2 ? @goal_not << pre = pre[1] : raise('Unexpected not in goal') + @predicates[pre[0].freeze] ||= false } end - else raise "#{group.first} is not recognized in problem" + else raise "#{group[0]} is not recognized in problem" end end else raise "File #{problem_filename} does not match problem pattern" diff --git a/tests/dependent.rb b/tests/dependent.rb index b8bc34f..0779486 100644 --- a/tests/dependent.rb +++ b/tests/dependent.rb @@ -550,7 +550,7 @@ def unify_a_x_before_dependency_buy_before_give_for_happy_a_x(_b) [:invisible_goal] ], # Debug - ARGV.first == 'debug', + ARGV[0] == 'debug', # Ordered false )"