module BehaviorTree::TreeStructure::Printer

Algorithm to print tree.

Public Instance Methods

print() click to toggle source

Private Instance Methods

bool_yes_no(bool) click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 53
def bool_yes_no(bool)
  bool ? 'yes' : 'no'
end
class_simple_name(node) click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 86
def class_simple_name(node)
  pretty_name snake_case(node.class.name.split('::').last)
end
cycle_string() click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 45
def cycle_string
  "Cycles: #{bool_yes_no(cycle?)}."
end
pretty_name(name) click to toggle source

Changes the name of some classes (maps it to a better name). Mapping is simply based on taste.

# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 92
def pretty_name(name)
  case name
  when 'task_base'
    'task'
  when 'force_success'
    'forcesuccess'
  when 'force_failure'
    'forcefailure'
  else
    name
  end
end
size_string() click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 41
def size_string
  "Tree has #{size - 1} nodes."
end
snake_case(str) click to toggle source

Copied from Rails' ActiveSupport.

# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 78
def snake_case(str)
  str.gsub(/::/, '/')
     .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
     .gsub(/([a-z\d])([A-Z])/, '\1_\2')
     .tr('-', '_')
     .downcase
end
status_string(node) click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 57
def status_string(node)
  if node.status.success?
    ColorizedString['success'].colorize(:blue)
  elsif node.status.running?
    ColorizedString['running'].colorize(:light_green)
  elsif node.status.failure?
    ColorizedString['failure'].colorize(:red)
  end
end
tick_count_string(node) click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 67
def tick_count_string(node)
  count = node.tick_count
  color = count.zero? ? :light_red : :light_black
  ColorizedString["(#{node.tick_count} ticks)"].colorize(color)
end
tree_lines() click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 24
def tree_lines
  # Store which depth values must continue to display a vertical line.
  vertical_lines_continues = Set.new

  each_node(:depth_preorder).map do |node, depth, _global_idx, parent_node|
    # Parent's last child?
    last_child = node == parent_node.children.last

    last_child ? vertical_lines_continues.delete(depth) : vertical_lines_continues << depth

    space = (0...depth).map { |d| vertical_lines_continues.include?(d) ? '│     ' : '      ' }.join
    connector = last_child ? '└─' : '├─'

    "#{space}#{connector}#{class_simple_name(node)} #{status_string(node)} #{tick_count_string(node)}"
  end
end
tree_tick_count_string() click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 73
def tree_tick_count_string
  "Tree has been ticked #{tick_count} times."
end
uniq_nodes_string() click to toggle source
# File lib/behavior_tree/concerns/tree_structure/printer.rb, line 49
def uniq_nodes_string
  "All nodes are unique object refs: #{bool_yes_no(uniq_nodes?)}."
end