if_then_else_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2020 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 
15 
16 namespace BT
17 {
18 
19 IfThenElseNode::IfThenElseNode(const std::string &name)
20  : ControlNode::ControlNode(name, {} )
21  , child_idx_(0)
22 {
23  setRegistrationID("IfThenElse");
24 }
25 
27 {
28  child_idx_ = 0;
30 }
31 
33 {
34  const size_t children_count = children_nodes_.size();
35 
36  if(children_count != 2 && children_count != 3)
37  {
38  throw std::logic_error("IfThenElseNode must have either 2 or 3 children");
39  }
40 
42 
43  if (child_idx_ == 0)
44  {
45  NodeStatus condition_status = children_nodes_[0]->executeTick();
46 
47  if (condition_status == NodeStatus::RUNNING)
48  {
49  return condition_status;
50  }
51  else if (condition_status == NodeStatus::SUCCESS)
52  {
53  child_idx_ = 1;
54  }
55  else if (condition_status == NodeStatus::FAILURE)
56  {
57  if( children_count == 3){
58  child_idx_ = 2;
59  }
60  else{
61  return condition_status;
62  }
63  }
64  }
65  // not an else
66  if (child_idx_ > 0)
67  {
68  NodeStatus status = children_nodes_[child_idx_]->executeTick();
69  if (status == NodeStatus::RUNNING)
70  {
71  return NodeStatus::RUNNING;
72  }
73  else{
74  haltChildren();
75  child_idx_ = 0;
76  return status;
77  }
78  }
79 
80  throw std::logic_error("Something unexpected happened in IfThenElseNode");
81 }
82 
83 } // namespace BT
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:25
virtual void halt() override
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
IfThenElseNode(const std::string &name)
virtual void halt() override
void setRegistrationID(StringView ID)
Definition: tree_node.h:163
NodeStatus status() const
Definition: tree_node.cpp:56
NodeStatus
Definition: basic_types.h:35
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:40


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25