2
3
4
5
6
7
8
9
10
11
12
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"
26#include <unordered_map>
35template <
typename T,
typename... Args>
36inline NodeBuilder CreateBuilder(Args... args)
38 return [=](
const std::string& name,
const NodeConfig& config) {
39 return TreeNode::Instantiate<T, Args...>(name, config, args...);
45 PortsList portlist = getProvidedPorts<T>())
47 if constexpr(has_static_method_metadata<T>::value)
49 return { getType<T>(), ID, portlist, T::metadata() };
51 return { getType<T>(), ID, portlist, {} };
54#ifdef BT_PLUGIN_EXPORT
57#define BTCPP_EXPORT extern "C" __declspec(dllexport)
60#define BTCPP_EXPORT extern "C" __attribute__((visibility("default")))
64#define BTCPP_EXPORT static
67
68
69
70
71
72
73
74
75
76
77
78
79
80
83#define BT_REGISTER_NODES(factory)
84 BTCPP_EXPORT void BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory)
87constexpr const char* PLUGIN_SYMBOL =
"BT_RegisterNodesFromPlugin";
89bool WildcardMatch(
const std::string& str, StringView filter);
92
93
94
101 using Ptr = std::shared_ptr<
Subtree>;
102 std::vector<TreeNode::Ptr> nodes;
104 std::string instance_name;
108 std::vector<Subtree::Ptr> subtrees;
109 std::unordered_map<std::string, TreeNodeManifest> manifests;
113 Tree(
const Tree&) =
delete;
114 Tree& operator=(
const Tree&) =
delete;
116 Tree(
Tree&& other) =
default;
117 Tree& operator=(
Tree&& other) =
default;
126
127
128
129
130
131
132
133 bool sleep(std::chrono::system_clock::duration timeout);
136
137
147
148
149
150
162 void applyVisitor(
const std::function<
void(
const TreeNode*)>& visitor)
const;
165 void applyVisitor(
const std::function<
void(
TreeNode*)>& visitor);
178 std::vector<
const TreeNode*> nodes;
179 for(
auto const& subtree : subtrees)
181 for(
auto const& node : subtree->nodes)
183 if(
auto node_recast =
dynamic_cast<
const NodeType*>(node.get()))
185 if(WildcardMatch(node->fullPath(), wildcard_filter))
187 nodes.push_back(node.get());
203 ONCE_UNLESS_WOKEN_UP,
207 NodeStatus tickRoot(TickOption opt, std::chrono::milliseconds sleep_time);
212 void remapManifestPointers();
214 uint16_t uid_counter_ = 0;
220
221
222
223
224
225
229 BehaviorTreeFactory();
230 ~BehaviorTreeFactory();
242
243
244
245
248 template <
typename T>
249 void registerBuilder(
const std::string& ID,
const NodeBuilder& builder)
251 auto manifest = CreateManifest<T>(ID);
252 registerBuilder(manifest, builder);
256
257
258
259
260
261
262
265 PortsList ports = {});
267
268
269
270
271
272
273
276 PortsList ports = {});
278
279
280
281
282
283
284
287 PortsList ports = {});
290
291
292
293
297
298
299
303
304
305
306
307
308
309
310
311
323
324
328
329
330
331
332
333
334
339
340
341 template <
typename T,
typename... ExtraArgs>
342 void registerNodeType(
const std::string& ID,
const PortsList& ports, ExtraArgs... args)
348 "[registerNode]: accepts only classed derived from either "
350 "DecoratorNode, ControlNode or ConditionNode");
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;
359 static_assert(!std::is_abstract<T>::value,
360 "[registerNode]: Some methods are pure virtual. "
361 "Did you override the methods tick() and halt()?");
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!)");
369 registerBuilder(CreateManifest<T>(ID, ports), CreateBuilder<T>(args...));
373
374
375
376
377 template <
typename T,
typename... ExtraArgs>
380 if constexpr(std::is_abstract_v<T>)
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?");
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;
395 static_assert(!(param_constructable && !has_static_ports_list),
396 "[registerNode]: you MUST implement the static method:\n"
397 " PortsList providedPorts();\n");
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");
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");
418 registerNodeType<T>(ID, getProvidedPorts<T>(), args...);
432
433
434
435
436
437
438
439
440
441
446
447
448
449
450
451
452
453
454
455
461 Blackboard::Ptr blackboard = Blackboard::create());
468
469
470
471
472
473
474
475
476
477
478
479
483
484
485
486
487
488
489
490 template <
typename EnumType>
493 constexpr auto entries = magic_enum::enum_entries<EnumType>();
494 for(
const auto& it : entries)
496 registerScriptingEnum(it.second,
static_cast<
int>(it.first));
500 void clearSubstitutionRules();
506
507
508
509
510
511
512
513
514
515
519
520
521
522
523
524
528
529
534
535
536
537
538
539
540
541
542
543
544
545
546
547 template <
typename Derived,
typename Base>
554
555
556
557
558
559
564
565
566
567
568
574 std::unique_ptr<PImpl> _p;
578
579
580
581
582
586
587
588
589
590
591
595
596
597
598
599
600
604
605
606
610
611
612
#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