AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
builder.cpp
1// Copyright 2024 Robin Müller
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "auto_apms_behavior_tree_core/builder.hpp"
16
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"
21
23{
24
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)
30{
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;
36}
37
38TreeBuilder::TreeBuilder(NodeRegistrationLoader::SharedPtr tree_node_loader)
40 std::shared_ptr<rclcpp::Node>(), std::shared_ptr<rclcpp::CallbackGroup>(),
41 std::shared_ptr<rclcpp::executors::SingleThreadedExecutor>(), tree_node_loader)
42{
43 only_non_ros_nodes_ = true;
44}
45
46TreeBuilder & TreeBuilder::registerNodes(const NodeManifest & tree_node_manifest, bool override)
47{
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);
53 already_registered_names = auto_apms_util::getCommonElements(provided_names, already_registered_names);
54 for (const std::string & name : provided_names) {
55 if (!auto_apms_util::contains(already_registered_names, name)) new_names.push_back(name);
56 }
57 RCLCPP_DEBUG(
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());
60 TreeDocument::registerNodes(tree_node_manifest, override);
61 return *this;
62}
63
64TreeBuilder & TreeBuilder::setScriptingEnum(const std::string & enum_name, int val)
65{
66 factory_.registerScriptingEnum(enum_name, val);
67 return *this;
68}
69
70Tree TreeBuilder::instantiate(const std::string & root_tree_name, TreeBlackboardSharedPtr bb_ptr)
71{
72 if (!hasTreeName(root_tree_name)) {
73 throw exceptions::TreeDocumentError(
74 "Cannot instantiate tree with name '" + root_tree_name + "' because it doesn't exist.");
75 }
76 Tree tree;
77 try {
78 factory_.registerBehaviorTreeFromText(writeToString());
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()));
83 }
84 return tree;
85}
86
87Tree TreeBuilder::instantiate(TreeBlackboardSharedPtr bb_ptr)
88{
89 if (hasRootTreeName()) return instantiate(getRootTreeName(), bb_ptr);
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().");
95}
96
97} // namespace auto_apms_behavior_tree::core
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.
Definition builder.cpp:25
TreeBuilder & setScriptingEnum(const std::string &enum_name, int val)
Define enums that may be used by any scripts inside the behavior tree.
Definition builder.cpp:64
Tree instantiate(const std::string &root_tree_name, TreeBlackboardSharedPtr bb_ptr=TreeBlackboard::create())
Create the behavior tree.
Definition builder.cpp:70
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.
Definition builder.cpp:46
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.
Definition container.hpp:36
std::set< KeyT, CompareT, AllocatorT > getCommonElements(std::set< KeyT, CompareT, AllocatorT > c1, std::set< KeyT, CompareT, AllocatorT > c2)
Assemble common elements of two sets.
Definition container.hpp:53
Core API for AutoAPMS's behavior tree implementation.
Definition builder.hpp:30