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 namespace BT
16 {
17 IfThenElseNode::IfThenElseNode(const std::string& name)
18  : ControlNode::ControlNode(name, {}), child_idx_(0)
19 {
20  setRegistrationID("IfThenElse");
21 }
22 
24 {
25  child_idx_ = 0;
27 }
28 
30 {
31  const size_t children_count = children_nodes_.size();
32 
33  if(children_count != 2 && children_count != 3)
34  {
35  throw std::logic_error("IfThenElseNode must have either 2 or 3 children");
36  }
37 
39 
40  if(child_idx_ == 0)
41  {
42  NodeStatus condition_status = children_nodes_[0]->executeTick();
43 
44  if(condition_status == NodeStatus::RUNNING)
45  {
46  return condition_status;
47  }
48  else if(condition_status == NodeStatus::SUCCESS)
49  {
50  child_idx_ = 1;
51  }
52  else if(condition_status == NodeStatus::FAILURE)
53  {
54  if(children_count == 3)
55  {
56  child_idx_ = 2;
57  }
58  else
59  {
60  return condition_status;
61  }
62  }
63  }
64  // not an else
65  if(child_idx_ > 0)
66  {
67  NodeStatus status = children_nodes_[child_idx_]->executeTick();
69  {
70  return NodeStatus::RUNNING;
71  }
72  else
73  {
74  resetChildren();
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
BT
Definition: ex01_wrap_legacy.cpp:29
BT::ControlNode::children_nodes_
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:24
BT::ControlNode::resetChildren
void resetChildren()
Definition: control_node.cpp:38
BT::IfThenElseNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: if_then_else_node.cpp:29
BT::IfThenElseNode::IfThenElseNode
IfThenElseNode(const std::string &name)
Definition: if_then_else_node.cpp:17
BT::TreeNode::status
NodeStatus status() const
Definition: tree_node.cpp:285
BT::NodeStatus::FAILURE
@ FAILURE
BT::TreeNode::setStatus
void setStatus(NodeStatus new_status)
setStatus changes the status of the node. it will throw if you try to change the status to IDLE,...
Definition: tree_node.cpp:160
BT::IfThenElseNode::halt
virtual void halt() override
Definition: if_then_else_node.cpp:23
if_then_else_node.h
BT::IfThenElseNode::child_idx_
size_t child_idx_
Definition: if_then_else_node.h:44
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:439
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeStatus::RUNNING
@ RUNNING
BT::ControlNode::halt
virtual void halt() override
Definition: control_node.cpp:32
BT::ControlNode
Definition: control_node.h:21
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16