Class: BehaviorTree::Builder

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

Overview

DSL for building a tree.

Class Method Summary collapse

Methods included from Dsl::InitialConfig

dsl_config, initial_config

Methods included from Dsl::SpellChecker

most_similar_name, raise_node_type_not_exists

Class Method Details

.build(&block) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/behavior_tree/builder.rb', line 14

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

.register(node_name, class_name, children: :none) ⇒ Object

Don’t validate class_name, because in some situations the user wants it to be evaluated in runtime.



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/behavior_tree/builder.rb', line 32

def register(node_name, class_name, children: :none)
  valid_children_values = i[none single multiple]
  raise "Children value must be in: #{valid_children_values}" unless valid_children_values.include?(children)

  node_name = node_name.to_sym
  raise RegisterDSLNodeAlreadyExistsError, node_name if @node_type_mapping.key?(node_name)

  @node_type_mapping[node_name] = {
    class:    class_name,
    children: children
  }
end

.register_alias(original, alias_key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/behavior_tree/builder.rb', line 45

def register_alias(original, alias_key)
  unless @node_type_mapping.key?(original)
    raise "Cannot register alias for '#{original}', since it doesn't exist."
  end
  raise RegisterDSLNodeAlreadyExistsError, alias_key if @node_type_mapping.key?(alias_key)
  raise 'Alias key cannot be empty' if alias_key.to_s.empty?

  @node_type_mapping[original][:alias] = alias_key
  @node_type_mapping[alias_key] = @node_type_mapping[original].dup
  @node_type_mapping[alias_key][:alias] = original
end