BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
parallel_node.h
1/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2 * Copyright (C) 2018-2025 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#pragma once
15
16#include "behaviortree_cpp/control_node.h"
17
18#include <set>
19
20namespace BT
21{
22/**
23 * @brief The ParallelNode execute all its children
24 * __concurrently__, but not in separate threads!
25 *
26 * Even if this may look similar to ReactiveSequence,
27 * this Control Node is the __only__ one that can have
28 * multiple children RUNNING at the same time.
29 *
30 * The Node is completed either when the THRESHOLD_SUCCESS
31 * or THRESHOLD_FAILURE number is reached (both configured using ports).
32 *
33 * If any of the thresholds is reached, and other children are still running,
34 * they will be halted.
35 *
36 * Note that threshold indexes work as in Python:
37 * https://www.i2tutorials.com/what-are-negative-indexes-and-why-are-they-used/
38 *
39 * Therefore -1 is equivalent to the number of children.
40 */
41class ParallelNode : public ControlNode
42{
43public:
44 ParallelNode(const std::string& name);
45
46 ParallelNode(const std::string& name, const NodeConfig& config);
47
48 static PortsList providedPorts()
49 {
50 return { InputPort<int>(THRESHOLD_SUCCESS, -1,
51 "number of children that need to succeed to trigger a "
52 "SUCCESS"),
53 InputPort<int>(THRESHOLD_FAILURE, 1,
54 "number of children that need to fail to trigger a "
55 "FAILURE") };
56 }
57
58 ~ParallelNode() override = default;
59
60 ParallelNode(const ParallelNode&) = delete;
61 ParallelNode& operator=(const ParallelNode&) = delete;
62 ParallelNode(ParallelNode&&) = delete;
63 ParallelNode& operator=(ParallelNode&&) = delete;
64
65 virtual void halt() override;
66
67 size_t successThreshold() const;
68 size_t failureThreshold() const;
69 void setSuccessThreshold(int threshold);
70 void setFailureThreshold(int threshold);
71
72private:
73 int success_threshold_;
74 int failure_threshold_;
75
76 std::set<size_t> completed_list_;
77
78 size_t success_count_ = 0;
79 size_t failure_count_ = 0;
80
81 bool read_parameter_from_ports_;
82 static constexpr const char* THRESHOLD_SUCCESS = "success_count";
83 static constexpr const char* THRESHOLD_FAILURE = "failure_count";
84
85 virtual BT::NodeStatus tick() override;
86
87 void clear();
88};
89
90} // namespace BT
The ControlNode is the base class for nodes that can have multiple children.
Definition: control_node.h:32
The ParallelNode execute all its children concurrently, but not in separate threads!
Definition: parallel_node.h:42
virtual void halt() override
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105