AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
from_string_build_handler.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/build_handler.hpp"
16#include "auto_apms_behavior_tree_core/exceptions.hpp"
17
19{
20
21class TreeFromStringBuildHandler : public TreeBuildHandler
22{
23public:
24 TreeFromStringBuildHandler(rclcpp::Node::SharedPtr ros_node_ptr, NodeLoader::SharedPtr tree_node_loader_ptr)
25 : TreeBuildHandler("tree_from_string", ros_node_ptr, tree_node_loader_ptr),
26 working_doc_(TreeDocument::BTCPP_FORMAT_DEFAULT_VERSION, tree_node_loader_ptr)
27 {
28 }
29
30 bool setBuildRequest(
31 const std::string & build_request, const NodeManifest & node_manifest,
32 const std::string & root_tree_name) override final
33 {
34 // Adopt the root tree if specified
35 working_doc_.reset().mergeString(build_request, true);
36 working_doc_.registerNodes(node_manifest, false);
37
38 if (const BT::Result res = working_doc_.verify(); !res) {
39 RCLCPP_WARN(logger_, "Tree verification failed: %s", res.error().c_str());
40 return false;
41 }
42
43 // Try to determine root tree name
44 if (root_tree_name.empty()) {
45 if (!working_doc_.hasRootTreeName()) {
46 RCLCPP_WARN(
47 logger_,
48 "Cannot determine root tree: You must either encode the root tree within the tree XML or provide a non-empty "
49 "name using the root_tree_name argument.");
50 return false;
51 }
52 } else {
53 try {
54 working_doc_.setRootTreeName(root_tree_name);
55 } catch (const exceptions::TreeDocumentError & e) {
56 RCLCPP_WARN(logger_, "Cannot determine root tree: %s", e.what());
57 return false;
58 }
59 }
60 return true;
61 }
62
63 TreeDocument::TreeElement buildTree(TreeDocument & doc, TreeBlackboard & /*bb*/) override final
64 {
65 // Merge document and adopt root tree
66 doc.mergeTreeDocument(working_doc_, true);
67
68 // Reset the local tree document, as the tree moved to doc
69 working_doc_.reset();
70
71 // The document MUST have a root tree. We made sure of that during setBuildRequest
72 return doc.getRootTree();
73 }
74
75private:
76 core::TreeDocument working_doc_;
77};
78
79} // namespace auto_apms_behavior_tree
80
81AUTO_APMS_BEHAVIOR_TREE_DECLARE_BUILD_HANDLER(auto_apms_behavior_tree::TreeFromStringBuildHandler)
Base class for plugins that implement patterns for creating behavior trees using a standardized inter...
TreeBuildHandler(const std::string &name, rclcpp::Node::SharedPtr ros_node_ptr, NodeLoader::SharedPtr tree_node_loader_ptr)
Constructor allowing to give the build handler a specific name.
const rclcpp::Logger logger_
ROS 2 logger initialized with the name of the build handler.
Handle for a single behavior tree of a TreeDocument.
#define AUTO_APMS_BEHAVIOR_TREE_DECLARE_BUILD_HANDLER(type)
Macro for registering a behavior tree build handler plugin which may be loaded at runtime to build a ...
Useful tooling for incorporating behavior trees for task development.
Definition builder.hpp:30