From 81ee7371da8e0879b92358f145f4048052850fab Mon Sep 17 00:00:00 2001 From: Gregory LeMasurier Date: Fri, 12 Apr 2024 11:16:08 -0400 Subject: [PATCH] Read description from xml --- include/behaviortree_cpp/bt_factory.h | 2 +- include/behaviortree_cpp/tree_node.h | 14 ++++++++------ src/basic_types.cpp | 2 +- src/behavior_tree.cpp | 2 +- src/bt_factory.cpp | 3 ++- src/loggers/bt_cout_logger.cpp | 4 ++-- src/xml_parsing.cpp | 7 +++++-- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/behaviortree_cpp/bt_factory.h b/include/behaviortree_cpp/bt_factory.h index ea43c6c91..2d897766c 100644 --- a/include/behaviortree_cpp/bt_factory.h +++ b/include/behaviortree_cpp/bt_factory.h @@ -360,7 +360,7 @@ class BehaviorTreeFactory * @return new node. */ [[nodiscard]] std::unique_ptr instantiateTreeNode( - const std::string& name, const std::string& ID, const NodeConfig& config) const; + const std::string& name, const std::string& ID, const std::string& description, const NodeConfig& config) const; /** registerNodeType where you explicitly pass the list of ports. * Doesn't require the implementation of static method providedPorts() diff --git a/include/behaviortree_cpp/tree_node.h b/include/behaviortree_cpp/tree_node.h index 47bb781d8..0765d2270 100644 --- a/include/behaviortree_cpp/tree_node.h +++ b/include/behaviortree_cpp/tree_node.h @@ -328,12 +328,13 @@ class TreeNode return parent_; } - std::string short_description() const { - std::string str = name(); - if (str.empty()) { - str = config().uid; - } - return str; + void setShortDescription(StringView description) + { + description_.assign(description.data(), description.size()); + } + + std::string getShortDescription() const { + return description_; } [[nodiscard]] NodeConfig& config(); @@ -383,6 +384,7 @@ class TreeNode virtual void halt() = 0; TreeNode *parent_; + std::string description_; bool failed_ = false; }; diff --git a/src/basic_types.cpp b/src/basic_types.cpp index 5e54ed980..24aa117db 100644 --- a/src/basic_types.cpp +++ b/src/basic_types.cpp @@ -433,7 +433,7 @@ bool IsAllowedPortName(StringView str) { return false; } - if(str == "name" || str == "ID") + if(str == "name" || str == "ID" || str == "description") { return false; } diff --git a/src/behavior_tree.cpp b/src/behavior_tree.cpp index b448c144d..b3cec2712 100644 --- a/src/behavior_tree.cpp +++ b/src/behavior_tree.cpp @@ -126,7 +126,7 @@ void printTreeRecursively(const TreeNode* root_node, std::ostream& stream) stream << "!nullptr!" << std::endl; return; } - stream << node->short_description() << std::endl; + stream << node->getShortDescription() << std::endl; indent++; if(auto control = dynamic_cast(node)) diff --git a/src/bt_factory.cpp b/src/bt_factory.cpp index fa373b141..c2a48aeb8 100644 --- a/src/bt_factory.cpp +++ b/src/bt_factory.cpp @@ -332,7 +332,7 @@ void BehaviorTreeFactory::clearRegisteredBehaviorTrees() } std::unique_ptr BehaviorTreeFactory::instantiateTreeNode( - const std::string& name, const std::string& ID, const NodeConfig& config) const + const std::string& name, const std::string& ID, const std::string& description, const NodeConfig& config) const { auto idNotFound = [this, ID] { std::cerr << ID << " not included in this list:" << std::endl; @@ -400,6 +400,7 @@ std::unique_ptr BehaviorTreeFactory::instantiateTreeNode( node->setRegistrationID(ID); node->config().enums = _p->scripting_enums; + node->setShortDescription(description); auto AssignConditions = [](auto& conditions, auto& executors) { for(const auto& [cond_id, script] : conditions) diff --git a/src/loggers/bt_cout_logger.cpp b/src/loggers/bt_cout_logger.cpp index 40e473aa5..c391b3488 100644 --- a/src/loggers/bt_cout_logger.cpp +++ b/src/loggers/bt_cout_logger.cpp @@ -26,8 +26,8 @@ void StdCoutLogger::callback(Duration timestamp, const TreeNode& node, constexpr const size_t ws_count = 25; double since_epoch = duration(timestamp).count(); - printf("[%.3f]: %s%s %s -> %s", since_epoch, node.short_description().c_str(), - &whitespaces[std::min(ws_count, node.short_description().size())], + printf("[%.3f]: %s%s %s -> %s", since_epoch, node.getShortDescription().c_str(), + &whitespaces[std::min(ws_count, node.getShortDescription().size())], toStr(prev_status, true).c_str(), toStr(status, true).c_str()); std::cout << std::endl; } diff --git a/src/xml_parsing.cpp b/src/xml_parsing.cpp index adb8c8247..47cffe589 100644 --- a/src/xml_parsing.cpp +++ b/src/xml_parsing.cpp @@ -629,6 +629,9 @@ TreeNode::Ptr XMLParser::PImpl::createNodeFromXML(const XMLElement* element, const char* attr_name = element->Attribute("name"); const std::string instance_name = (attr_name != nullptr) ? attr_name : type_ID; + const char* attr_description = element->Attribute("description"); + const std::string instance_description = (attr_description != nullptr) ? attr_description : instance_name; + const TreeNodeManifest* manifest = nullptr; auto manifest_it = factory.manifests().find(type_ID); @@ -718,7 +721,7 @@ TreeNode::Ptr XMLParser::PImpl::createNodeFromXML(const XMLElement* element, { config.input_ports = port_remap; new_node = - factory.instantiateTreeNode(instance_name, toStr(NodeType::SUBTREE), config); + factory.instantiateTreeNode(instance_name, toStr(NodeType::SUBTREE), instance_description, config); auto subtree_node = dynamic_cast(new_node.get()); subtree_node->setSubtreeID(type_ID); } @@ -832,7 +835,7 @@ TreeNode::Ptr XMLParser::PImpl::createNodeFromXML(const XMLElement* element, } } - new_node = factory.instantiateTreeNode(instance_name, type_ID, config); + new_node = factory.instantiateTreeNode(instance_name, type_ID, instance_description, config); } // add the pointer of this node to the parent