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