schema_editor/comdel/domain/rule.cpp

62 lines
1.8 KiB
C++
Raw Permalink Normal View History

2022-03-31 21:20:41 +00:00
#include "rule.h"
2022-05-27 06:18:17 +00:00
#include <utility>
2022-03-31 21:20:41 +00:00
namespace domain {
2022-05-27 06:18:17 +00:00
Condition::Condition(std::string function, std::vector<Value> params, bool negated)
: negated(negated), function(std::move(function)), params(std::move(params)) {}
bool Condition::evaluate(RuleContext &context) {
std::vector<Value> 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);
}
2022-03-31 21:20:41 +00:00
}
2022-05-27 06:18:17 +00:00
bool result = context.function[function]->validate(request);
return negated ? !result : result;
2022-03-31 21:20:41 +00:00
}
2022-05-27 06:18:17 +00:00
Action::Action(ActionType type, std::string message)
: type(type), message(std::move(message)) {}
Action::ActionType Action::getType() {
return type;
}
2022-03-31 21:20:41 +00:00
2022-05-27 06:18:17 +00:00
std::string Action::getMessage() {
return message;
}
2022-03-31 21:20:41 +00:00
2022-05-27 06:18:17 +00:00
IfStatement::IfStatement(Condition condition, Action action)
: condition(std::move(condition)), action(std::move(action)) {}
2022-03-31 21:20:41 +00:00
2022-05-27 06:18:17 +00:00
std::optional<Action> IfStatement::evaluate(RuleContext &context) {
if (condition.evaluate(context)) {
return action;
}
return std::nullopt;
2022-03-31 21:20:41 +00:00
}
2022-05-27 06:18:17 +00:00
Rule::Rule(std::vector<IfStatement> statements)
: statements(std::move(statements)) {}
std::optional<Action> Rule::evaluate(RuleContext &context) {
for (auto & statement : statements) {
auto response = statement.evaluate(context);
if (response) {
return response;
}
2022-03-31 21:20:41 +00:00
}
2022-05-27 06:18:17 +00:00
return std::nullopt;
2022-03-31 21:20:41 +00:00
}
} // namespace domain