AstraLog-HPC
Loading...
Searching...
No Matches
RulesFactory.hpp
Go to the documentation of this file.
1#pragma once
2#include "GenericFactory.hpp"
4#include "RuleOrdering.hpp"
5#include <functional>
6#include <memory>
7#include <mutex>
8#include <string>
9#include <vector>
10#include <unordered_map>
11#include <iostream>
12
13using Identifier = std::string;
14using Builder = std::function<std::unique_ptr<RuleStrategy>(const RuleConfig&)>;
16
24std::vector<RuleConfig> loadRules(const std::string& file_path);
25
34public:
39 explicit RulesFactory(const std::string& rules_path) {
41 auto& factory = MyObjectFactory::Instance();
42 auto rules = loadRules(rules_path);
43 auto validRules = decltype(rules){};
44
45 for (const auto& cfg : rules) {
46 try {
47 ruleMap[cfg.rule_id] = factory.create(cfg.type, cfg);
48 validRules.push_back(cfg);
49 } catch (const std::exception& e) {
50 std::cerr << "Error creating rule " << cfg.rule_id
51 << ": " << e.what() << std::endl;
52 }
53 }
54 pipeline = buildPipeline(ruleMap, validRules);
55 }
56
62
67 const std::unordered_map<std::string, std::shared_ptr<RuleStrategy>>&
68 getRuleMap() const { return ruleMap; }
69
70private:
71 static void loadFactory() {
72 static std::once_flag flag;
73 std::call_once(flag, []() {
74 auto& factory = MyObjectFactory::Instance();
75 factory.add("simple", [](const RuleConfig& cfg) { return std::make_unique<SimpleRule>(cfg); });
76 factory.add("correlation", [](const RuleConfig& cfg) { return std::make_unique<CorrelationRule>(cfg); });
77 factory.add("stateful", [](const RuleConfig& cfg) { return std::make_unique<StatefulRule>(cfg); });
78 factory.add("step_difference", [](const RuleConfig& cfg) { return std::make_unique<StepDifferenceRule>(cfg); });
79 });
80 }
81
82 std::unordered_map<std::string, std::shared_ptr<RuleStrategy>> ruleMap;
84};
EvaluationPipeline buildPipeline(const std::unordered_map< std::string, std::shared_ptr< RuleStrategy > > &ruleMap, const std::vector< RuleConfig > &configs)
Builds an evaluation pipeline by organizing and sorting rules.
Definition RuleOrdering.cpp:18
Defines the structure of the evaluation pipeline and the function to build it based on the provided r...
std::string Identifier
Definition RulesFactory.hpp:13
std::vector< RuleConfig > loadRules(const std::string &file_path)
Loads rule configurations from a JSON file.
Definition RulesFactory.cpp:11
std::function< std::unique_ptr< RuleStrategy >(const RuleConfig &)> Builder
Definition RulesFactory.hpp:14
Contains the implementation of the different rules.
A generic object factory.
Definition GenericFactory.hpp:42
static Factory & Instance()
Definition GenericFactory.hpp:133
Instantiates rule objects from a JSON config and assembles them into an EvaluationPipeline.
Definition RulesFactory.hpp:33
EvaluationPipeline pipeline
Definition RulesFactory.hpp:83
static void loadFactory()
Definition RulesFactory.hpp:71
const std::unordered_map< std::string, std::shared_ptr< RuleStrategy > > & getRuleMap() const
Returns the map of rule ID to its instantiated rule strategy.
Definition RulesFactory.hpp:68
std::unordered_map< std::string, std::shared_ptr< RuleStrategy > > ruleMap
Definition RulesFactory.hpp:82
EvaluationPipeline & getPipeline()
Returns the evaluation pipeline (primary rules per sensor plus correlation rules).
Definition RulesFactory.hpp:61
RulesFactory(const std::string &rules_path)
Builds the rule map and evaluation pipeline from a rules JSON file.
Definition RulesFactory.hpp:39
Holds the categorized and organized rules ready for execution.
Definition RuleOrdering.hpp:17
A configuration structure used to instantiate any type of RuleStrategy.
Definition Strategy.hpp:107