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

feat: Allow multiple aliases for one whiteboard entry in Examples #4035

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Sequencer {
std::vector<SequenceElementWithFpeResult> m_sequenceElements;
std::unique_ptr<const Acts::Logger> m_logger;

std::unordered_map<std::string, std::string> m_whiteboardObjectAliases;
std::unordered_multimap<std::string, std::string> m_whiteboardObjectAliases;

std::unordered_map<std::string, const DataHandleBase *> m_whiteBoardState;

Expand Down
18 changes: 11 additions & 7 deletions Examples/Framework/include/ActsExamples/Framework/WhiteBoard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <stdexcept>
#include <string>
#include <string_view>
#include <type_traits>
#include <typeinfo>
#include <unordered_map>
#include <utility>
Expand All @@ -34,9 +33,10 @@ namespace ActsExamples {
/// Its lifetime is bound to the lifetime of the white board.
class WhiteBoard {
public:
WhiteBoard(std::unique_ptr<const Acts::Logger> logger =
Acts::getDefaultLogger("WhiteBoard", Acts::Logging::INFO),
std::unordered_map<std::string, std::string> objectAliases = {});
WhiteBoard(
std::unique_ptr<const Acts::Logger> logger =
Acts::getDefaultLogger("WhiteBoard", Acts::Logging::INFO),
std::unordered_multimap<std::string, std::string> objectAliases = {});

// A WhiteBoard holds unique elements and can not be copied
WhiteBoard(const WhiteBoard& other) = delete;
Expand Down Expand Up @@ -82,7 +82,7 @@ class WhiteBoard {

std::unique_ptr<const Acts::Logger> m_logger;
std::unordered_map<std::string, std::shared_ptr<IHolder>> m_store;
std::unordered_map<std::string, std::string> m_objectAliases;
std::unordered_multimap<std::string, std::string> m_objectAliases;

const Acts::Logger& logger() const { return *m_logger; }

Expand All @@ -100,7 +100,7 @@ class WhiteBoard {

inline ActsExamples::WhiteBoard::WhiteBoard(
std::unique_ptr<const Acts::Logger> logger,
std::unordered_map<std::string, std::string> objectAliases)
std::unordered_multimap<std::string, std::string> objectAliases)
: m_logger(std::move(logger)), m_objectAliases(std::move(objectAliases)) {}

template <typename T>
Expand All @@ -111,10 +111,14 @@ inline void ActsExamples::WhiteBoard::add(const std::string& name, T&& object) {
if (m_store.contains(name)) {
throw std::invalid_argument("Object '" + name + "' already exists");
}

auto holder = std::make_shared<HolderT<T>>(std::forward<T>(object));
m_store.emplace(name, holder);
ACTS_VERBOSE("Added object '" << name << "' of type " << typeid(T).name());
if (auto it = m_objectAliases.find(name); it != m_objectAliases.end()) {

// deal with aliases
auto range = m_objectAliases.equal_range(name);
for (auto it = range.first; it != range.second; ++it) {
m_store[it->second] = holder;
ACTS_VERBOSE("Added alias object '" << it->second << "'");
}
Expand Down
29 changes: 16 additions & 13 deletions Examples/Framework/src/Framework/Sequencer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "ActsExamples/Framework/Sequencer.hpp"

#include "Acts/Plugins/FpeMonitoring/FpeMonitor.hpp"
#include "Acts/Utilities/Helpers.hpp"
#include "Acts/Utilities/Logger.hpp"
#include "ActsExamples/Framework/AlgorithmContext.hpp"
#include "ActsExamples/Framework/DataHandle.hpp"
Expand All @@ -26,9 +25,7 @@
#include <atomic>
#include <cctype>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <functional>
#include <iterator>
Expand Down Expand Up @@ -256,20 +253,26 @@ void Sequencer::addElement(const std::shared_ptr<SequenceElement>& element) {

void Sequencer::addWhiteboardAlias(const std::string& aliasName,
const std::string& objectName) {
auto [it, success] =
m_whiteboardObjectAliases.insert({objectName, aliasName});
if (!success) {
ACTS_INFO("Key '" << objectName << "' aliased to '" << aliasName
<< "' already set");
return;
const auto range = m_whiteboardObjectAliases.equal_range(objectName);
for (auto it = range.first; it != range.second; ++it) {
const auto& [key, value] = *it;
if (value == aliasName) {
ACTS_INFO("Key '" << objectName << "' aliased to '" << aliasName
<< "' already set");
return;
}
}

ACTS_INFO("Key '" << objectName << "' aliased to '" << aliasName << "'");
m_whiteboardObjectAliases.insert({objectName, aliasName});

if (auto oit = m_whiteBoardState.find(objectName);
oit != m_whiteBoardState.end()) {
m_whiteBoardState[aliasName] = oit->second;
auto oit = m_whiteBoardState.find(objectName);
if (oit == m_whiteBoardState.end()) {
ACTS_ERROR("Key '" << objectName << "' does not exist");
return;
}

ACTS_INFO("Key '" << objectName << "' aliased to '" << aliasName << "'");
m_whiteBoardState[aliasName] = oit->second;
}

std::vector<std::string> Sequencer::listAlgorithmNames() const {
Expand Down
Loading