-
Notifications
You must be signed in to change notification settings - Fork 8
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
Introduce a common TaskRemapper with wildcard support #55
Open
luca-della-vedova
wants to merge
6
commits into
main
Choose a base branch
from
luca/remap_wildcard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6eafff4
Introduce a common TaskRemapper with wildcard support
luca-della-vedova a8c5bf4
Add task remapper
luca-della-vedova a7297a5
Minor cleanup in test code
luca-della-vedova c61743e
Copyright
luca-della-vedova b881d1e
Change API to std::optional
luca-della-vedova 8c62c69
Add test case
luca-della-vedova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (C) 2025 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#ifndef NEXUS_COMMON__TASK_REMAPPER_HPP | ||
#define NEXUS_COMMON__TASK_REMAPPER_HPP | ||
|
||
#include "nexus_common_export.hpp" | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <optional> | ||
#include <string> | ||
|
||
namespace nexus::common { | ||
|
||
/** | ||
* Provides task remapping capability | ||
*/ | ||
class NEXUS_COMMON_EXPORT TaskRemapper | ||
{ | ||
public: | ||
/* | ||
* Initialize the remapper with the value of a ROS parameter containing a YAML | ||
*/ | ||
TaskRemapper(const std::string& param); | ||
|
||
/* | ||
* Remaps, if necessary, the input task | ||
* Returns a value if the task was remapped, std::nullopt otherwise | ||
*/ | ||
std::optional<std::string> remap(const std::string& task) const; | ||
|
||
private: | ||
// If present, match every incoming task to the target task | ||
std::optional<std::string> _wildcard_match; | ||
std::unordered_map<std::string, std::string> _task_remaps; | ||
}; | ||
|
||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) 2025 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#include "task_remapper.hpp" | ||
|
||
#include <yaml-cpp/yaml.h> | ||
|
||
namespace nexus::common { | ||
|
||
TaskRemapper::TaskRemapper(const std::string& param) | ||
{ | ||
const auto remaps = YAML::Load(param); | ||
for (const auto& n : remaps) | ||
{ | ||
const auto task_type = n.first.as<std::string>(); | ||
const auto& mappings = n.second; | ||
for (const auto& m : mappings) | ||
{ | ||
auto mapping = m.as<std::string>(); | ||
if (mapping == "*") | ||
{ | ||
this->_wildcard_match = task_type; | ||
this->_task_remaps.clear(); | ||
return; | ||
} | ||
// TODO(luca) check for duplicates, logging if found | ||
this->_task_remaps.emplace(mapping, task_type); | ||
} | ||
} | ||
} | ||
|
||
std::optional<std::string> TaskRemapper::remap(const std::string& task) const | ||
{ | ||
if (this->_wildcard_match.has_value()) | ||
{ | ||
return this->_wildcard_match.value(); | ||
} | ||
const auto it = this->_task_remaps.find(task); | ||
if (it != this->_task_remaps.end()) | ||
{ | ||
return it->second; | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (C) 2025 Open Source Robotics Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
#define CATCH_CONFIG_MAIN | ||
#include <rmf_utils/catch.hpp> | ||
|
||
#include "task_remapper.hpp" | ||
|
||
namespace nexus::common::test { | ||
|
||
TEST_CASE("task_remapping") { | ||
std::string param = | ||
R"( | ||
pick_and_place: [pick, place] | ||
)"; | ||
auto remapper = TaskRemapper(param); | ||
CHECK(remapper.remap("pick") == "pick_and_place"); | ||
CHECK(remapper.remap("place") == "pick_and_place"); | ||
CHECK(remapper.remap("other") == std::nullopt); | ||
} | ||
|
||
TEST_CASE("task_remapping_with_wildcard") { | ||
std::string param = | ||
R"( | ||
pick_and_place: [pick, place] | ||
main : ["*"] | ||
)"; | ||
auto remapper = TaskRemapper(param); | ||
CHECK(remapper.remap("pick") == "main"); | ||
CHECK(remapper.remap("place") == "main"); | ||
CHECK(remapper.remap("other") == "main"); | ||
} | ||
|
||
luca-della-vedova marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TEST_CASE("task_remapping_with_normal_and_wildcard") { | ||
std::string param = | ||
R"( | ||
pick_and_place: [pick, "*"] | ||
)"; | ||
auto remapper = TaskRemapper(param); | ||
CHECK(remapper.remap("pick") == "pick_and_place"); | ||
CHECK(remapper.remap("place") == "pick_and_place"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This API is error prone especially if users do not pass a string that can be deserialized into a YAML. ie
YAML::Load()
can throw an exception in the implementation.I suggest we accept a concrete type here instead like an
std::multimap
where the key is the remapped process and the values for each key are the process steps in the work order.This way if the param definition changes in the orchestrators, this API would not have to change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR doesn't introduce any new exception since all uses of this class were doing
YAML::Load
anyway, so they would have thrown on an invalid YAML.The "proper" way I considered going for was either passing a YAML node as an input (so we won't throw) or keep the string and make the constructor fallible (i.e. a
make
function, returnnullptr
if the YAML failed deserializing). I didn't do it only because I saw that the codebase doesn't seem to use this paradigm but I'm happy to do it.I feel that if we add a
multimap
and users of the API need to manually parse the YAML, handle exceptions, add its content to amultimap
, then we read themultimap
, iterate over it, check for wildcards, populate internal data structures accordingly, it becomes much more complex and clunky. Instead of being a refactor to reduce duplicated code we actually end up with a fair bit more code and complexity.