15#include "auto_apms_behavior_tree_core/node/node_manifest.hpp"
19#include "auto_apms_behavior_tree_core/exceptions.hpp"
20#include "auto_apms_behavior_tree_core/tree/tree_document.hpp"
21#include "auto_apms_util/resource.hpp"
22#include "auto_apms_util/string.hpp"
29 if (identity.empty()) {
30 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
31 "Cannot create node manifest resource identity from empty string. Must be <package_name>" +
32 std::string(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP) +
"<metadata_id>.");
34 if (std::size_t pos = identity.find(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP);
35 pos == std::string::npos) {
41 metadata_id = identity.substr(pos + std::string(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP).size());
44 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
45 "Node manifest resource identity string '" + identity +
"' is invalid. Metadata ID must not be empty.");
56 return str() == other.
str();
59bool NodeManifestResourceIdentity::operator<(
const NodeManifestResourceIdentity & other)
const
61 return str() < other.str();
73 std::set<NodeManifestResourceIdentity> identities;
75 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_TYPE_NAME__NODE_MANIFEST, exclude_packages)) {
77 std::string base_path;
78 if (ament_index_cpp::get_resource(
79 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_TYPE_NAME__NODE_MANIFEST, p, content, &base_path)) {
80 for (
const auto & line :
83 line, _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_MARKER_FILE_FIELD_PER_LINE_SEP,
false);
84 if (parts.size() > 0) {
101 for (
const auto & path : paths) {
103 manifest.
merge(fromFile(path));
104 }
catch (
const std::exception & e) {
105 throw exceptions::NodeManifestError(
"Error creating node manifest from multiple files: " + std::string(e.what()));
118 std::ofstream out_stream(file_path);
119 if (out_stream.is_open()) {
120 out_stream << this->encode();
123 throw exceptions::NodeManifestError(
"Error opening node manifest output file '" + file_path +
"'.");
131 if (
contains(node_name))
return map_[node_name];
132 throw std::out_of_range{
133 "Node '" + node_name +
"' doesn't exist in node manifest (Size: " + std::to_string(map_.size()) +
134 "). Use the add() method to add an entry."};
139 if (
contains(node_name))
return map_.at(node_name);
140 throw std::out_of_range{
141 "Node '" + node_name +
"' doesn't exist in node manifest (Size: " + std::to_string(map_.size()) +
")."};
147 throw exceptions::NodeManifestError{
148 "Node '" + node_name +
"' already exists in node manifest (Size: " + std::to_string(map_.size()) +
")."};
151 throw exceptions::NodeManifestError(
152 "Cannot add node '" + node_name +
"' to manifest. Parameter class_name must not be empty.");
154 map_[node_name] = opt;
161 throw std::out_of_range{
162 "Node '" + node_name +
"' doesn't exist in node manifest, so the corresponding entry cannot be removed."};
164 map_.erase(node_name);
170 for (
const auto & [node_name, params] : other.
map()) {
173 map_.erase(node_name);
175 throw exceptions::NodeManifestError(
176 "Cannot merge node manifests, because node '" + node_name +
177 "' already exists in other and argument replace is false (Won't replace existing entries).");
180 add(node_name, params);
187 std::vector<std::string> names;
188 names.reserve(map_.size());
189 for (
const auto & [name, _] : map_) names.push_back(name);
201 std::set<std::string> search_packages;
209 size_t matching_count = 0;
210 for (
const auto & p : search_packages) {
212 std::string base_path;
213 if (ament_index_cpp::get_resource(
214 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_TYPE_NAME__NODE_MANIFEST, p, content, &base_path)) {
215 std::vector<std::string> lines =
217 for (
const std::string & line : lines) {
219 line, _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_MARKER_FILE_FIELD_PER_LINE_SEP,
false);
220 if (parts.size() != 3) {
221 throw auto_apms_util::exceptions::ResourceError(
222 "Invalid node manifest resource file (Package: '" + p +
"'). Invalid line: " + line +
".");
226 std::string found_metadata_id = parts[0];
227 if (found_metadata_id != search_identity.
metadata_id)
continue;
235 unique_identity_.package_name = p;
236 unique_identity_.metadata_id = found_metadata_id;
237 node_manifest_file_path_ = base_path +
"/" + parts[1];
238 node_model_file_path_ = base_path +
"/" + parts[2];
239 node_manifest_ = NodeManifest::fromFile(node_manifest_file_path_);
241 tinyxml2::XMLDocument model_doc;
242 if (model_doc.LoadFile(node_model_file_path_.c_str()) != tinyxml2::XMLError::XML_SUCCESS) {
243 throw exceptions::TreeDocumentError(
244 "Error parsing the node model associated with node manifest " + unique_identity_.str());
251 if (matching_count == 0) {
252 throw auto_apms_util::exceptions::ResourceError(
253 "No node manifest resource was found using identity '" + search_identity.
str() +
"'.");
255 if (matching_count > 1) {
256 throw auto_apms_util::exceptions::ResourceError(
257 "There are multiple node manifest resources with metadata ID '" + search_identity.
str() +
"'.");
Class containing behavior tree node manifest resource data.
NodeManifestResource(const Identity &search_identity)
Constructor of a node manifest resource object.
const NodeModelMap & getNodeModel() const
Get the node model object associated with this resource.
const Identity & getIdentity() const
Get the unique identity for this resource.
const NodeManifest & getNodeManifest() const
Get the node manifest object associated with this resource.
Data structure for information about which behavior tree node plugin to load and how to configure the...
std::map< std::string, NodeRegistrationOptions > Map
Mapping of a node's name and its registration parameters.
static NodeManifest fromFiles(const std::vector< std::string > &paths)
Create a node plugin manifest from multiple files. They are loaded in the given order.
size_t size() const
Get the number of behavior tree nodes this manifest holds registration options for.
NodeManifest & add(const std::string &node_name, const RegistrationOptions &opt)
Add registration options for a behavior tree node to the manifest.
bool contains(const std::string &node_name) const
Determine if a behavior tree node has been added to the manifest.
bool empty() const
Determine whether any node registration options have been added to the manifest.
NodeManifest & merge(const NodeManifest &other, bool replace=false)
Merges another NodeManifest with this one.
const Map & map() const
Get a view of the internal map.
std::vector< std::string > getNodeNames()
Get all names of the behavior tree nodes specified by the manifest.
RegistrationOptions & operator[](const std::string &node_name)
Access the node manifest and retrieve registration options for a specific behavior tree node.
void toFile(const std::string &file_path) const
Write the node manifest to a file.
NodeManifest(const Map &map={})
Constructor of a NodeManifest data structure.
static NodeManifest fromResource(const NodeManifestResourceIdentity &search_identity)
Create a node manifest from an installed resource.
NodeManifest & remove(const std::string &node_name)
Remove registration options for a behavior tree node.
static NodeModelMap getNodeModel(tinyxml2::XMLDocument &doc, const NodeManifest &manifest)
Convert a behavior tree node model document to the corresponding data structure.
std::set< NodeManifestResourceIdentity > getNodeManifestResourceIdentities(const std::set< std::string > &exclude_packages={})
Get all registered behavior tree node manifest resource identities.
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...
std::set< std::string > getPackagesWithResourceType(const std::string &resource_type, const std::set< std::string > &exclude_packages={})
Get a list of all package names that register a certain type of ament_index resources.
Core API for AutoAPMS's behavior tree implementation.
std::map< std::string, NodeModel > NodeModelMap
Mapping of node registration names and their implementation details.
Struct that encapsulates the identity string for a registered behavior tree node manifest.
bool empty() const
Determine whether this node manifest resource identity object is considered empty.
NodeManifestResourceIdentity(const std::string &identity)
Constructor of a node manifest resource identity object.
NodeManifestResourceIdentity()=default
Constructor of an empty node manifest resource identity object.
std::string package_name
Name of the package that registers the behavior resource.
std::string metadata_id
Metadata ID determined when regitering the corresponding node manifest resource.
std::string str() const
Create the corresponding identity string.
bool valid() const
Verify that the options are valid (e.g. all required values are set).