Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support namespace and node name wildcards #1280

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion rclcpp/src/rclcpp/node_interfaces/node_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ NodeParameters::NodeParameters(

// Enforce wildcard matching precedence
// TODO(cottsay) implement further wildcard matching
rpaaron marked this conversation as resolved.
Show resolved Hide resolved
const std::vector<std::string> node_matching_names{"/**", combined_name_};
auto ns = std::string(node_base->get_namespace());
const std::vector<std::string> node_matching_names{
"/**",
"/*/" + std::string(node_base->get_name()),
(ns == "/" ? "" : ns) + "/*",
combined_name_};

for (const auto & node_name : node_matching_names) {
if (initial_map.count(node_name) > 0) {
// Combine parameter yaml files, overwriting values in older ones
Expand Down
55 changes: 55 additions & 0 deletions rclcpp/test/rclcpp/node_interfaces/test_node_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "../../mocking_utils/patch.hpp"
#include "../../utils/rclcpp_gtest_macros.hpp"

#include "rcpputils/filesystem_helper.hpp"

class TestNodeParameters : public ::testing::Test
{
public:
Expand All @@ -47,6 +49,7 @@ class TestNodeParameters : public ::testing::Test
dynamic_cast<rclcpp::node_interfaces::NodeParameters *>(
node->get_node_parameters_interface().get());
ASSERT_NE(nullptr, node_parameters);
test_resources_path /= "test_node_parameters";
}

void TearDown()
Expand All @@ -57,6 +60,8 @@ class TestNodeParameters : public ::testing::Test
protected:
std::shared_ptr<rclcpp::Node> node;
rclcpp::node_interfaces::NodeParameters * node_parameters;

rcpputils::fs::path test_resources_path{TEST_RESOURCES_DIRECTORY};
};

TEST_F(TestNodeParameters, construct_destruct_rcl_errors) {
Expand Down Expand Up @@ -199,3 +204,53 @@ TEST_F(TestNodeParameters, add_remove_parameters_callback) {
node_parameters->remove_on_set_parameters_callback(handle.get()),
std::runtime_error("Callback doesn't exist"));
}

TEST_F(TestNodeParameters, wildcard_with_namespace)
{
rclcpp::NodeOptions opts;
opts.arguments(
{
"--ros-args",
"--params-file", (test_resources_path / "wildcards.yaml").string()
});

std::shared_ptr<rclcpp::Node> node = std::make_shared<rclcpp::Node>("node", "ns", opts);

auto * node_parameters =
dynamic_cast<rclcpp::node_interfaces::NodeParameters *>(
node->get_node_parameters_interface().get());
ASSERT_NE(nullptr, node_parameters);

const auto & parameter_overrides = node_parameters->get_parameter_overrides();
EXPECT_EQ(4u, parameter_overrides.size());
EXPECT_EQ(parameter_overrides.at("full_wild").get<std::string>(), "full_wild");
EXPECT_EQ(parameter_overrides.at("namespace_wild").get<std::string>(), "namespace_wild");
EXPECT_EQ(parameter_overrides.at("node_wild_in_ns").get<std::string>(), "node_wild_in_ns");
EXPECT_EQ(parameter_overrides.at("explicit_in_ns").get<std::string>(), "explicit_in_ns");
EXPECT_EQ(parameter_overrides.count("should_not_appear"), 0u);
}

TEST_F(TestNodeParameters, wildcard_no_namespace)
{
rclcpp::NodeOptions opts;
opts.arguments(
{
"--ros-args",
"--params-file", (test_resources_path / "wildcards.yaml").string()
});

std::shared_ptr<rclcpp::Node> node = std::make_shared<rclcpp::Node>("node", opts);

auto * node_parameters =
dynamic_cast<rclcpp::node_interfaces::NodeParameters *>(
node->get_node_parameters_interface().get());
ASSERT_NE(nullptr, node_parameters);

const auto & parameter_overrides = node_parameters->get_parameter_overrides();
EXPECT_EQ(4u, parameter_overrides.size());
EXPECT_EQ(parameter_overrides.at("full_wild").get<std::string>(), "full_wild");
EXPECT_EQ(parameter_overrides.at("namespace_wild").get<std::string>(), "namespace_wild");
EXPECT_EQ(parameter_overrides.at("node_wild_no_ns").get<std::string>(), "node_wild_no_ns");
EXPECT_EQ(parameter_overrides.at("explicit_no_ns").get<std::string>(), "explicit_no_ns");
EXPECT_EQ(parameter_overrides.count("should_not_appear"), 0u);
}
36 changes: 36 additions & 0 deletions rclcpp/test/resources/test_node_parameters/wildcards.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**:
ros__parameters:
full_wild: "full_wild"

/*:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have another test case for matching namespaces with multiple tokens. Following the design doc:

  • /* should match exactly one token (e.g. /foo or /bar, but not /foo/bar or /)
  • /** should match zero or more tokens (e.g. /foo or /bar or /foo/bar or /)

node:
ros__parameters:
namespace_wild: "namespace_wild"

ns:
"*":
Copy link
Collaborator

@iuhilnehc-ynos iuhilnehc-ynos Aug 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if * without double quotes in YAML is invalid, I still think the node wildcards without (") here should be supported, just like the namespace, such as (/*).

ns:
  /*:           # "*" can be also supported, but I think users like this style.
    ros__parameters:
      node_wild_in_ns: "node_wild_in_ns" 

It should be discussed with the maintainer. rcl_yaml_param_parser should be updated if the above suggestion is accepted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Supporting both /* and "*" makes sense to me 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take back my previous comment. Name tokens separated by a YAML colon (:) should be treated as if they are separated with a slash (/). Therefore, the example:

ns:
  /*:
    ros__parameters:
      ...

Should resolve to the name /ns//*, which is invalid. The correct way to write it would be:

/ns/*:
  ros__parameters:
    ...

ros__parameters:
node_wild_in_ns: "node_wild_in_ns"

ns:
node:
ros__parameters:
explicit_in_ns: "explicit_in_ns"

"*":
ros__parameters:
node_wild_no_ns: "node_wild_no_ns"

node:
ros__parameters:
explicit_no_ns: "explicit_no_ns"

ns:
nodeX:
ros__parameters:
should_not_appear: "incorrect_node_name"

nsX:
node:
ros__parameters:
should_not_appear: "incorrect_namespace"