19#include "auto_apms_behavior_tree_core/node/node_manifest.hpp"
20#include "auto_apms_behavior_tree_core/node/node_registration_interface.hpp"
21#include "auto_apms_util/logging.hpp"
22#include "auto_apms_util/string.hpp"
23#include "behaviortree_cpp/xml_parsing.h"
24#include "class_loader/class_loader.hpp"
28int main(
int argc,
char ** argv)
31 std::cerr <<
"create_node_model: Missing inputs! The program requires: \n\t1.) The path to the node plugin "
32 "manifest.\n\t2. The exhaustive list of libraries to be loaded by ClassLoader (Separated by "
33 "';').\n\t3.) The xml file to "
35 std::cerr <<
"Usage: create_node_model <manifest_file> <library_paths> <output_file>.\n";
40 const std::filesystem::path manifest_file = std::filesystem::absolute(argv[1]);
45 if (manifest_file.empty()) {
46 throw std::runtime_error(
"Argument manifest_file must not be empty.");
48 if (library_paths.empty()) {
49 throw std::runtime_error(
"Argument library_paths must not be empty.");
51 if (output_file.empty()) {
52 throw std::runtime_error(
"Argument output_file must not be empty.");
56 if (output_file.extension() !=
".xml") {
57 throw std::runtime_error(
"Output file '" + output_file.string() +
"' has wrong extension. Must be '.xml'.");
60 const rclcpp::Logger logger = rclcpp::get_logger(
"create_node_model__" + output_file.stem().string());
63 BT::BehaviorTreeFactory factory;
64 const auto manifest = core::NodeManifest::fromFile(manifest_file.string());
73 std::vector<std::unique_ptr<class_loader::ClassLoader>> class_loaders;
74 for (
const auto & path : library_paths) class_loaders.push_back(std::make_unique<class_loader::ClassLoader>(path));
77 for (
const auto & [node_name, params] : manifest.map()) {
78 const std::string required_class_name =
79 "auto_apms_behavior_tree::core::NodeRegistrationTemplate<" + params.class_name +
">";
81 class_loader::ClassLoader * loader =
nullptr;
82 for (
const auto & l : class_loaders) {
83 if (l->isClassAvailable<core::NodeRegistrationInterface>(required_class_name)) loader = l.get();
87 throw std::runtime_error(
88 "Node '" + node_name +
" (Class: " + params.class_name +
89 ")' cannot be registered, because the required registration class '" + required_class_name +
90 "' couldn't be found. Check that the class name is spelled correctly and "
91 "the node is declared by calling auto_apms_behavior_tree_declare_nodes() in the CMakeLists.txt of the "
92 "corresponding package. Also make sure that you called the "
93 "AUTO_APMS_BEHAVIOR_TREE_DECLARE_NODE macro in the source file.");
97 logger,
"Declared behavior tree node '%s' (Class: %s) from library %s.", node_name.c_str(),
98 params.class_name.c_str(), loader->getLibraryPath().c_str());
101 const auto plugin_instance = loader->createUniqueInstance<core::NodeRegistrationInterface>(required_class_name);
102 rclcpp::Node::SharedPtr node =
nullptr;
103 rclcpp::CallbackGroup::SharedPtr group =
nullptr;
104 rclcpp::executors::SingleThreadedExecutor::SharedPtr executor =
nullptr;
106 node, group, executor, params);
107 plugin_instance->registerWithBehaviorTreeFactory(factory, node_name, &ros_node_context);
108 }
catch (
const std::exception & e) {
109 throw std::runtime_error(
110 "Failed to register node '" + node_name +
" (Class: " + params.class_name +
")': " + e.what());
115 std::ofstream out_stream(output_file);
116 if (out_stream.is_open()) {
117 out_stream << BT::writeTreeNodesModelXML(factory);
120 throw std::runtime_error(
"Error opening node model output file '" + output_file.string() +
"'.");
122 }
catch (
const std::exception & e) {
123 std::cerr <<
"ERROR (create_node_model): " << e.what() <<
"\n";
Additional parameters specific to ROS 2 determined at runtime by TreeBuilder.
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...
void exposeToGlobalDebugLogging(const rclcpp::Logger &logger)
Enable ROS 2 debug logging, if the C preprocessor flag _AUTO_APMS_DEBUG_LOGGING is set.
Useful tooling for incorporating behavior trees for task development.