Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 96910f2

Browse files
committed
simplify metadata, address review feedback
1 parent 49355fd commit 96910f2

File tree

6 files changed

+52
-133
lines changed

6 files changed

+52
-133
lines changed

include/behaviortree_cpp/basic_types.h

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -439,43 +439,6 @@ struct has_static_method_providedPorts<
439439
{
440440
};
441441

442-
template <typename T, typename = void>
443-
struct has_static_method_description : std::false_type
444-
{
445-
};
446-
447-
template <typename T>
448-
struct has_static_method_description<
449-
T, typename std::enable_if<
450-
std::is_same<decltype(T::description()), std::string>::value>::type>
451-
: std::true_type
452-
{
453-
};
454-
455-
/// Optional metadata for a TreeNodeManifest.
456-
/// The metadata can represent an xml element with text:
457-
/// <metadata_name>Descriptive text</metadata_name>
458-
/// Or an xml element with an attribute:
459-
/// <metadata_name attribute="value"/>
460-
struct ManifestMetadata
461-
{
462-
/// A pair containing the <name, value> of a metadata's XML attribute.
463-
using Attribute = std::pair<std::string, std::string>;
464-
465-
[[nodiscard]] bool representsText() const
466-
{
467-
return std::holds_alternative<std::string>(text_or_attribute);
468-
}
469-
470-
[[nodiscard]] bool representsAttribute() const
471-
{
472-
return std::holds_alternative<Attribute>(text_or_attribute);
473-
}
474-
475-
std::string name;
476-
std::variant<std::string, Attribute> text_or_attribute;
477-
};
478-
479442
template <typename T, typename = void>
480443
struct has_static_method_metadata : std::false_type
481444
{
@@ -484,7 +447,7 @@ struct has_static_method_metadata : std::false_type
484447
template <typename T>
485448
struct has_static_method_metadata<
486449
T, typename std::enable_if<
487-
std::is_same<decltype(T::metadata()), std::vector<ManifestMetadata>>::value>::type>
450+
std::is_same<decltype(T::metadata()), std::vector<std::pair<std::string, std::string>>>::value>::type>
488451
: std::true_type
489452
{
490453
};

include/behaviortree_cpp/bt_factory.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <unordered_map>
2323
#include <unordered_set>
2424
#include <set>
25+
#include <utility>
26+
#include <vector>
2527

2628
#include "behaviortree_cpp/contrib/magic_enum.hpp"
2729
#include "behaviortree_cpp/behavior_tree.h"
@@ -44,20 +46,11 @@ template <typename T>
4446
inline TreeNodeManifest CreateManifest(const std::string& ID,
4547
PortsList portlist = getProvidedPorts<T>())
4648
{
47-
if constexpr( has_static_method_description<T>::value && has_static_method_metadata<T>::value )
49+
if constexpr( has_static_method_metadata<T>::value )
4850
{
49-
return {getType<T>(), ID, portlist, T::description(), T::metadata()};
51+
return {getType<T>(), ID, portlist, T::metadata()};
5052
}
51-
else if constexpr( has_static_method_description<T>::value && !has_static_method_metadata<T>::value )
52-
{
53-
return {getType<T>(), ID, portlist, T::description(), {}};
54-
}
55-
else if constexpr( !has_static_method_description<T>::value && has_static_method_metadata<T>::value )
56-
{
57-
return {getType<T>(), ID, portlist, {}, T::metadata()};
58-
}
59-
60-
return {getType<T>(), ID, portlist, {}, {}};
53+
return {getType<T>(), ID, portlist, {}};
6154
}
6255

6356
#ifdef BT_PLUGIN_EXPORT
@@ -432,10 +425,10 @@ class BehaviorTreeFactory
432425
Tree createTree(const std::string& tree_name,
433426
Blackboard::Ptr blackboard = Blackboard::create());
434427

435-
/// Add a description to a specific manifest. This description will be added
428+
/// Add metadata to a specific manifest. This metadata will be added
436429
/// to <TreeNodesModel> with the function writeTreeNodesModelXML()
437-
void addDescriptionToManifest(const std::string& node_id,
438-
const std::string& description);
430+
void addMetadataToManifest(const std::string& node_id,
431+
const std::vector<std::pair<std::string, std::string>>& metadata);
439432

440433
/**
441434
* @brief Add an Enum to the scripting language.

include/behaviortree_cpp/tree_node.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <exception>
1818
#include <mutex>
1919
#include <map>
20+
#include <utility>
2021
#include <vector>
2122

2223
#include "behaviortree_cpp/utils/signal.h"
@@ -39,8 +40,7 @@ struct TreeNodeManifest
3940
NodeType type;
4041
std::string registration_ID;
4142
PortsList ports;
42-
std::string description;
43-
std::vector<ManifestMetadata> metadata;
43+
std::vector<std::pair<std::string, std::string>> metadata;
4444
};
4545

4646
using PortsRemapping = std::unordered_map<std::string, std::string>;

src/bt_factory.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void BehaviorTreeFactory::registerSimpleCondition(
157157
return std::make_unique<SimpleConditionNode>(name, tick_functor, config);
158158
};
159159

160-
TreeNodeManifest manifest = {NodeType::CONDITION, ID, std::move(ports), {}, {}};
160+
TreeNodeManifest manifest = {NodeType::CONDITION, ID, std::move(ports), {}};
161161
registerBuilder(manifest, builder);
162162
}
163163

@@ -170,7 +170,7 @@ void BehaviorTreeFactory::registerSimpleAction(
170170
return std::make_unique<SimpleActionNode>(name, tick_functor, config);
171171
};
172172

173-
TreeNodeManifest manifest = {NodeType::ACTION, ID, std::move(ports), {}, {}};
173+
TreeNodeManifest manifest = {NodeType::ACTION, ID, std::move(ports), {}};
174174
registerBuilder(manifest, builder);
175175
}
176176

@@ -183,7 +183,7 @@ void BehaviorTreeFactory::registerSimpleDecorator(
183183
return std::make_unique<SimpleDecoratorNode>(name, tick_functor, config);
184184
};
185185

186-
TreeNodeManifest manifest = {NodeType::DECORATOR, ID, std::move(ports), {}, {}};
186+
TreeNodeManifest manifest = {NodeType::DECORATOR, ID, std::move(ports), {}};
187187
registerBuilder(manifest, builder);
188188
}
189189

@@ -435,15 +435,15 @@ Tree BehaviorTreeFactory::createTree(const std::string& tree_name,
435435
return tree;
436436
}
437437

438-
void BehaviorTreeFactory::addDescriptionToManifest(const std::string& node_id,
439-
const std::string& description)
438+
void BehaviorTreeFactory::addMetadataToManifest(const std::string& node_id,
439+
const std::vector<std::pair<std::string, std::string>>& metadata)
440440
{
441441
auto it = _p->manifests.find(node_id);
442442
if (it == _p->manifests.end())
443443
{
444-
throw std::runtime_error("addDescriptionToManifest: wrong ID");
444+
throw std::runtime_error("addMetadataToManifest: wrong ID");
445445
}
446-
it->second.description = description;
446+
it->second.metadata = metadata;
447447
}
448448

449449
void BehaviorTreeFactory::registerScriptingEnum(StringView name, int value)

src/xml_parsing.cpp

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -982,29 +982,18 @@ void addNodeModelToXML(const TreeNodeManifest& model,
982982
element->InsertEndChild(port_element);
983983
}
984984

985-
if (!model.description.empty())
985+
if (!model.metadata.empty())
986986
{
987-
auto description_element = doc.NewElement("description");
988-
description_element->SetText(model.description.c_str());
989-
element->InsertEndChild(description_element);
990-
}
991-
992-
for (const auto& metadata : model.metadata)
993-
{
994-
auto metadata_element = doc.NewElement(metadata.name.c_str());
987+
auto metadata_root = doc.NewElement("MetadataFields");
995988

996-
if (metadata.representsText())
997-
{
998-
const auto element_text = std::get<std::string>(metadata.text_or_attribute);
999-
metadata_element->SetText(element_text.c_str());
1000-
}
1001-
else
989+
for (const auto& [name, value] : model.metadata)
1002990
{
1003-
const auto [attribute_name, attribute_val] = std::get<ManifestMetadata::Attribute>(metadata.text_or_attribute);
1004-
metadata_element->SetAttribute(attribute_name.c_str(), attribute_val.c_str());
991+
auto metadata_element = doc.NewElement("Metadata");
992+
metadata_element->SetAttribute(name.c_str(), value.c_str());
993+
metadata_root->InsertEndChild(metadata_element);
1005994
}
1006995

1007-
element->InsertEndChild(metadata_element);
996+
element->InsertEndChild(metadata_root);
1008997
}
1009998

1010999
model_root->InsertEndChild(element);

tests/gtest_factory.cpp

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -391,49 +391,12 @@ TEST(BehaviorTreeReload, ReloadSameTree)
391391
}
392392
}
393393

394-
class DescriptiveAction : public SyncActionNode
394+
std::vector<std::pair<std::string, std::string>> makeTestMetadata()
395395
{
396-
public:
397-
DescriptiveAction(const std::string& name, const NodeConfig& config):
398-
SyncActionNode(name, config) {}
399-
400-
BT::NodeStatus tick() override {
401-
return NodeStatus::SUCCESS;
402-
}
403-
404-
static PortsList providedPorts() {
405-
return {};
406-
}
407-
408-
static std::string description() {
409-
return "THE DESCRIPTION";
410-
}
411-
};
412-
413-
TEST(BehaviorTreeFactory, DescriptionMethod)
414-
{
415-
BehaviorTreeFactory factory;
416-
factory.registerNodeType<DescriptiveAction>("DescriptiveAction");
417-
const auto& manifest = factory.manifests().at("DescriptiveAction");
418-
ASSERT_EQ(manifest.description, "THE DESCRIPTION");
419-
420-
auto xml = writeTreeNodesModelXML(factory, false);
421-
std::cout << xml << std::endl;
422-
423-
ASSERT_NE(xml.find( "<description>THE DESCRIPTION</description>"), std::string::npos);
424-
}
425-
426-
std::vector<ManifestMetadata> makeTestMetadata()
427-
{
428-
ManifestMetadata text_metadata;
429-
text_metadata.name = "text_metadata";
430-
text_metadata.text_or_attribute = "text";
431-
432-
ManifestMetadata attribute_metadata;
433-
attribute_metadata.name = "attribute_metadata";
434-
attribute_metadata.text_or_attribute = std::make_pair<std::string, std::string>("attribute_name", "attribute_value");
435-
436-
return {text_metadata, attribute_metadata};
396+
return {
397+
std::make_pair<std::string, std::string>("foo", "hello"),
398+
std::make_pair<std::string, std::string>("bar", "42"),
399+
};
437400
}
438401

439402
class ActionWithMetadata : public SyncActionNode
@@ -450,28 +413,39 @@ class ActionWithMetadata : public SyncActionNode
450413
return {};
451414
}
452415

453-
static std::vector<ManifestMetadata> metadata() {
416+
static std::vector<std::pair<std::string, std::string>> metadata() {
454417
return makeTestMetadata();
455418
}
456419
};
457420

458421
TEST(BehaviorTreeFactory, ManifestMethod)
459422
{
423+
const char* expectedXML = R"(
424+
<Action ID="ActionWithMetadata">
425+
<MetadataFields>
426+
<Metadata foo="hello"/>
427+
<Metadata bar="42"/>
428+
</MetadataFields>
429+
</Action>)";
430+
460431
BehaviorTreeFactory factory;
461432
factory.registerNodeType<ActionWithMetadata>("ActionWithMetadata");
462433
const auto& manifest = factory.manifests().at("ActionWithMetadata");
463-
ASSERT_EQ(manifest.metadata.size(), 2u);
464-
auto expected_metadata = makeTestMetadata();
465-
EXPECT_EQ(manifest.metadata[0].name, expected_metadata[0].name);
466-
EXPECT_EQ(manifest.metadata[0].text_or_attribute, expected_metadata[0].text_or_attribute);
467-
EXPECT_TRUE(manifest.metadata[0].representsText());
468-
EXPECT_EQ(manifest.metadata[1].name, expected_metadata[1].name);
469-
EXPECT_EQ(manifest.metadata[1].text_or_attribute, expected_metadata[1].text_or_attribute);
470-
EXPECT_TRUE(manifest.metadata[1].representsAttribute());
434+
EXPECT_EQ(manifest.metadata, makeTestMetadata());
471435

472436
auto xml = writeTreeNodesModelXML(factory, false);
473437
std::cout << xml << std::endl;
474438

475-
ASSERT_NE(xml.find("<text_metadata>text</text_metadata>"), std::string::npos);
476-
ASSERT_NE(xml.find("<attribute_metadata attribute_name=\"attribute_value\"/>"), std::string::npos);
439+
EXPECT_NE(xml.find(expectedXML), std::string::npos);
440+
}
441+
442+
TEST(BehaviorTreeFactory, addMetadataToManifest)
443+
{
444+
BehaviorTreeFactory factory;
445+
factory.registerNodeType<DummyNodes::SaySomething>("SaySomething");
446+
const auto& initial_manifest = factory.manifests().at("SaySomething");
447+
EXPECT_TRUE(initial_manifest.metadata.empty());
448+
factory.addMetadataToManifest("SaySomething", makeTestMetadata());
449+
const auto& modified_manifest = factory.manifests().at("SaySomething");
450+
EXPECT_EQ(modified_manifest.metadata, makeTestMetadata());
477451
}

0 commit comments

Comments
 (0)