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#include "behaviortree_cpp/scripting/script_parser.hpp"
18#include "behaviortree_cpp/utils/timer_queue.h"
19
20namespace BT
21{
22
23struct TestNodeConfig
24{
25 /// status to return when the action is completed.
27
28 /// script to execute when complete_func() returns SUCCESS
29 std::string success_script;
30
31 /// script to execute when complete_func() returns FAILURE
32 std::string failure_script;
33
34 /// script to execute when actions is completed
35 std::string post_script;
36
37 /// if async_delay > 0, this action become asynchronous and wait this amount of time
38 std::chrono::milliseconds async_delay = std::chrono::milliseconds(0);
39
40 /// Function invoked when the action is completed.
41 /// If not specified, the node will return [return_status]
42 std::function<NodeStatus(void)> complete_func;
43};
44
45/**
46 * @brief The TestNode is a Node that can be configure to:
47 *
48 * 1. Return a specific status (SUCCESS / FAILURE)
49 * 2. Execute a post condition script (unless halted)
50 * 3. Either complete immediately (synchronous action), or after a
51 * given period of time (asynchronous action)
52 *
53 * This behavior is changed by the parameters passed with TestNodeConfig.
54 *
55 * This particular node is created by the factory when TestNodeConfig is
56 * added as a substitution rule:
57 *
58 * auto test_config = std::make_shared<TestNodeConfig>();
59 * // change fields of test_config
60 * factory.addSubstitutionRule(pattern, test_config);
61 *
62 * See tutorial 15 for more details.
63 */
64class TestNode : public BT::StatefulActionNode
65{
66public:
67 // This constructor is deprecated, because it may cause problems if TestNodeConfig::complete_func is capturing
68 // a reference to the TestNode, i.e. [this]. Use the constructor with std::shared_ptr<TestNodeConfig> instead.
69 // For more details, see https://github.com/BehaviorTree/BehaviorTree.CPP/pull/967
70 [[deprecated("prefer the constructor with std::shared_ptr<TestNodeConfig>")]] TestNode(
71 const std::string& name, const NodeConfig& config, TestNodeConfig test_config);
72
73 TestNode(const std::string& name, const NodeConfig& config,
74 std::shared_ptr<TestNodeConfig> test_config);
75
76 static PortsList providedPorts()
77 {
78 return {};
79 }
80
81protected:
82 virtual NodeStatus onStart() override;
83
84 virtual NodeStatus onRunning() override;
85
86 virtual void onHalted() override;
87
88 NodeStatus onCompleted();
89
90 std::shared_ptr<TestNodeConfig> _config;
91 ScriptFunction _success_executor;
92 ScriptFunction _failure_executor;
93 ScriptFunction _post_executor;
94 TimerQueue<> _timer;
95 std::atomic_bool _completed = false;
96};
97
98} // 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:65
virtual void onHalted() override
virtual NodeStatus onRunning() override
method invoked when the action is already in the RUNNING state.
virtual NodeStatus onStart() override
Definition: action_node.h:24
NodeStatus
Definition: basic_types.h:34
Definition: tree_node.h:105
Definition: test_node.h:24
std::string success_script
script to execute when complete_func() returns SUCCESS
Definition: test_node.h:29
std::string failure_script
script to execute when complete_func() returns FAILURE
Definition: test_node.h:32
std::chrono::milliseconds async_delay
if async_delay > 0, this action become asynchronous and wait this amount of time
Definition: test_node.h:38
NodeStatus return_status
status to return when the action is completed.
Definition: test_node.h:26
std::function< NodeStatus(void)> complete_func
Definition: test_node.h:42
std::string post_script
script to execute when actions is completed
Definition: test_node.h:35