AstraLog-HPC
Loading...
Searching...
No Matches
Strategy.hpp
Go to the documentation of this file.
1
7#pragma once
8#include <functional>
9#include <string>
10#include <unordered_map>
11#include <vector>
12#include <variant>
13
18using ResultMap = std::unordered_map<std::string, bool>;
19
25{
26public:
31 virtual std::string whoAmI() = 0;
32
33 [[nodiscard]] virtual int getConsecutive() const { return 0; }
34 virtual double getLastValue() { return 0; }
35 virtual void setConsecutive(int /*value*/) {}
36 virtual void setLastValue(double /*value*/) {}
37 [[nodiscard]] virtual bool hasPrevious() const { return false; }
38
39
40
47 virtual int evaluate([[maybe_unused]] const std::variant<double,std::string>& v) { return 0; }
48
55 virtual int evaluate([[maybe_unused]] const ResultMap& results) { return 0; }
56
60 virtual ~RuleStrategy() = default;
61
66 [[nodiscard]] const std::string& id() const { return rule_id; }
67
72 [[nodiscard]] const std::string& priority() const { return rule_priority; }
73
74 protected:
84 static bool compare(double left, double right, const std::string& op) {
85 static const std::unordered_map<std::string, std::function<bool(double, double)>> ops = {
86 { "<", std::less<>() },
87 { ">", std::greater<>() },
88 { "<=", std::less_equal<>() },
89 { ">=", std::greater_equal<>() },
90 { "==", std::equal_to<>() }
91 };
92
93 auto it = ops.find(op);
94 if (it != ops.end()) {
95 return it->second(left, right);
96 }
97 return false;
98 }
99 std::string rule_id;
100 std::string rule_priority;
101};
102
108 //mandatory fields for all rules
109 std::string type;
110 std::string sensor_id;
111 std::string rule_id;
112 std::string priority = "LOW";
113 double value = 0.0;
114
116 std::vector<std::string> conditions;
117 std::string op=">";
118 std::string logic = "AND";
119
120};
std::unordered_map< std::string, bool > ResultMap
Alias for a map containing the boolean evaluation results of rules, keyed by rule ID.
Definition Strategy.hpp:18
Abstract base class that defines the interface for all rule evaluation strategies.
Definition Strategy.hpp:25
virtual int getConsecutive() const
Definition Strategy.hpp:33
virtual bool hasPrevious() const
Definition Strategy.hpp:37
virtual int evaluate(const ResultMap &results)
Evaluates correlation rules based on the results of other primary rules.
Definition Strategy.hpp:55
virtual int evaluate(const std::variant< double, std::string > &v)
Evaluates the rule based on a sensor measurement or error state.
Definition Strategy.hpp:47
const std::string & priority() const
Gets the priority level of the rule.
Definition Strategy.hpp:72
virtual std::string whoAmI()=0
Returns the rule type name (e.g. "SimpleRule").
const std::string & id() const
Gets the unique identifier of the rule.
Definition Strategy.hpp:66
virtual void setConsecutive(int)
Definition Strategy.hpp:35
static bool compare(double left, double right, const std::string &op)
A static helper function to perform logical comparisons.
Definition Strategy.hpp:84
virtual void setLastValue(double)
Definition Strategy.hpp:36
virtual double getLastValue()
Definition Strategy.hpp:34
std::string rule_id
The unique identifier for this rule instance.
Definition Strategy.hpp:99
virtual ~RuleStrategy()=default
Virtual destructor for safe polymorphic deletion.
std::string rule_priority
The execution priority of this rule.
Definition Strategy.hpp:100
A configuration structure used to instantiate any type of RuleStrategy.
Definition Strategy.hpp:107
double value
The numerical threshold value used for comparisons.
Definition Strategy.hpp:113
std::string sensor_id
The ID of the sensor this rule applies to.
Definition Strategy.hpp:110
std::string op
The comparison operator used for standard rules (default: ">").
Definition Strategy.hpp:117
std::string priority
The execution priority level (default: "LOW").
Definition Strategy.hpp:112
std::string logic
The logical operator used for correlation rules (default: "AND").
Definition Strategy.hpp:118
std::string type
The type of the rule (e.g., "simple", "stateful", "correlation").
Definition Strategy.hpp:109
std::string rule_id
The unique identifier for the rule.
Definition Strategy.hpp:111
int consecutive_measurements
The required consecutive steps for stateful rules to trigger.
Definition Strategy.hpp:115
std::vector< std::string > conditions
A list of dependent rule IDs for correlation rules.
Definition Strategy.hpp:116