behavior_tree.cpp
Go to the documentation of this file.
00001 /*  Copyright (C) 2018 Davide Faconti -  All Rights Reserved
00002 *
00003 *   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
00004 *   to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00005 *   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:
00006 *   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00007 *
00008 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00009 *   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,
00010 *   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.
00011 */
00012 
00013 #include "behaviortree_cpp/behavior_tree.h"
00014 #include <cstring>
00015 
00016 namespace BT
00017 {
00018 void applyRecursiveVisitor(const TreeNode* node,
00019                            const std::function<void(const TreeNode*)>& visitor)
00020 {
00021     if (!node)
00022     {
00023         throw std::runtime_error("One of the children of a DecoratorNode or ControlNode is nulltr");
00024     }
00025 
00026     visitor(node);
00027 
00028     if (auto control = dynamic_cast<const BT::ControlNode*>(node))
00029     {
00030         for (const auto& child : control->children())
00031         {
00032             applyRecursiveVisitor(static_cast<const TreeNode*>(child), visitor);
00033         }
00034     }
00035     else if (auto decorator = dynamic_cast<const BT::DecoratorNode*>(node))
00036     {
00037         applyRecursiveVisitor(decorator->child(), visitor);
00038     }
00039 }
00040 
00041 void applyRecursiveVisitor(TreeNode* node, const std::function<void(TreeNode*)>& visitor)
00042 {
00043     if (!node)
00044     {
00045         throw std::runtime_error("One of the children of a DecoratorNode or ControlNode is nulltr");
00046     }
00047 
00048     visitor(node);
00049 
00050     if (auto control = dynamic_cast<BT::ControlNode*>(node))
00051     {
00052         for (const auto& child : control->children())
00053         {
00054             applyRecursiveVisitor(child, visitor);
00055         }
00056     }
00057     else if (auto decorator = dynamic_cast<BT::DecoratorNode*>(node))
00058     {
00059         applyRecursiveVisitor(decorator->child(), visitor);
00060     }
00061 }
00062 
00063 void printTreeRecursively(const TreeNode* root_node)
00064 {
00065     std::function<void(int, const BT::TreeNode*)> recursivePrint;
00066 
00067     recursivePrint = [&recursivePrint](unsigned indent, const BT::TreeNode* node) {
00068         for (unsigned i = 0; i < indent; i++)
00069         {
00070             std::cout << "   ";
00071         }
00072         if (!node)
00073         {
00074             std::cout << "!nullptr!" << std::endl;
00075             return;
00076         }
00077         std::cout << node->name() << std::endl;
00078         indent++;
00079 
00080         if (auto control = dynamic_cast<const BT::ControlNode*>(node))
00081         {
00082             for (const auto& child : control->children())
00083             {
00084                 recursivePrint(indent, child);
00085             }
00086         }
00087         else if (auto decorator = dynamic_cast<const BT::DecoratorNode*>(node))
00088         {
00089             recursivePrint(indent, decorator->child());
00090         }
00091     };
00092 
00093     std::cout << "----------------" << std::endl;
00094     recursivePrint(0, root_node);
00095     std::cout << "----------------" << std::endl;
00096 }
00097 
00098 void buildSerializedStatusSnapshot(TreeNode* root_node, SerializedTreeStatus& serialized_buffer)
00099 {
00100     serialized_buffer.clear();
00101 
00102     auto visitor = [&serialized_buffer](const TreeNode* node) {
00103         serialized_buffer.push_back(
00104             std::make_pair(node->UID(), static_cast<uint8_t>(node->status())));
00105     };
00106 
00107     applyRecursiveVisitor(root_node, visitor);
00108 }
00109 
00110 void assignBlackboardToEntireTree(TreeNode* root_node, const Blackboard::Ptr& bb)
00111 {
00112     auto visitor = [bb](TreeNode* node) { node->setBlackboard(bb); };
00113     applyRecursiveVisitor(root_node, visitor);
00114 }
00115 
00116 void haltAllActions(TreeNode* root_node)
00117 {
00118     auto visitor = [](TreeNode* node) {
00119         if (auto action = dynamic_cast<AsyncActionNode*>(node))
00120         {
00121             action->stopAndJoinThread();
00122         }
00123     };
00124     applyRecursiveVisitor(root_node, visitor);
00125 }
00126 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:09