From ffec81e4e8ec3308bca702337e653301775f67a2 Mon Sep 17 00:00:00 2001 From: Mau Magnaguagno Date: Sat, 24 Oct 2020 08:18:48 -0300 Subject: [PATCH] Rescue infinite recursion stack overflow in Hypertension planning There and back again. --- Hypertension.rb | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Hypertension.rb b/Hypertension.rb index 067b205..5597374 100644 --- a/Hypertension.rb +++ b/Hypertension.rb @@ -22,10 +22,13 @@ def planning(tasks, level = 0) when true, false puts "#{' ' * level}#{current_task.first}(#{current_task.drop(1).join(' ')})" if @debug old_state = @state - # Keep decomposing the hierarchy if operator applied - if send(*current_task) and plan = planning(tasks, level) - # Add visible operator to plan - return decomposition ? plan.unshift(current_task) : plan + begin + # Keep decomposing the hierarchy if operator applied + if send(*current_task) and plan = planning(tasks, level) + # Add visible operator to plan + return decomposition ? plan.unshift(current_task) : plan + end + rescue SystemStackError end @state = old_state # Method @@ -33,11 +36,14 @@ def planning(tasks, level = 0) # Keep decomposing the hierarchy task_name = current_task.shift level += 1 - decomposition.each {|method| - puts "#{' ' * level.pred}#{method}(#{current_task.join(' ')})" if @debug - # Every unification is tested - send(method, *current_task) {|subtasks| return plan if plan = planning(subtasks.concat(tasks), level)} - } + begin + decomposition.each {|method| + puts "#{' ' * level.pred}#{method}(#{current_task.join(' ')})" if @debug + # Every unification is tested + send(method, *current_task) {|subtasks| return plan if plan = planning(subtasks.concat(tasks), level)} + } + rescue SystemStackError + end current_task.unshift(task_name) # Error else raise "Domain defines no decomposition for #{current_task.first}"