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
27class TreeFromStringBuildHandler : public TreeBuildHandler
28{
29public:
30 TreeFromStringBuildHandler(rclcpp::Node::SharedPtr ros_node_ptr, NodeLoader::SharedPtr tree_node_loader_ptr)
31 : TreeBuildHandler("tree_from_string", ros_node_ptr, tree_node_loader_ptr),
32 working_doc_(TreeDocument::BTCPP_FORMAT_DEFAULT_VERSION, tree_node_loader_ptr)
33 {
34 }
35
36 bool setBuildRequest(
37 const std::string & build_request, const NodeManifest & node_manifest,
38 const std::string & root_tree_name) override final
39 {
40 // Adopt the root tree if specified
41 working_doc_.reset().mergeString(build_request, true);
42 working_doc_.registerNodes(node_manifest, false);
43
44 if (const BT::Result res = working_doc_.verify(); !res) {
45 RCLCPP_WARN(logger_, "Tree verification failed: %s", res.error().c_str());
46 return false;
47 }
48
49 // Try to determine root tree name
50 if (root_tree_name.empty()) {
51 if (!working_doc_.hasRootTreeName()) {
52 RCLCPP_WARN(
53 logger_,
54 "Cannot determine root tree: You must either encode the root tree within the tree XML or provide a non-empty "
55 "name using the root_tree_name argument.");
56 return false;
57 }
58 } else {
59 try {
60 working_doc_.setRootTreeName(root_tree_name);
61 } catch (const exceptions::TreeDocumentError & e) {
62 RCLCPP_WARN(logger_, "Cannot determine root tree: %s", e.what());
63 return false;
64 }
65 }
66 return true;
67 }
68
69 TreeDocument::TreeElement buildTree(TreeDocument & doc, TreeBlackboard & /*bb*/) override final
70 {
71 // Merge document and adopt root tree
72 doc.mergeTreeDocument(working_doc_, true);
73
74 // Reset the local tree document, as the tree moved to doc
75 working_doc_.reset();
76
77 // The document MUST have a root tree. We made sure of that during setBuildRequest
78 return doc.getRootTree();
79 }
80
81private:
82 core::TreeDocument working_doc_;
83};
84
85} // namespace auto_apms_behavior_tree
86
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.
Standard build handler for building a behavior tree directly from an XML string.
Handle for a single behavior tree of a TreeDocument.
Document Object Model (DOM) for the behavior tree XML schema. This class offers a programmatic approa...
TreeElement getRootTree()
Get the corresponding behavior tree element for the root tree of this document.
TreeDocument & mergeTreeDocument(const XMLDocument &other, bool adopt_root_tree=false)
Merge another tree document with this one.
TreeDocument & reset()
Clear this document and reset it to its initial state.
#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