Building a rule engine in c# (part 8: extending the rule engine with method calls of the objects)

In a comment on a blog article (https://netmatze.wordpress.com/2014/09/17/building-a-rule-engine-in-c-part-7-extending-the-rule-engine-with-a-like-operator/) about the rule engine project (ruleengine.codeplex.com) Matt wrote that he would need the feature that the makes it possible to call methodes at the object and check for the result of that method calls. So i made some changes to the rule engine project to make that possible. I released a new version at codeplex and here you can see a test that uses that method calls.

[TestMethod]
public void SimpleExpressionEvaluatorWasEmployedDateMethod()
{
    Person person1 = new Person()
    {
        Name = "Mathias",
        Age = 36,
        Children = 2,
        Married = true,
        Birthdate = new DateTime(1976, 5, 9),
        EmployDate = new DateTime(2012,12,1)
    };
    Person person2 = new Person()
    {
        Name = "Anna",
        Age = 32,
        Children = 2,
        Married = false,
        Birthdate = new DateTime(2002, 2, 2),
        EmployDate = new DateTime(2013, 12, 1)
    };
    Person person3 = new Person()
    {
        Name = "Karo",
        Age = 38,
        Children = 2,
        Married = true,
        Birthdate = new DateTime(1976, 2, 2),
        EmployDate = new DateTime(2011, 12, 1)
    };
    Evaluator evaluator = new Evaluator();
    var result1 = evaluator.Evaluate(" (CalculateAge() >= 10 && Married = true) 
&& WasEmployed('2013-12-01') = true ", person1);
    Assert.AreEqual(result1, true);
    var result2 = 
    evaluator.Evaluate(" CalculateAge() >= 10 && Married = true ||     
    WasEmployed('2014-12-01') = false ", person2);
    Assert.AreEqual(result2, false);
    var result3 = evaluator.Evaluate(
    " (CalculateAge() >= 2 || Married = true 
    || WasEmployed('2010-12-01') = true) then SetCanReceiveBenefits(true) 
    else SetCancelBenefits(true) ", person3);
    Assert.AreEqual(result3, true);
    Assert.AreEqual(person3.CancelBenefits, false);
    Assert.AreEqual(person3.ReceiveBenefits, true);
}
Advertisements