AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
node_manifest.hpp
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// http://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#pragma once
16
17#include <map>
18#include <vector>
19
20#include "auto_apms_behavior_tree_core/definitions.hpp"
21#include "auto_apms_behavior_tree_core/node/node_registration_options.hpp"
22#include "auto_apms_util/exceptions.hpp"
23#include "auto_apms_util/yaml.hpp"
24
26{
27
32{
40 NodeManifestResourceIdentity(const std::string & identity);
41
49 NodeManifestResourceIdentity(const char * identity);
50
57
58 virtual ~NodeManifestResourceIdentity() = default;
59
60 bool operator==(const NodeManifestResourceIdentity & other) const;
61
62 bool operator<(const NodeManifestResourceIdentity & other) const;
63
68 std::string str() const;
69
74 bool empty() const;
75
77 std::string package_name;
79 std::string metadata_id;
80};
81
89std::set<NodeManifestResourceIdentity> getNodeManifestResourceIdentities(
90 const std::set<std::string> & exclude_packages = {});
91
133{
134public:
135 using RegistrationOptions = NodeRegistrationOptions;
136
138 using Map = std::map<std::string, NodeRegistrationOptions>;
139
146 NodeManifest(const Map & map = {});
147
149
150
155 static NodeManifest fromFiles(const std::vector<std::string> & paths);
156
164 static NodeManifest fromResource(const NodeManifestResourceIdentity & search_identity);
165
171 void toFile(const std::string & file_path) const;
172
178 bool contains(const std::string & node_name) const;
179
186 RegistrationOptions & operator[](const std::string & node_name);
187
189 const RegistrationOptions & operator[](const std::string & node_name) const;
190
199 NodeManifest & add(const std::string & node_name, const RegistrationOptions & opt);
200
207 NodeManifest & remove(const std::string & node_name);
208
222 NodeManifest & merge(const NodeManifest & other, bool replace = false);
223
238 const NodeManifest & other, const std::string & with_namespace,
239 const std::string & sep = _AUTO_APMS_BEHAVIOR_TREE_CORE__NODE_NAMESPACE_DEFAULT_SEP);
240
248 const std::string & ns, const std::string & sep = _AUTO_APMS_BEHAVIOR_TREE_CORE__NODE_NAMESPACE_DEFAULT_SEP);
249
254 std::vector<std::string> getNodeNames();
255
260 size_t size() const;
261
266 bool empty() const;
267
272 const Map & map() const;
273
274private:
275 Map map_;
276};
277
282{
283public:
284 using Identity = NodeManifestResourceIdentity;
285
293 NodeManifestResource(const Identity & search_identity);
294
302 NodeManifestResource(const std::string & search_identity);
303
312 NodeManifestResource(const char * search_identity);
313
318 const Identity & getIdentity() const;
319
324 const NodeManifest & getNodeManifest() const;
325
330 const NodeModelMap & getNodeModel() const;
331
332private:
333 Identity unique_identity_;
334 std::string node_manifest_file_path_;
335 NodeManifest node_manifest_;
336 std::string node_model_file_path_;
337 NodeModelMap node_model_;
338};
339
340} // namespace auto_apms_behavior_tree::core
341
342// #####################################################################################################################
343// ################################ DEFINITIONS ##############################################
344// #####################################################################################################################
345
347namespace YAML
348{
349template <>
351{
353 static Node encode(const Identity & rhs)
354 {
355 Node node;
356 node = rhs.str();
357 return node;
358 }
359 static bool decode(const Node & node, Identity & rhs)
360 {
361 if (!node.IsScalar()) return false;
362 rhs = Identity(node.Scalar());
363 return true;
364 }
365};
366template <>
367struct convert<auto_apms_behavior_tree::core::NodeManifest>
368{
369 using Manifest = auto_apms_behavior_tree::core::NodeManifest;
370 inline static Node encode(const Manifest & rhs)
371 {
372 Node node(NodeType::Map);
373 for (const auto & [name, params] : rhs.map()) node[name] = params;
374 return node;
375 }
376 inline static bool decode(const Node & node, Manifest & rhs)
377 {
378 if (!node.IsMap())
379 throw auto_apms_util::exceptions::YAMLFormatError(
380 "YAML::Node for auto_apms_behavior_tree::core::NodeManifest must be map but is type " +
381 std::to_string(node.Type()) + " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
382
383 for (auto it = node.begin(); it != node.end(); ++it) {
384 const auto & name = it->first.as<std::string>();
385 try {
386 rhs.add(name, it->second.as<Manifest::RegistrationOptions>());
387 } catch (const std::exception & e) {
388 throw auto_apms_util::exceptions::YAMLFormatError(
389 "Node registration options for node '" + name + "' are invalid: " + e.what());
390 }
391 }
392 return true;
393 }
394};
395} // namespace YAML
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...
NodeManifest & applyNodeNamespace(const std::string &ns, const std::string &sep=_AUTO_APMS_BEHAVIOR_TREE_CORE__NODE_NAMESPACE_DEFAULT_SEP)
Apply a namespace prefix to all node names in this manifest.
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.
NodeManifest & mergeWithNamespace(const NodeManifest &other, const std::string &with_namespace, const std::string &sep=_AUTO_APMS_BEHAVIOR_TREE_CORE__NODE_NAMESPACE_DEFAULT_SEP)
Merges another NodeManifest with this one using a namespace for the registration names.
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.
std::set< NodeManifestResourceIdentity > getNodeManifestResourceIdentities(const std::set< std::string > &exclude_packages={})
Get all registered behavior tree node manifest resource identities.
#define AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS(ClassType)
Macro for defining YAML encode/decode methods for a class.
Definition yaml.hpp:32
Core API for AutoAPMS's behavior tree implementation.
Definition behavior.hpp:32
Powerful tooling for incorporating behavior trees for task development.
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.
Parameters for loading and registering a behavior tree node class from a shared library using e....