AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
from_resource_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/tree/tree_resource.hpp"
17
19{
20
28class TreeFromResourceBuildHandler : public TreeBuildHandler
29{
30public:
31 TreeFromResourceBuildHandler(rclcpp::Node::SharedPtr ros_node_ptr, NodeLoader::SharedPtr tree_node_loader_ptr)
32 : TreeBuildHandler("tree_from_resource", ros_node_ptr, tree_node_loader_ptr),
33 resource_doc_(TreeDocument::BTCPP_FORMAT_DEFAULT_VERSION, tree_node_loader_ptr)
34 {
35 }
36
37 bool setBuildRequest(
38 const std::string & build_request, const NodeManifest & /*node_manifest*/,
39 const std::string & root_tree_name) override final
40 {
41 TreeResource::Identity resource_identity(build_request);
42 TreeResource resource(resource_identity);
43
44 // We don't want to set the root tree yet
45 resource_doc_.reset().mergeResource(resource, false);
46
47 if (const BT::Result res = resource_doc_.verify(); !res) {
48 RCLCPP_WARN(logger_, "Tree verification failed: %s", res.error().c_str());
49 return false;
50 }
51
52 // Try to determine root tree name
53 std::string name = root_tree_name;
54 if (root_tree_name.empty()) {
55 if (resource.hasRootTreeName()) {
56 name = resource.getRootTreeName();
57 } else {
58 RCLCPP_WARN(
59 logger_,
60 "Cannot determine root tree from tree resource identity '%s': You must either provide an identity that "
61 "includes a tree name or specify the root_tree_name argument with a non empty string.",
62 resource_identity.str().c_str());
63 return false;
64 }
65 }
66 resource_doc_.setRootTreeName(name);
67 return true;
68 }
69
70 TreeDocument::TreeElement buildTree(TreeDocument & doc, TreeBlackboard & /*bb*/) override final
71 {
72 // Merge document and adopt root tree
73 doc.mergeTreeDocument(resource_doc_, true);
74
75 // Reset the local tree document, as the tree moved to doc
76 resource_doc_.reset();
77
78 // The document MUST have a root tree. We made sure of that during setBuildRequest
79 return doc.getRootTree();
80 }
81
82private:
83 core::TreeDocument resource_doc_;
84};
85
86} // namespace auto_apms_behavior_tree
87
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 from an installed resource.
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.
std::string getRootTreeName() const
Get the name of the root tree of this behavior tree resource.
bool hasRootTreeName() const
Determine if this behavior tree resource specifies a root tree.
#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
std::string str() const
Create the corresponding identity string.