20#include "auto_apms_behavior_tree_core/behavior.hpp"
21#include "auto_apms_behavior_tree_core/tree/tree_resource.hpp"
22#include "auto_apms_util/exceptions.hpp"
23#include "auto_apms_util/yaml.hpp"
28struct MissionConfigResourceIdentity :
public auto_apms_behavior_tree::core::BehaviorResourceIdentity
37 MissionConfigResourceIdentity(
const std::string & identity);
46 MissionConfigResourceIdentity(
const char * identity);
53 MissionConfigResourceIdentity() =
default;
64 static const std::string YAML_KEY_BRINGUP;
65 static const std::string YAML_KEY_MISSION;
66 static const std::string YAML_KEY_CONTINGENCY;
67 static const std::string YAML_KEY_EMERGENCY;
68 static const std::string YAML_KEY_SHUTDOWN;
70 MissionConfig() =
default;
84 static MissionConfig
fromResource(
const MissionConfigResourceIdentity & identity);
86 std::vector<TreeResourceIdentity> bringup;
87 std::vector<TreeResourceIdentity> mission;
88 std::vector<std::pair<TreeResourceIdentity, TreeResourceIdentity>> contingency;
89 std::vector<std::pair<TreeResourceIdentity, TreeResourceIdentity>> emergency;
90 std::vector<TreeResourceIdentity> shutdown;
93class MissionConfigResource
99 using BehaviorResourceTemplate::BehaviorResourceTemplate;
112struct convert<auto_apms_mission::MissionConfig>
114 using Config = auto_apms_mission::MissionConfig;
115 inline static Node encode(
const Config & rhs)
117 Node node(NodeType::Map);
118 node[Config::YAML_KEY_BRINGUP] = rhs.bringup;
119 node[Config::YAML_KEY_MISSION] = rhs.mission;
120 Node contingency_node = node[Config::YAML_KEY_CONTINGENCY];
121 for (
const auto & [key, val] : rhs.contingency) {
122 contingency_node[key.str()] = val;
124 Node emergency_node = node[Config::YAML_KEY_EMERGENCY];
125 for (
const auto & [key, val] : rhs.emergency) {
126 emergency_node[key.str()] = val;
128 node[Config::YAML_KEY_SHUTDOWN] = rhs.shutdown;
131 inline static bool decode(
const Node & node, Config & rhs)
134 throw auto_apms_util::exceptions::YAMLFormatError(
135 "YAML::Node for auto_apms_mission::MissionConfig must be map but is type " + std::to_string(node.Type()) +
136 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
138 for (
auto it = node.begin(); it != node.end(); ++it) {
139 const std::string key = it->first.as<std::string>();
140 const Node val = it->second;
142 if (key == Config::YAML_KEY_BRINGUP) {
143 if (val.IsNull())
continue;
144 if (!val.IsSequence()) {
145 throw auto_apms_util::exceptions::YAMLFormatError(
146 "Value for key '" + key +
"' must be a sequence but is type " + std::to_string(val.Type()) +
147 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
149 rhs.bringup = val.as<std::vector<Config::TreeResourceIdentity>>();
153 if (key == Config::YAML_KEY_MISSION) {
154 if (!val.IsSequence()) {
155 throw auto_apms_util::exceptions::YAMLFormatError(
156 "Value for key '" + key +
"' must be a sequence but is type " + std::to_string(val.Type()) +
157 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
159 rhs.mission = val.as<std::vector<Config::TreeResourceIdentity>>();
163 if (key == Config::YAML_KEY_CONTINGENCY) {
164 if (val.IsNull())
continue;
166 throw auto_apms_util::exceptions::YAMLFormatError(
167 "Value for key '" + key +
"' must be a map but is type " + std::to_string(val.Type()) +
168 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
170 for (YAML::const_iterator it2 = val.begin(); it2 != val.end(); ++it2) {
171 const std::string monitor_id = it2->first.as<std::string>();
172 if (!it2->second.IsScalar()) {
173 throw auto_apms_util::exceptions::YAMLFormatError(
174 "Value for key '" + monitor_id +
"' in the CONTINGENCY group must be scalar but is type " +
175 std::to_string(val.Type()) +
" (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
177 rhs.contingency.push_back(
178 {Config::TreeResourceIdentity(monitor_id), it2->second.as<Config::TreeResourceIdentity>()});
183 if (key == Config::YAML_KEY_EMERGENCY) {
184 if (val.IsNull())
continue;
186 throw auto_apms_util::exceptions::YAMLFormatError(
187 "Value for key '" + key +
"' must be a map but is type " + std::to_string(val.Type()) +
188 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
190 for (YAML::const_iterator it2 = val.begin(); it2 != val.end(); ++it2) {
191 const std::string monitor_id = it2->first.as<std::string>();
192 if (!it2->second.IsScalar()) {
193 throw auto_apms_util::exceptions::YAMLFormatError(
194 "Value for key '" + monitor_id +
"' in the EMERGENCY group must be scalar but is type " +
195 std::to_string(val.Type()) +
" (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
197 rhs.emergency.push_back(
198 {Config::TreeResourceIdentity(monitor_id), it2->second.as<Config::TreeResourceIdentity>()});
203 if (key == Config::YAML_KEY_SHUTDOWN) {
204 if (val.IsNull())
continue;
205 if (!val.IsSequence()) {
206 throw auto_apms_util::exceptions::YAMLFormatError(
207 "Value for key '" + key +
"' must be a sequence but is type " + std::to_string(val.Type()) +
208 " (0: Undefined - 1: Null - 2: Scalar - 3: Sequence - 4: Map).");
210 rhs.shutdown = val.as<std::vector<Config::TreeResourceIdentity>>();
215 throw auto_apms_util::exceptions::YAMLFormatError(
"Unkown parameter name '" + key +
"'.");
Helper temlpate class for creating behavior resource abstractions.
#define AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS(ClassType)
Macro for defining YAML encode/decode methods for a class.
Mission design utilities incorporating behavior trees to model the complexity of arbitrary operations...
Struct that encapsulates the identity string for a registered behavior tree.
Configuration parameters for generic missions supported by AutoAPMS.
static MissionConfig fromResource(const MissionConfigResourceIdentity &identity)
Create a mission configuration from an installed resource.