behavior_tree.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2018 Davide Faconti - 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 std::runtime_error("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 std::runtime_error("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(int, 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 
111 {
112  auto visitor = [bb](TreeNode* node) { node->setBlackboard(bb); };
113  applyRecursiveVisitor(root_node, visitor);
114 }
115 
116 void haltAllActions(TreeNode* root_node)
117 {
118  auto visitor = [](TreeNode* node) {
119  if (auto action = dynamic_cast<AsyncActionNode*>(node))
120  {
121  action->stopAndJoinThread();
122  }
123  };
124  applyRecursiveVisitor(root_node, visitor);
125 }
126 }
const std::string & name() const
Definition: tree_node.cpp:92
void printTreeRecursively(const TreeNode *root_node)
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:40
std::vector< std::pair< uint16_t, uint8_t > > SerializedTreeStatus
Definition: behavior_tree.h:57
uint16_t UID() const
Definition: tree_node.cpp:108
void haltAllActions(TreeNode *root_node)
void assignBlackboardToEntireTree(TreeNode *root_node, const Blackboard::Ptr &bb)
NodeStatus status() const
Definition: tree_node.cpp:75
void buildSerializedStatusSnapshot(const TreeNode *root_node, SerializedTreeStatus &serialized_buffer)
buildSerializedStatusSnapshot can be used to create a serialize buffer that can be stored (or sent to...
void setBlackboard(const Blackboard::Ptr &bb)
Definition: tree_node.cpp:59
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 04:01:53