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 <decorators/negation_node.h> 00014 #include <string> 00015 00016 BT::NegationNode::NegationNode(std::string name) : DecoratorNode::DecoratorNode(name) {} 00017 00018 BT::NegationNode::~NegationNode() {} 00019 00020 BT::ReturnStatus BT::NegationNode::Tick() 00021 { 00022 { 00023 // gets the number of children. The number could change if, at runtime, one edits the tree. 00024 N_of_children_ = children_nodes_.size(); 00025 00026 if (N_of_children_ > 0) 00027 { 00028 if (children_nodes_[0]->get_type() == BT::ACTION_NODE) 00029 { 00030 child_i_status_ = children_nodes_[0]->get_status(); 00031 00032 if (child_i_status_ == BT::IDLE || child_i_status_ == BT::HALTED) 00033 { 00034 DEBUG_STDOUT(get_name() << "NEEDS TO TICK " << children_nodes_[0]->get_name()); 00035 children_nodes_[0]->tick_engine.Tick(); 00036 00037 // waits for the tick to arrive to the child 00038 do 00039 { 00040 child_i_status_ = children_nodes_[0]->get_status(); 00041 std::this_thread::sleep_for(std::chrono::milliseconds(10)); 00042 } 00043 while (child_i_status_ != BT::RUNNING && child_i_status_ != BT::SUCCESS && child_i_status_ != BT::FAILURE); 00044 } 00045 } 00046 else 00047 { 00048 child_i_status_ = children_nodes_[0]->Tick(); 00049 } 00050 00051 if (child_i_status_ == BT::SUCCESS || child_i_status_ == BT::FAILURE) 00052 { 00053 children_nodes_[0]->set_status(BT::IDLE); 00054 } 00055 00056 if (child_i_status_ != BT::FAILURE) 00057 { 00058 HaltChildren(1); 00059 } 00060 00061 set_status(convert(child_i_status_)); 00062 return convert(child_i_status_); 00063 } 00064 } 00065 return BT::EXIT; 00066 } 00067 00068 BT::ReturnStatus BT::NegationNode::convert(BT::ReturnStatus input) { 00069 switch(input) { 00070 case BT::SUCCESS : return BT::FAILURE; 00071 case BT::FAILURE : return BT::SUCCESS; 00072 default : return input; 00073 } 00074 }