Class: BehaviorTree::Builder

Inherits:
Object
  • Object
show all
Extended by:
Dsl::InitialConfig, Dsl::Randomizer, Dsl::Registration, Dsl::SpellChecker, Dsl::Utils
Defined in:
lib/behavior_tree/builder.rb

Overview

DSL for building a tree.

Class Method Summary collapse

Methods included from Dsl::Registration

register, register_alias

Methods included from Dsl::Randomizer

build_random_tree

Methods included from Dsl::InitialConfig

dsl_config, initial_config

Class Method Details

.build(&block) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/behavior_tree/builder.rb', line 20

def build(&block)
  # Stack of lists. When a method like 'sequence' is executed, the resulting
  # sequence object will be stored in the last list. Then, the whole list will
  # be retrieved as the node children.
  @stack = []

  stack_children_from_block(block)
  tree_main_nodes = @stack.pop

  raise DSLStandardError, 'Tree main node should be a single node' if tree_main_nodes.count > 1

  raise 'Tree structure is incorrect. Probably a problem with the library.' unless @stack.empty?

  BehaviorTree::Tree.new tree_main_nodes.first
end