Ember
Loading...
Searching...
No Matches
EmberCore::BehaviorTree Class Reference

Represents a complete behavior tree data structure. More...

#include <BehaviorTree.h>

Classes

struct  ValidationResult
 Tree validation results. More...

Public Types

enum class  ExecutionState {
  Idle , Running , Paused , Completed ,
  Failed
}
 Tree execution states. More...
using NodeChangeCallback = std::function<void(Node *, const EmberCore::String &change_type)>
using TreeChangeCallback = std::function<void(const EmberCore::String &change_type)>

Public Member Functions

 BehaviorTree (const EmberCore::String &name="Behavior Tree")
 Constructor.
 ~BehaviorTree ()
 Destructor.
void SetRootNode (std::unique_ptr< Node > root)
void SetRootNode (std::shared_ptr< Node > root)
NodeGetRootNode () const
std::shared_ptr< NodeGetRootNodeShared () const
bool HasRootNode () const
const EmberCore::StringGetName () const
void SetName (const EmberCore::String &name)
const EmberCore::StringGetDescription () const
void SetDescription (const EmberCore::String &description)
size_t GetNodeCount () const
size_t GetMaxDepth () const
std::vector< Node * > GetAllNodes ()
std::vector< const Node * > GetAllNodes () const
NodeFindNodeById (size_t id) const
NodeFindNodeByName (const EmberCore::String &name) const
std::vector< Node * > FindNodesByType (Node::Type type) const
bool AddNode (std::unique_ptr< Node > node, Node *parent=nullptr)
bool RemoveNode (size_t node_id)
bool MoveNode (size_t node_id, Node *new_parent)
std::unique_ptr< NodeExtractNode (size_t node_id)
ValidationResult Validate () const
bool IsValid () const
bool HasCycles () const
ExecutionState GetExecutionState () const
void SetExecutionState (ExecutionState state)
void Clear ()
std::unique_ptr< BehaviorTreeClone () const
void Reset ()
void AddBlackboard (std::unique_ptr< Blackboard > blackboard)
BlackboardGetBlackboard (const EmberCore::String &id)
const BlackboardGetBlackboard (const EmberCore::String &id) const
bool HasBlackboard (const EmberCore::String &id) const
void RemoveBlackboard (const EmberCore::String &id)
const std::map< EmberCore::String, std::unique_ptr< Blackboard > > & GetBlackboards () const
size_t GetBlackboardCount () const
void ClearBlackboards ()
EmberCore::String Serialize () const
bool Deserialize (const EmberCore::String &data)
bool SaveToFile (const EmberCore::String &filename) const
bool LoadFromFile (const EmberCore::String &filename)
void TraverseNodes (std::function< void(Node *)> visitor)
void TraverseNodes (std::function< void(const Node *)> visitor) const
void TraversePreOrder (std::function< void(Node *)> visitor)
void TraversePostOrder (std::function< void(Node *)> visitor)
void SetNodeChangeCallback (NodeChangeCallback callback)
void SetTreeChangeCallback (TreeChangeCallback callback)
void PrintTree () const
EmberCore::String GetTreeStructure () const
std::unordered_map< Node::Type, size_t > GetNodeTypeStatistics () const
void SetMetadata (const EmberCore::String &key, const EmberCore::String &value)
EmberCore::String GetMetadata (const EmberCore::String &key) const
void RemoveMetadata (const EmberCore::String &key)
const std::unordered_map< EmberCore::String, EmberCore::String > & GetAllMetadata () const
XMLMetadataGetXMLMetadata ()
const XMLMetadataGetXMLMetadata () const

Protected Member Functions

void NotifyNodeChange (Node *node, const EmberCore::String &change_type)
void NotifyTreeChange (const EmberCore::String &change_type)
void UpdateNodeIndex ()
NodeFindNodeRecursive (Node *current, std::function< bool(const Node *)> predicate) const
void CollectNodesRecursive (Node *current, std::vector< Node * > &nodes)
void CollectNodesRecursive (const Node *current, std::vector< const Node * > &nodes) const

Private Member Functions

ValidationResult ValidateNode (const Node *node, std::vector< const Node * > &visited) const
void BuildNodeIndex (Node *node)

Private Attributes

std::unique_ptr< Noderoot_node_
std::shared_ptr< Noderoot_node_shared_
EmberCore::String name_
EmberCore::String description_
ExecutionState execution_state_
std::unordered_map< size_t, Node * > node_index_
std::unordered_map< EmberCore::String, EmberCore::Stringmetadata_
XMLMetadata xml_metadata_
std::map< EmberCore::String, std::unique_ptr< Blackboard > > blackboards_
NodeChangeCallback node_change_callback_
TreeChangeCallback tree_change_callback_

Detailed Description

Represents a complete behavior tree data structure.

This class manages the entire behavior tree as a data structure, separate from its visualization. It provides tree-level operations, validation, serialization, and manipulation capabilities.

Definition at line 54 of file BehaviorTree.h.

Member Typedef Documentation

◆ NodeChangeCallback

using EmberCore::BehaviorTree::NodeChangeCallback = std::function<void(Node *, const EmberCore::String &change_type)>

Definition at line 165 of file BehaviorTree.h.

◆ TreeChangeCallback

using EmberCore::BehaviorTree::TreeChangeCallback = std::function<void(const EmberCore::String &change_type)>

Definition at line 166 of file BehaviorTree.h.

Member Enumeration Documentation

◆ ExecutionState

Tree execution states.

Enumerator
Idle 
Running 
Paused 
Completed 
Failed 

Definition at line 59 of file BehaviorTree.h.

Constructor & Destructor Documentation

◆ BehaviorTree()

EmberCore::BehaviorTree::BehaviorTree ( const EmberCore::String & name = "Behavior Tree")
explicit

Constructor.

Parameters
nameTree name/identifier

Definition at line 10 of file BehaviorTree.cpp.

◆ ~BehaviorTree()

EmberCore::BehaviorTree::~BehaviorTree ( )

Destructor.

Definition at line 15 of file BehaviorTree.cpp.

Member Function Documentation

◆ AddBlackboard()

void EmberCore::BehaviorTree::AddBlackboard ( std::unique_ptr< Blackboard > blackboard)

Definition at line 496 of file BehaviorTree.cpp.

◆ AddNode()

bool EmberCore::BehaviorTree::AddNode ( std::unique_ptr< Node > node,
Node * parent = nullptr )

Definition at line 83 of file BehaviorTree.cpp.

◆ BuildNodeIndex()

void EmberCore::BehaviorTree::BuildNodeIndex ( Node * node)
private

Definition at line 456 of file BehaviorTree.cpp.

◆ Clear()

void EmberCore::BehaviorTree::Clear ( )

Definition at line 186 of file BehaviorTree.cpp.

◆ ClearBlackboards()

void EmberCore::BehaviorTree::ClearBlackboards ( )

Definition at line 534 of file BehaviorTree.cpp.

◆ Clone()

std::unique_ptr< BehaviorTree > EmberCore::BehaviorTree::Clone ( ) const

Definition at line 193 of file BehaviorTree.cpp.

◆ CollectNodesRecursive() [1/2]

void EmberCore::BehaviorTree::CollectNodesRecursive ( const Node * current,
std::vector< const Node * > & nodes ) const
protected

Definition at line 365 of file BehaviorTree.cpp.

◆ CollectNodesRecursive() [2/2]

void EmberCore::BehaviorTree::CollectNodesRecursive ( Node * current,
std::vector< Node * > & nodes )
protected

Definition at line 355 of file BehaviorTree.cpp.

◆ Deserialize()

bool EmberCore::BehaviorTree::Deserialize ( const EmberCore::String & data)

◆ ExtractNode()

std::unique_ptr< Node > EmberCore::BehaviorTree::ExtractNode ( size_t node_id)

◆ FindNodeById()

Node * EmberCore::BehaviorTree::FindNodeById ( size_t id) const

Definition at line 63 of file BehaviorTree.cpp.

◆ FindNodeByName()

Node * EmberCore::BehaviorTree::FindNodeByName ( const EmberCore::String & name) const

Definition at line 68 of file BehaviorTree.cpp.

◆ FindNodeRecursive()

Node * EmberCore::BehaviorTree::FindNodeRecursive ( Node * current,
std::function< bool(const Node *)> predicate ) const
protected

Definition at line 338 of file BehaviorTree.cpp.

◆ FindNodesByType()

std::vector< Node * > EmberCore::BehaviorTree::FindNodesByType ( Node::Type type) const

Definition at line 72 of file BehaviorTree.cpp.

◆ GetAllMetadata()

const std::unordered_map< EmberCore::String, EmberCore::String > & EmberCore::BehaviorTree::GetAllMetadata ( ) const
inline

Definition at line 180 of file BehaviorTree.h.

◆ GetAllNodes() [1/2]

std::vector< Node * > EmberCore::BehaviorTree::GetAllNodes ( )

Definition at line 45 of file BehaviorTree.cpp.

◆ GetAllNodes() [2/2]

std::vector< const Node * > EmberCore::BehaviorTree::GetAllNodes ( ) const

Definition at line 54 of file BehaviorTree.cpp.

◆ GetBlackboard() [1/2]

Blackboard * EmberCore::BehaviorTree::GetBlackboard ( const EmberCore::String & id)

Definition at line 512 of file BehaviorTree.cpp.

◆ GetBlackboard() [2/2]

const Blackboard * EmberCore::BehaviorTree::GetBlackboard ( const EmberCore::String & id) const

Definition at line 517 of file BehaviorTree.cpp.

◆ GetBlackboardCount()

size_t EmberCore::BehaviorTree::GetBlackboardCount ( ) const
inline

Definition at line 147 of file BehaviorTree.h.

◆ GetBlackboards()

const std::map< EmberCore::String, std::unique_ptr< Blackboard > > & EmberCore::BehaviorTree::GetBlackboards ( ) const
inline

Definition at line 146 of file BehaviorTree.h.

◆ GetDescription()

const EmberCore::String & EmberCore::BehaviorTree::GetDescription ( ) const
inline

Definition at line 106 of file BehaviorTree.h.

◆ GetExecutionState()

ExecutionState EmberCore::BehaviorTree::GetExecutionState ( ) const
inline

Definition at line 132 of file BehaviorTree.h.

◆ GetMaxDepth()

size_t EmberCore::BehaviorTree::GetMaxDepth ( ) const

Definition at line 38 of file BehaviorTree.cpp.

◆ GetMetadata()

EmberCore::String EmberCore::BehaviorTree::GetMetadata ( const EmberCore::String & key) const

Definition at line 309 of file BehaviorTree.cpp.

◆ GetName()

const EmberCore::String & EmberCore::BehaviorTree::GetName ( ) const
inline

Definition at line 103 of file BehaviorTree.h.

◆ GetNodeCount()

size_t EmberCore::BehaviorTree::GetNodeCount ( ) const

Definition at line 31 of file BehaviorTree.cpp.

◆ GetNodeTypeStatistics()

std::unordered_map< Node::Type, size_t > EmberCore::BehaviorTree::GetNodeTypeStatistics ( ) const

Definition at line 295 of file BehaviorTree.cpp.

◆ GetRootNode()

Node * EmberCore::BehaviorTree::GetRootNode ( ) const
inline

Definition at line 98 of file BehaviorTree.h.

◆ GetRootNodeShared()

std::shared_ptr< Node > EmberCore::BehaviorTree::GetRootNodeShared ( ) const
inline

Definition at line 99 of file BehaviorTree.h.

◆ GetTreeStructure()

EmberCore::String EmberCore::BehaviorTree::GetTreeStructure ( ) const

Definition at line 282 of file BehaviorTree.cpp.

◆ GetXMLMetadata() [1/2]

XMLMetadata & EmberCore::BehaviorTree::GetXMLMetadata ( )
inline

Definition at line 183 of file BehaviorTree.h.

◆ GetXMLMetadata() [2/2]

const XMLMetadata & EmberCore::BehaviorTree::GetXMLMetadata ( ) const
inline

Definition at line 184 of file BehaviorTree.h.

◆ HasBlackboard()

bool EmberCore::BehaviorTree::HasBlackboard ( const EmberCore::String & id) const

Definition at line 522 of file BehaviorTree.cpp.

◆ HasCycles()

bool EmberCore::BehaviorTree::HasCycles ( ) const

Definition at line 180 of file BehaviorTree.cpp.

◆ HasRootNode()

bool EmberCore::BehaviorTree::HasRootNode ( ) const
inline

Definition at line 100 of file BehaviorTree.h.

◆ IsValid()

bool EmberCore::BehaviorTree::IsValid ( ) const
inline

Definition at line 128 of file BehaviorTree.h.

◆ LoadFromFile()

bool EmberCore::BehaviorTree::LoadFromFile ( const EmberCore::String & filename)

◆ MoveNode()

bool EmberCore::BehaviorTree::MoveNode ( size_t node_id,
Node * new_parent )

Definition at line 143 of file BehaviorTree.cpp.

◆ NotifyNodeChange()

void EmberCore::BehaviorTree::NotifyNodeChange ( Node * node,
const EmberCore::String & change_type )
protected

Definition at line 319 of file BehaviorTree.cpp.

◆ NotifyTreeChange()

void EmberCore::BehaviorTree::NotifyTreeChange ( const EmberCore::String & change_type)
protected

Definition at line 325 of file BehaviorTree.cpp.

◆ PrintTree()

void EmberCore::BehaviorTree::PrintTree ( ) const

Definition at line 273 of file BehaviorTree.cpp.

◆ RemoveBlackboard()

void EmberCore::BehaviorTree::RemoveBlackboard ( const EmberCore::String & id)

Definition at line 526 of file BehaviorTree.cpp.

◆ RemoveMetadata()

void EmberCore::BehaviorTree::RemoveMetadata ( const EmberCore::String & key)

Definition at line 314 of file BehaviorTree.cpp.

◆ RemoveNode()

bool EmberCore::BehaviorTree::RemoveNode ( size_t node_id)

Definition at line 112 of file BehaviorTree.cpp.

◆ Reset()

void EmberCore::BehaviorTree::Reset ( )

Definition at line 207 of file BehaviorTree.cpp.

◆ SaveToFile()

bool EmberCore::BehaviorTree::SaveToFile ( const EmberCore::String & filename) const

◆ Serialize()

EmberCore::String EmberCore::BehaviorTree::Serialize ( ) const

◆ SetDescription()

void EmberCore::BehaviorTree::SetDescription ( const EmberCore::String & description)
inline

Definition at line 107 of file BehaviorTree.h.

◆ SetExecutionState()

void EmberCore::BehaviorTree::SetExecutionState ( ExecutionState state)
inline

Definition at line 133 of file BehaviorTree.h.

◆ SetMetadata()

void EmberCore::BehaviorTree::SetMetadata ( const EmberCore::String & key,
const EmberCore::String & value )

Definition at line 304 of file BehaviorTree.cpp.

◆ SetName()

void EmberCore::BehaviorTree::SetName ( const EmberCore::String & name)
inline

Definition at line 104 of file BehaviorTree.h.

◆ SetNodeChangeCallback()

void EmberCore::BehaviorTree::SetNodeChangeCallback ( NodeChangeCallback callback)
inline

Definition at line 168 of file BehaviorTree.h.

◆ SetRootNode() [1/2]

void EmberCore::BehaviorTree::SetRootNode ( std::shared_ptr< Node > root)

Definition at line 24 of file BehaviorTree.cpp.

◆ SetRootNode() [2/2]

void EmberCore::BehaviorTree::SetRootNode ( std::unique_ptr< Node > root)

Definition at line 17 of file BehaviorTree.cpp.

◆ SetTreeChangeCallback()

void EmberCore::BehaviorTree::SetTreeChangeCallback ( TreeChangeCallback callback)
inline

Definition at line 169 of file BehaviorTree.h.

◆ TraverseNodes() [1/2]

void EmberCore::BehaviorTree::TraverseNodes ( std::function< void(const Node *)> visitor) const

Definition at line 222 of file BehaviorTree.cpp.

◆ TraverseNodes() [2/2]

void EmberCore::BehaviorTree::TraverseNodes ( std::function< void(Node *)> visitor)

Definition at line 216 of file BehaviorTree.cpp.

◆ TraversePostOrder()

void EmberCore::BehaviorTree::TraversePostOrder ( std::function< void(Node *)> visitor)

Definition at line 250 of file BehaviorTree.cpp.

◆ TraversePreOrder()

void EmberCore::BehaviorTree::TraversePreOrder ( std::function< void(Node *)> visitor)

Definition at line 230 of file BehaviorTree.cpp.

◆ UpdateNodeIndex()

void EmberCore::BehaviorTree::UpdateNodeIndex ( )
protected

Definition at line 331 of file BehaviorTree.cpp.

◆ Validate()

BehaviorTree::ValidationResult EmberCore::BehaviorTree::Validate ( ) const

Definition at line 149 of file BehaviorTree.cpp.

◆ ValidateNode()

BehaviorTree::ValidationResult EmberCore::BehaviorTree::ValidateNode ( const Node * node,
std::vector< const Node * > & visited ) const
private

Definition at line 375 of file BehaviorTree.cpp.

Member Data Documentation

◆ blackboards_

std::map<EmberCore::String, std::unique_ptr<Blackboard> > EmberCore::BehaviorTree::blackboards_
private

Definition at line 213 of file BehaviorTree.h.

◆ description_

EmberCore::String EmberCore::BehaviorTree::description_
private

Definition at line 200 of file BehaviorTree.h.

◆ execution_state_

ExecutionState EmberCore::BehaviorTree::execution_state_
private

Definition at line 201 of file BehaviorTree.h.

◆ metadata_

std::unordered_map<EmberCore::String, EmberCore::String> EmberCore::BehaviorTree::metadata_
private

Definition at line 207 of file BehaviorTree.h.

◆ name_

EmberCore::String EmberCore::BehaviorTree::name_
private

Definition at line 199 of file BehaviorTree.h.

◆ node_change_callback_

NodeChangeCallback EmberCore::BehaviorTree::node_change_callback_
private

Definition at line 216 of file BehaviorTree.h.

◆ node_index_

std::unordered_map<size_t, Node *> EmberCore::BehaviorTree::node_index_
private

Definition at line 204 of file BehaviorTree.h.

◆ root_node_

std::unique_ptr<Node> EmberCore::BehaviorTree::root_node_
private

Definition at line 197 of file BehaviorTree.h.

◆ root_node_shared_

std::shared_ptr<Node> EmberCore::BehaviorTree::root_node_shared_
private

Definition at line 198 of file BehaviorTree.h.

◆ tree_change_callback_

TreeChangeCallback EmberCore::BehaviorTree::tree_change_callback_
private

Definition at line 217 of file BehaviorTree.h.

◆ xml_metadata_

XMLMetadata EmberCore::BehaviorTree::xml_metadata_
private

Definition at line 210 of file BehaviorTree.h.


The documentation for this class was generated from the following files: