behavior_tree.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2018-2019 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 nulltr");
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 nulltr");
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  applyRecursiveVisitor(decorator->child(), visitor);
60  }
61 }
62 
63 void printTreeRecursively(const TreeNode* root_node)
64 {
65  std::function<void(unsigned, const BT::TreeNode*)> recursivePrint;
66 
67  recursivePrint = [&recursivePrint](unsigned indent, const BT::TreeNode* node) {
68  for (unsigned i = 0; i < indent; i++)
69  {
70  std::cout << " ";
71  }
72  if (!node)
73  {
74  std::cout << "!nullptr!" << std::endl;
75  return;
76  }
77  std::cout << node->name() << std::endl;
78  indent++;
79 
80  if (auto control = dynamic_cast<const BT::ControlNode*>(node))
81  {
82  for (const auto& child : control->children())
83  {
84  recursivePrint(indent, child);
85  }
86  }
87  else if (auto decorator = dynamic_cast<const BT::DecoratorNode*>(node))
88  {
89  recursivePrint(indent, decorator->child());
90  }
91  };
92 
93  std::cout << "----------------" << std::endl;
94  recursivePrint(0, root_node);
95  std::cout << "----------------" << std::endl;
96 }
97 
98 void buildSerializedStatusSnapshot(TreeNode* root_node, SerializedTreeStatus& serialized_buffer)
99 {
100  serialized_buffer.clear();
101 
102  auto visitor = [&serialized_buffer](const TreeNode* node) {
103  serialized_buffer.push_back(
104  std::make_pair(node->UID(), static_cast<uint8_t>(node->status())));
105  };
106 
107  applyRecursiveVisitor(root_node, visitor);
108 }
109 
110 void haltAllActions(TreeNode* root_node)
111 {
112  auto visitor = [](TreeNode* node) {
113  if (auto action = dynamic_cast<AsyncActionNode*>(node))
114  {
115  action->stopAndJoinThread();
116  }
117  };
118  applyRecursiveVisitor(root_node, visitor);
119 }
120 
121 } // end namespace
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
void printTreeRecursively(const TreeNode *root_node)
std::vector< std::pair< uint16_t, uint8_t > > SerializedTreeStatus
Definition: behavior_tree.h:61
uint16_t UID() const
Definition: tree_node.cpp:89
void haltAllActions(TreeNode *root_node)
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)


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:04