AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
behavior.cpp
1// Copyright 2025 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_behavior_tree_core/behavior.hpp"
16
18{
19
20bool isInternalBehaviorCategory(const std::string & category_name)
21{
22 const char * suffix = _AUTO_APMS_BEHAVIOR_TREE_CORE__INTERNAL_BEHAVIOR_CATEGORY_SUFFIX;
23 size_t suffix_len = std::strlen(suffix);
24 return category_name.size() >= suffix_len &&
25 category_name.compare(category_name.size() - suffix_len, suffix_len, suffix) == 0;
26}
27
29{
30 if (identity.empty()) {
31 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
32 "Behavior resource identity string must not be empty.");
33 }
34 std::string resource_part;
35 if (std::size_t pos = identity.find(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_CATEGORY_SEP);
36 pos == std::string::npos) {
37 category_name = "";
38 resource_part = identity;
39 } else {
40 category_name = identity.substr(0, pos);
41 resource_part =
42 identity.substr(pos + std::string(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_CATEGORY_SEP).size());
43 }
44 if (resource_part.empty()) {
45 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
46 "Behavior resource identity string '" + identity + "' is invalid: You must specify more than just the category.");
47 }
48 if (std::size_t pos = resource_part.find(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP);
49 pos == std::string::npos) {
50 // If only a single token is given, assume it's behavior_alias
51 package_name = "";
52 behavior_alias = resource_part;
53 } else {
54 package_name = resource_part.substr(0, pos);
56 resource_part.substr(pos + std::string(_AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP).size());
57 }
58 if (package_name.empty() && behavior_alias.empty()) {
59 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
60 "Behavior resource identity string '" + identity +
61 "' is invalid. Package name and behavior alias must not be empty.");
62 }
63}
64
65BehaviorResourceIdentity::BehaviorResourceIdentity(const std::string & identity, const std::string & default_category)
67{
68 // If no category is explicitly specified, use the given default one
69 if (category_name.empty() || category_name == _AUTO_APMS_BEHAVIOR_TREE_CORE__DEFAULT_BEHAVIOR_CATEGORY) {
70 category_name = default_category;
71 }
72}
73
75: BehaviorResourceIdentity(std::string(identity))
76{
77}
78
79bool BehaviorResourceIdentity::operator==(const BehaviorResourceIdentity & other) const { return str() == other.str(); }
80
81bool BehaviorResourceIdentity::operator<(const BehaviorResourceIdentity & other) const { return str() < other.str(); }
82
84{
85 std::string str;
86 if (!category_name.empty()) {
87 str += category_name + _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_CATEGORY_SEP;
88 }
89 str += package_name + _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP + behavior_alias;
90 return str;
91}
92
93bool BehaviorResourceIdentity::empty() const { return package_name.empty() && behavior_alias.empty(); }
94
95std::set<BehaviorResourceIdentity> getBehaviorResourceIdentities(
96 const std::set<std::string> & include_categories, bool include_internal,
97 const std::set<std::string> & exclude_packages)
98{
99 std::set<BehaviorResourceIdentity> identities;
101 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_TYPE_NAME__BEHAVIOR, exclude_packages)) {
102 std::string content;
103 std::string base_path;
104 if (ament_index_cpp::get_resource(
105 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_TYPE_NAME__BEHAVIOR, p, content, &base_path)) {
106 for (const auto & line :
107 auto_apms_util::splitString(content, _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_MARKER_FILE_LINE_SEP)) {
108 const std::vector<std::string> parts = auto_apms_util::splitString(
109 line, _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_MARKER_FILE_FIELD_PER_LINE_SEP, false);
110 if (parts.size() > 1) {
112 i.category_name = parts[0];
113 i.package_name = p;
114 i.behavior_alias = parts[1];
115 if (include_categories.empty()) {
116 if (!include_internal && isInternalBehaviorCategory(i.category_name))
117 continue; // Skip identities in the internal category if not requested otherwise
118 } else {
119 if (include_categories.find(i.category_name) == include_categories.end())
120 continue; // Skip identities not in the specified include categories
121 }
122 identities.insert(i);
123 }
124 }
125 }
126 }
127 return identities;
128}
129
130} // namespace auto_apms_behavior_tree::core
std::set< BehaviorResourceIdentity > getBehaviorResourceIdentities(const std::set< std::string > &include_categories={}, bool include_internal=false, const std::set< std::string > &exclude_packages={})
Get all registered behavior resource identities.
Definition behavior.cpp:95
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
Core API for AutoAPMS's behavior tree implementation.
Definition behavior.hpp:32
Struct that encapsulates the identity string for a registered behavior.
Definition behavior.hpp:38
std::string category_name
Name of the category this behavior resource belongs to.
Definition behavior.hpp:93
BehaviorResourceIdentity(const std::string &identity)
Constructor of a behavior resource identity object.
Definition behavior.cpp:28
std::string behavior_alias
Alias for a single registered behavior.
Definition behavior.hpp:97
bool empty() const
Determine whether this behavior resource identity object is considered empty.
Definition behavior.cpp:93
BehaviorResourceIdentity()=default
Constructor of an empty behavior resource identity object.
std::string package_name
Name of the package that registers the behavior resource.
Definition behavior.hpp:95
std::string str() const
Create the corresponding identity string.
Definition behavior.cpp:83