19 #include <unordered_map> 20 #include <unordered_set> 32 typedef std::function<std::unique_ptr<TreeNode>(
const std::string&,
const NodeConfiguration&)>
37 #ifndef BT_PLUGIN_EXPORT 50 #define BT_REGISTER_NODES(factory) \ 51 static void BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory) 57 #define BT_REGISTER_NODES(factory) \ 58 extern "C" void __attribute__((visibility("default"))) \ 59 BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory) 63 #define BT_REGISTER_NODES(factory) \ 64 __declspec(dllexport) void BT_RegisterNodesFromPlugin(BT::BehaviorTreeFactory& factory) 81 std::vector<TreeNode::Ptr>
nodes;
83 std::unordered_map<std::string, TreeNodeManifest>
manifests;
85 Tree() : root_node(nullptr) { }
104 bool unregisterBuilder(
const std::string& ID);
109 template <
typename T>
112 auto manifest = BehaviorTreeFactory::buildManifest<T>(ID);
113 registerBuilder(manifest, builder);
124 void registerSimpleAction(
const std::string& ID,
135 void registerSimpleCondition(
const std::string& ID,
146 void registerSimpleDecorator(
const std::string& ID,
155 void registerFromPlugin(
const std::string &file_path);
165 std::unique_ptr<TreeNode> instantiateTreeNode(
const std::string& name,
const std::string &ID,
173 template <
typename T>
176 static_assert(std::is_base_of<ActionNodeBase, T>::value ||
177 std::is_base_of<ControlNode, T>::value ||
178 std::is_base_of<DecoratorNode, T>::value ||
179 std::is_base_of<ConditionNode, T>::value,
180 "[registerBuilder]: accepts only classed derived from either ActionNodeBase, " 181 "DecoratorNode, ControlNode or ConditionNode");
183 static_assert(!std::is_abstract<T>::value,
184 "[registerBuilder]: Some methods are pure virtual. " 185 "Did you override the methods tick() and halt()?");
187 constexpr
bool default_constructable = std::is_constructible<T, const std::string&>::value;
188 constexpr
bool param_constructable =
189 std::is_constructible<T, const std::string&, const NodeConfiguration&>::value;
190 constexpr
bool has_static_ports_list =
193 static_assert(default_constructable || param_constructable,
194 "[registerBuilder]: the registered class must have at least one of these two " 196 " (const std::string&, const NodeParameters&) or (const std::string&).");
198 static_assert(!(param_constructable && !has_static_ports_list),
199 "[registerBuilder]: you MUST implement the static method: " 200 " PortsList providedPorts();\n");
202 static_assert(!(has_static_ports_list && !param_constructable),
203 "[registerBuilder]: since you have a static method requiredNodeParameters(), " 204 "you MUST add a constructor sign signature (const std::string&, const " 205 "NodeParameters&)\n");
207 registerNodeTypeImpl<T>(ID);
211 const std::unordered_map<std::string, NodeBuilder>& builders()
const;
214 const std::unordered_map<std::string, TreeNodeManifest>&
manifests()
const;
217 const std::set<std::string>& builtinNodes()
const;
219 Tree createTreeFromText(
const std::string& text,
222 Tree createTreeFromFile(
const std::string& file_path,
225 template <
typename T>
static 228 return { getType<T>(), ID, getProvidedPorts<T>() };
233 std::unordered_map<std::string, TreeNodeManifest>
manifests_;
239 template <
typename T>
242 template <
typename T>
245 template <
typename T>
249 registerBuilder( buildManifest<T>(ID), builder);
252 template <
typename T>
static 265 return std::make_unique<T>(name);
267 return std::make_unique<T>(name, config);
271 template <
typename T>
static 277 return std::unique_ptr<TreeNode>(
new T(name, params));
281 template <
typename T>
static 287 return std::unique_ptr<TreeNode>(
new T(name));
295 #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) ...
void registerNodeTypeImpl(const std::string &ID)
std::unordered_map< std::string, NodeBuilder > builders_
std::set< std::string > builtin_IDs_
std::shared_ptr< Blackboard > Ptr
This information is used mostly by the XMLParser.
std::unordered_map< std::string, TreeNodeManifest > manifests_
static NodeBuilder getBuilder(typename std::enable_if< has_default_constructor< T >::value &&has_params_constructor< T >::value >::type *=nullptr)
void registerBuilder(const std::string &ID, const NodeBuilder &builder)
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
std::vector< Blackboard::Ptr > blackboard_stack
typename std::is_constructible< T, const std::string & > has_default_constructor
PortsRemapping output_ports
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
static NodeBuilder getBuilder(typename std::enable_if<!has_default_constructor< T >::value &&has_params_constructor< T >::value >::type *=nullptr)
static TreeNodeManifest buildManifest(const std::string &ID)
Struct used to store a tree. If this object goes out of scope, the tree is destroyed.
std::unordered_map< std::string, TreeNodeManifest > manifests
PortsRemapping input_ports
static NodeBuilder getBuilder(typename std::enable_if< has_default_constructor< T >::value &&!has_params_constructor< T >::value >::type *=nullptr)
std::function< NodeStatus(TreeNode &)> TickFunctor
Blackboard::Ptr rootBlackboard()
typename std::is_constructible< T, const std::string &, const NodeConfiguration & > has_params_constructor