BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
action_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#ifndef BEHAVIORTREECORE_ACTIONNODE_H
15#define BEHAVIORTREECORE_ACTIONNODE_H
16
17#include "leaf_node.h"
18
19#include <atomic>
20#include <future>
21#include <mutex>
22
23namespace BT
24{
25// IMPORTANT: Actions which returned SUCCESS or FAILURE will not be ticked
26// again unless resetStatus() is called first.
27// Keep this in mind when writing your custom Control and Decorator nodes.
28
29/**
30 * @brief The ActionNodeBase is the base class to use to create any kind of action.
31 * A particular derived class is free to override executeTick() as needed.
32 *
33 */
34class ActionNodeBase : public LeafNode
35{
36public:
37 ActionNodeBase(const std::string& name, const NodeConfig& config);
38 ~ActionNodeBase() override = default;
39
40 ActionNodeBase(const ActionNodeBase&) = delete;
41 ActionNodeBase& operator=(const ActionNodeBase&) = delete;
42 ActionNodeBase(ActionNodeBase&&) = delete;
43 ActionNodeBase& operator=(ActionNodeBase&&) = delete;
44
45 virtual NodeType type() const override final
46 {
47 return NodeType::ACTION;
48 }
49};
50
51/**
52 * @brief The SyncActionNode is an ActionNode that
53 * explicitly prevents the status RUNNING and doesn't require
54 * an implementation of halt().
55 */
56class SyncActionNode : public ActionNodeBase
57{
58public:
59 SyncActionNode(const std::string& name, const NodeConfig& config);
60 ~SyncActionNode() override = default;
61
62 SyncActionNode(const SyncActionNode&) = delete;
63 SyncActionNode& operator=(const SyncActionNode&) = delete;
64 SyncActionNode(SyncActionNode&&) = delete;
65 SyncActionNode& operator=(SyncActionNode&&) = delete;
66
67 /// throws if the derived class return RUNNING.
68 virtual NodeStatus executeTick() override;
69
70 /// You don't need to override this
71 virtual void halt() override final
72 {
74 }
75};
76
77/**
78 * @brief The SimpleActionNode provides an easy to use SyncActionNode.
79 * The user should simply provide a callback with this signature
80 *
81 * BT::NodeStatus functionName(TreeNode&)
82 *
83 * This avoids the hassle of inheriting from a ActionNode.
84 *
85 * Using lambdas or std::bind it is easy to pass a pointer to a method.
86 * SimpleActionNode is executed synchronously and does not support halting.
87 */
89{
90public:
91 using TickFunctor = std::function<NodeStatus(TreeNode&)>;
92
93 // You must provide the function to call when tick() is invoked
94 SimpleActionNode(const std::string& name, TickFunctor tick_functor,
95 const NodeConfig& config);
96
97 ~SimpleActionNode() override = default;
98
99 SimpleActionNode(const SimpleActionNode&) = delete;
100 SimpleActionNode& operator=(const SimpleActionNode&) = delete;
101 SimpleActionNode(SimpleActionNode&&) = delete;
102 SimpleActionNode& operator=(SimpleActionNode&&) = delete;
103
104protected:
105 virtual NodeStatus tick() override final;
106
107 TickFunctor tick_functor_;
108};
109
110/**
111 * @brief The ThreadedAction executes the tick in a different thread.
112 *
113 * IMPORTANT: this action is quite hard to implement correctly.
114 * Please make sure that you know what you are doing.
115 *
116 * - In your overridden tick() method, you must check periodically
117 * the result of the method isHaltRequested() and stop your execution accordingly.
118 *
119 * - in the overridden halt() method, you can do some cleanup, but do not forget to
120 * invoke the base class method ThreadedAction::halt();
121 *
122 * - remember, with few exceptions, a halted ThreadedAction must return NodeStatus::IDLE.
123 *
124 * For a complete example, look at __AsyncActionTest__ in action_test_node.h in the folder test.
125 *
126 * NOTE: when the thread is completed, i.e. the tick() returns its status,
127 * a TreeNode::emitWakeUpSignal() will be called.
128 */
129
130class ThreadedAction : public ActionNodeBase
131{
132public:
133 ThreadedAction(const std::string& name, const NodeConfig& config)
134 : ActionNodeBase(name, config)
135 {}
136
137 bool isHaltRequested() const
138 {
139 return halt_requested_.load();
140 }
141
142 // This method spawn a new thread. Do NOT remove the "final" keyword.
143 virtual NodeStatus executeTick() override final;
144
145 virtual void halt() override;
146
147private:
148 std::exception_ptr exptr_;
149 std::atomic_bool halt_requested_ = false;
150 std::future<void> thread_handle_;
151 std::mutex mutex_;
152};
153
154#ifdef USE_BTCPP3_OLD_NAMES
156#endif
157
158/**
159 * @brief The StatefulActionNode is the preferred way to implement asynchronous Actions.
160 * It is actually easier to use correctly, when compared with ThreadedAction
161 *
162 * It is particularly useful when your code contains a request-reply pattern,
163 * i.e. when the actions sends an asynchronous request, then checks periodically
164 * if the reply has been received and, eventually, analyze the reply to determine
165 * if the result is SUCCESS or FAILURE.
166 *
167 * -) an action that was in IDLE state will call onStart()
168 *
169 * -) A RUNNING action will call onRunning()
170 *
171 * -) if halted, method onHalted() is invoked
172 */
174{
175public:
176 StatefulActionNode(const std::string& name, const NodeConfig& config)
177 : ActionNodeBase(name, config)
178 {}
179
180 /// Method called once, when transitioning from the state IDLE.
181 /// If it returns RUNNING, this becomes an asynchronous node.
182 virtual NodeStatus onStart() = 0;
183
184 /// method invoked when the action is already in the RUNNING state.
185 virtual NodeStatus onRunning() = 0;
186
187 /// when the method halt() is called and the action is RUNNING, this method is invoked.
188 /// This is a convenient place todo a cleanup, if needed.
189 virtual void onHalted() = 0;
190
191 bool isHaltRequested() const;
192
193protected:
194 // do not override this method
195 NodeStatus tick() override final;
196 // do not override this method
197 void halt() override final;
198
199private:
200 std::atomic_bool halt_requested_ = false;
201};
202
203/**
204 * @brief The CoroActionNode class is an a good candidate for asynchronous actions
205 * which need to communicate with an external service using an async request/reply interface.
206 *
207 * It is up to the user to decide when to suspend execution of the Action and resume
208 * the parent node, invoking the method setStatusRunningAndYield().
209 */
210class CoroActionNode : public ActionNodeBase
211{
212public:
213 CoroActionNode(const std::string& name, const NodeConfig& config);
214 ~CoroActionNode() override;
215
216 CoroActionNode(const CoroActionNode&) = delete;
217 CoroActionNode& operator=(const CoroActionNode&) = delete;
218 CoroActionNode(CoroActionNode&&) = delete;
219 CoroActionNode& operator=(CoroActionNode&&) = delete;
220
221 /// Use this method to return RUNNING and temporary "pause" the Action.
223
224 // This method triggers the TickEngine. Do NOT remove the "final" keyword.
225 virtual NodeStatus executeTick() override final;
226
227 // Used internally, but it needs to be public
228 void tickImpl();
229
230 /** You may want to override this method. But still, remember to call this
231 * implementation too.
232 *
233 * Example:
234 *
235 * void MyAction::halt()
236 * {
237 * // do your stuff here
238 * CoroActionNode::halt();
239 * }
240 */
241 void halt() override;
242
243protected:
244 struct Pimpl; // The Pimpl idiom
245 std::unique_ptr<Pimpl> _p;
246
247 void destroyCoroutine();
248};
249
250} // namespace BT
251
252#endif
The ActionNodeBase is the base class to use to create any kind of action. A particular derived class ...
Definition: action_node.h:35
The CoroActionNode class is an a good candidate for asynchronous actions which need to communicate wi...
Definition: action_node.h:211
void setStatusRunningAndYield()
Use this method to return RUNNING and temporary "pause" the Action.
void halt() override
virtual NodeStatus executeTick() override final
The method that should be used to invoke tick() and setStatus();.
Definition: leaf_node.h:22
The SimpleActionNode provides an easy to use SyncActionNode. The user should simply provide a callbac...
Definition: action_node.h:89
virtual NodeStatus tick() override final
Method to be implemented by the user.
The StatefulActionNode is the preferred way to implement asynchronous Actions. It is actually easier ...
Definition: action_node.h:174
virtual NodeStatus onRunning()=0
method invoked when the action is already in the RUNNING state.
void halt() override final
virtual void onHalted()=0
NodeStatus tick() override final
Method to be implemented by the user.
virtual NodeStatus onStart()=0
The SyncActionNode is an ActionNode that explicitly prevents the status RUNNING and doesn't require a...
Definition: action_node.h:57
virtual void halt() override final
You don't need to override this.
Definition: action_node.h:71
virtual NodeStatus executeTick() override
throws if the derived class return RUNNING.
The ThreadedAction executes the tick in a different thread.
Definition: action_node.h:131
virtual NodeStatus executeTick() override final
The method that should be used to invoke tick() and setStatus();.
virtual void halt() override
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:154
void resetStatus()
Set the status to IDLE.
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
NodeType
Enumerates the possible types of nodes.
Definition: basic_types.h:21
Definition: tree_node.h:105