Skip to content

Commit

Permalink
Rescue infinite recursion stack overflow in Hypertension planning
Browse files Browse the repository at this point in the history
There and back again.
  • Loading branch information
Maumagnaguagno committed Oct 24, 2020
1 parent 57d9d3b commit ffec81e
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions Hypertension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,28 @@ 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
when Array
# 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}"
Expand Down

0 comments on commit ffec81e

Please sign in to comment.