AutoAPMS
Resilient Robot Mission Management
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// 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#pragma once
16
17#include <map>
18#include <vector>
19
20#include "auto_apms_behavior_tree_core/node/node_registration_options.hpp"
21#include "auto_apms_util/exceptions.hpp"
22#include "auto_apms_util/yaml.hpp"
23
25{
26class NodeManifest;
27}
28
30namespace YAML
31{
32template <>
33struct convert<auto_apms_behavior_tree::core::NodeManifest>
34{
35 using Manifest = auto_apms_behavior_tree::core::NodeManifest;
36 static Node encode(const Manifest & rhs);
37 static bool decode(const Node & node, Manifest & lhs);
38};
39} // namespace YAML
41
43{
44
104{
105public:
106 using RegistrationOptions = NodeRegistrationOptions;
107
109 using Map = std::map<std::string, NodeRegistrationOptions>;
110
117 NodeManifest(const Map & map = {});
118
120
121
126 static NodeManifest fromFiles(const std::vector<std::string> & paths);
127
137 static NodeManifest fromResourceIdentity(const std::string & identity);
138
144 void toFile(const std::string & file_path) const;
145
151 bool contains(const std::string & node_name) const;
152
159 RegistrationOptions & operator[](const std::string & node_name);
160
162 const RegistrationOptions & operator[](const std::string & node_name) const;
163
172 NodeManifest & add(const std::string & node_name, const RegistrationOptions & opt);
173
180 NodeManifest & remove(const std::string & node_name);
181
190 NodeManifest & merge(const NodeManifest & other, bool replace = false);
191
196 std::vector<std::string> getNodeNames();
197
202 size_t size() const;
203
208 bool empty() const;
209
214 const Map & map() const;
215
216private:
217 Map map_;
218};
219
220} // namespace auto_apms_behavior_tree::core
221
222// #####################################################################################################################
223// ################################ DEFINITIONS ##############################################
224// #####################################################################################################################
225
227namespace YAML
228{
229inline Node convert<auto_apms_behavior_tree::core::NodeManifest>::encode(const Manifest & rhs)
230{
231 Node node(NodeType::Map);
232 for (const auto & [name, params] : rhs.map()) node[name] = params;
233 return node;
234}
235inline bool convert<auto_apms_behavior_tree::core::NodeManifest>::decode(const Node & node, Manifest & rhs)
236{
237 if (!node.IsMap())
238 throw auto_apms_util::exceptions::YAMLFormatError(
239 "YAML::Node for auto_apms_behavior_tree::core::NodeManifest must be map but is type " +
240 std::to_string(node.Type()) + " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
241
242 for (auto it = node.begin(); it != node.end(); ++it) {
243 const auto & name = it->first.as<std::string>();
244 try {
245 rhs.add(name, it->second.as<Manifest::RegistrationOptions>());
246 } catch (const std::exception & e) {
247 throw auto_apms_util::exceptions::YAMLFormatError(
248 "Node registration parameters for node '" + name + "' are invalid: " + e.what());
249 }
250 }
251 return true;
252}
253} // namespace YAML
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.
static NodeManifest fromResourceIdentity(const std::string &identity)
Create a node manifest from an installed resource.
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.
NodeManifest & remove(const std::string &node_name)
Remove registration options for a behavior tree node.
#define AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS(ClassType)
Macro for defining YAML encode/decode methods for a class.
Definition yaml.hpp:33
Core API for AutoAPMS's behavior tree implementation.
Definition builder.hpp:30
Parameters for loading and registering a behavior tree node class from a shared library using e....