AutoAPMS
Resilient Robot Mission Management
Loading...
Searching...
No Matches
mission_configuration.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_mission/mission_configuration.hpp"
16
17#include <filesystem>
18
19#include "ament_index_cpp/get_resource.hpp"
20#include "auto_apms_util/exceptions.hpp"
21#include "auto_apms_util/resource.hpp"
22#include "auto_apms_util/string.hpp"
23
24namespace auto_apms_mission
25{
26const std::string MissionConfiguration::YAML_KEY_BRINGUP = "BRINGUP";
27const std::string MissionConfiguration::YAML_KEY_MISSION = "MISSION";
28const std::string MissionConfiguration::YAML_KEY_CONTINGENCY = "CONTINGENCY";
29const std::string MissionConfiguration::YAML_KEY_EMERGENCY = "EMERGENCY";
30const std::string MissionConfiguration::YAML_KEY_SHUTDOWN = "SHUTDOWN";
31
32MissionConfiguration MissionConfiguration::fromResourceIdentity(const std::string identity)
33{
34 const auto tokens = auto_apms_util::splitString(identity, "::", false);
35 std::string package_name = "";
36 std::string file_name;
37 switch (tokens.size()) {
38 case 1:
39 file_name = tokens[0];
40 break;
41 case 2:
42 package_name = tokens[0];
43 file_name = tokens[1];
44 break;
45 default:
46 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
47 "Mission configuration resource identity string '" + identity +
48 "' has wrong format. Must be '<package_name>::<file_name>'.");
49 }
50
51 std::set<std::string> search_packages;
52 if (!package_name.empty()) {
53 search_packages.insert(package_name);
54 } else {
55 search_packages =
56 auto_apms_util::getPackagesWithResourceType(_AUTO_APMS_MISSION__RESOURCE_TYPE_NAME__MISSION_CONFIG);
57 }
58
59 std::vector<std::string> matching_file_paths;
60 for (const auto & p : search_packages) {
61 std::string content;
62 std::string base_path;
63 if (ament_index_cpp::get_resource(_AUTO_APMS_MISSION__RESOURCE_TYPE_NAME__MISSION_CONFIG, p, content, &base_path)) {
64 std::vector<std::string> lines = auto_apms_util::splitString(content, "\n");
65 for (const std::string & line : lines) {
66 if (std::filesystem::path(line).stem() == std::filesystem::path(file_name).stem()) {
67 matching_file_paths.push_back(base_path + "/" + line);
68 }
69 }
70 }
71 }
72
73 if (matching_file_paths.empty()) {
74 throw auto_apms_util::exceptions::ResourceError(
75 "No mission configuration resource was found using identity '" + identity + "'.");
76 }
77 if (matching_file_paths.size() > 1) {
78 throw auto_apms_util::exceptions::ResourceError(
79 "There are multiple mission configuration resources with file name '" + file_name + "'.");
80 }
81 return fromFile(matching_file_paths[0]);
82}
83} // namespace auto_apms_mission
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
Mission design utilities incorporating behavior trees to model the complexity of arbitrary operations...
Configuration parameters for standard mission supported by AutoAPMS.