15#include "auto_apms_behavior_tree_core/tree/tree_document.hpp"
20#include "ament_index_cpp/get_package_share_directory.hpp"
21#include "auto_apms_behavior_tree_core/builder.hpp"
22#include "auto_apms_behavior_tree_core/exceptions.hpp"
23#include "auto_apms_behavior_tree_core/node/node_model_type.hpp"
24#include "auto_apms_behavior_tree_core/tree/tree_resource.hpp"
25#include "auto_apms_util/container.hpp"
26#include "auto_apms_util/logging.hpp"
27#include "auto_apms_util/string.hpp"
28#include "behaviortree_cpp/xml_parsing.h"
33std::vector<std::string> getAllTreeNamesImpl(
const tinyxml2::XMLDocument & doc)
35 std::vector<std::string> names;
36 if (
const tinyxml2::XMLElement * root = doc.RootElement()) {
37 if (strcmp(root->Name(), TreeDocument::ROOT_ELEMENT_NAME) == 0) {
39 for (
const tinyxml2::XMLElement * child = root->FirstChildElement(); child !=
nullptr;
40 child = child->NextSiblingElement()) {
41 if (strcmp(TreeDocument::TREE_ELEMENT_NAME, child->Name()) == 0) {
42 if (
const char * name = child->Attribute(TreeDocument::TREE_NAME_ATTRIBUTE_NAME)) {
43 names.push_back(name);
45 throw exceptions::TreeDocumentError(
46 "Cannot get tree name, because required attribute '" +
47 std::string(TreeDocument::TREE_NAME_ATTRIBUTE_NAME) +
"' is missing.");
51 }
else if (strcmp(root->Name(), TreeDocument::TREE_ELEMENT_NAME) == 0) {
53 if (
const char * name = root->Attribute(TreeDocument::TREE_NAME_ATTRIBUTE_NAME)) names.push_back(name);
63 throw exceptions::TreeDocumentError(
"Cannot create an instance of NodeElement with doc_ptr=nullptr.");
66 throw exceptions::TreeDocumentError(
"Cannot create an instance of NodeElement with ele_ptr=nullptr.");
69 NodeModelMap::const_iterator it =
model.find(
ele_ptr_->Name());
70 std::vector<NodePortInfo> port_infos_str_vec;
71 if (it !=
model.end()) port_infos_str_vec = it->second.port_infos;
78 if (!port_infos_str_vec.empty()) {
79 for (const NodePortInfo & port_info : port_infos_str_vec) {
80 port_names_.push_back(port_info.port_name);
81 if (!port_info.port_default.empty()) port_default_values_[port_info.port_name] = port_info.port_default;
87 for (
const auto & [name, val] : port_default_values_) {
88 if (existing_port_values.find(name) == existing_port_values.end()) {
89 ele_ptr_->SetAttribute(name.c_str(), val.c_str());
97 XMLElement * other_ele = other.
ele_ptr_;
100 other_ele = other_ele->DeepClone(
doc_ptr_)->ToElement();
102 XMLElement * prev =
ele_ptr_->PreviousSiblingElement();
104 ele_ptr_->Parent()->InsertAfterChild(
ele_ptr_->PreviousSibling(), other_ele);
106 ele_ptr_->Parent()->InsertFirstChild(other_ele);
113 port_names_ = other.port_names_;
114 port_default_values_ = other.port_default_values_;
120bool TreeDocument::NodeElement::operator!=(
const NodeElement & other)
const {
return !this->operator==(other); }
123 const std::string & name,
const NodeElement * before_this)
125 if (
const std::set<std::string> names =
doc_ptr_->getRegisteredNodeNames(
true); names.find(name) == names.end()) {
126 throw exceptions::TreeDocumentError(
127 "Cannot insert unkown node <" + name +
128 ">. Before inserting a new node, the associated document must register the corresponding behavior tree "
129 "node. Consider using a signature of insertNode() that does this automatically.");
131 XMLElement * ele =
doc_ptr_->NewElement(name.c_str());
132 return insertBeforeImpl(before_this, ele);
140 if (
const std::set<std::string> names =
doc_ptr_->getRegisteredNodeNames(
false); names.find(name) == names.end()) {
147 const std::string & tree_name,
const NodeElement * before_this)
149 if (!
doc_ptr_->hasTreeName(tree_name)) {
150 throw exceptions::TreeDocumentError(
151 "Cannot insert subtree node for tree '" + tree_name +
"' because no tree with that name exists.");
154 ele.
ele_ptr_->SetAttribute(TREE_NAME_ATTRIBUTE_NAME, tree_name.c_str());
160 const std::string tree_name = tree.
getName();
161 if (
doc_ptr_->hasTreeName(tree_name)) {
168 if (existing_tree != tree) {
169 throw exceptions::TreeDocumentError(
170 "Cannot insert subtree node using the provided tree element, because another tree with name '" + tree_name +
171 "' already exists.");
175 const std::string existing_xml = existing_tree.
writeToString();
177 if (existing_xml != incoming_xml) {
178 throw exceptions::TreeDocumentError(
179 "Cannot insert subtree node using the provided tree element, because another tree with name '" + tree_name +
180 "' already exists with different content.");
194 const XMLElement * root_child = tree.
ele_ptr_->FirstChildElement();
196 throw exceptions::TreeDocumentError(
199 if (root_child->NextSibling()) {
200 throw exceptions::TreeDocumentError(
201 "Cannot insert tree element '" + tree.
getFullyQualifiedName() +
"' because it has more than one child node.");
210 const std::vector<std::string> other_tree_names = doc.
getAllTreeNames();
211 if (other_tree_names.empty()) {
212 throw exceptions::TreeDocumentError(
213 "Cannot insert tree '" + tree_name +
"' because document has no <" + TREE_ELEMENT_NAME +
"> elements.");
216 throw exceptions::TreeDocumentError(
217 "Cannot insert tree '" + tree_name +
"' because document doesn't specify a tree with that name.");
221 std::set<std::string> required_tree_names = {tree_name};
227 doc.DeepCopy(&temp_doc);
228 const TreeElement other_tree(&temp_doc, temp_doc.getXMLElementForTreeWithName(tree_name));
230 collect_dependency_tree_names = [&required_tree_names, &collect_dependency_tree_names](
const NodeElement & ele) {
231 if (ele.getRegistrationName() == SUBTREE_ELEMENT_NAME) {
232 if (
const char * name = ele.ele_ptr_->Attribute(TREE_NAME_ATTRIBUTE_NAME)) {
234 required_tree_names.insert(name);
237 ele.doc_ptr_->getTree(name).deepApplyConst(collect_dependency_tree_names);
239 throw exceptions::TreeDocumentError(
"Subtree element has no name attribute.");
249 return available_names.find(ele.getRegistrationName()) == available_names.end();
251 for (
const std::string & name : required_tree_names) {
252 const NodeElement ele(&temp_doc, temp_doc.getXMLElementForTreeWithName(name)->FirstChildElement());
253 if (
const std::vector<NodeElement> found = ele.
deepApplyConst(apply); !found.empty()) {
254 std::vector<std::string> names;
256 throw exceptions::TreeDocumentError(
257 "Cannot insert tree '" + tree_name +
"' because the following nodes found in tree '" + name +
258 "' are unkown to the builder:\n\t- " + auto_apms_util::join(names,
"\n\t- "));
264 required_tree_names.erase(tree_name);
265 for (
const std::string & name : other_tree_names) {
266 if (required_tree_names.find(name) == required_tree_names.end()) {
271 doc_ptr_->mergeTreeDocument(
static_cast<const XMLDocument &
>(temp_doc),
false);
274 return insertBeforeImpl(
275 before_this, doc.getXMLElementForTreeWithName(tree_name)->FirstChildElement()->DeepClone(
doc_ptr_)->ToElement());
285 const std::string & tree_str,
const std::string & tree_name,
const NodeElement * before_this)
293 const std::string & tree_str,
const NodeElement * before_this)
301 const std::string & path,
const std::string & tree_name,
const NodeElement * before_this)
309 const std::string & path,
const NodeElement * before_this)
320 insert_doc.
mergeFile(resource.build_request_file_path_);
339 const std::string & registration_name,
const std::string & instance_name)
const
345 if (registration_name.empty())
return ele.getName() == instance_name;
346 if (instance_name.empty())
return ele.getRegistrationName() == registration_name;
347 return ele.getRegistrationName() == registration_name && ele.getName() == instance_name;
349 if (
const std::vector<NodeElement> found =
deepApplyConst(apply); !found.empty())
return found[0];
352 throw exceptions::TreeDocumentError(
353 "Cannot find a child node that matches the search arguments (registration_name: '" + registration_name +
358 const std::string & registration_name,
const std::string & instance_name)
375 for (
const tinyxml2::XMLAttribute * attr =
ele_ptr_->FirstAttribute(); attr !=
nullptr; attr = attr->Next()) {
377 values[attr_name] = attr->Value();
386 std::vector<std::string> unkown_keys;
387 for (
const auto & [key, _] : port_values) {
390 if (!unkown_keys.empty()) {
391 throw exceptions::TreeDocumentError(
392 "Cannot set ports. According to the node model, the following ports are not implemented by '" +
393 std::string(
ele_ptr_->Name()) +
"': [ " + auto_apms_util::join(unkown_keys,
", ") +
" ].");
397 for (
const auto & [key, val] : port_values) {
398 ele_ptr_->SetAttribute(key.c_str(), val.c_str());
405 for (
const std::string & name : port_names_) {
406 PortValues::const_iterator it = port_default_values_.find(name);
407 if (it != port_default_values_.end()) {
408 ele_ptr_->SetAttribute(it->first.c_str(), it->second.c_str());
410 ele_ptr_->DeleteAttribute(name.c_str());
418 ele_ptr_->SetAttribute(BT::toStr(type).c_str(), script.
str().c_str());
424 ele_ptr_->SetAttribute(BT::toStr(type).c_str(), script.
str().c_str());
430 ele_ptr_->SetAttribute(NODE_INSTANCE_NAME_ATTRIBUTE_NAME, instance_name.c_str());
438 if (
const char * name =
ele_ptr_->Attribute(NODE_INSTANCE_NAME_ATTRIBUTE_NAME))
return name;
445 std::string instance_name =
getName();
446 if (registration_name == instance_name)
return registration_name;
447 return instance_name +
" (" + registration_name +
")";
455 std::vector<NodeElement> found;
456 deepApplyImpl(*
this, apply_callback, found);
462 std::vector<NodeElement> found;
463 deepApplyImpl(*
this, apply_callback, found);
468 const NodeElement * before_this, XMLElement * add_this)
471 XMLElement * prev =
nullptr;
472 XMLElement * curr = ele_ptr_->FirstChildElement();
478 if (curr == before_this->
ele_ptr_) {
483 curr = curr->NextSiblingElement();
487 ele_ptr_->InsertAfterChild(prev, add_this);
489 throw exceptions::TreeDocumentError(
491 getFullyQualifiedName() +
".");
494 ele_ptr_->InsertFirstChild(add_this);
497 ele_ptr_->InsertEndChild(add_this);
502void TreeDocument::NodeElement::deepApplyImpl(
503 const NodeElement & parent, ConstDeepApplyCallback apply_callback, std::vector<NodeElement> & vec)
505 for (XMLElement * child = parent.ele_ptr_->FirstChildElement(); child !=
nullptr;
506 child = child->NextSiblingElement()) {
507 const NodeElement child_ele(parent.doc_ptr_, child);
510 if (apply_callback(child_ele)) vec.push_back(child_ele);
513 deepApplyImpl(child_ele, apply_callback, vec);
517void TreeDocument::NodeElement::deepApplyImpl(
518 NodeElement & parent, DeepApplyCallback apply_callback, std::vector<NodeElement> & vec)
520 for (XMLElement * child = parent.ele_ptr_->FirstChildElement(); child !=
nullptr;
521 child = child->NextSiblingElement()) {
525 if (apply_callback(child_ele)) vec.push_back(child_ele);
528 deepApplyImpl(child_ele, apply_callback, vec);
534 if (!ele_ptr->Attribute(TREE_NAME_ATTRIBUTE_NAME)) {
535 throw exceptions::TreeDocumentError(
536 "Cannot create tree element without a '" + std::string(TREE_NAME_ATTRIBUTE_NAME) +
"' attribute.");
542 const std::string other_tree_name = other.
getName();
546 throw exceptions::TreeDocumentError(
547 "Cannot copy tree '" + other.
getName() +
"' because another tree with this name already exists.");
555 ele_ptr_->SetAttribute(TREE_NAME_ATTRIBUTE_NAME, tree_name.c_str());
561 if (
const char * name =
ele_ptr_->Attribute(TREE_NAME_ATTRIBUTE_NAME))
return name;
576 const bool is_native_node =
doc_ptr_->native_node_names_.find(name) !=
doc_ptr_->native_node_names_.end();
577 if (!is_native_node && !m.
contains(name)) {
578 if (!
doc_ptr_->registered_nodes_manifest_.contains(name)) {
579 throw exceptions::NodeManifestError(
580 "Cannot assemble the required node manifest for tree '" +
getName() +
581 "' since there are no registration options for node '" + name +
"'.");
583 m.
add(name,
doc_ptr_->registered_nodes_manifest_[name]);
592 TreeDocument doc(
doc_ptr_->format_version_,
doc_ptr_->tree_node_loader_ptr_);
598 XMLDocument tree_doc;
599 tree_doc.InsertEndChild(
ele_ptr_->DeepClone(&tree_doc));
600 tinyxml2::XMLPrinter printer;
601 tree_doc.Print(&printer);
602 return printer.CStr();
606 const std::string & registration_name,
const std::string & instance_name)
621: XMLDocument(true, tinyxml2::PRESERVE_WHITESPACE),
623 native_node_names_(BT::BehaviorTreeFactory().builtinNodes()),
624 format_version_(format_version),
625 tree_node_loader_ptr_(tree_node_loader),
626 registered_nodes_manifest_(),
628 logger_(rclcpp::get_logger(LOGGER_NAME))
635 std::set<std::string> include_stack;
636 return mergeTreeDocumentImpl(other, adopt_root_tree, include_stack);
640 const XMLDocument & other,
bool adopt_root_tree, std::set<std::string> & include_stack)
642 const XMLElement * other_root = other.RootElement();
644 throw exceptions::TreeDocumentError(
"Cannot merge tree documents: other_root is nullptr.");
647 auto verify_tree_structure = [](
const XMLElement * tree_ele) {
648 const char * tree_id = tree_ele->Attribute(TREE_NAME_ATTRIBUTE_NAME);
650 throw exceptions::TreeDocumentError(
651 "Cannot merge tree document: Found a <" + std::string(TREE_ELEMENT_NAME) +
652 "> element that doesn't specify the required attribute '" + TREE_NAME_ATTRIBUTE_NAME +
"'.");
654 const XMLElement * tree_root_child = tree_ele->FirstChildElement();
655 if (!tree_root_child) {
656 throw exceptions::TreeDocumentError(
657 "Cannot merge tree document: Tree '" + std::string(tree_id) +
"' has no child nodes.");
659 if (tree_root_child->NextSibling()) {
660 throw exceptions::TreeDocumentError(
661 "Cannot merge tree document: Tree '" + std::string(tree_id) +
"' has more than one child node.");
666 auto check_duplicates = [
this](
const std::vector<std::string> & new_tree_names,
const std::string & source) {
668 if (!common.empty()) {
669 throw exceptions::TreeDocumentError(
670 "Cannot merge tree document: The following trees from " + source +
" are already defined: [ " +
671 auto_apms_util::join(common,
", ") +
" ].");
676 const std::vector<std::string> other_tree_names = getAllTreeNamesImpl(other);
678 if (strcmp(other_root->Name(), ROOT_ELEMENT_NAME) == 0) {
680 if (
const char * ver = other_root->Attribute(BTCPP_FORMAT_ATTRIBUTE_NAME)) {
681 if (std::string(ver) != format_version_) {
682 throw exceptions::TreeDocumentError(
683 "Cannot merge tree document: Format of other document (" + std::string(BTCPP_FORMAT_ATTRIBUTE_NAME) +
": " +
684 ver +
") is not compatible with this document (" + std::string(BTCPP_FORMAT_ATTRIBUTE_NAME) +
": " +
685 format_version_ +
").");
688 throw exceptions::TreeDocumentError(
689 "Cannot merge tree document: Root element of other document doesn't have required attribute '" +
690 std::string(BTCPP_FORMAT_ATTRIBUTE_NAME) +
"'.");
694 TreeDocument include_doc(format_version_, tree_node_loader_ptr_);
695 for (
const XMLElement * include_ele = other_root->FirstChildElement(INCLUDE_ELEMENT_NAME); include_ele !=
nullptr;
696 include_ele = include_ele->NextSiblingElement(INCLUDE_ELEMENT_NAME)) {
697 if (
const char * path = include_ele->Attribute(INCLUDE_PATH_ATTRIBUTE_NAME)) {
698 if (std::string(path).empty()) {
699 throw exceptions::TreeDocumentError(
700 "Cannot merge tree document: Found an <" + std::string(INCLUDE_ELEMENT_NAME) +
701 "> element that specifies an empty path in attribute '" + INCLUDE_PATH_ATTRIBUTE_NAME +
"'.");
703 std::string absolute_path = path;
704 if (
const char * ros_pkg = include_ele->Attribute(INCLUDE_ROS_PKG_ATTRIBUTE_NAME)) {
705 if (std::string(ros_pkg).empty()) {
706 throw exceptions::TreeDocumentError(
707 "Cannot merge tree document: Found an <" + std::string(INCLUDE_ELEMENT_NAME) +
708 "> element that specifies an empty ROS package name in attribute '" + INCLUDE_ROS_PKG_ATTRIBUTE_NAME +
711 if (std::filesystem::path(path).is_absolute()) {
712 throw exceptions::TreeDocumentError(
713 "Cannot merge tree document: Found an <" + std::string(INCLUDE_ELEMENT_NAME) +
714 "> element that specifies an absolute path '" + std::string(path) +
"' in attribute '" +
715 INCLUDE_PATH_ATTRIBUTE_NAME +
"' together with a ROS package name in attribute '" +
716 INCLUDE_ROS_PKG_ATTRIBUTE_NAME +
"'. Please remove the attribute " + INCLUDE_ROS_PKG_ATTRIBUTE_NAME +
717 " or use a relative path if you want to refer to a location "
718 "relative to the package's share directory.");
722 (std::filesystem::path(ament_index_cpp::get_package_share_directory(ros_pkg)) / path).
string();
723 }
catch (
const ament_index_cpp::PackageNotFoundError & e) {
724 throw exceptions::TreeDocumentError(
725 "Cannot merge tree document: Found an <" + std::string(INCLUDE_ELEMENT_NAME) +
726 "> element that specifies a non-existing ROS package '" + std::string(ros_pkg) +
"' in attribute '" +
727 INCLUDE_ROS_PKG_ATTRIBUTE_NAME +
"'.");
733 include_doc.mergeFileImpl(absolute_path,
false, include_stack);
734 }
catch (
const std::exception & e) {
735 throw exceptions::TreeDocumentError(
736 "Cannot merge tree document: Failed to include file '" + absolute_path +
"': " + e.what());
739 throw exceptions::TreeDocumentError(
740 "Cannot merge tree document: Found an <" + std::string(INCLUDE_ELEMENT_NAME) +
741 "> element that doesn't specify the required attribute '" + INCLUDE_PATH_ATTRIBUTE_NAME +
"'.");
746 check_duplicates(include_doc.getAllTreeNames(),
"included files");
749 for (
const XMLElement * child = include_doc.RootElement()->FirstChildElement(TREE_ELEMENT_NAME); child !=
nullptr;
750 child = child->NextSiblingElement(TREE_ELEMENT_NAME)) {
755 RootElement()->InsertEndChild(child->DeepClone(
this));
759 check_duplicates(other_tree_names,
"the merged document");
762 for (
const XMLElement * child = other_root->FirstChildElement(TREE_ELEMENT_NAME); child !=
nullptr;
763 child = child->NextSiblingElement(TREE_ELEMENT_NAME)) {
764 verify_tree_structure(child);
767 RootElement()->InsertEndChild(child->DeepClone(
this));
770 if (adopt_root_tree) {
773 if (
const char * name = other_root->Attribute(ROOT_TREE_ATTRIBUTE_NAME))
setRootTreeName(name);
775 }
else if (strcmp(other_root->Name(), TREE_ELEMENT_NAME) == 0) {
778 verify_tree_structure(other_root);
781 check_duplicates(other_tree_names,
"the merged document");
784 RootElement()->InsertEndChild(other_root->DeepClone(
this));
786 throw exceptions::TreeDocumentError(
787 "Cannot merge tree document: Root element of other document must either be <" + std::string(ROOT_ELEMENT_NAME) +
788 "> or <" + TREE_ELEMENT_NAME +
">.");
791 if (adopt_root_tree && other_tree_names.size() == 1) {
803 std::set<std::string> include_stack;
804 return mergeTreeDocumentImpl(
static_cast<const XMLDocument &
>(other), adopt_root_tree, include_stack);
809 XMLDocument other_doc;
810 if (other_doc.Parse(tree_str.c_str()) != tinyxml2::XMLError::XML_SUCCESS) {
811 throw exceptions::TreeDocumentError(
"Cannot merge tree document from string: " + std::string(other_doc.ErrorStr()));
813 std::set<std::string> include_stack;
814 return mergeTreeDocumentImpl(other_doc, adopt_root_tree, include_stack);
819 std::set<std::string> include_stack;
820 return mergeFileImpl(path, adopt_root_tree, include_stack);
824 const std::string & path,
bool adopt_root_tree, std::set<std::string> & include_stack)
827 std::string canonical_path;
829 canonical_path = std::filesystem::canonical(path).string();
830 }
catch (
const std::filesystem::filesystem_error &) {
833 canonical_path = path;
837 if (include_stack.count(canonical_path) > 0) {
838 throw exceptions::TreeDocumentError(
839 "Cannot merge tree document: Circular include detected for file '" + path +
"'.");
843 include_stack.insert(canonical_path);
845 XMLDocument other_doc;
846 if (other_doc.LoadFile(path.c_str()) != tinyxml2::XMLError::XML_SUCCESS) {
847 throw exceptions::TreeDocumentError(
"Cannot create tree document from file " + path +
": " + other_doc.ErrorStr());
850 mergeTreeDocumentImpl(other_doc, adopt_root_tree, include_stack);
853 include_stack.erase(canonical_path);
860 return mergeFile(resource.build_request_file_path_, adopt_root_tree);
865 XMLDocument tree_doc;
866 tree_doc.InsertEndChild(tree.
ele_ptr_->DeepClone(&tree_doc));
874 if (tree_name.empty()) {
875 throw exceptions::TreeDocumentError(
"Cannot create a new tree with an empty name");
878 throw exceptions::TreeDocumentError(
879 "Cannot create a new tree with name '" + tree_name +
"' because it already exists.");
881 TreeDocument::XMLElement * new_ele = RootElement()->InsertNewChildElement(TreeDocument::TREE_ELEMENT_NAME);
882 new_ele->SetAttribute(TreeDocument::TREE_NAME_ATTRIBUTE_NAME, tree_name.c_str());
893 std::string name(tree_name);
897 }
else if (
const std::vector<std::string> names = other.
getAllTreeNames(); names.size() == 1) {
900 throw exceptions::TreeDocumentError(
901 "Failed to create new tree element from another document because argument tree_name was omitted and it was not "
902 "possible to determine the root tree automatically.");
912 TreeDocument new_doc(format_version_, tree_node_loader_ptr_);
919 TreeDocument new_doc(format_version_, tree_node_loader_ptr_);
925 const TreeResource & resource,
const std::string & tree_name)
927 TreeDocument new_doc(format_version_, tree_node_loader_ptr_);
928 new_doc.
mergeFile(resource.build_request_file_path_);
942 return TreeElement(
this, getXMLElementForTreeWithName(tree_name));
947 if (tree_name.empty()) {
948 throw exceptions::TreeDocumentError(
"Cannot set root tree name with empty string.");
951 throw exceptions::TreeDocumentError(
952 "Cannot make tree with name '" + tree_name +
"' the root tree because it doesn't exist.");
954 RootElement()->SetAttribute(ROOT_TREE_ATTRIBUTE_NAME, tree_name.c_str());
960 if (RootElement()->Attribute(ROOT_TREE_ATTRIBUTE_NAME))
return true;
966 if (
const auto tree_name = RootElement()->Attribute(ROOT_TREE_ATTRIBUTE_NAME))
return tree_name;
967 throw exceptions::TreeDocumentError(
968 "Cannot get root tree name because the document's root element has no attribute '" +
969 std::string(ROOT_TREE_ATTRIBUTE_NAME) +
"'.");
976 RootElement()->DeleteChild(getXMLElementForTreeWithName(tree_name));
987 std::set<std::string> old_node_names;
988 for (
const auto & [name, _] : registered_nodes_manifest_.map()) {
989 old_node_names.insert(name);
996 auto rename_recursive = [&old_node_names, &node_namespace, &sep](XMLElement * parent,
auto & self) ->
void {
997 for (XMLElement * child = parent->FirstChildElement(); child !=
nullptr; child = child->NextSiblingElement()) {
998 const char * name = child->Name();
1000 if (name && old_node_names.count(name) > 0) {
1001 child->SetName((node_namespace + sep + name).c_str());
1008 for (XMLElement * tree_ele = RootElement()->FirstChildElement(TREE_ELEMENT_NAME); tree_ele !=
nullptr;
1009 tree_ele = tree_ele->NextSiblingElement(TREE_ELEMENT_NAME)) {
1010 rename_recursive(tree_ele, rename_recursive);
1017 std::vector<std::pair<std::string, std::pair<BT::TreeNodeManifest, BT::NodeBuilder>>> registrations_to_update;
1018 const auto & factory_manifests = factory_.manifests();
1019 const auto & factory_builders = factory_.builders();
1020 for (
const auto & old_name : old_node_names) {
1021 const auto manifest_it = factory_manifests.find(old_name);
1022 const auto builder_it = factory_builders.find(old_name);
1023 if (manifest_it == factory_manifests.end() || builder_it == factory_builders.end()) {
1025 throw exceptions::TreeDocumentError(
1026 "Internal error while applying node namespace: Node '" + old_name +
1027 "' not found in BehaviorTreeFactory registrations.");
1030 registrations_to_update.emplace_back(old_name, std::make_pair(manifest_it->second, builder_it->second));
1034 for (
const auto & [old_name, manifest_builder_pair] : registrations_to_update) {
1035 factory_.unregisterBuilder(old_name);
1036 BT::TreeNodeManifest new_manifest = manifest_builder_pair.first;
1037 new_manifest.registration_ID = node_namespace + sep + old_name;
1038 factory_.registerBuilder(new_manifest, manifest_builder_pair.second);
1047 std::set<std::string> all_registration_names;
1048 for (
const auto & [name, _] : tree_node_manifest.
map()) all_registration_names.insert(name);
1049 if (
const std::set<std::string> common =
1052 throw exceptions::TreeDocumentError(
1053 "Found reserved node registration names in the node manifest. The following names are not allowed, because "
1054 "they refer to native behavior tree nodes: [ " +
1055 auto_apms_util::join(std::vector<std::string>(common.begin(), common.end()),
", ") +
" ].");
1058 for (
const auto & [node_name, params] : tree_node_manifest.
map()) {
1063 if (registered_nodes_manifest_.contains(node_name)) {
1066 factory_.unregisterBuilder(node_name);
1067 registered_nodes_manifest_.remove(node_name);
1070 throw exceptions::TreeDocumentError(
1071 "Tried to register node '" + node_name +
"' (Class: " + params.class_name +
1072 ") which is already known. You must make sure that the registration names are unique or explicitly allow "
1073 "overriding previously registered nodes with the same name by setting override=true.");
1078 if (!tree_node_loader_ptr_->isClassAvailable(params.class_name)) {
1079 if (all_node_classes_package_map_.find(params.class_name) == all_node_classes_package_map_.end()) {
1080 throw exceptions::TreeDocumentError(
1081 "Node '" + node_name +
" (" + params.class_name +
1082 ")' cannot be registered, because the class name is not known to the class loader. "
1083 "Make sure that it's spelled correctly and registered by calling "
1084 "auto_apms_behavior_tree_register_nodes() in the CMakeLists.txt of the "
1085 "corresponding package.");
1087 throw exceptions::TreeDocumentError(
1088 "Node '" + node_name +
" (" + params.class_name +
1089 ")' cannot be registered, because the corresponding resource belongs to excluded package '" +
1090 all_node_classes_package_map_.at(params.class_name) +
"'.");
1093 pluginlib::UniquePtr<NodeRegistrationInterface> plugin_instance;
1095 plugin_instance = tree_node_loader_ptr_->createUniqueInstance(params.class_name);
1096 }
catch (
const pluginlib::CreateClassException & e) {
1097 throw pluginlib::CreateClassException(
1098 "Failed to create an instance of node '" + node_name +
" (" + params.class_name +
1099 ")'. Remember that the AUTO_APMS_BEHAVIOR_TREE_REGISTER_NODE "
1100 "macro must be called in the source file for the node class to be discoverable. "
1106 if (plugin_instance->requiresRosNodeContext()) {
1107 if (only_non_ros_nodes_) {
1108 throw exceptions::NodeRegistrationError(
1109 "Node '" + node_name +
1110 "' relies on ROS 2 functionality but this instance only allows to use non-ROS nodes.");
1113 ros_node_wptr_.lock(), tree_node_waitables_callback_group_wptr_.lock(),
1114 tree_node_waitables_executor_wptr_.lock(), params);
1115 plugin_instance->registerWithBehaviorTreeFactory(factory_, node_name, &ros_node_context);
1118 RosNodeContext ros_node_context(
nullptr,
nullptr,
nullptr, params);
1119 plugin_instance->registerWithBehaviorTreeFactory(factory_, node_name, &ros_node_context);
1121 }
catch (
const std::exception & e) {
1122 throw exceptions::NodeRegistrationError(
1123 "Cannot register node '" + node_name +
" (" + params.class_name +
")': " + e.what() +
".");
1125 registered_nodes_manifest_.add(node_name, params);
1132 std::set<std::string> names;
1133 if (include_native) names = native_node_names_;
1134 for (
const auto & [name, _] : registered_nodes_manifest_.map()) names.insert(name);
1143 XMLElement * ptr =
const_cast<XMLElement *
>(getXMLElementForTreeWithName(tree_name));
1153 tinyxml2::XMLElement * model_root = RootElement()->FirstChildElement(TREE_NODE_MODEL_ELEMENT_NAME);
1157 model_root = NewElement(TREE_NODE_MODEL_ELEMENT_NAME);
1158 RootElement()->InsertEndChild(model_root);
1162 for (
const auto & [node_name,
model] : model_map) {
1164 tinyxml2::XMLElement * node_element = NewElement(BT::toStr(
model.type).c_str());
1165 node_element->SetAttribute(
"ID", node_name.c_str());
1168 for (
const auto & port_info :
model.port_infos) {
1169 tinyxml2::XMLElement * port_element =
nullptr;
1172 switch (port_info.port_direction) {
1173 case BT::PortDirection::INPUT:
1174 port_element = NewElement(
"input_port");
1176 case BT::PortDirection::OUTPUT:
1177 port_element = NewElement(
"output_port");
1179 case BT::PortDirection::INOUT:
1180 port_element = NewElement(
"inout_port");
1185 port_element->SetAttribute(
"name", port_info.port_name.c_str());
1187 if (!port_info.port_type.empty()) {
1188 port_element->SetAttribute(
"type", port_info.port_type.c_str());
1191 if (port_info.port_has_default) {
1192 port_element->SetAttribute(
"default", port_info.port_default.c_str());
1196 if (!port_info.port_description.empty()) {
1197 port_element->SetText(port_info.port_description.c_str());
1200 node_element->InsertEndChild(port_element);
1203 model_root->InsertEndChild(node_element);
1211 const tinyxml2::XMLElement * root = doc.RootElement();
1213 throw exceptions::TreeDocumentError(
"Node model document has no root element.");
1215 if (
const char * ver = root->Attribute(TreeDocument::BTCPP_FORMAT_ATTRIBUTE_NAME)) {
1216 const std::string expected_format = TreeDocument::BTCPP_FORMAT_DEFAULT_VERSION;
1217 if (std::string(ver) != expected_format) {
1218 throw exceptions::TreeDocumentError(
1219 "Cannot parse node model document: Format of model document (" +
1220 std::string(TreeDocument::BTCPP_FORMAT_ATTRIBUTE_NAME) +
": " + ver +
1221 ") doesn't comply with the expected format (" + std::string(TreeDocument::BTCPP_FORMAT_ATTRIBUTE_NAME) +
": " +
1222 expected_format +
").");
1225 throw exceptions::TreeDocumentError(
1226 "Cannot parse node model document: Root element of model document doesn't have required attribute '" +
1227 std::string(TreeDocument::BTCPP_FORMAT_ATTRIBUTE_NAME) +
"'.");
1229 tinyxml2::XMLElement * model_ele = doc.RootElement()->FirstChildElement(TreeDocument::TREE_NODE_MODEL_ELEMENT_NAME);
1231 throw exceptions::TreeDocumentError(
1232 "Element <" + std::string(TreeDocument::TREE_NODE_MODEL_ELEMENT_NAME) +
1233 "> doesn't exist in node model document.");
1237 for (tinyxml2::XMLElement * ele = model_ele->FirstChildElement(); ele !=
nullptr; ele = ele->NextSiblingElement()) {
1238 const char * node_name = ele->Attribute(
"ID");
1240 throw exceptions::TreeDocumentError(
1241 "Element '" + std::string(ele->Name()) +
"' in node model document is missing the required attribute 'ID'");
1244 model.type = BT::convertFromString<BT::NodeType>(ele->Name());
1245 for (
const tinyxml2::XMLElement * port_ele = ele->FirstChildElement(); port_ele !=
nullptr;
1246 port_ele = port_ele->NextSiblingElement()) {
1247 const std::string direction = port_ele->Name();
1249 if (direction ==
"input_port") {
1251 }
else if (direction ==
"output_port") {
1253 }
else if (direction ==
"inout_port") {
1256 throw exceptions::TreeDocumentError(
1257 "Unkown port direction in node model for '" + std::string(node_name) +
"': " + direction);
1259 if (
const char * c = port_ele->Attribute(
"name")) {
1262 if (
const char * c = port_ele->Attribute(
"type")) {
1265 if (
const char * c = port_ele->Attribute(
"default")) {
1271 if (
const char * c = port_ele->GetText()) {
1274 model.port_infos.push_back(std::move(port_info));
1280 std::map<std::string, std::vector<std::string>> hidden_ports;
1281 for (
const auto & [node_name, registration_options] : manifest.
map()) {
1283 for (
const std::string & port_name : registration_options.hidden_ports) {
1284 hidden_ports[node_name].push_back(port_name);
1288 for (
const auto & [port_name, _] : registration_options.port_alias) {
1289 hidden_ports[node_name].push_back(port_name);
1294 for (
const auto & [node_name, ports_to_hide] : hidden_ports) {
1295 auto it = model_map.find(node_name);
1296 if (it == model_map.end()) {
1300 model.port_infos.erase(
1302 model.port_infos.begin(),
model.port_infos.end(),
1304 return std::find(ports_to_hide.begin(), ports_to_hide.end(), port_info.port_name) != ports_to_hide.end();
1306 model.port_infos.end());
1315 std::string model_xml;
1317 model_xml = BT::writeTreeNodesModelXML(factory_, include_native);
1318 }
catch (
const std::exception & e) {
1319 throw exceptions::TreeDocumentError(
"Error generating node model XML from factory: " + std::string(e.what()));
1323 tinyxml2::XMLDocument model_doc;
1324 if (model_doc.Parse(model_xml.c_str()) != tinyxml2::XMLError::XML_SUCCESS) {
1325 throw exceptions::TreeDocumentError(
1326 "Error parsing the model of the currently registered nodes: " + std::string(model_doc.ErrorStr()));
1330 return getNodeModel(model_doc, registered_nodes_manifest_);
1336 std::unordered_map<std::string, BT::NodeType> registered_nodes;
1337 for (
const auto & [node_name,
model] : model_map) {
1338 registered_nodes[node_name] =
model.type;
1342 }
catch (
const BT::RuntimeError & e) {
1343 return nonstd::make_unexpected(e.what());
1350 tinyxml2::XMLPrinter printer;
1352 return printer.CStr();
1359 tinyxml2::XMLError result = doc.SaveFile(path.c_str());
1360 if (result != tinyxml2::XML_SUCCESS) {
1361 throw exceptions::TreeDocumentError(
1362 "Failed to write tree document to file. Error ID: " + std::string(doc.ErrorIDToName(result)));
1369 tinyxml2::XMLElement * root_ele = NewElement(TreeDocument::ROOT_ELEMENT_NAME);
1370 root_ele->SetAttribute(TreeDocument::BTCPP_FORMAT_ATTRIBUTE_NAME, format_version_.c_str());
1371 InsertFirstChild(root_ele);
1375const TreeDocument::XMLElement * TreeDocument::getXMLElementForTreeWithName(
const std::string & tree_name)
const
1377 return getXMLElementForTreeWithNameImpl<const XMLElement *>(*
this, tree_name);
1380TreeDocument::XMLElement * TreeDocument::getXMLElementForTreeWithName(
const std::string & tree_name)
1382 return getXMLElementForTreeWithNameImpl<XMLElement *>(*
this, tree_name);
1385template <
typename ReturnT,
typename DocumentT>
1386ReturnT TreeDocument::getXMLElementForTreeWithNameImpl(DocumentT & doc,
const std::string & tree_name)
1388 if (tree_name.empty()) {
1389 throw exceptions::TreeDocumentError(
"Cannot get tree with an empty name.");
1391 if (!doc.hasTreeName(tree_name)) {
1392 throw exceptions::TreeDocumentError(
"Cannot get tree with name '" + tree_name +
"' because it doesn't exist.");
1394 auto child = doc.RootElement()->FirstChildElement(TreeDocument::TREE_ELEMENT_NAME);
1395 while (child && !child->Attribute(TreeDocument::TREE_NAME_ATTRIBUTE_NAME, tree_name.c_str())) {
1396 child = child->NextSiblingElement();
1399 throw std::logic_error(
1400 "Unexpected error trying to get tree element with name '" + tree_name +
1401 "'. Since hasTreeName() returned true, there MUST be a corresponding element.");
const NodeManifest & getNodeManifest() const
Get the node manifest associated with this resource.
Data structure for information about which behavior tree node plugin to load and how to configure the...
NodeManifest & add(const std::string &node_name, const RegistrationOptions &opt)
Add registration options for a behavior tree node to the manifest.
bool contains(const std::string &node_name) const
Determine if a behavior tree node has been added to the manifest.
NodeManifest & merge(const NodeManifest &other, bool replace=false)
Merges another NodeManifest with this one.
const Map & map() const
Get a view of the internal map.
A pluginlib::ClassLoader specifically for loading installed behavior tree node plugins.
Additional parameters specific to ROS 2 determined at runtime by TreeBuilder.
Class that encapsulates behavior tree script expressions.
std::string str() const
Concatenate all expressions of this instance to a single string.
Handle for a single node of a TreeDocument.
const TreeDocument & getParentDocument() const
Get a const view of this node's parent tree document.
NodeElement insertTreeFromResource(const TreeResource &resource, const std::string &tree_name, const NodeElement *before_this=nullptr)
Concatenate a tree from one of the installed package's behavior tree resources and add its first chil...
const std::vector< std::string > & getPortNames() const
Get the names of the data ports implemented by the node represented by this element.
virtual std::string getRegistrationName() const
Get the name of this node given during registration representing its dynamic type.
std::function< bool(const NodeElement &)> ConstDeepApplyCallback
Callback invoked for every node found under another node. It cannot modify the current node.
NodeElement & resetPorts()
Delete all currently specified port values and reset with the defaults.
std::function< bool(NodeElement &)> DeepApplyCallback
Callback invoked for every node found under another node. It may modify the current node.
NodeElement & removeFirstChild(const std::string ®istration_name="", const std::string &instance_name="")
Recursively visit this node's children in execution order and remove the first node with a particular...
NodeElement & setPorts(const PortValues &port_values)
Populate the the node's data ports.
virtual NodeElement & setName(const std::string &instance_name)
Assign a name for this specific node instance.
NodeElement getFirstNode(const std::string ®istration_name="", const std::string &instance_name="") const
Recursively visit this node's children in execution order and get the first node with a particular re...
NodeElement(TreeDocument *doc_ptr, XMLElement *ele_ptr)
Protected constructor intended for internal use only.
std::map< std::string, std::string > PortValues
Mapping of port names and its respective value encoded as string.
const std::vector< NodeElement > deepApplyConst(ConstDeepApplyCallback apply_callback) const
Recursively apply a callback to this node's children.
TreeDocument * doc_ptr_
Pointer to the tree document that created the tree this node belongs to.
virtual std::string getName() const
Get the name of this node given to this specific instance by the developer.
PortValues getPorts() const
Assemble the values given to each data port implemented by this node.
NodeElement & setConditionalScript(BT::PreCond type, const Script &script)
Specify a script that is evaluated before this node is ticked.
NodeElement insertTreeFromString(const std::string &tree_str, const std::string &tree_name, const NodeElement *before_this=nullptr)
Concatenate a tree from a document created from a string and add its first child node to the children...
tinyxml2::XMLElement * ele_ptr_
Pointer to the corresponding XMLElement of the base document.
NodeElement insertTree(const TreeElement &tree, const NodeElement *before_this=nullptr)
Concatenate a tree and add its first child node to the children of this node.
std::string getFullyQualifiedName() const
Create a string that uniquely identifies this node considering its registration and its instance name...
NodeElement insertTreeFromDocument(const TreeDocument &doc, const std::string &tree_name, const NodeElement *before_this=nullptr)
Concatenate a tree from a document and add its first child node to the children of this node.
model::SubTree insertSubTreeNode(const std::string &tree_name, const NodeElement *before_this=nullptr)
Add a subtree node for a specific tree element to the children of this node.
bool hasChildren() const
Determine whether any children have been given to this node.
bool operator==(const NodeElement &other) const
Determine if two node elements refer to the same node.
NodeElement insertNode(const std::string &name, const NodeElement *before_this=nullptr)
Add a new node to the children of this node.
NodeElement insertTreeFromFile(const std::string &path, const std::string &tree_name, const NodeElement *before_this=nullptr)
Concatenate a tree from a document created from a file and add its first child node to the children o...
NodeElement & removeChildren()
Recursively remove all children of this node element.
NodeElement & operator=(const NodeElement &other)
Replace this node with another.
std::vector< NodeElement > deepApply(DeepApplyCallback apply_callback)
Recursively apply a callback to this node's children.
Handle for a single behavior tree of a TreeDocument.
TreeElement & removeChildren()
TreeElement(TreeDocument *doc_ptr, XMLElement *ele_ptr)
Protected constructor intended to be used only by certain factory methods of TreeDocument.
std::string getName() const override
Get the name of the behavior tree.
BT::Result verify() const
Verify that this behavior tree is structured correctly and can be created successfully.
NodeManifest getRequiredNodeManifest() const
Assemble the node manifest that is required for successfully creating an instance of this tree.
TreeElement & makeRoot()
Set this behavior tree as the root tree of the parent document.
TreeElement & removeFirstChild(const std::string ®istration_name="", const std::string &instance_name="")
TreeElement & operator=(const TreeElement &other)
Replace the behavior tree represented by this element with another.
TreeElement & setName(const std::string &tree_name) override
Set the name of the behavior tree.
std::string writeToString() const
Write this behavior tree to an XML encoded in a string.
Document Object Model (DOM) for the behavior tree XML schema. This class offers a programmatic approa...
TreeElement getRootTree()
Get the corresponding behavior tree element for the root tree of this document.
TreeDocument & mergeResource(const TreeResource &resource, bool adopt_root_tree=false)
Merge the behavior trees from one of the installed package's behavior tree resources.
std::string getRootTreeName() const
Get the name of this document's root tree.
TreeDocument & mergeTreeDocument(const XMLDocument &other, bool adopt_root_tree=false)
Merge another tree document with this one.
TreeDocument & mergeString(const std::string &tree_str, bool adopt_root_tree=false)
Create a tree document from a string and merge it with this one.
TreeElement newTreeFromDocument(const TreeDocument &other, const std::string &tree_name="")
Create a new behavior tree inside this document with the content of one found inside another tree doc...
void writeToFile(const std::string &path) const
Write the XML of this tree document to a file.
TreeElement newTreeFromFile(const std::string &path, const std::string &tree_name="")
Create a new behavior tree inside this document with the content of one found inside the XML file.
TreeDocument & applyNodeNamespace(const std::string &node_namespace, const std::string &sep=_AUTO_APMS_BEHAVIOR_TREE_CORE__NODE_NAMESPACE_DEFAULT_SEP)
Prepend a namespace to all nodes associated with this document.
BT::Result verify() const
Verify that all behavior trees of this document are structured correctly and can be created successfu...
NodeManifest getRequiredNodeManifest() const
Assemble the node manifest that is required for successfully creating an instance of any of the docum...
std::set< std::string > getRegisteredNodeNames(bool include_native=true) const
Get the names of all nodes that are known to this document.
TreeElement newTreeFromString(const std::string &tree_str, const std::string &tree_name="")
Create a new behavior tree inside this document with the content of one found inside the XML string.
TreeElement newTreeFromResource(const TreeResource &resource, const std::string &tree_name="")
Create a new behavior tree inside this document with the content of one the trees found inside a part...
TreeDocument & reset()
Clear this document and reset it to its initial state.
TreeElement getTree(const std::string &tree_name)
Get the corresponding behavior tree element for a tree inside this document.
virtual TreeDocument & registerNodes(const NodeManifest &tree_node_manifest, bool override=false)
Load behavior tree node plugins and register them with the internal behavior tree factory.
TreeDocument & setRootTreeName(const std::string &tree_name)
Define the root tree of this document.
bool hasTreeName(const std::string &tree_name) const
Determine if this document contains a behavior tree with a particular name.
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.
TreeDocument & addNodeModel(NodeModelMap model_map)
Add a behavior tree node model element to the document by parsing the contents of model_map.
TreeDocument & mergeTree(const TreeElement &tree, bool make_root_tree=false)
Merge an existing behavior tree with this tree document.
std::string writeToString() const
Write the XML of this tree document to a string.
bool hasRootTreeName() const
Determine if this document specifies which of its trees is the root tree.
TreeDocument & removeTree(const std::string &tree_name)
Remove a particular behavior tree from this document.
TreeDocument(const std::string &format_version=BTCPP_FORMAT_DEFAULT_VERSION, NodeRegistrationLoader::SharedPtr tree_node_loader=NodeRegistrationLoader::make_shared())
Create a an empty tree document.
static NodeModelMap getNodeModel(tinyxml2::XMLDocument &doc, const NodeManifest &manifest)
Convert a behavior tree node model document to the corresponding data structure.
TreeElement newTree(const std::string &tree_name)
Create a new behavior tree inside this document.
Class containing behavior tree resource data.
std::string getRootTreeName() const
Get the name of the root tree of this behavior tree resource.
bool hasRootTreeName() const
Determine if this behavior tree resource specifies a root tree.
Subtree behavior tree node model.
bool contains(const ContainerT< ValueT, AllocatorT > &c, const ValueT &val)
Check whether a particular container structure contains a value.
std::set< KeyT, CompareT, AllocatorT > getCommonElements(std::set< KeyT, CompareT, AllocatorT > c1, std::set< KeyT, CompareT, AllocatorT > c2)
Assemble common elements of two sets.
Core API for AutoAPMS's behavior tree implementation.
Models for all available behavior tree nodes.
Powerful tooling for incorporating behavior trees for task development.
std::map< std::string, NodeModel > NodeModelMap
Mapping of node registration names and their implementation details.
Data structure encapsulating the information of all ports implemented by a behavior tree node.
Implementation details of a single data port.
std::string port_default
Default value of the port encoded as string.
bool port_has_default
Flag whether the port implements a default value or not.
std::string port_description
Description of the port.
BT::PortDirection port_direction
Direction of the port.
std::string port_type
String representation of the C++ type given to the port.
std::string port_name
Name of the port.
Parameters for loading and registering a behavior tree node class from a shared library using e....