00001 /* Copyright (C) 2015-2017 Michele Colledanchise - 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/decorator_node.h" 00014 00015 namespace BT 00016 { 00017 DecoratorNode::DecoratorNode(const std::string& name, const NodeParameters& parameters) 00018 : TreeNode::TreeNode(name, parameters), child_node_(nullptr) 00019 { 00020 // TODO(...) In case it is desired to set to idle remove the ReturnStatus 00021 // type in order to set the member variable 00022 // ReturnStatus const NodeStatus child_status = NodeStatus::IDLE; // commented out as unused 00023 } 00024 00025 void DecoratorNode::setChild(TreeNode* child) 00026 { 00027 if (child_node_) 00028 { 00029 throw BehaviorTreeException("Decorator '" + name() + "' has already a child assigned"); 00030 } 00031 00032 child_node_ = child; 00033 } 00034 00035 void DecoratorNode::halt() 00036 { 00037 haltChild(); 00038 setStatus(NodeStatus::IDLE); 00039 } 00040 00041 const TreeNode* DecoratorNode::child() const 00042 { 00043 return child_node_; 00044 } 00045 00046 TreeNode* DecoratorNode::child() 00047 { 00048 return child_node_; 00049 } 00050 00051 void DecoratorNode::haltChild() 00052 { 00053 if (child_node_->status() == NodeStatus::RUNNING) 00054 { 00055 child_node_->halt(); 00056 } 00057 child_node_->setStatus(NodeStatus::IDLE); 00058 } 00059 00060 SimpleDecoratorNode::SimpleDecoratorNode(const std::string& name, TickFunctor tick_functor, 00061 const NodeParameters ¶ms) 00062 : DecoratorNode(name, params), tick_functor_(std::move(tick_functor)) 00063 { 00064 } 00065 00066 NodeStatus SimpleDecoratorNode::tick() 00067 { 00068 return tick_functor_(child()->executeTick(), *this); 00069 } 00070 00071 NodeStatus DecoratorNode::executeTick() 00072 { 00073 NodeStatus status = TreeNode::executeTick(); 00074 NodeStatus child_status =child()->status(); 00075 if( child_status == NodeStatus::SUCCESS || child_status == NodeStatus::FAILURE ) 00076 { 00077 child()->setStatus(NodeStatus::IDLE); 00078 } 00079 return status; 00080 } 00081 00082 }