BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
test_node.h
1/* Copyright (C) 2022-2025 Davide Faconti - All Rights Reserved
2 *
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/action_node.h"
17
18#include <chrono>
19#include <functional>
20#include <memory>
21#include <string>
22
23namespace BT
24{
25
26struct TestNodeConfig
27{
28 /// Status to return when the action is completed.
29 /// If return_status_script is also set, the script takes precedence.
31
32 /// script to execute when complete_func() returns SUCCESS
33 std::string success_script;
34
35 /// script to execute when complete_func() returns FAILURE
36 std::string failure_script;
37
38 /// script to execute when actions is completed
39 std::string post_script;
40
41 /// if async_delay > 0, this action become asynchronous and wait this amount of time
42 std::chrono::milliseconds async_delay = std::chrono::milliseconds(0);
43
44 /// Function invoked when the action is completed.
45 /// If not specified, the node will use return_status_script when present,
46 /// otherwise it will return [return_status].
47 std::function<NodeStatus(void)> complete_func;
48
49 /// Optional script to compute the completion status dynamically.
50 ///
51 /// This script is evaluated when the TestNode completes, after any
52 /// async_delay has elapsed, using the current blackboard state.
53 /// The result must resolve to the same set of statuses supported by
54 /// return_status, except IDLE which is always rejected.
55 /// When set, this takes precedence over return_status.
56 ///
57 /// NOTE: kept last to preserve the byte offsets of the fields above.
58 std::string return_status_script;
59};
60
61/**
62 * @brief The TestNode is a Node that can be configure to:
63 *
64 * 1. Return a specific status (SUCCESS / FAILURE)
65 * 1.b Compute the returned status from a script evaluated at completion time
66 * 2. Execute a post condition script (unless halted)
67 * 3. Either complete immediately (synchronous action), or after a
68 * given period of time (asynchronous action)
69 *
70 * This behavior is changed by the parameters passed with TestNodeConfig.
71 *
72 * This particular node is created by the factory when TestNodeConfig is
73 * added as a substitution rule:
74 *
75 * auto test_config = std::make_shared<TestNodeConfig>();
76 * // change fields of test_config
77 * factory.addSubstitutionRule(pattern, test_config);
78 *
79 * See tutorial 15 for more details.
80 */
81class TestNode : public BT::StatefulActionNode
82{
83public:
84 // This constructor is deprecated, because it may cause problems if TestNodeConfig::complete_func is capturing
85 // a reference to the TestNode, i.e. [this]. Use the constructor with std::shared_ptr<TestNodeConfig> instead.
86 // For more details, see https://github.com/BehaviorTree/BehaviorTree.CPP/pull/967
87 [[deprecated("prefer the constructor with std::shared_ptr<TestNodeConfig>")]] TestNode(
88 const std::string& name, const NodeConfig& config, TestNodeConfig test_config);
89
90 TestNode(const std::string& name, const NodeConfig& config,
91 std::shared_ptr<TestNodeConfig> test_config);
92
93 ~TestNode() override;
94
95 TestNode(const TestNode& other) = delete;
96 TestNode& operator=(const TestNode& other) = delete;
97
98 TestNode(TestNode&& other) = delete;
99 TestNode& operator=(TestNode&& other) = delete;
100
101 static PortsList providedPorts()
102 {
103 return {};
104 }
105
106protected:
107 NodeStatus onStart() override;
108
109 NodeStatus onRunning() override;
110
111 void onHalted() override;
112
113 NodeStatus onCompleted();
114
115 // All state lives behind this pointer, so that adding or changing it does not
116 // alter the layout that consumers compile against.
117 struct PImpl;
118 std::unique_ptr<PImpl> _p;
119};
120
121} // namespace BT
The StatefulActionNode is the preferred way to implement asynchronous Actions. It is actually easier ...
Definition: action_node.h:174
The TestNode is a Node that can be configure to:
Definition: test_node.h:82
NodeStatus onRunning() override
method invoked when the action is already in the RUNNING state.
NodeStatus onStart() override
void onHalted() override
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105
Definition: test_node.h:27
std::string success_script
script to execute when complete_func() returns SUCCESS
Definition: test_node.h:33
std::string failure_script
script to execute when complete_func() returns FAILURE
Definition: test_node.h:36
std::chrono::milliseconds async_delay
if async_delay > 0, this action become asynchronous and wait this amount of time
Definition: test_node.h:42
NodeStatus return_status
Definition: test_node.h:30
std::function< NodeStatus(void)> complete_func
Definition: test_node.h:47
std::string return_status_script
Definition: test_node.h:58
std::string post_script
script to execute when actions is completed
Definition: test_node.h:39