Skip to content

Instantly share code, notes, and snippets.

@maidis
Created January 27, 2019 14:18
Show Gist options
  • Save maidis/3729b803f62f9d334514643e4439d6cf to your computer and use it in GitHub Desktop.
Save maidis/3729b803f62f9d334514643e4439d6cf to your computer and use it in GitHub Desktop.
Basitleştirilmiş Kural Motoru
#include <iostream>
#include "RulesEngine.hpp"
struct Customer
{
std::string name;
int purchasedGoodsValue;
bool hasReturnedItems;
bool hasDefaulted;
};
int main()
{
Customer customer = {"Anıl", 1000, false, false};
auto isAGoodCustomer = RulesEngine{};
isAGoodCustomer.If(customer.purchasedGoodsValue >= 1000);
isAGoodCustomer.If(!customer.hasReturnedItems);
//isAGoodCustomer.If(std::find(begin(surveyResponders), end(surveyResponders), customer) != end(surveyResponders));
isAGoodCustomer.NotIf(customer.hasDefaulted);
if (isAGoodCustomer())
{
std::cout << "Dear esteemed customer,";
}
else
{
std::cout << "Dear customer,";
}
std::cout << ' ' << customer.name << '\n';
}
#include <algorithm>
#include "RulesEngine.hpp"
RulesEngine::RulesEngine() : Not(*this) {}
void RulesEngine::If(bool sufficientCondition)
{
sufficientConditions.push_back(sufficientCondition);
}
void RulesEngine::NotIf(bool preventingCondition)
{
preventingConditions.push_back(preventingCondition);
}
bool RulesEngine::operator()() const
{
auto isTrue = [](bool b) {
return b;
};
return std::any_of(begin(sufficientConditions), end(sufficientConditions), isTrue)
&& std::none_of(begin(preventingConditions), end(preventingConditions), isTrue);
}
PreventingRulesEngine::PreventingRulesEngine(RulesEngine& rulesEngine) : rulesEngine_(rulesEngine) {}
void PreventingRulesEngine::If(bool preventingCondition)
{
rulesEngine_.NotIf(preventingCondition);
}
#pragma once
#include <deque>
class RulesEngine;
class PreventingRulesEngine
{
public:
explicit PreventingRulesEngine(RulesEngine& rulesEngine);
void If(bool preventingCondition);
private:
RulesEngine& rulesEngine_;
};
class RulesEngine
{
public:
RulesEngine();
void If(bool sufficientCondition);
void NotIf(bool preventingCondition);
PreventingRulesEngine Not;
bool operator()() const;
private:
std::deque<bool> sufficientConditions;
std::deque<bool> preventingConditions;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment