21#include "auto_apms_behavior_tree_core/node/node_manifest.hpp"
22#include "auto_apms_behavior_tree_core/node/node_registration_interface.hpp"
23#include "auto_apms_behavior_tree_core/tree/tree_document.hpp"
24#include "auto_apms_util/logging.hpp"
25#include "auto_apms_util/string.hpp"
26#include "behaviortree_cpp/xml_parsing.h"
27#include "class_loader/class_loader.hpp"
31int main(
int argc,
char ** argv)
34 std::cerr <<
"create_node_model: Missing inputs! The program requires: \n\t1.) The path to the node plugin "
35 "manifest.\n\t2. The exhaustive list of libraries to be loaded by ClassLoader (Separated by "
36 "';').\n\t3.) Registration type mappings (Separated by ';', format: "
37 "'class_name=registration_type').\n\t4.) The xml file to store the model.\n";
38 std::cerr <<
"Usage: create_node_model <manifest_file> <library_paths> <registration_types> <output_file>.\n";
49 std::map<std::string, std::string> registration_type_map;
50 for (
const auto & entry : registration_type_entries) {
51 const auto eq_pos = entry.find(
'=');
52 if (eq_pos == std::string::npos) {
53 throw std::runtime_error(
54 "Invalid registration type entry '" + entry +
"'. Expected format: 'class_name=registration_type'.");
56 registration_type_map[entry.substr(0, eq_pos)] = entry.substr(eq_pos + 1);
59 if (!std::filesystem::exists(manifest_file)) {
60 throw std::runtime_error(
"File manifest_file must exist.");
62 if (library_paths.empty()) {
63 throw std::runtime_error(
"Argument library_paths must not be empty.");
65 if (!output_file.has_filename()) {
66 throw std::runtime_error(
"Output file path must include a filename.");
70 if (output_file.extension() !=
".xml") {
71 throw std::runtime_error(
"Output file '" + output_file.string() +
"' has wrong extension. Must be '.xml'.");
74 const rclcpp::Logger logger = rclcpp::get_logger(
"create_node_model__" + output_file.stem().string());
76 BT::BehaviorTreeFactory factory;
77 const auto manifest = core::NodeManifest::fromFile(manifest_file.string());
86 std::vector<std::unique_ptr<class_loader::ClassLoader>> class_loaders;
87 for (
const auto & path : library_paths) {
89 class_loaders.push_back(std::make_unique<class_loader::ClassLoader>(path));
90 }
catch (
const std::exception & e) {
91 throw std::runtime_error(
"Failed to load library '" + path +
"': " + e.what());
96 for (
const auto & [node_name, params] : manifest.map()) {
98 const auto reg_type_it = registration_type_map.find(params.class_name);
99 if (reg_type_it == registration_type_map.end()) {
100 throw std::runtime_error(
101 "Node '" + node_name +
"' (Class: " + params.class_name +
102 ") has no registration type mapping. Please rebuild the package.");
104 const std::string & registration_type = reg_type_it->second;
106 class_loader::ClassLoader * loader =
nullptr;
107 for (
const auto & l : class_loaders) {
114 throw std::runtime_error(
115 "Node '" + node_name +
"' (Class: " + params.class_name +
116 ") cannot be registered, because the registration class '" + registration_type +
117 "' could not be found in any loaded library. Check that the class name is spelled correctly and "
118 "the node is registered by calling auto_apms_behavior_tree_register_nodes() in the CMakeLists.txt of the "
119 "corresponding package. Also make sure that you called the "
120 "AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE macro or PLUGINLIB_EXPORT_CLASS in the source file.");
124 logger,
"Registering behavior tree node '%s' (Class: %s) from library %s.", node_name.c_str(),
125 params.class_name.c_str(), loader->getLibraryPath().c_str());
129 rclcpp::Node::SharedPtr node =
nullptr;
130 rclcpp::CallbackGroup::SharedPtr group =
nullptr;
131 rclcpp::executors::SingleThreadedExecutor::SharedPtr executor =
nullptr;
133 node, group, executor, params);
134 plugin_instance->registerWithBehaviorTreeFactory(factory, node_name, &ros_node_context);
135 }
catch (
const std::exception & e) {
136 throw std::runtime_error(
137 "Failed to register node '" + node_name +
" (Class: " + params.class_name +
")': " + e.what());
142 const std::string model_xml = BT::writeTreeNodesModelXML(factory);
145 tinyxml2::XMLDocument model_doc;
146 if (model_doc.Parse(model_xml.c_str()) != tinyxml2::XMLError::XML_SUCCESS) {
147 throw std::runtime_error(
"Error parsing the generated node model XML: " + std::string(model_doc.ErrorStr()));
159 }
catch (
const std::exception & e) {
160 std::cerr <<
"ERROR (create_node_model): " << e.what() <<
"\n";
Interface used for registering behavior tree node plugins.
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.
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...
Powerful tooling for incorporating behavior trees for task development.