15#include "auto_apms_behavior_tree_core/tree/tree_resource.hpp"
21#include "ament_index_cpp/get_resource.hpp"
22#include "auto_apms_behavior_tree_core/definitions.hpp"
23#include "auto_apms_behavior_tree_core/exceptions.hpp"
24#include "auto_apms_behavior_tree_core/tree/tree_document.hpp"
25#include "auto_apms_util/resource.hpp"
26#include "auto_apms_util/string.hpp"
34 std::vector<std::string> tokens =
38 if (tokens.size() > 1) {
41 }
else if (tokens.size() > 0) {
47 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
48 "Behavior tree resource identity string '" + identity +
49 "' is invalid. It's not allowed to omit both <tree_file_stem> and <tree_name>.");
59 unique_identity_.behavior_alias, _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP,
false);
61 if (tokens.size() != 2) {
62 throw auto_apms_util::exceptions::ResourceIdentityFormatError(
63 "Unique tree resource identity string '" + unique_identity_.str() +
64 "' is invalid. Behavior alias must be <tree_file_stem>" +
65 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP +
"<tree_name>.");
68 unique_identity_.file_stem = tokens[0];
69 unique_identity_.tree_name = tokens[1];
74 doc.
mergeFile(build_request_file_path_,
true);
75 }
catch (
const std::exception & e) {
76 throw auto_apms_util::exceptions::ResourceError(
77 "Failed to create TreeResource with identity '" + unique_identity_.str() +
"' because tree file " +
78 build_request_file_path_ +
" cannot be parsed: " + e.what());
82 if (!unique_identity_.tree_name.empty()) {
84 throw auto_apms_util::exceptions::ResourceError(
85 "Cannot create TreeResource with identity '" + unique_identity_.str() +
"' because '" +
86 unique_identity_.tree_name +
"' does not exist in tree file " + build_request_file_path_ +
".");
103 package_name + _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP +
104 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP + tree_name);
110 package_name + _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP + file_name +
111 _AUTO_APMS_BEHAVIOR_TREE_CORE__RESOURCE_IDENTITY_ALIAS_SEP);
116 return !unique_identity_.tree_name.empty() || !doc_root_tree_name_.empty();
121 if (!unique_identity_.tree_name.empty())
return unique_identity_.tree_name;
124 if (!doc_root_tree_name_.empty())
return doc_root_tree_name_;
127 throw auto_apms_util::exceptions::ResourceError(
128 "Cannot get root tree name of tree resource '" + unique_identity_.str() +
129 "'. Since there is no XML attribute named '" + TreeDocument::ROOT_TREE_ATTRIBUTE_NAME +
130 "' and the resource identity doesn't specify <tree_name>, the root tree is unkown.");
137 i.
file_stem = unique_identity_.file_stem;
BehaviorResourceTemplate(const Identity &search_identity)
std::string getRootTreeName() const
Get the name of this document's root tree.
TreeDocument & mergeFile(const std::string &path, bool adopt_root_tree=false)
Create a tree document from a file and merge it with this one.
std::vector< std::string > getAllTreeNames() const
Get the names of all behavior trees inside this document.
bool hasRootTreeName() const
Determine if this document specifies which of its trees is the root tree.
TreeResourceIdentity createIdentityForTree(const std::string &tree_name="") const
Create a valid identity string for a specific behavior tree of this resource.
std::string getRootTreeName() const
Get the name of the root tree of this behavior tree resource.
static TreeResource findByTreeName(const std::string &tree_name, const std::string &package_name="")
Find an installed behavior tree resource using a specific behavior tree name.
static TreeResource findByFileStem(const std::string &file_stem, const std::string &package_name="")
Find an installed behavior tree resource using a specific behavior tree XML file stem.
TreeResource(const TreeResourceIdentity &search_identity)
Assemble a behavior tree resource using a TreeResourceIdentity.
bool hasRootTreeName() const
Determine if this behavior tree resource specifies a root tree.
bool contains(const ContainerT< ValueT, AllocatorT > &c, const ValueT &val)
Check whether a particular container structure contains a value.
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...
Core API for AutoAPMS's behavior tree implementation.
BehaviorResourceIdentity(const std::string &identity)
Constructor of a behavior resource identity object.
std::string behavior_alias
Alias for a single registered behavior.
std::string package_name
Name of the package that registers the behavior resource.
Struct that encapsulates the identity string for a registered behavior tree.
std::string tree_name
Name of a specific tree inside the resource's tree document.
std::string file_stem
Name of the file (without extension) that contains the resource's tree document.
TreeResourceIdentity()=default
Constructor of an empty behavior tree resource identity object.
TreeResourceIdentity(const std::string &identity)
Constructor of a tree resource identity object.