parallel_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * 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:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * 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,
11 * 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.
12 */
13 
14 #include <algorithm>
15 #include <cstddef>
16 
18 
19 namespace BT
20 {
21 constexpr const char* ParallelNode::THRESHOLD_FAILURE;
22 constexpr const char* ParallelNode::THRESHOLD_SUCCESS;
23 
24 ParallelNode::ParallelNode(const std::string& name)
25  : ControlNode::ControlNode(name, {})
26  , success_threshold_(-1)
27  , failure_threshold_(1)
28  , read_parameter_from_ports_(false)
29 {
30  setRegistrationID("Parallel");
31 }
32 
33 ParallelNode::ParallelNode(const std::string& name, const NodeConfig& config)
34  : ControlNode::ControlNode(name, config)
35  , success_threshold_(-1)
36  , failure_threshold_(1)
37  , read_parameter_from_ports_(true)
38 {}
39 
41 {
43  {
45  {
46  throw RuntimeError("Missing parameter [", THRESHOLD_SUCCESS, "] in ParallelNode");
47  }
48 
50  {
51  throw RuntimeError("Missing parameter [", THRESHOLD_FAILURE, "] in ParallelNode");
52  }
53  }
54 
55  const size_t children_count = children_nodes_.size();
56 
57  if(children_count < successThreshold())
58  {
59  throw LogicError("Number of children is less than threshold. Can never succeed.");
60  }
61 
62  if(children_count < failureThreshold())
63  {
64  throw LogicError("Number of children is less than threshold. Can never fail.");
65  }
66 
68 
69  size_t skipped_count = 0;
70 
71  // Routing the tree according to the sequence node's logic:
72  for(size_t i = 0; i < children_count; i++)
73  {
74  if(completed_list_.count(i) == 0)
75  {
76  TreeNode* child_node = children_nodes_[i];
77  NodeStatus const child_status = child_node->executeTick();
78 
79  switch(child_status)
80  {
81  case NodeStatus::SKIPPED: {
82  skipped_count++;
83  }
84  break;
85 
86  case NodeStatus::SUCCESS: {
87  completed_list_.insert(i);
89  }
90  break;
91 
92  case NodeStatus::FAILURE: {
93  completed_list_.insert(i);
95  }
96  break;
97 
98  case NodeStatus::RUNNING: {
99  // Still working. Check the next
100  }
101  break;
102 
103  case NodeStatus::IDLE: {
104  throw LogicError("[", name(), "]: A children should not return IDLE");
105  }
106  }
107  }
108 
109  const size_t required_success_count = successThreshold();
110 
111  if(success_count_ >= required_success_count ||
112  (success_threshold_ < 0 &&
113  (success_count_ + skipped_count) >= required_success_count))
114  {
115  clear();
116  resetChildren();
117  return NodeStatus::SUCCESS;
118  }
119 
120  // It fails if it is not possible to succeed anymore or if
121  // number of failures are equal to failure_threshold_
122  if(((children_count - failure_count_) < required_success_count) ||
124  {
125  clear();
126  resetChildren();
127  return NodeStatus::FAILURE;
128  }
129  }
130  // Skip if ALL the nodes have been skipped
131  return (skipped_count == children_count) ? NodeStatus::SKIPPED : NodeStatus::RUNNING;
132 }
133 
135 {
136  completed_list_.clear();
137  success_count_ = 0;
138  failure_count_ = 0;
139 }
140 
142 {
143  clear();
145 }
146 
148 {
149  if(success_threshold_ < 0)
150  {
151  return size_t(std::max(int(children_nodes_.size()) + success_threshold_ + 1, 0));
152  }
153  else
154  {
155  return size_t(success_threshold_);
156  }
157 }
158 
160 {
161  if(failure_threshold_ < 0)
162  {
163  return size_t(std::max(int(children_nodes_.size()) + failure_threshold_ + 1, 0));
164  }
165  else
166  {
167  return size_t(failure_threshold_);
168  }
169 }
170 
172 {
173  success_threshold_ = threshold;
174 }
175 
177 {
178  failure_threshold_ = threshold;
179 }
180 
181 } // namespace BT
BT::ParallelNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: parallel_node.cpp:40
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:547
BT
Definition: ex01_wrap_legacy.cpp:29
BT::ParallelNode::clear
void clear()
Definition: parallel_node.cpp:134
BT::TreeNode::executeTick
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:71
BT::ParallelNode::success_count_
size_t success_count_
Definition: parallel_node.h:72
BT::ParallelNode::THRESHOLD_FAILURE
static constexpr const char * THRESHOLD_FAILURE
Definition: parallel_node.h:77
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
BT::ParallelNode::success_threshold_
int success_threshold_
Definition: parallel_node.h:67
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::LogicError
Definition: exceptions.h:45
BT::ParallelNode::read_parameter_from_ports_
bool read_parameter_from_ports_
Definition: parallel_node.h:75
BT::ParallelNode::failureThreshold
size_t failureThreshold() const
Definition: parallel_node.cpp:159
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:154
BT::NodeStatus::SKIPPED
@ SKIPPED
BT::ParallelNode::failure_count_
size_t failure_count_
Definition: parallel_node.h:73
BT::ParallelNode::successThreshold
size_t successThreshold() const
Definition: parallel_node.cpp:147
BT::RuntimeError
Definition: exceptions.h:58
BT::ParallelNode::setFailureThreshold
void setFailureThreshold(int threshold)
Definition: parallel_node.cpp:176
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:296
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::NodeStatus::RUNNING
@ RUNNING
BT::ParallelNode::ParallelNode
ParallelNode(const std::string &name)
Definition: parallel_node.cpp:24
BT::ParallelNode::THRESHOLD_SUCCESS
static constexpr const char * THRESHOLD_SUCCESS
Definition: parallel_node.h:76
BT::NodeStatus::IDLE
@ IDLE
BT::ParallelNode::setSuccessThreshold
void setSuccessThreshold(int threshold)
Definition: parallel_node.cpp:171
BT::ControlNode::halt
virtual void halt() override
Definition: control_node.cpp:32
BT::NodeConfig
Definition: tree_node.h:73
parallel_node.h
BT::ParallelNode::halt
virtual void halt() override
Definition: parallel_node.cpp:141
BT::ControlNode
Definition: control_node.h:21
BT::ParallelNode::failure_threshold_
int failure_threshold_
Definition: parallel_node.h:68
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
BT::ParallelNode::completed_list_
std::set< size_t > completed_list_
Definition: parallel_node.h:70


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07