Iguana 0.0.0
Implementation Guardian of Analysis Algorithms
Loading...
Searching...
No Matches
Algorithm.h
1#pragma once
2
3#include <mutex>
4#include <optional>
5#include <set>
6
7#include <hipo4/bank.h>
8
9
11#include "iguana/services/YAMLReader.h"
12#include <iguana/services/GlobalParam.h>
13
14namespace iguana {
15
17 /* NOTE: if you modify this, you also must modify:
18 * - [ ] `PrintOptionValue`
19 * - [ ] Template specializations in this class
20 * - [ ] Template specializations in `YAMLReader` or `ConfigFileReader`, and `ConcurrentParam`
21 * - [ ] Add new tests, if you added new types
22 */
23 using option_t = std::variant<
24 int,
25 double,
26 std::string,
27 std::vector<int>,
28 std::vector<double>,
29 std::vector<std::string>>;
30
39 class Algorithm : public Object
40 {
41
42 public:
43
45 Algorithm(std::string_view name)
46 : Object(name)
47 , m_rows_only(false)
51 {}
52 virtual ~Algorithm() {}
53
59 virtual void Start(hipo::banklist& banks) = 0;
60
64 void Start();
65
68 virtual void Run(hipo::banklist& banks) const = 0;
69
71 virtual void Stop() = 0;
72
78 template <typename OPTION_TYPE>
79 OPTION_TYPE SetOption(std::string const& key, const OPTION_TYPE val)
80 {
81 // FIXME: this template is not specialized, to be friendlier to python `cppyy` bindings
82 if(key == "log") {
83 if constexpr(std::disjunction<
84 std::is_same<OPTION_TYPE, std::string>,
85 std::is_same<OPTION_TYPE, char const*>,
86 std::is_same<OPTION_TYPE, Logger::Level>>::value)
87 m_log->SetLevel(val);
88 else
89 m_log->Error("Option '{}' must be a string or a Logger::Level", key);
90 }
91 m_option_cache[key] = val;
92 return val;
93 }
94
99 template <typename OPTION_TYPE>
100 OPTION_TYPE GetOptionScalar(std::string const& key, YAMLReader::node_path_t node_path = {}) const;
101
106 template <typename OPTION_TYPE>
107 std::vector<OPTION_TYPE> GetOptionVector(std::string const& key, YAMLReader::node_path_t node_path = {}) const;
108
113 template <typename OPTION_TYPE>
114 std::set<OPTION_TYPE> GetOptionSet(std::string const& key, YAMLReader::node_path_t node_path = {}) const;
115
118 void SetName(std::string_view name);
119
122 std::unique_ptr<YAMLReader> const& GetConfig() const;
123
126 void SetConfig(std::unique_ptr<YAMLReader>&& yaml_config);
127
131 void SetConfigFile(std::string const& name);
132
136 void SetConfigDirectory(std::string const& name);
137
138 protected: // methods
139
142
148 hipo::bank& GetBank(hipo::banklist& banks, hipo::banklist::size_type const idx, std::string const& expected_bank_name = "") const noexcept(false);
149
154 hipo::banklist::size_type GetBankIndex(hipo::banklist& banks, std::string const& bank_name) const noexcept(false);
155
164 hipo::schema CreateBank(
165 hipo::banklist& banks,
166 hipo::banklist::size_type& bank_idx,
167 std::string const& bank_name,
168 std::vector<std::string> schema_def,
169 int group_id, // FIXME: generalize group_id and item_id setting
170 int item_id) const noexcept(false);
171
176 void ShowBanks(hipo::banklist& banks, std::string_view message = "", Logger::Level const level = Logger::trace) const;
177
182 void ShowBank(hipo::bank& bank, std::string_view message = "", Logger::Level const level = Logger::trace) const;
183
187 template <typename OPTION_TYPE>
188 std::optional<OPTION_TYPE> GetCachedOption(std::string const& key) const;
189
190 private: // methods
191
195 void CompleteOptionNodePath(std::string const& key, YAMLReader::node_path_t& node_path) const;
196
197 // PrintOptionValue: overloaded for different value types
198 void PrintOptionValue(std::string const& key, int const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
199 void PrintOptionValue(std::string const& key, double const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
200 void PrintOptionValue(std::string const& key, std::string const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
201 void PrintOptionValue(std::string const& key, std::vector<int> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
202 void PrintOptionValue(std::string const& key, std::vector<double> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
203 void PrintOptionValue(std::string const& key, std::vector<std::string> const& val, Logger::Level const level = Logger::debug, std::string_view prefix = "OPTION") const;
204
205 protected: // members
206
208 std::string m_class_name;
209
212
215
219
222 std::string o_user_config_dir;
223
225 mutable std::mutex m_mutex;
226
227 private: // members
228
230 std::unique_ptr<YAMLReader> m_yaml_config;
231
233 std::unordered_map<std::string, option_t> m_option_cache;
234
235 };
236
238
240 using algo_t = std::unique_ptr<Algorithm>;
241
244 {
245
246 public:
247
249 using algo_creator_t = std::function<algo_t()>;
250
251 AlgorithmFactory() = delete;
252
258 static bool Register(std::string const& name, algo_creator_t creator, std::vector<std::string> const new_banks = {}) noexcept;
259
263 static algo_t Create(std::string const& name) noexcept(false);
264
268 static std::optional<std::vector<std::string>> QueryNewBank(std::string const& bank_name) noexcept;
269
270 private:
271
273 static std::unordered_map<std::string, algo_creator_t> s_creators;
274
276 static std::unordered_map<std::string, std::vector<std::string>> s_created_banks;
277 };
278}
Preprocessor macros to generate standardized algorithm boilerplate code.
Factory to create an algorithm.
Definition Algorithm.h:244
static algo_t Create(std::string const &name) noexcept(false)
std::function< algo_t()> algo_creator_t
Algorithm creator function type.
Definition Algorithm.h:249
static bool Register(std::string const &name, algo_creator_t creator, std::vector< std::string > const new_banks={}) noexcept
static std::optional< std::vector< std::string > > QueryNewBank(std::string const &bank_name) noexcept
Base class for all algorithms to inherit from.
Definition Algorithm.h:40
void SetName(std::string_view name)
void SetConfigDirectory(std::string const &name)
std::optional< OPTION_TYPE > GetCachedOption(std::string const &key) const
virtual void Start(hipo::banklist &banks)=0
Initialize this algorithm before any events are processed, with the intent to process banks
std::set< OPTION_TYPE > GetOptionSet(std::string const &key, YAMLReader::node_path_t node_path={}) const
std::unique_ptr< YAMLReader > const & GetConfig() const
void SetConfig(std::unique_ptr< YAMLReader > &&yaml_config)
std::string m_class_name
Class name of this algorithm.
Definition Algorithm.h:208
OPTION_TYPE GetOptionScalar(std::string const &key, YAMLReader::node_path_t node_path={}) const
virtual void Run(hipo::banklist &banks) const =0
Run this algorithm for an event.
std::vector< OPTION_TYPE > GetOptionVector(std::string const &key, YAMLReader::node_path_t node_path={}) const
void ShowBank(hipo::bank &bank, std::string_view message="", Logger::Level const level=Logger::trace) const
hipo::bank & GetBank(hipo::banklist &banks, hipo::banklist::size_type const idx, std::string const &expected_bank_name="") const noexcept(false)
std::mutex m_mutex
A mutex for this algorithm.
Definition Algorithm.h:225
void Start()
Initialize this algorithm before any events are processed, with the intent to process bank rows rathe...
void SetConfigFile(std::string const &name)
void ParseYAMLConfig()
Parse YAML configuration files. Sets m_yaml_config.
std::string o_user_config_dir
Definition Algorithm.h:222
virtual void Stop()=0
Finalize this algorithm after all events are processed.
OPTION_TYPE SetOption(std::string const &key, const OPTION_TYPE val)
Definition Algorithm.h:79
bool m_rows_only
If true, algorithm can only operate on bank rows; Algorithm::GetBank, and therefore Algorithm::Run,...
Definition Algorithm.h:211
hipo::schema CreateBank(hipo::banklist &banks, hipo::banklist::size_type &bank_idx, std::string const &bank_name, std::vector< std::string > schema_def, int group_id, int item_id) const noexcept(false)
hipo::banklist::size_type GetBankIndex(hipo::banklist &banks, std::string const &bank_name) const noexcept(false)
std::string o_user_config_file
Definition Algorithm.h:218
std::string m_default_config_file
Default configuration file name.
Definition Algorithm.h:214
void ShowBanks(hipo::banklist &banks, std::string_view message="", Logger::Level const level=Logger::trace) const
Algorithm(std::string_view name)
Definition Algorithm.h:45
Simple logger service.
Definition Logger.h:31
A named object with a Logger instance.
Definition Object.h:12
std::unique_ptr< Logger > m_log
Logger instance for this object
Definition Object.h:52
A YAMLReader based on yaml-cpp.
Definition YAMLReader.h:15
std::deque< node_id_t > node_path_t
Representation of a path of YAML::Nodes in a YAML::Node tree, e.g., in a YAML file.
Definition YAMLReader.h:28
General, top-level namespace for algorithms and infrastructure. For algorithms and bindings,...
Definition Algorithm.h:14
std::variant< int, double, std::string, std::vector< int >, std::vector< double >, std::vector< std::string > > option_t
Option value variant type.
Definition Algorithm.h:23
std::unique_ptr< Algorithm > algo_t
Algorithm pointer type.
Definition Algorithm.h:240