Skip to content

Commit

Permalink
Read description from xml
Browse files Browse the repository at this point in the history
  • Loading branch information
GregoryLeMasurier committed Apr 12, 2024
1 parent dbf9733 commit 81ee737
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion include/behaviortree_cpp/bt_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ class BehaviorTreeFactory
* @return new node.
*/
[[nodiscard]] std::unique_ptr<TreeNode> 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()
Expand Down
14 changes: 8 additions & 6 deletions include/behaviortree_cpp/tree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -383,6 +384,7 @@ class TreeNode
virtual void halt() = 0;

TreeNode *parent_;
std::string description_;
bool failed_ = false;
};

Expand Down
2 changes: 1 addition & 1 deletion src/basic_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ bool IsAllowedPortName(StringView str)
{
return false;
}
if(str == "name" || str == "ID")
if(str == "name" || str == "ID" || str == "description")
{
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/behavior_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const BT::ControlNode*>(node))
Expand Down
3 changes: 2 additions & 1 deletion src/bt_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ void BehaviorTreeFactory::clearRegisteredBehaviorTrees()
}

std::unique_ptr<TreeNode> 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;
Expand Down Expand Up @@ -400,6 +400,7 @@ std::unique_ptr<TreeNode> 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)
Expand Down
4 changes: 2 additions & 2 deletions src/loggers/bt_cout_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void StdCoutLogger::callback(Duration timestamp, const TreeNode& node,
constexpr const size_t ws_count = 25;

double since_epoch = duration<double>(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;
}
Expand Down
7 changes: 5 additions & 2 deletions src/xml_parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<SubTreeNode*>(new_node.get());
subtree_node->setSubtreeID(type_ID);
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 81ee737

Please sign in to comment.