12 #include <unordered_map>
50 int evaluate(
const std::variant<double,std::string> &v)
override {
53 if(std::holds_alternative<std::string>(v))
57 double value = std::get<double>(v);
60 return triggered ? 1 : 0;
92 return "StatefulRule";
111 int evaluate(
const std::variant<double,std::string> &v)
override {
117 if(std::holds_alternative<std::string>(v)) {
122 double value = std::get<double>(v);
129 return triggered ? 1 : 0;
163 return "CorrelationRule";
173 bool triggered =
false;
174 if (
logic ==
"AND") {
177 auto it = results.find(cond_id);
178 if (it == results.end() || !it->second) {
183 }
else if (
logic ==
"OR") {
185 auto it = results.find(cond_id);
186 if (it != results.end() && it->second) {
192 return triggered ? 1 : 0;
225 return "StepDifferenceRule";
235 int evaluate(
const std::variant<double,std::string> &v)
override {
238 if(std::holds_alternative<std::string>(v)) {
244 double current_value = std::get<double>(v);
245 bool triggered =
false;
255 return triggered ? 1 : 0;
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