ForEach Class
A class for creating a behavior tree decorator node to iterate through a vector of items. More...
Declaration
Included Headers
Base class
| class | BT::DecoratorNode |
Public Constructors Index
| ForEach (const std::string &name, const BT::NodeConfiguration &config) | |
Public Destructor Index
| ~ForEach () override=default | |
Private Member Functions Index
| BT::NodeStatus | tick () override |
|
Implementation of tick() for ForEach behavior. This implements the logic of iterating through the input vector and ticking the child node for each iteration. More... | |
| void | halt () override |
Private Member Attributes Index
| std::size_t | current_index_ { 0 } |
|
Tracks the current index into the input vector. Initialized to be 0. More... | |
Public Static Functions Index
| static BT::PortsList | providedPorts () |
|
Required implementation of the static providedPorts function. More... | |
| static BT::KeyValueVector | metadata () |
Description
A class for creating a behavior tree decorator node to iterate through a vector of items.
- Given an instance of std::vector<BT::Any> which is provided on an input data port and contains N items, set the value of an output data port to the item in the vector which is at index 0 and then tick the ForEach decorator's child node until the child returns SUCCESS or FAILURE.
- If the child returns SUCCESS, set the value of the output data port to the item at index 1 and repeat ticking the child node.
- If the child returns FAILURE, stop iterating through the vector or ticking the child and return FAILURE.
- Repeat until we have iterated through all elements of the input vector, and finally return SUCCESS.
For each element set in the output, the index of the element in the input vector is also set in the index output data port. Therefore this Behavior outputs pairs [index, out] for each element in the input vector, containing the element index (starting at 0) and its value.
NOTE: The output values are only expected to be used by the child node or subtree of the ForEach node which sets it. While it is possible to access these ports from elsewhere in the behavior tree, this may result in unexpected behavior so it is not recommended.
| Data Port Name | Port Type | Object Type |
|---|---|---|
| vector_in | input | std::vector<BT::Any> |
| index | output | int |
| out | output | BT::Any |
Definition at line 44 of file for_each.hpp.
Public Constructors
ForEach()
|
Declaration at line 47 of file for_each.hpp, definition at line 34 of file for_each.cpp.
Public Destructor
~ForEach()
| default |
Definition at line 49 of file for_each.hpp.
Private Member Functions
halt()
|
Implementation of halt() for ForEach behaviors.
Sets current_index_ to 0, then calls DecoratorNode::halt().
Declaration at line 93 of file for_each.hpp, definition at line 107 of file for_each.cpp.
tick()
|
Implementation of tick() for ForEach behavior. This implements the logic of iterating through the input vector and ticking the child node for each iteration.
The first time tick() is called when the ForEach node is IDLE, set the status to RUNNING.
For every tick, get the value of the input vector from the input data port, and then compare current_index_ to the size of the input vector.
If current_index_ is less than the size of the input vector, set the value of the output data port to the value of the vector at that index, tick the child node, and then evaluate the child node's status:
- If the child node returns SUCCESS, increment current_index_ and return RUNNING.
- If the child node returns RUNNING, return RUNNING.
- If the child node returns FAILURE, set current_index_ to 0 and return FAILURE.
If current_index_ is equal to the size of the input vector (or greater than it, but that is not expected to happen unless the value in the data port is changed while ForEach is still RUNNING), then set current_index_ to 0 and return SUCCESS since we've successfully visited every element in the input vector.
NOTE: when the child node returns SUCCESS after ForEach has visited the last index of the input vector, then ForEach will return SUCCESS on the next tick.
- Exceptions
-
BT::LogicError if the child node returns IDLE, since this is an internal BehaviorTree.CPP error condition. This follows the pattern of how other BehaviorTree.CPP Decorator nodes are implemented.
- Returns
Returns BT::NodeStatus::FAILURE if the input vector could not be retrieved from the input data port. Returns BT::NodeStatus::FAILURE if the child node returns FAILURE. Returns BT::NodeStatus::SUCCESS after iterating through all elements of the input vector. Otherwise, returns BT::NodeStatus::RUNNING while still in-progress.
Declaration at line 87 of file for_each.hpp, definition at line 48 of file for_each.cpp.
Private Member Attributes
current_index_
|
Tracks the current index into the input vector. Initialized to be 0.
Definition at line 96 of file for_each.hpp.
Public Static Functions
metadata()
| static |
Declaration at line 57 of file for_each.hpp, definition at line 43 of file for_each.cpp.
providedPorts()
| static |
Required implementation of the static providedPorts function.
- Returns
The list of input and output ports used by this behavior.
Declaration at line 55 of file for_each.hpp, definition at line 38 of file for_each.cpp.
The documentation for this class was generated from the following files:
Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.9.8.