AstraLog-HPC
Loading...
Searching...
No Matches
Rules.hpp
Go to the documentation of this file.
1
8 #pragma once
9 #include <cmath>
10 #include <iostream>
11 #include <string>
12 #include <unordered_map>
13 #include <functional>
14 #include <vector>
15 #include <variant>
16 #include "Strategy.hpp"
17
23 class SimpleRule : public RuleStrategy {
24 public:
29 explicit SimpleRule(const RuleConfig& cfg)
30 : sensor_id(cfg.sensor_id), op(cfg.op), threshold(cfg.value)
31 {
32 rule_id = cfg.rule_id;
34 }
35
40 std::string whoAmI() override {
41 return "SimpleRule";
42 }
43
50 int evaluate(const std::variant<double,std::string> &v) override {
51
52 // if it's a string, we assume it's an error case and return 0 (not triggered)
53 if(std::holds_alternative<std::string>(v))
54 return 0;
55
56 // if it's a double, we proceed with the normal evaluation
57 double value = std::get<double>(v);
58
59 bool triggered = compare(value, threshold, op);
60 return triggered ? 1 : 0;
61 }
62
63 private:
64 std::string sensor_id;
65 std::string op;
66 double threshold;
67 };
68
73 class StatefulRule : public RuleStrategy {
74 public:
79 explicit StatefulRule(const RuleConfig& cfg)
80 : sensor_id(cfg.sensor_id), op(cfg.op),
81 threshold(cfg.value), consecutive(cfg.consecutive_measurements)
82 {
83 rule_id = cfg.rule_id;
85 }
86
91 std::string whoAmI() override {
92 return "StatefulRule";
93 }
97 [[nodiscard]] int getConsecutive() const override { return current_consecutive; }
98
103 void setConsecutive(int value) override{ current_consecutive = value; }
104
111 int evaluate(const std::variant<double,std::string> &v) override {
112
113 // if it's a string, we assume it's an error case and return 0 (not triggered)
114 // A malformed/missing reading carries no information about the condition,
115 // so we leave the consecutive counter unchanged (the streak is preserved
116 // across the gap) and simply do not trigger on this tick.
117 if(std::holds_alternative<std::string>(v)) {
118 return 0;
119 }
120
121 // if it's a double, we proceed with the normal evaluation
122 double value = std::get<double>(v);
123 if (compare(value, threshold, op))
124 current_consecutive++; // condition met: keep counting
125 else
126 current_consecutive = 0; // condition broken: reset
127
128 bool triggered = current_consecutive >= consecutive;
129 return triggered ? 1 : 0;
130 }
131
132
133 private:
134 std::string sensor_id;
135 std::string op;
136 double threshold;
139 };
140
146 public:
151 explicit CorrelationRule(const RuleConfig& cfg)
152 : logic(cfg.logic), conditions(cfg.conditions)
153 {
154 rule_id = cfg.rule_id;
156 }
157
162 std::string whoAmI() override {
163 return "CorrelationRule";
164 }
165
172 int evaluate(const ResultMap& results) override {
173 bool triggered = false;
174 if (logic == "AND") {
175 triggered = true;
176 for (const auto& cond_id : conditions) {
177 auto it = results.find(cond_id);
178 if (it == results.end() || !it->second) {
179 triggered = false;
180 break;
181 }
182 }
183 } else if (logic == "OR") {
184 for (const auto& cond_id : conditions) {
185 auto it = results.find(cond_id);
186 if (it != results.end() && it->second) {
187 triggered = true;
188 break;
189 }
190 }
191 }
192 return triggered ? 1 : 0;
193 }
194
195 public:
196 [[nodiscard]] const std::vector<std::string>& getConditions() const { return conditions; }
197
198 private:
199 std::string logic;
200 std::vector<std::string> conditions;
201 };
202
208 public:
213 explicit StepDifferenceRule(const RuleConfig& cfg)
214 : sensor_id(cfg.sensor_id), op(cfg.op), threshold(cfg.value)
215 {
216 rule_id = cfg.rule_id;
218 }
219
224 std::string whoAmI() override {
225 return "StepDifferenceRule";
226 }
227
235 int evaluate(const std::variant<double,std::string> &v) override {
236
237 // if it's a string, we assume it's an error case and return 0 (not triggered)
238 if(std::holds_alternative<std::string>(v)) {
239 has_previous = false; // reset state on error
240 return 0;
241 }
242
243 // if it's a double, we proceed with the normal evaluation
244 double current_value = std::get<double>(v);
245 bool triggered = false;
246
247 if (has_previous) {
248 double diff = current_value - last_value; // signed: preserves < / >
249 triggered = compare(diff, threshold, op);
250 }
251
252 last_value = current_value;
253 has_previous = true;
254
255 return triggered ? 1 : 0;
256 }
260 double getLastValue() override { return last_value; }
261
266 void setLastValue(double value) override
267 { last_value = value;
268 has_previous = true; }
274 [[nodiscard]] bool hasPrevious() const override { return has_previous; }
275
276 private:
277 std::string sensor_id;
278 std::string op;
279 double threshold;
280 double last_value = 0.0;
281 bool has_previous = false;
282 };
Abstract class for the strategy pattern, defining the interface for different rule strategies.
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
A rule that evaluates a combination of other rules based on logical operators (AND/OR).
Definition Rules.hpp:145
const std::vector< std::string > & getConditions() const
Definition Rules.hpp:196
CorrelationRule(const RuleConfig &cfg)
Constructs a CorrelationRule from a configuration object.
Definition Rules.hpp:151
int evaluate(const ResultMap &results) override
Evaluates the rule based on the outcomes of its dependency conditions.
Definition Rules.hpp:172
std::string logic
The logical operator ("AND" or "OR").
Definition Rules.hpp:199
std::string whoAmI() override
Returns the rule type name.
Definition Rules.hpp:162
std::vector< std::string > conditions
The rule IDs that this rule depends on.
Definition Rules.hpp:200
Abstract base class that defines the interface for all rule evaluation strategies.
Definition Strategy.hpp:25
static bool compare(double left, double right, const std::string &op)
A static helper function to perform logical comparisons.
Definition Strategy.hpp:84
std::string rule_id
The unique identifier for this rule instance.
Definition Strategy.hpp:99
std::string rule_priority
The execution priority of this rule.
Definition Strategy.hpp:100
A straightforward rule that compares a single sensor measurement against a threshold.
Definition Rules.hpp:23
double threshold
The threshold value to compare against.
Definition Rules.hpp:66
std::string whoAmI() override
Returns the rule type name.
Definition Rules.hpp:40
std::string sensor_id
The ID of the sensor this rule applies to.
Definition Rules.hpp:64
int evaluate(const std::variant< double, std::string > &v) override
Evaluates the sensor value against the configured threshold.
Definition Rules.hpp:50
SimpleRule(const RuleConfig &cfg)
Constructs a SimpleRule from a configuration object.
Definition Rules.hpp:29
std::string op
The comparison operator (e.g., ">", "<", "==").
Definition Rules.hpp:65
A rule that requires a condition to be met for a specified number of consecutive measurements.
Definition Rules.hpp:73
std::string whoAmI() override
Returns the rule type name.
Definition Rules.hpp:91
double threshold
The threshold value to compare against.
Definition Rules.hpp:136
int getConsecutive() const override
Gets the number of consecutive measurements required for the rule to trigger.
Definition Rules.hpp:97
std::string op
The comparison operator.
Definition Rules.hpp:135
int current_consecutive
The current sequence length of consecutive matches.
Definition Rules.hpp:138
std::string sensor_id
The ID of the sensor this rule applies to.
Definition Rules.hpp:134
int evaluate(const std::variant< double, std::string > &v) override
Evaluates the sensor value and updates the consecutive match counter.
Definition Rules.hpp:111
void setConsecutive(int value) override
Sets the number of consecutive measurements required for the rule to trigger.
Definition Rules.hpp:103
int consecutive
The target number of consecutive condition matches required.
Definition Rules.hpp:137
StatefulRule(const RuleConfig &cfg)
Constructs a StatefulRule from a configuration object.
Definition Rules.hpp:79
A rule that triggers based on the difference between the current and the previous measurement.
Definition Rules.hpp:207
double last_value
The stored value from the previous measurement.
Definition Rules.hpp:280
bool has_previous
Flag indicating if a valid previous measurement exists.
Definition Rules.hpp:281
double getLastValue() override
Gets the last value stored by the rule, which is used for the step difference calculation.
Definition Rules.hpp:260
std::string sensor_id
The ID of the sensor this rule applies to.
Definition Rules.hpp:277
std::string whoAmI() override
Returns the rule type name.
Definition Rules.hpp:224
bool hasPrevious() const override
Checks if a valid previous measurement exists for the step difference calculation.
Definition Rules.hpp:274
StepDifferenceRule(const RuleConfig &cfg)
Constructs a StepDifferenceRule from a configuration object.
Definition Rules.hpp:213
int evaluate(const std::variant< double, std::string > &v) override
Evaluates the difference between the current sensor value and the previous one.
Definition Rules.hpp:235
std::string op
The comparison operator.
Definition Rules.hpp:278
double threshold
The threshold step difference value to compare against.
Definition Rules.hpp:279
void setLastValue(double value) override
Sets the last value stored by the rule, which is used for the step difference calculation.
Definition Rules.hpp:266
A configuration structure used to instantiate any type of RuleStrategy.
Definition Strategy.hpp:107
std::string priority
The execution priority level (default: "LOW").
Definition Strategy.hpp:112
std::string rule_id
The unique identifier for the rule.
Definition Strategy.hpp:111