2022-03-31 21:20:41 +00:00
|
|
|
#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;
|
2022-04-12 21:37:05 +00:00
|
|
|
for(unsigned int i=0; i<params.size(); i++) {
|
2022-04-10 12:23:18 +00:00
|
|
|
if(params[i].isType(Value::ADDRESS_SPACE_REFERENCE)) {
|
|
|
|
request.push_back(Value::fromAddressSpace(context.addressSpaces.at(params[i].asReference())));
|
|
|
|
} else if(params[i].isType(Value::ATTRIBUTE_REFERENCE)) {
|
2022-03-31 21:20:41 +00:00
|
|
|
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) {
|
2022-04-12 21:37:05 +00:00
|
|
|
for(unsigned int i=0; i<statements.size(); i++) {
|
2022-03-31 21:20:41 +00:00
|
|
|
auto response = statements[i].evaluate(context);
|
|
|
|
if(response) {
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace domain
|