behavior_tree.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
14 #include <cstring>
15 
16 namespace BT
17 {
19  const std::function<void(const TreeNode*)>& visitor)
20 {
21  if(!node)
22  {
23  throw LogicError("One of the children of a DecoratorNode or ControlNode is nullptr");
24  }
25 
26  visitor(node);
27 
28  if(auto control = dynamic_cast<const BT::ControlNode*>(node))
29  {
30  for(const auto& child : control->children())
31  {
32  applyRecursiveVisitor(static_cast<const TreeNode*>(child), visitor);
33  }
34  }
35  else if(auto decorator = dynamic_cast<const BT::DecoratorNode*>(node))
36  {
37  applyRecursiveVisitor(decorator->child(), visitor);
38  }
39 }
40 
41 void applyRecursiveVisitor(TreeNode* node, const std::function<void(TreeNode*)>& visitor)
42 {
43  if(!node)
44  {
45  throw LogicError("One of the children of a DecoratorNode or ControlNode is nullptr");
46  }
47 
48  visitor(node);
49 
50  if(auto control = dynamic_cast<BT::ControlNode*>(node))
51  {
52  for(const auto& child : control->children())
53  {
54  applyRecursiveVisitor(child, visitor);
55  }
56  }
57  else if(auto decorator = dynamic_cast<BT::DecoratorNode*>(node))
58  {
59  if(decorator->child())
60  {
61  applyRecursiveVisitor(decorator->child(), visitor);
62  }
63  }
64 }
65 
66 void printTreeRecursively(const TreeNode* root_node, std::ostream& stream)
67 {
68  std::function<void(unsigned, const BT::TreeNode*)> recursivePrint;
69 
70  recursivePrint = [&recursivePrint, &stream](unsigned indent, const BT::TreeNode* node) {
71  for(unsigned i = 0; i < indent; i++)
72  {
73  stream << " ";
74  }
75  if(!node)
76  {
77  stream << "!nullptr!" << std::endl;
78  return;
79  }
80  stream << node->name() << std::endl;
81  indent++;
82 
83  if(auto control = dynamic_cast<const BT::ControlNode*>(node))
84  {
85  for(const auto& child : control->children())
86  {
87  recursivePrint(indent, child);
88  }
89  }
90  else if(auto decorator = dynamic_cast<const BT::DecoratorNode*>(node))
91  {
92  recursivePrint(indent, decorator->child());
93  }
94  };
95 
96  stream << "----------------" << std::endl;
97  recursivePrint(0, root_node);
98  stream << "----------------" << std::endl;
99 }
100 
102  SerializedTreeStatus& serialized_buffer)
103 {
104  serialized_buffer.clear();
105 
106  auto visitor = [&serialized_buffer](const TreeNode* node) {
107  serialized_buffer.push_back(
108  std::make_pair(node->UID(), static_cast<uint8_t>(node->status())));
109  };
110 
111  applyRecursiveVisitor(root_node, visitor);
112 }
113 
115 {
116  static int number = -1;
117  if(number == -1)
118  {
119  auto const parts = splitString(BTCPP_LIBRARY_VERSION, '.');
120  number = std::stoi(std::string(parts[0])) * 10000 +
121  std::stoi(std::string(parts[1])) * 100 + std::stoi(std::string(parts[2]));
122  }
123  return number;
124 }
125 
126 const char* LibraryVersionString()
127 {
128  return BTCPP_LIBRARY_VERSION;
129 }
130 
131 } // namespace BT
BT
Definition: ex01_wrap_legacy.cpp:29
BT::TreeNode::UID
uint16_t UID() const
Definition: tree_node.cpp:330
BT::LibraryVersionNumber
int LibraryVersionNumber()
Definition: behavior_tree.cpp:114
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
BT::DecoratorNode
Definition: decorator_node.h:8
BT::LogicError
Definition: exceptions.h:45
BT::SerializedTreeStatus
std::vector< std::pair< uint16_t, uint8_t > > SerializedTreeStatus
Definition: behavior_tree.h:72
detail::void
j template void())
Definition: json.hpp:4893
BT::TreeNode::status
NodeStatus status() const
Definition: tree_node.cpp:279
lexy_ext::child
auto child(const lexy::parse_tree< Reader, TokenKind, MemoryResource > &tree, typename lexy::parse_tree< Reader, TokenKind, MemoryResource >::node node, Predicate predicate) -> std::optional< typename lexy::parse_tree< Reader, TokenKind, MemoryResource >::node >
Returns the first child that matches predicate, if there is any.
Definition: parse_tree_algorithm.hpp:244
BT::splitString
std::vector< StringView > splitString(const StringView &strToSplit, char delimeter)
Definition: basic_types.cpp:348
behavior_tree.h
BT::applyRecursiveVisitor
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)
Definition: behavior_tree.cpp:18
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:296
BT::printTreeRecursively
void printTreeRecursively(const TreeNode *root_node, std::ostream &stream=std::cout)
Definition: behavior_tree.cpp:66
BT::LibraryVersionString
const char * LibraryVersionString()
Definition: behavior_tree.cpp:126
lexyd::ascii::control
constexpr auto control
Definition: ascii.hpp:42
BT::buildSerializedStatusSnapshot
void buildSerializedStatusSnapshot(const TreeNode *root_node, SerializedTreeStatus &serialized_buffer)
buildSerializedStatusSnapshot can be used to create a buffer that can be stored (or sent to a client ...
BT::ControlNode
Definition: control_node.h:21


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07