AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
mission_configuration.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 <utility>
18#include <vector>
19
20#include "auto_apms_behavior_tree_core/tree/tree_resource.hpp"
21#include "auto_apms_util/exceptions.hpp"
22#include "auto_apms_util/yaml.hpp"
23
24namespace auto_apms_mission
25{
27}
28
30namespace YAML
31{
32template <>
33struct convert<auto_apms_mission::MissionConfiguration>
34{
35 using Config = auto_apms_mission::MissionConfiguration;
36 static Node encode(const Config & rhs);
37 static bool decode(const Node & node, Config & rhs);
38};
39} // namespace YAML
41
42namespace auto_apms_mission
43{
44
49struct MissionConfiguration
50{
51 using TreeResourceIdentity = auto_apms_behavior_tree::core::TreeResourceIdentity;
52
53 static const std::string YAML_KEY_BRINGUP;
54 static const std::string YAML_KEY_MISSION;
55 static const std::string YAML_KEY_CONTINGENCY;
56 static const std::string YAML_KEY_EMERGENCY;
57 static const std::string YAML_KEY_SHUTDOWN;
58
59 MissionConfiguration() = default;
60
62
63 static MissionConfiguration fromResourceIdentity(const std::string identity);
64
65 std::vector<TreeResourceIdentity> bringup;
66 std::vector<TreeResourceIdentity> mission;
67 std::vector<std::pair<TreeResourceIdentity, TreeResourceIdentity>> contingency;
68 std::vector<std::pair<TreeResourceIdentity, TreeResourceIdentity>> emergency;
69 std::vector<TreeResourceIdentity> shutdown;
70};
71
72} // namespace auto_apms_mission
73
74// #####################################################################################################################
75// ################################ DEFINITIONS ##############################################
76// #####################################################################################################################
77
79namespace YAML
80{
81inline Node convert<auto_apms_mission::MissionConfiguration>::encode(const Config & rhs)
82{
83 Node node(NodeType::Map);
84 node[Config::YAML_KEY_BRINGUP] = rhs.bringup;
85 node[Config::YAML_KEY_MISSION] = rhs.mission;
86 Node contingency_node = node[Config::YAML_KEY_CONTINGENCY];
87 for (const auto & [key, val] : rhs.contingency) {
88 contingency_node[key.str()] = val;
89 }
90 Node emergency_node = node[Config::YAML_KEY_EMERGENCY];
91 for (const auto & [key, val] : rhs.emergency) {
92 emergency_node[key.str()] = val;
93 }
94 node[Config::YAML_KEY_SHUTDOWN] = rhs.shutdown;
95 return node;
96}
97inline bool convert<auto_apms_mission::MissionConfiguration>::decode(const Node & node, Config & rhs)
98{
99 if (!node.IsMap())
100 throw auto_apms_util::exceptions::YAMLFormatError(
101 "YAML::Node for auto_apms_mission::MissionConfiguration must be map but is type " + std::to_string(node.Type()) +
102 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
103
104 for (auto it = node.begin(); it != node.end(); ++it) {
105 const std::string key = it->first.as<std::string>();
106 const Node val = it->second;
107
108 if (key == Config::YAML_KEY_BRINGUP) {
109 if (val.IsNull()) continue;
110 if (!val.IsSequence()) {
111 throw auto_apms_util::exceptions::YAMLFormatError(
112 "Value for key '" + key + "' must be a sequence but is type " + std::to_string(val.Type()) +
113 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
114 }
115 rhs.bringup = val.as<std::vector<Config::TreeResourceIdentity>>();
116 continue;
117 }
118
119 if (key == Config::YAML_KEY_MISSION) {
120 if (!val.IsSequence()) {
121 throw auto_apms_util::exceptions::YAMLFormatError(
122 "Value for key '" + key + "' must be a sequence but is type " + std::to_string(val.Type()) +
123 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
124 }
125 rhs.mission = val.as<std::vector<Config::TreeResourceIdentity>>();
126 continue;
127 }
128
129 if (key == Config::YAML_KEY_CONTINGENCY) {
130 if (val.IsNull()) continue;
131 if (!val.IsMap()) {
132 throw auto_apms_util::exceptions::YAMLFormatError(
133 "Value for key '" + key + "' must be a map but is type " + std::to_string(val.Type()) +
134 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
135 }
136 for (YAML::const_iterator it2 = val.begin(); it2 != val.end(); ++it2) {
137 const std::string monitor_id = it2->first.as<std::string>();
138 if (!it2->second.IsScalar()) {
139 throw auto_apms_util::exceptions::YAMLFormatError(
140 "Value for key '" + monitor_id + "' in the CONTINGENCY group must be scalar but is type " +
141 std::to_string(val.Type()) + " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
142 }
143 rhs.contingency.push_back(
144 {Config::TreeResourceIdentity(monitor_id), it2->second.as<Config::TreeResourceIdentity>()});
145 }
146 continue;
147 }
148
149 if (key == Config::YAML_KEY_EMERGENCY) {
150 if (val.IsNull()) continue;
151 if (!val.IsMap()) {
152 throw auto_apms_util::exceptions::YAMLFormatError(
153 "Value for key '" + key + "' must be a map but is type " + std::to_string(val.Type()) +
154 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
155 }
156 for (YAML::const_iterator it2 = val.begin(); it2 != val.end(); ++it2) {
157 const std::string monitor_id = it2->first.as<std::string>();
158 if (!it2->second.IsScalar()) {
159 throw auto_apms_util::exceptions::YAMLFormatError(
160 "Value for key '" + monitor_id + "' in the EMERGENCY group must be scalar but is type " +
161 std::to_string(val.Type()) + " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
162 }
163 rhs.emergency.push_back(
164 {Config::TreeResourceIdentity(monitor_id), it2->second.as<Config::TreeResourceIdentity>()});
165 }
166 continue;
167 }
168
169 if (key == Config::YAML_KEY_SHUTDOWN) {
170 if (val.IsNull()) continue;
171 if (!val.IsSequence()) {
172 throw auto_apms_util::exceptions::YAMLFormatError(
173 "Value for key '" + key + "' must be a sequence but is type " + std::to_string(val.Type()) +
174 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
175 }
176 rhs.shutdown = val.as<std::vector<Config::TreeResourceIdentity>>();
177 continue;
178 }
179
180 // Unkown parameter
181 throw auto_apms_util::exceptions::YAMLFormatError("Unkown parameter name '" + key + "'.");
182 }
183 return true;
184}
185} // namespace YAML
#define AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS(ClassType)
Macro for defining YAML encode/decode methods for a class.
Definition yaml.hpp:33
Mission design utilities incorporating behavior trees to model the complexity of arbitrary operations...
Struct that encapsulates the identity string for a declared behavior tree.
Configuration parameters for standard mission supported by AutoAPMS.