BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
BT::TreeNode Class Referenceabstract

Abstract base class for Behavior Tree Nodes. More...

#include <tree_node.h>

Inheritance diagram for BT::TreeNode:

Public Types

typedef std::shared_ptr< TreeNodePtr
 
using StatusChangeSignal = Signal< TimePoint, const TreeNode &, NodeStatus, NodeStatus >
 
using StatusChangeSubscriber = StatusChangeSignal::Subscriber
 
using StatusChangeCallback = StatusChangeSignal::CallableFunction
 
using PreTickCallback = std::function< NodeStatus(TreeNode &)>
 
using PostTickCallback = std::function< NodeStatus(TreeNode &, NodeStatus)>
 
using TickMonitorCallback = std::function< void(TreeNode &, NodeStatus, std::chrono::microseconds)>
 

Public Member Functions

 TreeNode (std::string name, NodeConfig config)
 TreeNode main constructor.
 
 TreeNode (const TreeNode &other)=delete
 
TreeNodeoperator= (const TreeNode &other)=delete
 
 TreeNode (TreeNode &&other) noexcept
 
TreeNodeoperator= (TreeNode &&other) noexcept
 
virtual BT::NodeStatus executeTick ()
 The method that should be used to invoke tick() and setStatus();.
 
void haltNode ()
 
bool isHalted () const
 
NodeStatus status () const
 
const std::string & name () const
 Name of the instance, not the type.
 
BT::NodeStatus waitValidStatus ()
 
virtual NodeType type () const =0
 
StatusChangeSubscriber subscribeToStatusChange (StatusChangeCallback callback)
 subscribeToStatusChange is used to attach a callback to a status change. When StatusChangeSubscriber goes out of scope (it is a shared_ptr) the callback is unsubscribed automatically.
 
void setPreTickFunction (PreTickCallback callback)
 
void setPostTickFunction (PostTickCallback callback)
 
void setTickMonitorCallback (TickMonitorCallback callback)
 
uint16_t UID () const
 
const std::string & fullPath () const
 
const std::string & registrationName () const
 registrationName is the ID used by BehaviorTreeFactory to create an instance.
 
const NodeConfigconfig () const
 
template<typename T >
Result getInput (const std::string &key, T &destination) const
 
template<typename T >
Expected< TimestampgetInputStamped (const std::string &key, T &destination) const
 getInputStamped is similar to getInput(dey, destination), but it returns also the Timestamp object, that can be used to check if a value was updated and when.
 
template<typename T >
Expected< T > getInput (const std::string &key) const
 
template<typename T >
Expected< StampedValue< T > > getInputStamped (const std::string &key) const
 
template<typename T >
Result setOutput (const std::string &key, const T &value)
 setOutput modifies the content of an Output port
 
AnyPtrLocked getLockedPortContent (const std::string &key)
 getLockedPortContent should be used when:
 
StringView getRawPortValue (const std::string &key) const
 
void emitWakeUpSignal ()
 Notify that the tree should be ticked again()
 
bool requiresWakeUp () const
 

Static Public Member Functions

static bool isBlackboardPointer (StringView str, StringView *stripped_pointer=nullptr)
 Check a string and return true if it matches the pattern: {...}.
 
static StringView stripBlackboardPointer (StringView str)
 
static Expected< StringView > getRemappedKey (StringView port_name, StringView remapped_port)
 
template<class DerivedT , typename... ExtraArgs>
static std::unique_ptr< TreeNodeInstantiate (const std::string &name, const NodeConfig &config, ExtraArgs... args)
 

Protected Types

using PreScripts = std::array< ScriptFunction, size_t(PreCond::COUNT_)>
 
using PostScripts = std::array< ScriptFunction, size_t(PostCond::COUNT_)>
 

Protected Member Functions

NodeConfigconfig ()
 
virtual BT::NodeStatus tick ()=0
 Method to be implemented by the user.
 
void resetStatus ()
 Set the status to IDLE.
 
void setRegistrationID (StringView ID)
 
void setWakeUpInstance (std::shared_ptr< WakeUpSignal > instance)
 
void modifyPortsRemapping (const PortsRemapping &new_remapping)
 
void setStatus (NodeStatus new_status)
 setStatus changes the status of the node. it will throw if you try to change the status to IDLE, because your parent node should do that, not the user!
 
PreScripts & preConditionsScripts ()
 
PostScripts & postConditionsScripts ()
 
template<typename T >
parseString (const std::string &str) const
 

Friends

class BehaviorTreeFactory
 
class DecoratorNode
 
class ControlNode
 
class Tree
 

Detailed Description

Abstract base class for Behavior Tree Nodes.

Constructor & Destructor Documentation

◆ TreeNode()

BT::TreeNode::TreeNode ( std::string  name,
NodeConfig  config 
)

TreeNode main constructor.

Parameters
namename of the instance, not the type.
configinformation about input/output ports. See NodeConfig

Note: If your custom node has ports, the derived class must implement:

static PortsList providedPorts();

Member Function Documentation

◆ config()

const NodeConfig & BT::TreeNode::config ( ) const

Configuration passed at construction time. Can never change after the creation of the TreeNode instance.

◆ executeTick()

virtual BT::NodeStatus BT::TreeNode::executeTick ( )
virtual

The method that should be used to invoke tick() and setStatus();.

Reimplemented in BT::SyncActionNode, BT::DecoratorNode, BT::ThreadedAction, and BT::CoroActionNode.

◆ fullPath()

const std::string & BT::TreeNode::fullPath ( ) const

Human readable identifier, that includes the hierarchy of Subtrees See tutorial 10 as an example.

◆ getInput() [1/2]

template<typename T >
Expected< T > BT::TreeNode::getInput ( const std::string &  key) const
inline

Same as bool getInput(const std::string& key, T& destination) but using optional.

Parameters
keythe name of the port.

◆ getInput() [2/2]

template<typename T >
Result BT::TreeNode::getInput ( const std::string &  key,
T &  destination 
) const
inline

Read an input port, which, in practice, is an entry in the blackboard. If the blackboard contains a std::string and T is not a string, convertFromString<T>() is used automatically to parse the text.

Parameters
keythe name of the port.
destinationreference to the object where the value should be stored
Returns
false if an error occurs.

◆ getInputStamped() [1/2]

template<typename T >
Expected< StampedValue< T > > BT::TreeNode::getInputStamped ( const std::string &  key) const
inline

Same as bool getInputStamped(const std::string& key, T& destination) but return StampedValue<T>

Parameters
keythe name of the port.

◆ getInputStamped() [2/2]

template<typename T >
Expected< Timestamp > BT::TreeNode::getInputStamped ( const std::string &  key,
T &  destination 
) const
inline

getInputStamped is similar to getInput(dey, destination), but it returns also the Timestamp object, that can be used to check if a value was updated and when.

Parameters
keythe name of the port.
destinationreference to the object where the value should be stored

◆ getLockedPortContent()

AnyPtrLocked BT::TreeNode::getLockedPortContent ( const std::string &  key)

getLockedPortContent should be used when:

  • your port contains an object with reference semantic (usually a smart pointer)
  • you want to modify the object we are pointing to.
  • you are concerned about thread-safety.

For example, if your port has type std::shared_ptr<Foo>, the code below is NOT thread safe:

auto foo_ptr = getInput<std::shared_ptr<Foo>>("port_name"); // modifying the content of foo_ptr is NOT thread-safe

What you must do, instead, to guaranty thread-safety, is:

if(auto any_ref = getLockedPortContent("port_name")) { Any* any = any_ref.get(); auto foo_ptr = any->cast<std::shared_ptr<Foo>>(); // modifying the content of foo_ptr inside this scope IS thread-safe }

It is important to destroy the object AnyPtrLocked, to release the lock.

NOTE: this method doesn't work, if the port contains a static string, instead of a blackboard pointer.

Parameters
keythe identifier of the port.
Returns
empty AnyPtrLocked if the blackboard entry doesn't exist or the content of the port was a static string.

◆ Instantiate()

template<class DerivedT , typename... ExtraArgs>
static std::unique_ptr< TreeNode > BT::TreeNode::Instantiate ( const std::string &  name,
const NodeConfig config,
ExtraArgs...  args 
)
inlinestatic

Used to inject config into a node, even if it doesn't have the proper constructor

◆ setOutput()

template<typename T >
Result BT::TreeNode::setOutput ( const std::string &  key,
const T &  value 
)
inline

setOutput modifies the content of an Output port

Parameters
keythe name of the port.
valuenew value
Returns
valid Result, if successful.

◆ setPostTickFunction()

void BT::TreeNode::setPostTickFunction ( PostTickCallback  callback)

This method attaches to the TreeNode a callback with signature:

NodeStatus myCallback(TreeNode& node, NodeStatus status)

This callback is executed AFTER the tick() and, if it returns SUCCESS or FAILURE, the value returned by the actual tick() is overridden with this one.

◆ setPreTickFunction()

void BT::TreeNode::setPreTickFunction ( PreTickCallback  callback)

This method attaches to the TreeNode a callback with signature:

 NodeStatus callback(TreeNode& node)

This callback is executed BEFORE the tick() and, if it returns SUCCESS or FAILURE, the actual tick() will NOT be executed and this result will be returned instead.

This is useful to inject a "dummy" implementation of the TreeNode at run-time

◆ setTickMonitorCallback()

void BT::TreeNode::setTickMonitorCallback ( TickMonitorCallback  callback)

This method attaches to the TreeNode a callback with signature:

void myCallback(TreeNode& node, NodeStatus status, std::chrono::microseconds duration)

This callback is executed AFTER the tick() and will inform the user about its status and the execution time. Works only if the tick was not substituted by a pre-condition.

◆ subscribeToStatusChange()

StatusChangeSubscriber BT::TreeNode::subscribeToStatusChange ( StatusChangeCallback  callback)

subscribeToStatusChange is used to attach a callback to a status change. When StatusChangeSubscriber goes out of scope (it is a shared_ptr) the callback is unsubscribed automatically.

Parameters
callbackThe callback to be execute when status change.
Returns
the subscriber handle.

◆ tick()

◆ UID()

uint16_t BT::TreeNode::UID ( ) const

The unique identifier of this instance of treeNode. It is assigneld by the factory

◆ waitValidStatus()

BT::NodeStatus BT::TreeNode::waitValidStatus ( )

Blocking function that will sleep until the setStatus() is called with either RUNNING, FAILURE or SUCCESS.


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