BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
retry_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/decorator_node.h"
17
18namespace BT
19{
20/**
21 * @brief The RetryNode is used to execute a child several times if it fails.
22 *
23 * If the child returns SUCCESS, the loop is stopped and this node
24 * returns SUCCESS.
25 *
26 * If the child returns FAILURE, this node will try again up to N times
27 * (N is read from port "num_attempts").
28 *
29 * Example:
30 *
31 * <RetryUntilSuccessful num_attempts="3">
32 * <OpenDoor/>
33 * </RetryUntilSuccessful>
34 *
35 * Note:
36 * RetryNodeTypo is only included to support the deprecated typo
37 * "RetryUntilSuccesful" (note the single 's' in Succesful)
38 */
39class RetryNode : public DecoratorNode
40{
41public:
42 RetryNode(const std::string& name, int NTries);
43
44 RetryNode(const std::string& name, const NodeConfig& config);
45
46 ~RetryNode() override = default;
47
48 RetryNode(const RetryNode&) = delete;
49 RetryNode& operator=(const RetryNode&) = delete;
50 RetryNode(RetryNode&&) = delete;
51 RetryNode& operator=(RetryNode&&) = delete;
52
53 static PortsList providedPorts()
54 {
55 return { InputPort<int>(NUM_ATTEMPTS, "Execute again a failing child up to N times. "
56 "Use -1 to create an infinite loop.") };
57 }
58
59 virtual void halt() override;
60
61private:
62 int max_attempts_;
63 int try_count_;
64
65 bool read_parameter_from_ports_;
66 static constexpr const char* NUM_ATTEMPTS = "num_attempts";
67
68 virtual BT::NodeStatus tick() override;
69};
70
71class [[deprecated("RetryUntilSuccesful was a typo and deprecated, use "
72 "RetryUntilSuccessful "
73 "instead.")]] RetryNodeTypo : public RetryNode
74{
75public:
76 RetryNodeTypo(const std::string& name, int NTries) : RetryNode(name, NTries){};
77
78 RetryNodeTypo(const std::string& name, const NodeConfig& config)
79 : RetryNode(name, config){};
80
81 ~RetryNodeTypo() override = default;
82
83 RetryNodeTypo(const RetryNodeTypo&) = delete;
84 RetryNodeTypo& operator=(const RetryNodeTypo&) = delete;
85 RetryNodeTypo(RetryNodeTypo&&) = delete;
86 RetryNodeTypo& operator=(RetryNodeTypo&&) = delete;
87};
88
89} // namespace BT
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
The RetryNode is used to execute a child several times if it fails.
Definition: retry_node.h:40
virtual void halt() override
The method used to interrupt the execution of this node.
Definition: retry_node.h:74
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105