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
21class TreeFromResourceBuildHandler : public TreeBuildHandler
22{
23public:
24 TreeFromResourceBuildHandler(rclcpp::Node::SharedPtr ros_node_ptr, NodeLoader::SharedPtr tree_node_loader_ptr)
25 : TreeBuildHandler("tree_from_resource", ros_node_ptr, tree_node_loader_ptr),
26 resource_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 TreeResource::Identity resource_identity(build_request);
35 TreeResource resource(resource_identity);
36
37 // We don't want to set the root tree yet
38 resource_doc_.reset().mergeResource(resource, false);
39
40 if (const BT::Result res = resource_doc_.verify(); !res) {
41 RCLCPP_WARN(logger_, "Tree verification failed: %s", res.error().c_str());
42 return false;
43 }
44
45 // Try to determine root tree name
46 std::string name = root_tree_name;
47 if (root_tree_name.empty()) {
48 if (resource.hasRootTreeName()) {
49 name = resource.getRootTreeName();
50 } else {
51 RCLCPP_WARN(
52 logger_,
53 "Cannot determine root tree from tree resource identity '%s': You must either provide an identity that "
54 "includes a tree name or specify the root_tree_name argument with a non empty string.",
55 resource_identity.str().c_str());
56 return false;
57 }
58 }
59 resource_doc_.setRootTreeName(name);
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(resource_doc_, true);
67
68 // Reset the local tree document, as the tree moved to doc
69 resource_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 resource_doc_;
77};
78
79} // namespace auto_apms_behavior_tree
80
81AUTO_APMS_BEHAVIOR_TREE_DECLARE_BUILD_HANDLER(auto_apms_behavior_tree::TreeFromResourceBuildHandler)
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