From 0a3393cabc2e5a7dec86186c3b6779127922c0cd Mon Sep 17 00:00:00 2001 From: Davide Faconti Date: Sat, 9 Mar 2024 11:06:15 +0100 Subject: [PATCH] test moved and port remapping fixed --- src/xml_parsing.cpp | 24 ++++--- tests/gtest_enums.cpp | 115 +++++++++++++++++++++++++++++++++ tests/gtest_postconditions.cpp | 58 ----------------- 3 files changed, 129 insertions(+), 68 deletions(-) diff --git a/src/xml_parsing.cpp b/src/xml_parsing.cpp index f3fdc7bd3..adb8c8247 100644 --- a/src/xml_parsing.cpp +++ b/src/xml_parsing.cpp @@ -881,15 +881,19 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(const std::string& tree_ID, { auto new_bb = Blackboard::create(blackboard); const std::string subtree_ID = element->Attribute("ID"); - std::unordered_map remapping; + std::unordered_map subtree_remapping; bool do_autoremap = false; for(auto attr = element->FirstAttribute(); attr != nullptr; attr = attr->Next()) { - const char* attr_name = attr->Name(); - const char* attr_value = attr->Value(); + std::string attr_name = attr->Name(); + std::string attr_value = attr->Value(); + if(attr_value == "{=}") + { + attr_value = StrCat("{", attr_name, "}"); + } - if(StrEqual(attr_name, "_autoremap")) + if(attr_name == "_autoremap") { do_autoremap = convertFromString(attr_value); new_bb->enableAutoRemapping(do_autoremap); @@ -899,10 +903,10 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(const std::string& tree_ID, { continue; } - remapping.insert({ attr_name, attr_value }); + subtree_remapping.insert({ attr_name, attr_value }); } // check if this subtree has a model. If it does, - // we want o check if all the mandatory ports were remapped and + // we want to check if all the mandatory ports were remapped and // add default ones, if necessary auto subtree_model_it = subtree_models.find(subtree_ID); if(subtree_model_it != subtree_models.end()) @@ -913,9 +917,9 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(const std::string& tree_ID, // - if any of these has default value for(const auto& [port_name, port_info] : subtree_model_ports) { - auto it = remapping.find(port_name); + auto it = subtree_remapping.find(port_name); // don't override existing remapping - if(it == remapping.end() && !do_autoremap) + if(it == subtree_remapping.end() && !do_autoremap) { // remapping is not explicitly defined in the XML: use the model if(port_info.defaultValueString().empty()) @@ -927,13 +931,13 @@ void BT::XMLParser::PImpl::recursivelyCreateSubtree(const std::string& tree_ID, } else { - remapping.insert({ port_name, port_info.defaultValueString() }); + subtree_remapping.insert({ port_name, port_info.defaultValueString() }); } } } } - for(const auto& [attr_name, attr_value] : remapping) + for(const auto& [attr_name, attr_value] : subtree_remapping) { if(TreeNode::isBlackboardPointer(attr_value)) { diff --git a/tests/gtest_enums.cpp b/tests/gtest_enums.cpp index f19fa6a78..58f663959 100644 --- a/tests/gtest_enums.cpp +++ b/tests/gtest_enums.cpp @@ -124,3 +124,118 @@ TEST(Enums, SwitchNodeWithEnum) ASSERT_EQ(status, NodeStatus::SUCCESS); } + +enum BatteryStatus +{ + NO_FAULT = 0, + LOW_BATTERY = 1 +}; + +class PrintEnum : public BT::ConditionNode +{ +public: + explicit PrintEnum(const std::string& name, const BT::NodeConfig& config) + : ConditionNode(name, config) + {} + + ~PrintEnum() override = default; + + static BT::PortsList providedPorts() + { + return { + BT::InputPort("enum", "Name of the check"), + }; + } + +private: + BT::NodeStatus tick() override + { + auto enum_value = getInput("enum"); + if(!enum_value) + { + std::cout << "missing required input [enum]" << std::endl; + return BT::NodeStatus::FAILURE; + } + std::cout << "Enum value: " << (enum_value == NO_FAULT ? "NO_FAULT" : "LOW_BATTERY") + << std::endl; + return BT::NodeStatus::SUCCESS; + } +}; + +class IsHealthOk : public BT::ConditionNode +{ +public: + explicit IsHealthOk(const std::string& name, const BT::NodeConfig& config) + : BT::ConditionNode(name, config) + {} + + ~IsHealthOk() override = default; + + static BT::PortsList providedPorts() + { + return { BT::InputPort("check_name"), BT::InputPort("health") }; + } + +private: + BT::NodeStatus tick() override + { + auto health = getInput("health"); + if(!health) + { + std::cout << "missing required input [health]" << std::endl; + return BT::NodeStatus::FAILURE; + } + + if(health.value()) + { + return BT::NodeStatus::SUCCESS; + } + else + { + std::cerr << "IsHealthOk FAILED " << std::endl; + return BT::NodeStatus::FAILURE; + } + return BT::NodeStatus::SUCCESS; + } +}; + +TEST(Enums, SubtreeRemapping) +{ + const std::string xml_txt = R"( + + + +