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 ~Tree();
141
142 /// Tick the root of the tree once, even if a node invoked
143 /// emitWakeUpSignal()
145
146 /**
147 * @brief by default, tickOnce() sends a single tick, BUT
148 * as long as there is at least one node of the tree
149 * invoking TreeNode::emitWakeUpSignal(), it will be ticked again.
150 */
152
153 /// Call tickOnce until the status is different from RUNNING.
154 /// Note that between one tick and the following one,
155 /// a Tree::sleep() is used
157 tickWhileRunning(std::chrono::milliseconds sleep_time = std::chrono::milliseconds(10));
158
159 [[nodiscard]] Blackboard::Ptr rootBlackboard();
160
161 //Call the visitor for each node of the tree.
162 void applyVisitor(const std::function<void(const TreeNode*)>& visitor) const;
163
164 //Call the visitor for each node of the tree.
165 void applyVisitor(const std::function<void(TreeNode*)>& visitor);
166
167 [[nodiscard]] uint16_t getUID();
168
169 /// Get a list of nodes which fullPath() match a wildcard filter and
170 /// a given path. Example:
171 ///
172 /// move_nodes = tree.getNodesByPath<MoveBaseNode>("move_*");
173 ///
174 template <typename NodeType = BT::TreeNode>
175 [[nodiscard]] std::vector<const TreeNode*>
177 {
178 std::vector<const TreeNode*> nodes;
179 for(auto const& subtree : subtrees)
180 {
181 for(auto const& node : subtree->nodes)
182 {
183 if(auto node_recast = dynamic_cast<const NodeType*>(node.get()))
184 {
185 if(WildcardMatch(node->fullPath(), wildcard_filter))
186 {
187 nodes.push_back(node.get());
188 }
189 }
190 }
191 }
192 return nodes;
193 }
194
195private:
196 friend class BehaviorTreeFactory;
197
198 std::shared_ptr<WakeUpSignal> wake_up_;
199
200 enum TickOption
201 {
202 EXACTLY_ONCE,
203 ONCE_UNLESS_WOKEN_UP,
204 WHILE_RUNNING
205 };
206
207 NodeStatus tickRoot(TickOption opt, std::chrono::milliseconds sleep_time);
208
209 // Fix #1046: re-point each node's NodeConfig::manifest from the
210 // factory's map to the tree's own copy so the pointers remain
211 // valid after the factory is destroyed.
212 void remapManifestPointers();
213
214 uint16_t uid_counter_ = 0;
215};
216
217class Parser;
218
219/**
220 * @brief The BehaviorTreeFactory is used to create instances of a
221 * TreeNode at run-time.
222 *
223 * Some node types are "builtin", whilst other are used defined and need
224 * to be registered using a unique ID.
225 */
227{
228public:
229 BehaviorTreeFactory();
230 ~BehaviorTreeFactory();
231
232 BehaviorTreeFactory(const BehaviorTreeFactory& other) = delete;
233 BehaviorTreeFactory& operator=(const BehaviorTreeFactory& other) = delete;
234
235 BehaviorTreeFactory(BehaviorTreeFactory&& other) noexcept;
236 BehaviorTreeFactory& operator=(BehaviorTreeFactory&& other) noexcept;
237
238 /// Remove a registered ID.
239 bool unregisterBuilder(const std::string& ID);
240
241 /** The most generic way to register a NodeBuilder.
242 *
243 * Throws if you try to register twice a builder with the same
244 * registration_ID.
245 */
246 void registerBuilder(const TreeNodeManifest& manifest, const NodeBuilder& builder);
247
248 template <typename T>
249 void registerBuilder(const std::string& ID, const NodeBuilder& builder)
250 {
251 auto manifest = CreateManifest<T>(ID);
252 registerBuilder(manifest, builder);
253 }
254
255 /**
256 * @brief registerSimpleAction help you register nodes of type SimpleActionNode.
257 *
258 * @param ID registration ID
259 * @param tick_functor the callback to be invoked in the tick() method.
260 * @param ports if your SimpleNode requires ports, provide the list here.
261 *
262 * */
263 void registerSimpleAction(const std::string& ID,
264 const SimpleActionNode::TickFunctor& tick_functor,
265 PortsList ports = {});
266 /**
267 * @brief registerSimpleCondition help you register nodes of type SimpleConditionNode.
268 *
269 * @param ID registration ID
270 * @param tick_functor the callback to be invoked in the tick() method.
271 * @param ports if your SimpleNode requires ports, provide the list here.
272 *
273 * */
274 void registerSimpleCondition(const std::string& ID,
275 const SimpleConditionNode::TickFunctor& tick_functor,
276 PortsList ports = {});
277 /**
278 * @brief registerSimpleDecorator help you register nodes of type SimpleDecoratorNode.
279 *
280 * @param ID registration ID
281 * @param tick_functor the callback to be invoked in the tick() method.
282 * @param ports if your SimpleNode requires ports, provide the list here.
283 *
284 * */
285 void registerSimpleDecorator(const std::string& ID,
286 const SimpleDecoratorNode::TickFunctor& tick_functor,
287 PortsList ports = {});
288
289 /**
290 * @brief registerFromPlugin load a shared library and execute the function BT_REGISTER_NODES (see macro).
291 *
292 * @param file_path path of the file
293 */
294 void registerFromPlugin(const std::string& file_path);
295
296 /**
297 * @brief registerFromROSPlugins finds all shared libraries that export ROS plugins for behaviortree_cpp, and calls registerFromPlugin for each library.
298 * @throws If not compiled with ROS support or if the library cannot load for any reason
299 */
300 [[deprecated("Removed support for ROS1")]] void registerFromROSPlugins();
301
302 /**
303 * @brief registerBehaviorTreeFromFile.
304 * Load the definition of an entire behavior tree, but don't instantiate it.
305 * You can instantiate it later with:
306 *
307 * BehaviorTreeFactory::createTree(tree_id)
308 *
309 * where "tree_id" come from the XML attribute <BehaviorTree ID="tree_id">
310 *
311 */
312 void registerBehaviorTreeFromFile(const std::filesystem::path& filename);
313
314 /// Same of registerBehaviorTreeFromFile, but passing the XML text,
315 /// instead of the filename.
316 void registerBehaviorTreeFromText(const std::string& xml_text);
317
318 /// Returns the ID of the trees registered either with
319 /// registerBehaviorTreeFromFile or registerBehaviorTreeFromText.
321
322 /**
323 * @brief Clear previously-registered behavior trees.
324 */
326
327 /**
328 * @brief instantiateTreeNode creates an instance of a previously registered TreeNode.
329 *
330 * @param name name of this particular instance
331 * @param ID ID used when it was registered
332 * @param config configuration that is passed to the constructor of the TreeNode.
333 * @return new node.
334 */
336 const std::string& name, const std::string& ID, const NodeConfig& config) const;
337
338 /** registerNodeType where you explicitly pass the list of ports.
339 * Doesn't require the implementation of static method providedPorts()
340 */
341 template <typename T, typename... ExtraArgs>
342 void registerNodeType(const std::string& ID, const PortsList& ports, ExtraArgs... args)
343 {
344 static_assert(std::is_base_of<ActionNodeBase, T>::value ||
345 std::is_base_of<ControlNode, T>::value ||
346 std::is_base_of<DecoratorNode, T>::value ||
347 std::is_base_of<ConditionNode, T>::value,
348 "[registerNode]: accepts only classed derived from either "
349 "ActionNodeBase, "
350 "DecoratorNode, ControlNode or ConditionNode");
351
352 constexpr bool default_constructable =
353 std::is_constructible<T, const std::string&>::value;
354 constexpr bool param_constructable =
355 std::is_constructible<T, const std::string&, const NodeConfig&,
356 ExtraArgs...>::value;
357
358 // clang-format off
359 static_assert(!std::is_abstract<T>::value,
360 "[registerNode]: Some methods are pure virtual. "
361 "Did you override the methods tick() and halt()?");
362
363 static_assert(default_constructable || param_constructable,
364 "[registerNode]: the registered class must have at least one of these two constructors:\n"
365 " (const std::string&, const NodeConfig&) or (const std::string&)\n"
366 "Check also if the constructor is public!)");
367 // clang-format on
368
369 registerBuilder(CreateManifest<T>(ID, ports), CreateBuilder<T>(args...));
370 }
371
372 /** registerNodeType is the method to use to register your custom TreeNode.
373 *
374 * It accepts only classed derived from either ActionNodeBase, DecoratorNode,
375 * ControlNode or ConditionNode.
376 */
377 template <typename T, typename... ExtraArgs>
378 void registerNodeType(const std::string& ID, ExtraArgs... args)
379 {
380 if constexpr(std::is_abstract_v<T>)
381 {
382 // check first if the given class is abstract
383 static_assert(!std::is_abstract_v<T>, "The Node type can't be abstract. "
384 "Did you forget to implement an abstract "
385 "method in the derived class?");
386 }
387 else
388 {
389 constexpr bool param_constructable =
390 std::is_constructible<T, const std::string&, const NodeConfig&,
391 ExtraArgs...>::value;
392 constexpr bool has_static_ports_list = has_static_method_providedPorts<T>::value;
393
394 // clang-format off
395 static_assert(!(param_constructable && !has_static_ports_list),
396 "[registerNode]: you MUST implement the static method:\n"
397 " PortsList providedPorts();\n");
398
399 // When extra arguments were passed to registerNodeType but the full
400 // constructor signature doesn't match, the problem is most likely a
401 // type mismatch in those extra arguments (issue #837).
402 static_assert(!(has_static_ports_list && !param_constructable
403 && sizeof...(ExtraArgs) > 0),
404 "[registerNode]: the constructor is NOT compatible with the "
405 "arguments provided.\n"
406 "Verify that the types of the extra arguments passed to "
407 "registerNodeType match\n"
408 "the constructor signature: "
409 "(const std::string&, const NodeConfig&, ...)\n");
410
411 static_assert(!(has_static_ports_list && !param_constructable
412 && sizeof...(ExtraArgs) == 0),
413 "[registerNode]: since you have a static method providedPorts(),\n"
414 "you MUST add a constructor with signature:\n"
415 "(const std::string&, const NodeConfig&)\n");
416 }
417 // clang-format on
418 registerNodeType<T>(ID, getProvidedPorts<T>(), args...);
419 }
420
421 /// All the builders. Made available mostly for debug purposes.
423
424 /// Manifests of all the registered TreeNodes.
426 manifests() const;
427
428 /// List of builtin IDs.
429 [[nodiscard]] const std::set<std::string>& builtinNodes() const;
430
431 /**
432 * @brief createTreeFromText will parse the XML directly from string.
433 * The XML needs to contain either a single <BehaviorTree> or specify
434 * the attribute [main_tree_to_execute].
435 *
436 * Consider using instead registerBehaviorTreeFromText() and createTree().
437 *
438 * @param text string containing the XML
439 * @param blackboard blackboard of the root tree
440 * @return the newly created tree
441 */
443 const std::string& text, Blackboard::Ptr blackboard = Blackboard::create());
444
445 /**
446 * @brief createTreeFromFile will parse the XML from a given file.
447 * The XML needs to contain either a single <BehaviorTree> or specify
448 * the attribute [main_tree_to_execute].
449 *
450 * Consider using instead registerBehaviorTreeFromFile() and createTree().
451 *
452 * @param file_path location of the file to load
453 * @param blackboard blackboard of the root tree
454 * @return the newly created tree
455 */
456 [[nodiscard]] Tree
457 createTreeFromFile(const std::filesystem::path& file_path,
458 Blackboard::Ptr blackboard = Blackboard::create());
459
460 [[nodiscard]] Tree createTree(const std::string& tree_name,
461 Blackboard::Ptr blackboard = Blackboard::create());
462
463 /// Add metadata to a specific manifest. This metadata will be added
464 /// to <TreeNodesModel> with the function writeTreeNodesModelXML()
465 void addMetadataToManifest(const std::string& node_id, const KeyValueVector& metadata);
466
467 /**
468 * @brief Add an Enum to the scripting language.
469 * For instance if you do:
470 *
471 * registerScriptingEnum("THE_ANSWER", 42),
472 *
473 * You may type this in your scripts:
474 *
475 * <Script code="myport:=THE_ANSWER" />
476 *
477 * @param name string representation of the enum
478 * @param value its value.
479 */
480 void registerScriptingEnum(StringView name, int value);
481
482 /**
483 * @brief registerScriptingEnums is syntactic sugar
484 * to automatically register multiple enums. We use
485 * https://github.com/Neargye/magic_enum.
486 *
487 * Please refer to https://github.com/Neargye/magic_enum/blob/master/doc/limitations.md
488 * for limitations.
489 */
490 template <typename EnumType>
492 {
493 constexpr auto entries = magic_enum::enum_entries<EnumType>();
494 for(const auto& it : entries)
495 {
496 registerScriptingEnum(it.second, static_cast<int>(it.first));
497 }
498 }
499
500 void clearSubstitutionRules();
501
502 using SubstitutionRule =
504
505 /**
506 * @brief addSubstitutionRule replace a node with another one when the tree is
507 * created.
508 * If the rule ia a string, we will use a different node type (already registered)
509 * instead.
510 * If the rule is a TestNodeConfig, a test node with that configuration will be created instead.
511 *
512 * @param filter filter used to select the node to sobstitute. The node path is used.
513 * You may use wildcard matching.
514 * @param rule pass either a string or a TestNodeConfig
515 */
516 void addSubstitutionRule(StringView filter, SubstitutionRule rule);
517
518 /**
519 * @brief loadSubstitutionRuleFromJSON will parse a JSON file to
520 * create a set of substitution rules. See Tutorial 11
521 * for an example of the syntax.
522 *
523 * @param json_text the JSON file as text (BOT the path of the file)
524 */
525 void loadSubstitutionRuleFromJSON(const std::string& json_text);
526
527 /**
528 * @brief substitutionRules return the current substitution rules.
529 */
531 substitutionRules() const;
532
533 /**
534 * @brief Register a polymorphic cast relationship between Derived and Base types.
535 *
536 * This enables passing shared_ptr<Derived> to ports expecting shared_ptr<Base>
537 * without type mismatch errors. The relationship is automatically applied
538 * to all trees created from this factory.
539 *
540 * Example:
541 * factory.registerPolymorphicCast<Cat, Animal>();
542 * factory.registerPolymorphicCast<Sphynx, Cat>();
543 *
544 * @tparam Derived The derived class (must inherit from Base)
545 * @tparam Base The base class (must be polymorphic)
546 */
547 template <typename Derived, typename Base>
549 {
550 polymorphicCastRegistry().registerCast<Derived, Base>();
551 }
552
553 /**
554 * @brief Access the polymorphic cast registry.
555 *
556 * The registry is shared with all trees created from this factory,
557 * allowing trees to outlive the factory while maintaining access
558 * to polymorphic cast relationships.
559 */
561 [[nodiscard]] const PolymorphicCastRegistry& polymorphicCastRegistry() const;
562
563 /**
564 * @brief Get a shared pointer to the polymorphic cast registry.
565 *
566 * This allows trees and blackboards to hold a reference to the registry
567 * that outlives the factory.
568 */
569 [[nodiscard]] std::shared_ptr<PolymorphicCastRegistry>
571
572private:
573 struct PImpl;
574 std::unique_ptr<PImpl> _p;
575};
576
577/**
578 * @brief BlackboardClone make a copy of the content of the
579 * blackboard
580 * @param src source
581 * @param dst destination
582 */
583void BlackboardClone(const Blackboard& src, Blackboard& dst);
584
585/**
586 * @brief BlackboardBackup uses Blackboard::cloneInto to backup
587 * all the blackboards of the tree
588 *
589 * @param tree source
590 * @return destination (the backup)
591 */
593
594/**
595 * @brief BlackboardRestore uses Blackboard::cloneInto to restore
596 * all the blackboards of the tree
597 *
598 * @param backup a vector of blackboards
599 * @param tree the destination
600 */
601void BlackboardRestore(const std::vector<Blackboard::Ptr>& backup, BT::Tree& tree);
602
603/**
604 * @brief ExportTreeToJSON it calls ExportBlackboardToJSON
605 * for all the blackboards in the tree
606 */
608
609/**
610 * @brief ImportTreeFromJSON it calls ImportBlackboardFromJSON
611 * for all the blackboards in the tree
612 */
613void ImportTreeFromJSON(const nlohmann::json& json, BT::Tree& tree);
614
615} // namespace BT
616
617#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:227
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:378
void clearRegisteredBehaviorTrees()
Clear previously-registered behavior trees.
void registerPolymorphicCast()
Register a polymorphic cast relationship between Derived and Base types.
Definition: bt_factory.h:548
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:491
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:342
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
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:176
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