#include "rule.h" #include namespace domain { Condition::Condition(std::string function, std::vector params, bool negated) : negated(negated), function(std::move(function)), params(std::move(params)) {} bool Condition::evaluate(RuleContext &context) { std::vector request; for (auto & param : params) { if (param.isType(Value::ADDRESS_SPACE_REFERENCE)) { request.push_back(Value::fromAddressSpace(context.addressSpaces.at(param.asReference()))); } else if (param.isType(Value::ATTRIBUTE_REFERENCE)) { request.push_back(context.attributes[param.asReference()]); } else { request.push_back(param); } } bool result = context.function[function]->validate(request); return negated ? !result : result; } Action::Action(ActionType type, std::string message) : type(type), message(std::move(message)) {} Action::ActionType Action::getType() { return type; } std::string Action::getMessage() { return message; } IfStatement::IfStatement(Condition condition, Action action) : condition(std::move(condition)), action(std::move(action)) {} std::optional IfStatement::evaluate(RuleContext &context) { if (condition.evaluate(context)) { return action; } return std::nullopt; } Rule::Rule(std::vector statements) : statements(std::move(statements)) {} std::optional Rule::evaluate(RuleContext &context) { for (auto & statement : statements) { auto response = statement.evaluate(context); if (response) { return response; } } return std::nullopt; } } // namespace domain