61 lines
1.4 KiB
C++
61 lines
1.4 KiB
C++
|
#include "rule.h"
|
||
|
|
||
|
namespace domain {
|
||
|
|
||
|
Condition::Condition(std::string function, std::vector<Value> params, bool negated)
|
||
|
: negated(negated), function(function), params(params)
|
||
|
{}
|
||
|
|
||
|
bool Condition::evaluate(RuleContext &context) {
|
||
|
std::vector<Value> request;
|
||
|
for(uint i=0; i<params.size(); i++) {
|
||
|
if(params[i].isType(Value::ATTRIBUTE_REFERENCE)) {
|
||
|
request.push_back(context.attributes[params[i].asReference()]);
|
||
|
} else {
|
||
|
request.push_back(params[i]);
|
||
|
}
|
||
|
}
|
||
|
bool result = context.function[function](request);
|
||
|
return negated ? !result : result;
|
||
|
}
|
||
|
|
||
|
|
||
|
Action::Action(ActionType type, std::string message)
|
||
|
: type(type), message(message)
|
||
|
{}
|
||
|
|
||
|
Action::ActionType Action::getType() {
|
||
|
return type;
|
||
|
}
|
||
|
std::string Action::getMessage() {
|
||
|
return message;
|
||
|
}
|
||
|
|
||
|
|
||
|
IfStatement::IfStatement(Condition condition, Action action)
|
||
|
: condition(condition), action(action)
|
||
|
{}
|
||
|
|
||
|
std::optional<Action> IfStatement::evaluate(RuleContext &context) {
|
||
|
if(condition.evaluate(context)) {
|
||
|
return action;
|
||
|
}
|
||
|
return std::nullopt;
|
||
|
}
|
||
|
|
||
|
Rule::Rule(std::vector<IfStatement> statements)
|
||
|
: statements(statements)
|
||
|
{}
|
||
|
|
||
|
std::optional<Action> Rule::evaluate(RuleContext &context) {
|
||
|
for(uint i=0; i<statements.size(); i++) {
|
||
|
auto response = statements[i].evaluate(context);
|
||
|
if(response) {
|
||
|
return response;
|
||
|
}
|
||
|
}
|
||
|
return std::nullopt;
|
||
|
}
|
||
|
|
||
|
} // namespace domain
|