parallel_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2019 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 
15 
16 namespace BT
17 {
18 
19 constexpr const char* ParallelNode::THRESHOLD_KEY;
20 
21 ParallelNode::ParallelNode(const std::string& name, unsigned threshold)
22  : ControlNode::ControlNode(name, {} ),
23  threshold_(threshold),
25 {
26  setRegistrationID("Parallel");
27 }
28 
29 ParallelNode::ParallelNode(const std::string &name,
31  : ControlNode::ControlNode(name, config),
32  threshold_(0),
34 {
35 }
36 
38 {
40  {
42  {
43  throw RuntimeError("Missing parameter [", THRESHOLD_KEY, "] in ParallelNode");
44  }
45  }
46 
47  size_t success_childred_num = 0;
48  size_t failure_childred_num = 0;
49 
50  const size_t children_count = children_nodes_.size();
51 
52  if( children_count < threshold_)
53  {
54  throw LogicError("Number of children is less than threshold. Can never suceed.");
55  }
56 
57  // Routing the tree according to the sequence node's logic:
58  for (unsigned int i = 0; i < children_count; i++)
59  {
60  TreeNode* child_node = children_nodes_[i];
61 
62  bool in_skip_list = (skip_list_.count(i) != 0);
63 
64  NodeStatus child_status;
65  if( in_skip_list )
66  {
67  child_status = child_node->status();
68  }
69  else {
70  child_status = child_node->executeTick();
71  }
72 
73  switch (child_status)
74  {
76  {
77  if( !in_skip_list )
78  {
79  skip_list_.insert(i);
80  }
81  success_childred_num++;
82 
83  if (success_childred_num == threshold_)
84  {
85  skip_list_.clear();
86  haltChildren(0);
87  return NodeStatus::SUCCESS;
88  }
89  } break;
90 
92  {
93  if( !in_skip_list )
94  {
95  skip_list_.insert(i);
96  }
97  failure_childred_num++;
98 
99  if (failure_childred_num > children_count - threshold_)
100  {
101  skip_list_.clear();
102  haltChildren(0);
103  return NodeStatus::FAILURE;
104  }
105  } break;
106 
107  case NodeStatus::RUNNING:
108  {
109  // do nothing
110  } break;
111 
112  default:
113  {
114  throw LogicError("A child node must never return IDLE");
115  }
116  }
117  }
118 
119  return NodeStatus::RUNNING;
120 }
121 
123 {
124  skip_list_.clear();
126 }
127 
129 {
130  return threshold_;
131 }
132 
133 void ParallelNode::setThresholdM(unsigned int threshold_M)
134 {
135  threshold_ = threshold_M;
136 }
137 
138 }
unsigned int thresholdM()
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:25
const NodeConfiguration & config() const
Definition: tree_node.cpp:99
virtual void halt() override
std::set< int > skip_list_
Definition: parallel_node.h:46
static constexpr const char * THRESHOLD_KEY
Definition: parallel_node.h:49
bool read_parameter_from_ports_
Definition: parallel_node.h:48
void setThresholdM(unsigned int threshold_M)
virtual void halt() override
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:185
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:53
unsigned int threshold_
Definition: parallel_node.h:44
void haltChildren(unsigned i)
call halt() for all the children in the range [i, childrenCount() )
void setRegistrationID(StringView ID)
Definition: tree_node.h:158
ParallelNode(const std::string &name, unsigned threshold)
NodeStatus status() const
Definition: tree_node.cpp:56
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
NodeStatus
Definition: basic_types.h:35
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:33


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 18:04:05