AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
create_node_model.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 <algorithm>
16#include <filesystem>
17#include <fstream>
18#include <iostream>
19
20#include "auto_apms_behavior_tree_core/node/node_manifest.hpp"
21#include "auto_apms_behavior_tree_core/node/node_registration_interface.hpp"
22#include "auto_apms_behavior_tree_core/tree/tree_document.hpp"
23#include "auto_apms_util/logging.hpp"
24#include "auto_apms_util/string.hpp"
25#include "behaviortree_cpp/xml_parsing.h"
26#include "class_loader/class_loader.hpp"
27
28using namespace auto_apms_behavior_tree;
29
30int main(int argc, char ** argv)
31{
32 if (argc < 4) {
33 std::cerr << "create_node_model: Missing inputs! The program requires: \n\t1.) The path to the node plugin "
34 "manifest.\n\t2. The exhaustive list of libraries to be loaded by ClassLoader (Separated by "
35 "';').\n\t3.) The xml file to store the model.\n";
36 std::cerr << "Usage: create_node_model <manifest_file> <library_paths> <output_file>.\n";
37 return EXIT_FAILURE;
38 }
39
40 try {
41 const std::filesystem::path manifest_file = std::filesystem::absolute(auto_apms_util::trimWhitespaces(argv[1]));
42 const std::vector<std::string> library_paths = auto_apms_util::splitString(argv[2], ";");
43 const std::filesystem::path output_file = std::filesystem::absolute(auto_apms_util::trimWhitespaces(argv[3]));
44
45 if (!std::filesystem::exists(manifest_file)) {
46 throw std::runtime_error("File manifest_file must exist.");
47 }
48 if (library_paths.empty()) {
49 throw std::runtime_error("Argument library_paths must not be empty.");
50 }
51 if (!output_file.has_filename()) {
52 throw std::runtime_error("Output file path must include a filename.");
53 }
54
55 // Ensure correct extensions
56 if (output_file.extension() != ".xml") {
57 throw std::runtime_error("Output file '" + output_file.string() + "' has wrong extension. Must be '.xml'.");
58 }
59
60 const rclcpp::Logger logger = rclcpp::get_logger("create_node_model__" + output_file.stem().string());
61
62 BT::BehaviorTreeFactory factory;
63 const auto manifest = core::NodeManifest::fromFile(manifest_file.string());
64
69
70 // Instantiate loaders for all libraries in library_paths (We don't use class_loader::MultiLibraryClassLoader
71 // because we want to keep track of the libraries that the nodes come from for debugging purposes)
72 std::vector<std::unique_ptr<class_loader::ClassLoader>> class_loaders;
73 for (const auto & path : library_paths) class_loaders.push_back(std::make_unique<class_loader::ClassLoader>(path));
74
75 // Walk manifest and register all plugins with BT::BehaviorTreeFactory
76 for (const auto & [node_name, params] : manifest.map()) {
77 const std::string required_class_name =
78 "auto_apms_behavior_tree::core::NodeRegistrationTemplate<" + params.class_name + ">";
79
80 class_loader::ClassLoader * loader = nullptr;
81 for (const auto & l : class_loaders) {
82 if (l->isClassAvailable<core::NodeRegistrationInterface>(required_class_name)) loader = l.get();
83 }
84
85 if (!loader) {
86 throw std::runtime_error(
87 "Node '" + node_name + "' (Class: " + params.class_name +
88 ") cannot be registered, because the required registration class '" + required_class_name +
89 "' couldn't be found. Check that the class name is spelled correctly and "
90 "the node is registered by calling auto_apms_behavior_tree_register_nodes() in the CMakeLists.txt of the "
91 "corresponding package. Also make sure that you called the "
92 "AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE macro in the source file.");
93 }
94
95 RCLCPP_DEBUG(
96 logger, "Registering behavior tree node '%s' (Class: %s) from library %s.", node_name.c_str(),
97 params.class_name.c_str(), loader->getLibraryPath().c_str());
98
99 try {
100 const auto plugin_instance = loader->createUniqueInstance<core::NodeRegistrationInterface>(required_class_name);
101 rclcpp::Node::SharedPtr node = nullptr;
102 rclcpp::CallbackGroup::SharedPtr group = nullptr;
103 rclcpp::executors::SingleThreadedExecutor::SharedPtr executor = nullptr;
104 core::RosNodeContext ros_node_context(
105 node, group, executor, params); // Values don't matter when not instantiating it
106 plugin_instance->registerWithBehaviorTreeFactory(factory, node_name, &ros_node_context);
107 } catch (const std::exception & e) {
108 throw std::runtime_error(
109 "Failed to register node '" + node_name + " (Class: " + params.class_name + ")': " + e.what());
110 }
111 }
112
113 // Generate node model XML from the factory
114 const std::string model_xml = BT::writeTreeNodesModelXML(factory);
115
116 // Parse the XML into a document to extract the NodeModelMap later
117 tinyxml2::XMLDocument model_doc;
118 if (model_doc.Parse(model_xml.c_str()) != tinyxml2::XMLError::XML_SUCCESS) {
119 throw std::runtime_error("Error parsing the generated node model XML: " + std::string(model_doc.ErrorStr()));
120 }
121
122 // Convert to NodeModelMap with hidden ports applied
123 auto model_map = core::TreeDocument::getNodeModel(model_doc, manifest);
124
125 // Create a TreeDocument and add the filtered node model
127 doc.addNodeModel(model_map);
128
129 // Write to file
130 doc.writeToFile(output_file.string());
131 } catch (const std::exception & e) {
132 std::cerr << "ERROR (create_node_model): " << e.what() << "\n";
133 return EXIT_FAILURE;
134 }
135
136 return EXIT_SUCCESS;
137}
Additional parameters specific to ROS 2 determined at runtime by TreeBuilder.
Document Object Model (DOM) for the behavior tree XML schema. This class offers a programmatic approa...
void writeToFile(const std::string &path) const
Write the XML of this tree document to a file.
TreeDocument & addNodeModel(NodeModelMap model_map)
Add a behavior tree node model element to the document by parsing the contents of model_map.
static NodeModelMap getNodeModel(tinyxml2::XMLDocument &doc, const NodeManifest &manifest)
Convert a behavior tree node model document to the corresponding data structure.
std::string trimWhitespaces(const std::string &str)
Trim whitespaces from both ends of a string.
Definition string.cpp:84
std::vector< std::string > splitString(const std::string &str, const std::string &delimiter, bool remove_empty=true)
Split a string into multiple tokens using a specific delimiter string (Delimiter may consist of multi...
Definition string.cpp:24
Powerful tooling for incorporating behavior trees for task development.
Definition behavior.hpp:32