19 #include <unordered_map> 20 #include <unordered_set> 32 typedef std::function<std::unique_ptr<TreeNode>(
const std::string&,
const NodeConfiguration&)>
42 template <
typename T>
inline 49 if( config.input_ports.empty() &&
50 config.output_ports.empty() &&
53 return std::make_unique<T>(name);
55 return std::make_unique<T>(name, config);
59 template <
typename T>
inline 65 return std::unique_ptr<TreeNode>(
new T(name, params));
69 template <
typename T>
inline 75 return std::unique_ptr<TreeNode>(
new T(name));
80 template <
typename T>
inline 83 return { getType<T>(), ID, portlist };
89 #ifndef BT_PLUGIN_EXPORT 102 #define BT_REGISTER_NODES(factory) \ 103 static void BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory) 107 #if defined(__linux__) || defined __APPLE__ 109 #define BT_REGISTER_NODES(factory) \ 110 extern "C" void __attribute__((visibility("default"))) \ 111 BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory) 115 #define BT_REGISTER_NODES(factory) \ 116 extern "C" void __declspec(dllexport) BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory) 136 std::unordered_map<std::string, TreeNodeManifest>
manifests;
146 (*this) = std::move(other);
151 nodes = std::move(other.nodes);
152 blackboard_stack = std::move(other.blackboard_stack);
153 manifests = std::move(other.manifests);
178 return nodes.empty() ?
nullptr : nodes.front().get();
213 bool unregisterBuilder(
const std::string& ID);
218 template <
typename T>
221 auto manifest = CreateManifest<T>(ID);
222 registerBuilder(manifest, builder);
233 void registerSimpleAction(
const std::string& ID,
244 void registerSimpleCondition(
const std::string& ID,
255 void registerSimpleDecorator(
const std::string& ID,
264 void registerFromPlugin(
const std::string &file_path);
271 void registerFromROSPlugins();
281 std::unique_ptr<TreeNode> instantiateTreeNode(
const std::string& name,
const std::string &ID,
289 template <
typename T>
292 static_assert(std::is_base_of<ActionNodeBase, T>::value ||
293 std::is_base_of<ControlNode, T>::value ||
294 std::is_base_of<DecoratorNode, T>::value ||
295 std::is_base_of<ConditionNode, T>::value,
296 "[registerNode]: accepts only classed derived from either ActionNodeBase, " 297 "DecoratorNode, ControlNode or ConditionNode");
299 static_assert(!std::is_abstract<T>::value,
300 "[registerNode]: Some methods are pure virtual. " 301 "Did you override the methods tick() and halt()?");
303 constexpr
bool default_constructable = std::is_constructible<T, const std::string&>::value;
304 constexpr
bool param_constructable =
305 std::is_constructible<T, const std::string&, const NodeConfiguration&>::value;
306 constexpr
bool has_static_ports_list =
309 static_assert(default_constructable || param_constructable,
310 "[registerNode]: the registered class must have at least one of these two " 312 " (const std::string&, const NodeConfiguration&) or (const std::string&).");
314 static_assert(!(param_constructable && !has_static_ports_list),
315 "[registerNode]: you MUST implement the static method: " 316 " PortsList providedPorts();\n");
318 static_assert(!(has_static_ports_list && !param_constructable),
319 "[registerNode]: since you have a static method providedPorts(), " 320 "you MUST add a constructor sign signature (const std::string&, const " 321 "NodeParameters&)\n");
323 registerBuilder( CreateManifest<T>(ID), CreateBuilder<T>());
326 template <
typename T>
329 static_assert(std::is_base_of<ActionNodeBase, T>::value ||
330 std::is_base_of<ControlNode, T>::value ||
331 std::is_base_of<DecoratorNode, T>::value ||
332 std::is_base_of<ConditionNode, T>::value,
333 "[registerNode]: accepts only classed derived from either ActionNodeBase, " 334 "DecoratorNode, ControlNode or ConditionNode");
336 static_assert(!std::is_abstract<T>::value,
337 "[registerNode]: Some methods are pure virtual. " 338 "Did you override the methods tick() and halt()?");
340 constexpr
bool default_constructable = std::is_constructible<T, const std::string&>::value;
341 constexpr
bool param_constructable =
342 std::is_constructible<T, const std::string&, const NodeConfiguration&>::value;
343 constexpr
bool has_static_ports_list =
346 static_assert(default_constructable || param_constructable,
347 "[registerNode]: the registered class must have at least one of these two " 348 "constructors: (const std::string&, const NodeConfiguration&) or (const std::string&).");
350 static_assert(!has_static_ports_list,
351 "[registerNode]: ports are passed to this node explicitly. The static method" 352 "providedPorts() should be removed to avoid ambiguities\n");
354 static_assert(param_constructable,
355 "[registerNode]: since this node has ports, " 356 "you MUST add a constructor sign signature (const std::string&, const " 357 "NodeParameters&)\n");
359 registerBuilder( CreateManifest<T>(ID, ports), CreateBuilder<T>());
363 const std::unordered_map<std::string, NodeBuilder>& builders()
const;
366 const std::unordered_map<std::string, TreeNodeManifest>&
manifests()
const;
369 const std::set<std::string>& builtinNodes()
const;
371 Tree createTreeFromText(
const std::string& text,
374 Tree createTreeFromFile(
const std::string& file_path,
379 std::unordered_map<std::string, TreeNodeManifest>
manifests_;
388 #endif // BT_FACTORY_H constexpr const char * PLUGIN_SYMBOL
static Blackboard::Ptr create(Blackboard::Ptr parent={})
void registerNodeType(const std::string &ID)
std::function< NodeStatus(NodeStatus, TreeNode &)> TickFunctor
std::function< std::unique_ptr< TreeNode >const std::string &, const NodeConfiguration &)> NodeBuilder
The term "Builder" refers to the Builder Pattern (https://en.wikipedia.org/wiki/Builder_pattern) ...
NodeBuilder CreateBuilder(typename std::enable_if< has_default_constructor< T >::value &&has_params_constructor< T >::value >::type *=nullptr)
std::unordered_map< std::string, NodeBuilder > builders_
std::set< std::string > builtin_IDs_
std::vector< Blackboard::Ptr > blackboard_stack
std::shared_ptr< Blackboard > Ptr
This information is used mostly by the XMLParser.
std::unordered_map< std::string, TreeNodeManifest > manifests_
Blackboard::Ptr rootBlackboard()
Tree & operator=(Tree &&other)
TreeNode * rootNode() const
void registerBuilder(const std::string &ID, const NodeBuilder &builder)
typename std::is_constructible< T, const std::string &, const NodeConfiguration & > has_params_constructor
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
TreeNodeManifest CreateManifest(const std::string &ID, PortsList portlist=getProvidedPorts< T >())
Tree & operator=(const Tree &)=delete
typename std::is_constructible< T, const std::string & > has_default_constructor
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
Abstract base class for Behavior Tree Nodes.
typename std::enable_if< Predicate::value >::type * enable_if
std::function< NodeStatus(TreeNode &)> TickFunctor
std::unordered_map< std::string, PortInfo > PortsList
std::vector< TreeNode::Ptr > nodes
std::unordered_map< std::string, TreeNodeManifest > manifests
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
void setStatus(NodeStatus new_status)
void registerNodeType(const std::string &ID, PortsList ports)
std::function< NodeStatus(TreeNode &)> TickFunctor
void applyRecursiveVisitor(const TreeNode *root_node, const std::function< void(const TreeNode *)> &visitor)