runxc1 / MicroRuleEngine

A .Net Rule Engine for dynamically evaluating business rules compiled on the fly.
MIT License
186 stars 74 forks source link

How evaluate an Object in a collection ? #5

Closed jperezmedina closed 5 years ago

jperezmedina commented 9 years ago

I rule example of how to use the MicroRuleEngine with a childProperty is included in the solution. For instance:

[TestMethod]
    public void ChildProperties()
    {
        Order order = this.GetOrder();
        Rule rule = new Rule()
        {
            MemberName = "Customer.Country.CountryCode",
            Operator = System.Linq.Expressions.ExpressionType.Equal.ToString("g"),
            TargetValue = "AUS"
        };
        MRE engine = new MRE();
        var compiledRule = engine.CompileRule<Order>(rule);
        bool passes = compiledRule(order);
        Assert.IsTrue(passes);
        order.Customer.Country.CountryCode = "USA";
        passes = compiledRule(order);
        Assert.IsFalse(passes);
    }

But, in my case, I need create a rule that evaluates is the cost of the Item with the ItemCode = "MM23" (so, the first Item object) is 'LessThanOrEqual' to 8.10M. How i can make it ?

runxc1 commented 9 years ago

Do you have the ability to alter the Base Class? Currently the MicroRuleEngine can execute a boolean method given an array of inputs, and there are a couple examples of that.

jperezmedina commented 9 years ago

I'm new with C#. Could you detail more about it? How alter the base Class to support collection[0].feature? Where are the examples? Thanks On Nov 7, 2015 12:56 AM, "runxc1(Bret Ferrier)" notifications@github.com wrote:

Do you have the ability to alter the Base Class? Currently the MicroRuleEngine can execute a boolean method given an array of inputs, and there are a couple examples of that.

— Reply to this email directly or view it on GitHub https://github.com/runxc1/MicroRuleEngine/issues/5#issuecomment-154580765 .

jamescurran commented 6 years ago

I've added this feature to my fork (https://github.com/jamescurran/MicroRuleEngine). Using the new syntax, it would be:

        Rule rule = Rule.Create("Items[0].Cost", mreOperator.LessThanOrEqual, 8.10m);
fschaal commented 6 years ago

@jamescurran , would you also be able to use the linq method All / Any on a collection?

somthing like Rule rule = Rule.Create("Items[].Status", mreOperator.All, "Shipped");

jamescurran commented 6 years ago

So, basically, you'd want it to be the equivalent of "Items.All(i=>i.Status == Shipped)" ?

jamescurran commented 6 years ago

Actually, this should work for that (I'd forgotten about it)

        Rule rule = Rule.All("Items", Rule.Create("Status", mreOperator.Equal, "Shipped"));
runxc1 commented 5 years ago

The changes from @jamescurran enabling this have been pulled in so this is now possible.