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  applyRecursiveVisitor(decorator->child(), visitor);
61  }
62  }
63 }
64 
65 void printTreeRecursively(const TreeNode* root_node, std::ostream& stream)
66 {
67  std::function<void(unsigned, const BT::TreeNode*)> recursivePrint;
68 
69  recursivePrint = [&recursivePrint, &stream](unsigned indent, const BT::TreeNode* node) {
70  for (unsigned i = 0; i < indent; i++)
71  {
72  stream << " ";
73  }
74  if (!node)
75  {
76  stream << "!nullptr!" << std::endl;
77  return;
78  }
79  stream << node->name() << std::endl;
80  indent++;
81 
82  if (auto control = dynamic_cast<const BT::ControlNode*>(node))
83  {
84  for (const auto& child : control->children())
85  {
86  recursivePrint(indent, child);
87  }
88  }
89  else if (auto decorator = dynamic_cast<const BT::DecoratorNode*>(node))
90  {
91  recursivePrint(indent, decorator->child());
92  }
93  };
94 
95  stream << "----------------" << std::endl;
96  recursivePrint(0, root_node);
97  stream << "----------------" << std::endl;
98 }
99 
100 void buildSerializedStatusSnapshot(TreeNode* root_node, SerializedTreeStatus& serialized_buffer)
101 {
102  serialized_buffer.clear();
103 
104  auto visitor = [&serialized_buffer](const TreeNode* node) {
105  serialized_buffer.push_back(
106  std::make_pair(node->UID(), static_cast<uint8_t>(node->status())));
107  };
108 
109  applyRecursiveVisitor(root_node, visitor);
110 }
111 
112 } // end namespace
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
std::vector< std::pair< uint16_t, uint8_t > > SerializedTreeStatus
Definition: behavior_tree.h:64
uint16_t UID() const
Definition: tree_node.cpp:89
void printTreeRecursively(const TreeNode *root_node, std::ostream &stream=std::cout)
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:53
NodeStatus status() const
Definition: tree_node.cpp:56
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 ...
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:24