BehaviorTree
Core Library to create and execute Behavior Trees
Loading...
Searching...
No Matches
bt_factory.h
1/* Copyright (C) 2018 Michele Colledanchise - All Rights Reserved
2 * Copyright (C) 2018-2025 Davide Faconti - 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 BT_FACTORY_H
15#define BT_FACTORY_H
16
17#include "behaviortree_cpp/behavior_tree.h"
18#ifdef BTCPP_SYSTEM_JSON
19#include <nlohmann/json.hpp>
20#else
21#include "behaviortree_cpp/contrib/json.hpp"
22#endif
23#include "behaviortree_cpp/contrib/magic_enum.hpp"
24#include "behaviortree_cpp/utils/polymorphic_cast_registry.hpp"
25
26#include <filesystem>
27#include <functional>
28#include <memory>
29#include <set>
30#include <unordered_map>
31#include <vector>
32
33namespace BT
34{
35/// The term "Builder" refers to the Builder Pattern (https://en.wikipedia.org/wiki/Builder_pattern)
36using NodeBuilder =
38
39template <typename T, typename... Args>
40inline NodeBuilder CreateBuilder(Args... args)
41{
42 return [=](const std::string& name, const NodeConfig& config) {
43 return TreeNode::Instantiate<T, Args...>(name, config, args...);
44 };
45}
46
47template <typename T>
48inline TreeNodeManifest CreateManifest(const std::string& ID,
49 PortsList portlist = getProvidedPorts<T>())
50{
51 if constexpr(has_static_method_metadata<T>::value)
52 {
53 return { getType<T>(), ID, portlist, T::metadata() };
54 }
55 return { getType<T>(), ID, portlist, {} };
56}
57
58#ifdef BT_PLUGIN_EXPORT
59
60#if defined(_WIN32)
61#define BTCPP_EXPORT extern "C" __declspec(dllexport)
62#else
63// Unix-like OSes
64#define BTCPP_EXPORT extern "C" __attribute__((visibility("default")))
65#endif
66
67#else
68#define BTCPP_EXPORT static
69#endif
70/* Use this macro to automatically register one or more custom Nodes
71* into a factory. For instance:
72*
73* BT_REGISTER_NODES(factory)
74* {
75* factory.registerNodeType<MoveBaseAction>("MoveBase");
76* }
77*
78* IMPORTANT: this function MUST be declared in a cpp file, NOT a header file.
79* You must add the definition [BT_PLUGIN_EXPORT] in CMakeLists.txt using:
80*
81* target_compile_definitions(my_plugin_target PRIVATE BT_PLUGIN_EXPORT )
82
83* See examples in sample_nodes directory.
84*/
85
86// NOLINTBEGIN(cppcoreguidelines-macro-usage,bugprone-macro-parentheses)
87#define BT_REGISTER_NODES(factory)
88 BTCPP_EXPORT void BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory)
89// NOLINTEND(cppcoreguidelines-macro-usage,bugprone-macro-parentheses)
90
91constexpr const char* PLUGIN_SYMBOL = "BT_RegisterNodesFromPlugin";
92
93bool WildcardMatch(const std::string& str, StringView filter);
94
95/**
96 * @brief Struct used to store a tree.
97 * If this object goes out of scope, the tree is destroyed.
98 */
99class Tree
100{
101public:
102 // a tree can contain multiple subtree.
103 struct Subtree
104 {
105 using Ptr = std::shared_ptr<Subtree>;
106 std::vector<TreeNode::Ptr> nodes;
107 Blackboard::Ptr blackboard;
108 std::string instance_name;
109 std::string tree_ID;
110 };
111
112 std::vector<Subtree::Ptr> subtrees;
113 std::unordered_map<std::string, TreeNodeManifest> manifests;
114
115 Tree();
116
117 Tree(const Tree&) = delete;
118 Tree& operator=(const Tree&) = delete;
119
120 Tree(Tree&& other) = default;
121 Tree& operator=(Tree&& other) = default;
122
123 void initialize();
124
125 void haltTree();
126
127 [[nodiscard]] TreeNode* rootNode() const;
128
129 /**
130 * @brief Sleep for a certain amount of time. This sleep could be interrupted by the methods
131 * TreeNode::emitWakeUpSignal() or Tree::emitWakeUpSignal()
132 *
133 * @param timeout duration of the sleep
134 * @return true if the timeout was NOT reached and the signal was received.
135 *
136 * */
137 bool sleep(std::chrono::system_clock::duration timeout);
138
139 /**
140 * @brief Wake up the tree. This will interrupt the sleep() method.
141 */
142 void emitWakeUpSignal();
143
144 /**
145 * @brief Returns the shared WakeUpSignal used by this tree.
146 * This can be used to check for preemption externally, e.g. when
147 * implementing custom sleep logic with a different clock source.
148 * Returns nullptr if the tree has not been initialized yet.
149 */
150 [[nodiscard]] std::shared_ptr<WakeUpSignal> wakeUpSignal() const;
151
152 ~Tree();
153
154 /// Tick the root of the tree once, even if a node invoked
155 /// emitWakeUpSignal()
157
158 /**
159 * @brief by default, tickOnce() sends a single tick, BUT
160 * as long as there is at least one node of the tree
161 * invoking TreeNode::emitWakeUpSignal(), it will be ticked again.
162 */
164
165 /// Call tickOnce until the status is different from RUNNING.
166 /// Note that between one tick and the following one,
167 /// a Tree::sleep() is used
169 tickWhileRunning(std::chrono::milliseconds sleep_time = std::chrono::milliseconds(10));
170
171 [[nodiscard]] Blackboard::Ptr rootBlackboard();
172
173 //Call the visitor for each node of the tree.
174 void applyVisitor(const std::function<void(const TreeNode*)>& visitor) const;
175
176 //Call the visitor for each node of the tree.
177 void applyVisitor(const std::function<void(TreeNode*)>& visitor);
178
179 [[nodiscard]] uint16_t getUID();
180
181 /// Get a list of nodes which fullPath() match a wildcard filter and
182 /// a given path. Example:
183 ///
184 /// move_nodes = tree.getNodesByPath<MoveBaseNode>("move_*");
185 ///
186 template <typename NodeType = BT::TreeNode>
187 [[nodiscard]] std::vector<const TreeNode*>
189 {
190 std::vector<const TreeNode*> nodes;
191 for(auto const& subtree : subtrees)
192 {
193 for(auto const& node : subtree->nodes)
194 {
195 if(auto node_recast = dynamic_cast<const NodeType*>(node.get()))
196 {
197 if(WildcardMatch(node->fullPath(), wildcard_filter))
198 {
199 nodes.push_back(node.get());
200 }
201 }
202 }
203 }
204 return nodes;
205 }
206
207private:
208 friend class BehaviorTreeFactory;
209
210 std::shared_ptr<WakeUpSignal> wake_up_;
211
212 enum TickOption
213 {
214 EXACTLY_ONCE,
215 ONCE_UNLESS_WOKEN_UP,
216 WHILE_RUNNING
217 };
218
219 NodeStatus tickRoot(TickOption opt, std::chrono::milliseconds sleep_time);
220
221 // Fix #1046: re-point each node's NodeConfig::manifest from the
222 // factory's map to the tree's own copy so the pointers remain
223 // valid after the factory is destroyed.
224 void remapManifestPointers();
225
226 uint16_t uid_counter_ = 0;
227};
228
229class Parser;
230
231/**
232 * @brief The BehaviorTreeFactory is used to create instances of a
233 * TreeNode at run-time.
234 *
235 * Some node types are "builtin", whilst other are used defined and need
236 * to be registered using a unique ID.
237 */
239{
240public:
241 BehaviorTreeFactory();
242 ~BehaviorTreeFactory();
243
244 BehaviorTreeFactory(const BehaviorTreeFactory& other) = delete;
245 BehaviorTreeFactory& operator=(const BehaviorTreeFactory& other) = delete;
246
247 BehaviorTreeFactory(BehaviorTreeFactory&& other) noexcept;
248 BehaviorTreeFactory& operator=(BehaviorTreeFactory&& other) noexcept;
249
250 /// Remove a registered ID.
251 bool unregisterBuilder(const std::string& ID);
252
253 /** The most generic way to register a NodeBuilder.
254 *
255 * Throws if you try to register twice a builder with the same
256 * registration_ID.
257 */
258 void registerBuilder(const TreeNodeManifest& manifest, const NodeBuilder& builder);
259
260 template <typename T>
261 void registerBuilder(const std::string& ID, const NodeBuilder& builder)
262 {
263 auto manifest = CreateManifest<T>(ID);
264 registerBuilder(manifest, builder);
265 }
266
267 /**
268 * @brief registerSimpleAction help you register nodes of type SimpleActionNode.
269 *
270 * @param ID registration ID
271 * @param tick_functor the callback to be invoked in the tick() method.
272 * @param ports if your SimpleNode requires ports, provide the list here.
273 *
274 * */
275 void registerSimpleAction(const std::string& ID,
276 const SimpleActionNode::TickFunctor& tick_functor,
277 PortsList ports = {});
278 /**
279 * @brief registerSimpleCondition help you register nodes of type SimpleConditionNode.
280 *
281 * @param ID registration ID
282 * @param tick_functor the callback to be invoked in the tick() method.
283 * @param ports if your SimpleNode requires ports, provide the list here.
284 *
285 * */
286 void registerSimpleCondition(const std::string& ID,
287 const SimpleConditionNode::TickFunctor& tick_functor,
288 PortsList ports = {});
289 /**
290 * @brief registerSimpleDecorator help you register nodes of type SimpleDecoratorNode.
291 *
292 * @param ID registration ID
293 * @param tick_functor the callback to be invoked in the tick() method.
294 * @param ports if your SimpleNode requires ports, provide the list here.
295 *
296 * */
297 void registerSimpleDecorator(const std::string& ID,
298 const SimpleDecoratorNode::TickFunctor& tick_functor,
299 PortsList ports = {});
300
301 /**
302 * @brief registerFromPlugin load a shared library and execute the function BT_REGISTER_NODES (see macro).
303 *
304 * @param file_path path of the file
305 */
306 void registerFromPlugin(const std::string& file_path);
307
308 /**
309 * @brief registerFromROSPlugins finds all shared libraries that export ROS plugins for behaviortree_cpp, and calls registerFromPlugin for each library.
310 * @throws If not compiled with ROS support or if the library cannot load for any reason
311 */
312 [[deprecated("Removed support for ROS1")]] void registerFromROSPlugins();
313
314 /**
315 * @brief registerBehaviorTreeFromFile.
316 * Load the definition of an entire behavior tree, but don't instantiate it.
317 * You can instantiate it later with:
318 *
319 * BehaviorTreeFactory::createTree(tree_id)
320 *
321 * where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">
322 *
323 */
324 void registerBehaviorTreeFromFile(const std::filesystem::path& filename);
325
326 /// Same of registerBehaviorTreeFromFile, but passing the XML text,
327 /// instead of the filename.
328 void registerBehaviorTreeFromText(const std::string& xml_text);
329
330 /// Returns the ID of the trees registered either with
331 /// registerBehaviorTreeFromFile or registerBehaviorTreeFromText.
333
334 /**
335 * @brief Clear previously-registered behavior trees.
336 */
338
339 /**
340 * @brief instantiateTreeNode creates an instance of a previously registered TreeNode.
341 *
342 * @param name name of this particular instance
343 * @param ID ID used when it was registered
344 * @param config configuration that is passed to the constructor of the TreeNode.
345 * @return new node.
346 */
348 const std::string& name, const std::string& ID, const NodeConfig& config) const;
349
350 /** registerNodeType where you explicitly pass the list of ports.
351 * Doesn't require the implementation of static method providedPorts()
352 */
353 template <typename T, typename... ExtraArgs>
354 void registerNodeType(const std::string& ID, const PortsList& ports, ExtraArgs... args)
355 {
356 static_assert(std::is_base_of<ActionNodeBase, T>::value ||
357 std::is_base_of<ControlNode, T>::value ||
358 std::is_base_of<DecoratorNode, T>::value ||
359 std::is_base_of<ConditionNode, T>::value,
360 "[registerNode]: accepts only classed derived from either "
361 "ActionNodeBase, "
362 "DecoratorNode, ControlNode or ConditionNode");
363
364 constexpr bool default_constructable =
365 std::is_constructible<T, const std::string&>::value;
366 constexpr bool param_constructable =
367 std::is_constructible<T, const std::string&, const NodeConfig&,
368 ExtraArgs...>::value;
369
370 // clang-format off
371 static_assert(!std::is_abstract<T>::value,
372 "[registerNode]: Some methods are pure virtual. "
373 "Did you override the methods tick() and halt()?");
374
375 static_assert(default_constructable || param_constructable,
376 "[registerNode]: the registered class must have at least one of these two constructors:\n"
377 " (const std::string&, const NodeConfig&) or (const std::string&)\n"
378 "Check also if the constructor is public!)");
379 // clang-format on
380
381 registerBuilder(CreateManifest<T>(ID, ports), CreateBuilder<T>(args...));
382 }
383
384 /** registerNodeType is the method to use to register your custom TreeNode.
385 *
386 * It accepts only classed derived from either ActionNodeBase, DecoratorNode,
387 * ControlNode or ConditionNode.
388 */
389 template <typename T, typename... ExtraArgs>
390 void registerNodeType(const std::string& ID, ExtraArgs... args)
391 {
392 if constexpr(std::is_abstract_v<T>)
393 {
394 // check first if the given class is abstract
395 static_assert(!std::is_abstract_v<T>, "The Node type can't be abstract. "
396 "Did you forget to implement an abstract "
397 "method in the derived class?");
398 }
399 else
400 {
401 constexpr bool param_constructable =
402 std::is_constructible<T, const std::string&, const NodeConfig&,
403 ExtraArgs...>::value;
404 constexpr bool has_static_ports_list = has_static_method_providedPorts<T>::value;
405
406 // clang-format off
407 static_assert(!(param_constructable && !has_static_ports_list),
408 "[registerNode]: you MUST implement the static method:\n"
409 " PortsList providedPorts();\n");
410
411 // When extra arguments were passed to registerNodeType but the full
412 // constructor signature doesn't match, the problem is most likely a
413 // type mismatch in those extra arguments (issue #837).
414 static_assert(!(has_static_ports_list && !param_constructable
415 && sizeof...(ExtraArgs) > 0),
416 "[registerNode]: the constructor is NOT compatible with the "
417 "arguments provided.\n"
418 "Verify that the types of the extra arguments passed to "
419 "registerNodeType match\n"
420 "the constructor signature: "
421 "(const std::string&, const NodeConfig&, ...)\n");
422
423 static_assert(!(has_static_ports_list && !param_constructable
424 && sizeof...(ExtraArgs) == 0),
425 "[registerNode]: since you have a static method providedPorts(),\n"
426 "you MUST add a constructor with signature:\n"
427 "(const std::string&, const NodeConfig&)\n");
428 }
429 // clang-format on
430 registerNodeType<T>(ID, getProvidedPorts<T>(), args...);
431 }
432
433 /// All the builders. Made available mostly for debug purposes.
435
436 /// Manifests of all the registered TreeNodes.
438 manifests() const;
439
440 /// List of builtin IDs.
441 [[nodiscard]] const std::set<std::string>& builtinNodes() const;
442
443 /**
444 * @brief createTreeFromText will parse the XML directly from string.
445 * The XML needs to contain either a single <BehaviorTree> or specify
446 * the attribute [main_tree_to_execute].
447 *
448 * Consider using instead registerBehaviorTreeFromText() and createTree().
449 *
450 * @param text string containing the XML
451 * @param blackboard blackboard of the root tree
452 * @return the newly created tree
453 */
455 const std::string& text, Blackboard::Ptr blackboard = Blackboard::create());
456
457 /**
458 * @brief createTreeFromFile will parse the XML from a given file.
459 * The XML needs to contain either a single <BehaviorTree> or specify
460 * the attribute [main_tree_to_execute].
461 *
462 * Consider using instead registerBehaviorTreeFromFile() and createTree().
463 *
464 * @param file_path location of the file to load
465 * @param blackboard blackboard of the root tree
466 * @return the newly created tree
467 */
468 [[nodiscard]] Tree
469 createTreeFromFile(const std::filesystem::path& file_path,
470 Blackboard::Ptr blackboard = Blackboard::create());
471
472 [[nodiscard]] Tree createTree(const std::string& tree_name,
473 Blackboard::Ptr blackboard = Blackboard::create());
474
475 /// Add metadata to a specific manifest. This metadata will be added
476 /// to <TreeNodesModel> with the function writeTreeNodesModelXML()
477 void addMetadataToManifest(const std::string& node_id, const KeyValueVector& metadata);
478
479 /**
480 * @brief Add an Enum to the scripting language.
481 * For instance if you do:
482 *
483 * registerScriptingEnum("THE_ANSWER", 42),
484 *
485 * You may type this in your scripts:
486 *
487 * <Script code="myport:=THE_ANSWER" />
488 *
489 * @param name string representation of the enum
490 * @param value its value.
491 */
492 void registerScriptingEnum(StringView name, int value);
493
494 /**
495 * @brief registerScriptingEnums is syntactic sugar
496 * to automatically register multiple enums. We use
497 * https://github.com/Neargye/magic_enum.
498 *
499 * Please refer to https://github.com/Neargye/magic_enum/blob/master/doc/limitations.md
500 * for limitations.
501 */
502 template <typename EnumType>
504 {
505 constexpr auto entries = magic_enum::enum_entries<EnumType>();
506 for(const auto& it : entries)
507 {
508 registerScriptingEnum(it.second, static_cast<int>(it.first));
509 }
510 }
511
512 void clearSubstitutionRules();
513
514 using SubstitutionRule =
516
517 /**
518 * @brief addSubstitutionRule replace a node with another one when the tree is
519 * created.
520 * If the rule ia a string, we will use a different node type (already registered)
521 * instead.
522 * If the rule is a TestNodeConfig, a test node with that configuration will be created instead.
523 *
524 * @param filter filter used to select the node to sobstitute. The node path is used.
525 * You may use wildcard matching.
526 * @param rule pass either a string or a TestNodeConfig
527 */
528 void addSubstitutionRule(StringView filter, SubstitutionRule rule);
529
530 /**
531 * @brief loadSubstitutionRuleFromJSON will parse a JSON file to
532 * create a set of substitution rules. See Tutorial 11
533 * for an example of the syntax.
534 *
535 * @param json_text the JSON file as text (BOT the path of the file)
536 */
537 void loadSubstitutionRuleFromJSON(const std::string& json_text);
538
539 /**
540 * @brief substitutionRules return the current substitution rules.
541 */
543 substitutionRules() const;
544
545 /**
546 * @brief Register a polymorphic cast relationship between Derived and Base types.
547 *
548 * This enables passing shared_ptr<Derived> to ports expecting shared_ptr<Base>
549 * without type mismatch errors. The relationship is automatically applied
550 * to all trees created from this factory.
551 *
552 * Example:
553 * factory.registerPolymorphicCast<Cat, Animal>();
554 * factory.registerPolymorphicCast<Sphynx, Cat>();
555 *
556 * @tparam Derived The derived class (must inherit from Base)
557 * @tparam Base The base class (must be polymorphic)
558 */
559 template <typename Derived, typename Base>
561 {
562 polymorphicCastRegistry().registerCast<Derived, Base>();
563 }
564
565 /**
566 * @brief Access the polymorphic cast registry.
567 *
568 * The registry is shared with all trees created from this factory,
569 * allowing trees to outlive the factory while maintaining access
570 * to polymorphic cast relationships.
571 */
573 [[nodiscard]] const PolymorphicCastRegistry& polymorphicCastRegistry() const;
574
575 /**
576 * @brief Get a shared pointer to the polymorphic cast registry.
577 *
578 * This allows trees and blackboards to hold a reference to the registry
579 * that outlives the factory.
580 */
581 [[nodiscard]] std::shared_ptr<PolymorphicCastRegistry>
583
584private:
585 struct PImpl;
586 std::unique_ptr<PImpl> _p;
587};
588
589/**
590 * @brief BlackboardClone make a copy of the content of the
591 * blackboard
592 * @param src source
593 * @param dst destination
594 */
595void BlackboardClone(const Blackboard& src, Blackboard& dst);
596
597/**
598 * @brief BlackboardBackup uses Blackboard::cloneInto to backup
599 * all the blackboards of the tree
600 *
601 * @param tree source
602 * @return destination (the backup)
603 */
605
606/**
607 * @brief BlackboardRestore uses Blackboard::cloneInto to restore
608 * all the blackboards of the tree
609 *
610 * @param backup a vector of blackboards
611 * @param tree the destination
612 */
613void BlackboardRestore(const std::vector<Blackboard::Ptr>& backup, BT::Tree& tree);
614
615/**
616 * @brief ExportTreeToJSON it calls ExportBlackboardToJSON
617 * for all the blackboards in the tree
618 */
620
621/**
622 * @brief ImportTreeFromJSON it calls ImportBlackboardFromJSON
623 * for all the blackboards in the tree
624 */
625void ImportTreeFromJSON(const nlohmann::json& json, BT::Tree& tree);
626
627} // namespace BT
628
629#endif // BT_FACTORY_H
#define BTCPP_EXPORT
Definition: bt_factory.h:68
The ActionNodeBase is the base class to use to create any kind of action. A particular derived class ...
Definition: action_node.h:35
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:239
void addSubstitutionRule(StringView filter, SubstitutionRule rule)
addSubstitutionRule replace a node with another one when the tree is created. If the rule ia a string...
void registerSimpleCondition(const std::string &ID, const SimpleConditionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleCondition help you register nodes of type SimpleConditionNode.
std::shared_ptr< PolymorphicCastRegistry > polymorphicCastRegistryPtr() const
Get a shared pointer to the polymorphic cast registry.
void registerBuilder(const TreeNodeManifest &manifest, const NodeBuilder &builder)
void loadSubstitutionRuleFromJSON(const std::string &json_text)
loadSubstitutionRuleFromJSON will parse a JSON file to create a set of substitution rules....
void registerNodeType(const std::string &ID, ExtraArgs... args)
Definition: bt_factory.h:390
void clearRegisteredBehaviorTrees()
Clear previously-registered behavior trees.
void registerPolymorphicCast()
Register a polymorphic cast relationship between Derived and Base types.
Definition: bt_factory.h:560
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
PolymorphicCastRegistry & polymorphicCastRegistry()
Access the polymorphic cast registry.
std::vector< std::string > registeredBehaviorTrees() const
void registerBehaviorTreeFromText(const std::string &xml_text)
const std::unordered_map< std::string, SubstitutionRule > & substitutionRules() const
substitutionRules return the current substitution rules.
void registerScriptingEnum(StringView name, int value)
Add an Enum to the scripting language. For instance if you do:
void registerSimpleAction(const std::string &ID, const SimpleActionNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleAction help you register nodes of type SimpleActionNode.
void registerScriptingEnums()
registerScriptingEnums is syntactic sugar to automatically register multiple enums....
Definition: bt_factory.h:503
void registerBehaviorTreeFromFile(const std::filesystem::path &filename)
registerBehaviorTreeFromFile. Load the definition of an entire behavior tree, but don't instantiate i...
const std::unordered_map< std::string, NodeBuilder > & builders() const
All the builders. Made available mostly for debug purposes.
const std::set< std::string > & builtinNodes() const
List of builtin IDs.
void addMetadataToManifest(const std::string &node_id, const KeyValueVector &metadata)
const std::unordered_map< std::string, TreeNodeManifest > & manifests() const
Manifests of all the registered TreeNodes.
std::unique_ptr< TreeNode > instantiateTreeNode(const std::string &name, const std::string &ID, const NodeConfig &config) const
instantiateTreeNode creates an instance of a previously registered TreeNode.
void registerNodeType(const std::string &ID, const PortsList &ports, ExtraArgs... args)
Definition: bt_factory.h:354
Tree createTreeFromFile(const std::filesystem::path &file_path, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromFile will parse the XML from a given file. The XML needs to contain either a single <Be...
void registerSimpleDecorator(const std::string &ID, const SimpleDecoratorNode::TickFunctor &tick_functor, PortsList ports={})
registerSimpleDecorator help you register nodes of type SimpleDecoratorNode.
bool unregisterBuilder(const std::string &ID)
Remove a registered ID.
void registerFromPlugin(const std::string &file_path)
registerFromPlugin load a shared library and execute the function BT_REGISTER_NODES (see macro).
void registerFromROSPlugins()
registerFromROSPlugins finds all shared libraries that export ROS plugins for behaviortree_cpp,...
The Blackboard is the mechanism used by BehaviorTrees to exchange typed data.
Definition: blackboard.h:39
The ConditionNode is a leaf node used to check a condition.
Definition: condition_node.h:32
The ControlNode is the base class for nodes that can have multiple children.
Definition: control_node.h:32
The DecoratorNode is the base class for nodes that have exactly one child.
Definition: decorator_node.h:19
The BehaviorTreeParser is a class used to read the model of a BehaviorTree from file or text and inst...
Definition: bt_parser.h:28
Registry for polymorphic shared_ptr cast relationships.
Definition: polymorphic_cast_registry.hpp:47
The SimpleActionNode provides an easy to use SyncActionNode. The user should simply provide a callbac...
Definition: action_node.h:89
The SimpleConditionNode provides an easy to use ConditionNode. The user should simply provide a callb...
Definition: condition_node.h:66
The SimpleDecoratorNode provides an easy to use DecoratorNode. The user should simply provide a callb...
Definition: decorator_node.h:68
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Definition: bt_factory.h:100
std::shared_ptr< WakeUpSignal > wakeUpSignal() const
Returns the shared WakeUpSignal used by this tree. This can be used to check for preemption externall...
NodeStatus tickOnce()
by default, tickOnce() sends a single tick, BUT as long as there is at least one node of the tree inv...
std::vector< const TreeNode * > getNodesByPath(StringView wildcard_filter) const
Definition: bt_factory.h:188
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
NodeStatus tickExactlyOnce()
bool sleep(std::chrono::system_clock::duration timeout)
Sleep for a certain amount of time. This sleep could be interrupted by the methods TreeNode::emitWake...
void emitWakeUpSignal()
Wake up the tree. This will interrupt the sleep() method.
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:154
Definition: wakeup_signal.hpp:13
Definition: action_node.h:24
nlohmann::json ExportTreeToJSON(const BT::Tree &tree)
ExportTreeToJSON it calls ExportBlackboardToJSON for all the blackboards in the tree.
NodeStatus
Definition: basic_types.h:34
std::vector< Blackboard::Ptr > BlackboardBackup(const BT::Tree &tree)
BlackboardBackup uses Blackboard::cloneInto to backup all the blackboards of the tree.
void ImportTreeFromJSON(const nlohmann::json &json, BT::Tree &tree)
ImportTreeFromJSON it calls ImportBlackboardFromJSON for all the blackboards in the tree.
void BlackboardRestore(const std::vector< Blackboard::Ptr > &backup, BT::Tree &tree)
BlackboardRestore uses Blackboard::cloneInto to restore all the blackboards of the tree.
void BlackboardClone(const Blackboard &src, Blackboard &dst)
BlackboardClone make a copy of the content of the blackboard.
Definition: tree_node.h:105
Definition: bt_factory.h:104
This information is used mostly by the XMLParser.
Definition: tree_node.h:37