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
141
142
143
144
145
155
156
157
158
170 void applyVisitor(
const std::function<
void(
const TreeNode*)>& visitor)
const;
173 void applyVisitor(
const std::function<
void(
TreeNode*)>& visitor);
186 std::vector<
const TreeNode*> nodes;
187 for(
auto const& subtree : subtrees)
189 for(
auto const& node : subtree->nodes)
191 if(
auto node_recast =
dynamic_cast<
const NodeType*>(node.get()))
193 if(WildcardMatch(node->fullPath(), wildcard_filter))
195 nodes.push_back(node.get());
211 ONCE_UNLESS_WOKEN_UP,
215 NodeStatus tickRoot(TickOption opt, std::chrono::milliseconds sleep_time);
220 void remapManifestPointers();
222 uint16_t uid_counter_ = 0;
228
229
230
231
232
233
237 BehaviorTreeFactory();
238 ~BehaviorTreeFactory();
250
251
252
253
256 template <
typename T>
257 void registerBuilder(
const std::string& ID,
const NodeBuilder& builder)
259 auto manifest = CreateManifest<T>(ID);
260 registerBuilder(manifest, builder);
264
265
266
267
268
269
270
273 PortsList ports = {});
275
276
277
278
279
280
281
284 PortsList ports = {});
286
287
288
289
290
291
292
295 PortsList ports = {});
298
299
300
301
305
306
307
311
312
313
314
315
316
317
318
319
331
332
336
337
338
339
340
341
342
347
348
349 template <
typename T,
typename... ExtraArgs>
350 void registerNodeType(
const std::string& ID,
const PortsList& ports, ExtraArgs... args)
356 "[registerNode]: accepts only classed derived from either "
358 "DecoratorNode, ControlNode or ConditionNode");
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;
367 static_assert(!std::is_abstract<T>::value,
368 "[registerNode]: Some methods are pure virtual. "
369 "Did you override the methods tick() and halt()?");
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!)");
377 registerBuilder(CreateManifest<T>(ID, ports), CreateBuilder<T>(args...));
381
382
383
384
385 template <
typename T,
typename... ExtraArgs>
388 if constexpr(std::is_abstract_v<T>)
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?");
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;
403 static_assert(!(param_constructable && !has_static_ports_list),
404 "[registerNode]: you MUST implement the static method:\n"
405 " PortsList providedPorts();\n");
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");
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");
426 registerNodeType<T>(ID, getProvidedPorts<T>(), args...);
440
441
442
443
444
445
446
447
448
449
454
455
456
457
458
459
460
461
462
463
469 Blackboard::Ptr blackboard = Blackboard::create());
476
477
478
479
480
481
482
483
484
485
486
487
491
492
493
494
495
496
497
498 template <
typename EnumType>
501 constexpr auto entries = magic_enum::enum_entries<EnumType>();
502 for(
const auto& it : entries)
504 registerScriptingEnum(it.second,
static_cast<
int>(it.first));
508 void clearSubstitutionRules();
514
515
516
517
518
519
520
521
522
523
527
528
529
530
531
532
536
537
542
543
544
545
546
547
548
549
550
551
552
553
554
555 template <
typename Derived,
typename Base>
562
563
564
565
566
567
572
573
574
575
576
582 std::unique_ptr<PImpl> _p;
586
587
588
589
590
594
595
596
597
598
599
603
604
605
606
607
608
612
613
614
618
619
620
#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