Class: BehaviorTree::Tree

Inherits:
SingleChildNodeBase
  • Object
show all
Includes:
BehaviorTree::TreeStructure::Algorithms, BehaviorTree::TreeStructure::Printer
Defined in:
lib/behavior_tree/tree.rb

Overview

Root node of the tree. This is the class that must be instantiated by the user.

Constant Summary collapse

CHILD_VALID_CLASSES =
[
  Decorators::DecoratorBase, ControlNodeBase, TaskBase
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BehaviorTree::TreeStructure::Printer

#print

Methods included from BehaviorTree::TreeStructure::Algorithms

#cycle?, #each_node, #repeated_nodes, #uniq_nodes?

Constructor Details

#initialize(child) ⇒ Tree

Returns a new instance of Tree.



15
16
17
18
19
20
21
22
23
24
# File 'lib/behavior_tree/tree.rb', line 15

def initialize(child)
  super(child) if child.nil? # Cannot be leaf, raise error.

  if CHILD_VALID_CLASSES.any? { |node_class| child.is_a?(NodeBase) && child.chainable_node.is_a?(node_class) }
    super(child)
    return
  end

  raise InvalidTreeMainNodeError, child.class
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/behavior_tree/tree.rb', line 9

def context
  @context
end

Instance Method Details

#chainable_nodeObject



26
27
28
# File 'lib/behavior_tree/tree.rb', line 26

def chainable_node
  @child
end

#ensure_after_tickObject



30
31
32
33
# File 'lib/behavior_tree/tree.rb', line 30

def ensure_after_tick
  # Copy the main node status to self.
  self.status = child.status
end