15#include "auto_apms_behavior_tree_core/builder.hpp"
17#include "auto_apms_behavior_tree_core/exceptions.hpp"
18#include "auto_apms_util/container.hpp"
19#include "auto_apms_util/string.hpp"
20#include "behaviortree_cpp/xml_parsing.h"
26 rclcpp::Node::SharedPtr ros_node, rclcpp::CallbackGroup::SharedPtr tree_node_waitables_callback_group,
27 rclcpp::executors::SingleThreadedExecutor::SharedPtr tree_node_waitables_executor,
28 NodeRegistrationLoader::SharedPtr tree_node_loader)
29:
TreeDocument(BTCPP_FORMAT_DEFAULT_VERSION, tree_node_loader)
31 if (ros_node) logger_ = ros_node->get_logger().get_child(LOGGER_NAME);
32 ros_node_wptr_ = ros_node;
33 tree_node_waitables_callback_group_wptr_ = tree_node_waitables_callback_group;
34 tree_node_waitables_executor_wptr_ = tree_node_waitables_executor;
35 only_non_ros_nodes_ =
false;
40 std::shared_ptr<rclcpp::Node>(), std::shared_ptr<rclcpp::CallbackGroup>(),
41 std::shared_ptr<rclcpp::executors::SingleThreadedExecutor>(), tree_node_loader)
43 only_non_ros_nodes_ =
true;
48 std::vector<std::string> provided_names;
49 std::vector<std::string> already_registered_names;
50 std::vector<std::string> new_names;
51 for (
const auto & [name, _] : tree_node_manifest.
map()) provided_names.push_back(name);
52 for (
const auto & [name, _] : factory_.builders()) already_registered_names.push_back(name);
54 for (
const std::string & name : provided_names) {
58 logger_,
"Registering nodes with TreeBuilder\n-> Already registered node names: [ %s ]\n-> New node names: [ %s ]",
59 auto_apms_util::join(already_registered_names,
", ").c_str(), auto_apms_util::join(new_names,
", ").c_str());
66 factory_.registerScriptingEnum(enum_name, val);
73 throw exceptions::TreeDocumentError(
74 "Cannot instantiate tree with name '" + root_tree_name +
"' because it doesn't exist.");
79 tree = factory_.createTree(root_tree_name, bb_ptr);
80 factory_.clearRegisteredBehaviorTrees();
81 }
catch (
const std::exception & e) {
82 throw exceptions::TreeBuildError(
"Error during instantiate(): " + std::string(e.what()));
90 throw exceptions::TreeBuildError(
91 "Cannot instantiate tree without a root tree name. You must either specify the attribute '" +
92 std::string(TreeDocument::ROOT_TREE_ATTRIBUTE_NAME) +
93 "' of the tree document's root element or call setRootTreeName() to define the root tree. Alternatively, you may "
94 "call a different signature of instantiate().");
Data structure for information about which behavior tree node plugin to load and how to configure the...
const Map & map() const
Get a view of the internal map.
TreeBuilder(rclcpp::Node::SharedPtr ros_node, rclcpp::CallbackGroup::SharedPtr tree_node_waitables_callback_group, rclcpp::executors::SingleThreadedExecutor::SharedPtr tree_node_waitables_executor, NodeRegistrationLoader::SharedPtr tree_node_loader=NodeRegistrationLoader::make_shared())
Constructor.
TreeBuilder & setScriptingEnum(const std::string &enum_name, int val)
Define enums that may be used by any scripts inside the behavior tree.
Tree instantiate(const std::string &root_tree_name, TreeBlackboardSharedPtr bb_ptr=TreeBlackboard::create())
Create the behavior tree.
TreeBuilder & registerNodes(const NodeManifest &tree_node_manifest, bool override=false) override
Load behavior tree node plugins and register them with the internal behavior tree factory.
std::string getRootTreeName() const
Get the name of this document's root tree.
virtual TreeDocument & registerNodes(const NodeManifest &tree_node_manifest, bool override=false)
Load behavior tree node plugins and register them with the internal behavior tree factory.
bool hasTreeName(const std::string &tree_name) const
Determine if this document specifies a behavior tree with a particular name.
std::string writeToString() const
Write the XML of this tree document to a string.
bool hasRootTreeName() const
Determine if this document specifies which of its trees is the root tree.
TreeDocument(const std::string &format_version=BTCPP_FORMAT_DEFAULT_VERSION, NodeRegistrationLoader::SharedPtr tree_node_loader=NodeRegistrationLoader::make_shared())
Create a an empty tree document.
bool contains(const ContainerT< ValueT, AllocatorT > &c, const ValueT &val)
Check whether a particular container structure contains a value.
std::set< KeyT, CompareT, AllocatorT > getCommonElements(std::set< KeyT, CompareT, AllocatorT > c1, std::set< KeyT, CompareT, AllocatorT > c2)
Assemble common elements of two sets.
Core API for AutoAPMS's behavior tree implementation.