AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
node_manifest.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_core/node/node_manifest.hpp"
16
17#include <fstream>
18
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"
23
25{
26
28{
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>.");
33 }
34 if (std::size_t pos = identity.find(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP);
35 pos == std::string::npos) {
36 // If only a single token is given, assume it's metadata_id
37 package_name = "";
39 } else {
40 package_name = identity.substr(0, pos);
41 metadata_id = identity.substr(pos + std::string(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP).size());
42 }
43 if (metadata_id.empty()) {
44 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
45 "Node manifest resource identity string '" + identity + "' is invalid. Metadata ID must not be empty.");
46 }
47}
48
50: NodeManifestResourceIdentity(std::string(identity))
51{
52}
53
54bool NodeManifestResourceIdentity::operator==(const NodeManifestResourceIdentity & other) const
55{
56 return str() == other.str();
57}
58
59bool NodeManifestResourceIdentity::operator<(const NodeManifestResourceIdentity & other) const
60{
61 return str() < other.str();
62}
63
65{
66 return package_name + _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP + metadata_id;
67}
68
69bool NodeManifestResourceIdentity::empty() const { return package_name.empty() && metadata_id.empty(); }
70
71std::set<NodeManifestResourceIdentity> getNodeManifestResourceIdentities(const std::set<std::string> & exclude_packages)
72{
73 std::set<NodeManifestResourceIdentity> identities;
75 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_TYPE_NAME__NODE_MANIFEST, exclude_packages)) {
76 std::string content;
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 :
81 auto_apms_util::splitString(content, _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_MARKER_FILE_LINE_SEP)) {
82 const std::vector<std::string> parts = auto_apms_util::splitString(
83 line, _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_MARKER_FILE_FIELD_PER_LINE_SEP, false);
84 if (parts.size() > 0) {
86 i.package_name = p;
87 i.metadata_id = parts[0];
88 identities.insert(i);
89 }
90 }
91 }
92 }
93 return identities;
94}
95
97
98NodeManifest NodeManifest::fromFiles(const std::vector<std::string> & paths)
99{
100 NodeManifest manifest;
101 for (const auto & path : paths) {
102 try {
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()));
106 }
107 }
108 return manifest;
109}
110
112{
113 return NodeManifestResource(search_identity).getNodeManifest();
114}
115
116void NodeManifest::toFile(const std::string & file_path) const
117{
118 std::ofstream out_stream(file_path);
119 if (out_stream.is_open()) {
120 out_stream << this->encode();
121 out_stream.close();
122 } else {
123 throw exceptions::NodeManifestError("Error opening node manifest output file '" + file_path + "'.");
124 }
125}
126
127bool NodeManifest::contains(const std::string & node_name) const { return map_.find(node_name) != map_.end(); }
128
129NodeManifest::RegistrationOptions & NodeManifest::operator[](const std::string & node_name)
130{
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."};
135}
136
137const NodeManifest::RegistrationOptions & NodeManifest::operator[](const std::string & node_name) const
138{
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()) + ")."};
142}
143
144NodeManifest & NodeManifest::add(const std::string & node_name, const RegistrationOptions & opt)
145{
146 if (contains(node_name)) {
147 throw exceptions::NodeManifestError{
148 "Node '" + node_name + "' already exists in node manifest (Size: " + std::to_string(map_.size()) + ")."};
149 }
150 if (!opt.valid()) {
151 throw exceptions::NodeManifestError(
152 "Cannot add node '" + node_name + "' to manifest. Parameter class_name must not be empty.");
153 }
154 map_[node_name] = opt;
155 return *this;
156}
157
158NodeManifest & NodeManifest::remove(const std::string & node_name)
159{
160 if (!contains(node_name)) {
161 throw std::out_of_range{
162 "Node '" + node_name + "' doesn't exist in node manifest, so the corresponding entry cannot be removed."};
163 }
164 map_.erase(node_name);
165 return *this;
166}
167
168NodeManifest & NodeManifest::merge(const NodeManifest & other, bool replace)
169{
170 for (const auto & [node_name, params] : other.map()) {
171 if (contains(node_name)) {
172 if (replace) {
173 map_.erase(node_name);
174 } else {
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).");
178 }
179 }
180 add(node_name, params);
181 }
182 return *this;
183}
184
185std::vector<std::string> NodeManifest::getNodeNames()
186{
187 std::vector<std::string> names;
188 names.reserve(map_.size());
189 for (const auto & [name, _] : map_) names.push_back(name);
190 return names;
191}
192
193size_t NodeManifest::size() const { return map_.size(); }
194
195bool NodeManifest::empty() const { return map_.empty(); }
196
197const NodeManifest::Map & NodeManifest::map() const { return map_; }
198
199NodeManifestResource::NodeManifestResource(const Identity & search_identity)
200{
201 std::set<std::string> search_packages;
202 if (!search_identity.package_name.empty()) {
203 search_packages.insert(search_identity.package_name);
204 } else {
205 search_packages =
206 auto_apms_util::getPackagesWithResourceType(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_TYPE_NAME__NODE_MANIFEST);
207 }
208
209 size_t matching_count = 0;
210 for (const auto & p : search_packages) {
211 std::string content;
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 =
216 auto_apms_util::splitString(content, _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_MARKER_FILE_LINE_SEP);
217 for (const std::string & line : lines) {
218 std::vector<std::string> parts = auto_apms_util::splitString(
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 + ".");
223 }
224
225 // Determine if resource is matching
226 std::string found_metadata_id = parts[0];
227 if (found_metadata_id != search_identity.metadata_id) continue;
228
229 // Found matching resource - Increase counter
230 matching_count++;
231
232 // Now fill the other member variables in case the resource matches (if match is not unique, error is thrown
233 // later and the object is discarded)
234
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_);
240
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());
245 }
246 node_model_ = TreeDocument::getNodeModel(model_doc, node_manifest_);
247 }
248 }
249 }
250
251 if (matching_count == 0) {
252 throw auto_apms_util::exceptions::ResourceError(
253 "No node manifest resource was found using identity '" + search_identity.str() + "'.");
254 }
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() + "'.");
258 }
259}
260
261NodeManifestResource::NodeManifestResource(const std::string & search_identity)
263{
264}
265
267: NodeManifestResource(std::string(search_identity))
268{
269}
270
271const NodeManifestResource::Identity & NodeManifestResource::getIdentity() const { return unique_identity_; }
272
273const NodeManifest & NodeManifestResource::getNodeManifest() const { return node_manifest_; }
274
275const NodeModelMap & NodeManifestResource::getNodeModel() const { return node_model_; }
276
277} // namespace auto_apms_behavior_tree::core
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...
Definition string.cpp:24
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.
Definition resource.cpp:28
Core API for AutoAPMS's behavior tree implementation.
Definition behavior.hpp:32
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).