Skip to content

Instantly share code, notes, and snippets.

@adamstrickland
Created September 19, 2011 21:41
Show Gist options
  • Save adamstrickland/1227691 to your computer and use it in GitHub Desktop.
Save adamstrickland/1227691 to your computer and use it in GitHub Desktop.
possible rule engine impl
/*interface IGrade
{
decimal Min{ get; set; }
decimal Max{ get; set; }
}
Grade grade0 = new Grade{ Min = 0, Max = 10000 };
Grade grade1 = new Grade{ Min = 10000, Max = 50000 };
Grade grade2 = new Grade{ Min = 50000, 100000 };
IEnumerable<IGrade> grades = [ grade0, grade1, grade2 ];*/
interface ILevel
{
decimal Min{ get; set; }
decimal Max{ get; set; }
decimal Pct{ get; set; }
}
class Level : ILevel {
public static Level Create(decimal min, decimal max, decimal pct)
{
return new Level()
{
Min = min,
Max = max,
Pct = pct
};
}
}
Func<Broker, IEnumerable<ILevel>, decimal> @algo = (b, levels) =>
{
var vol = this.VolumeByBroker(b);
var scales = levels.Map(tup => (vol > min && vol <= max) ? tup.Item3 : 0).Where(l => l > 0);
Enforce.True(scales, (obj) => obj.Count() == 1);
return scales.First();
}
Func<Broker, decimal> @premium = (b) =>
{
return @algo.Curry( [
Level.Create(0, 10000, 0.07),
Level.Create(10000, 50000, 0.065),
Level.Create(50000, 100000, 0.05)
]);
}
Func<Broker, decimal> @normal = (b) =>
{
return @algo.Curry([
Level.Create(0, 10000, 0.06),
Level.Create(10000, 50000, 0.05),
Level.Create(50000, 100000, 0.04)
]);
}
Func<Broker, Func<Broker, decimal> @brokerRule = (b) => b.IsTier0 ? @normal : @premium;
Dictionary<string, Func<Broker, Func<Broker, decimal>>> Functions = {
{ "@brokerRule", @brokerRule },
{ "@adminRule", (receipt) => {
//need implementation here...
}}
}
class Production<T>
{
public IEnumerable<Production<T>> Antecedents { get; set; }
public IEnumerable<Exercise> Exercises { get; set; }
public Production(Func<T, bool> condition)
{
}
public ConditionChain Chain<T>()
{
return Chain<T>(t => t);
}
public ConditionChain<T2> Chain<T2>(Func<T, T2> @currier)
{
return new ConditionChain<T2>(@currier);
}
}
class ConditionChain<T>
{
public ConditionChain(Func<T> @delegate)
{
}
public ConditionChain Condition(Func<T, bool> @condition)
{
}
public ConditionChain Condition(Func<T, bool> @condition, Func<T, Exercise> @delegate)
{
}
}
Production aging = new Production((receipt) => receipt.PaymentMonth > Convert.ToDateTime("1/1/2011") && receipt.PaymentMonth <= Convert.ToDateTime("12/31/2011"));
IEnumerable<Production> Productions = [
aging.Chain((receipt) => receipt.GroupCoverage).Condition(gc => gc.Carrier.Name == "Aetna")
.Condition(gc => gc.LineOfCoverage.Name == "Medical", gc => new RemainderSplit(gc.Carrier))
.Condition(gc => gc.Plan.Name.Like("^.*((HMO)|(PPO)).*$"), gc => new PercentageOfPremiumSplit()
{
Payee = Payee.BenefitMall,
Amount = Functions["@adminRule"](gc)(gc)
})
.Condition(gc => gc.CommissionableLives > 0 && gc.CommissionableLives <= 100, gc => gc.Brokers.Map(b => new PercentageOfPremiumSplit()
{
Payee = b.Payee,
Amount = Functions["@brokerRule"](b)(b)
}));
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment