hyperjumptech/grule-rule-engine

How to iterate over an array/map to find certain subitems from it

Closed this issue · 1 comments

How can I use the slice/map data type to iterate over it and create a new slice/map type where a specific condition is satisfied?
Consider the following case, what rule to write in the commented part?

rule AddRecentPurchases "If it is a active customer then fetch it's recent purchases" salience 10 {
    when
        Customer.IsActive
    then
        Purchases = Customer.GetRecentPurchases();
        Retract("AddRecentPurchases");
}

rule FindIntrestedPurchase "Find purchase that satisfy certain condition based on the single purchase fact" salience 9 {
    when
        Customer.IsActive && Purchases.Len() > 0
    then
        // do something to find the intrested purchase from Purchase fact
        // for eg I want to find the purchase where Purchase[i].month == "July" and Purchase[i].amout >= 1000
        Customer.UpdateIntrestedPurchase(purchase);
        Retract("FindIntrestedPurchase");
}
newm4n commented

Hi @ponder2000 ,

If you read this ...
https://docs.jboss.org/drools/release/5.3.0.Final/drools-expert-docs/html/ch01.html#:~:text=Rule%20engines%20allow%20you%20to,easier%20to%20read%20than%20code.

Rule engines allow you to say "What to do", but NOT "How to do it".

"Its up to you How you iterate over your data. What the rule engine should do is to tell you What todo."
In programming term, "Its up to you how you code the program, rule engine job is to invoke that function if certain rule are met".

How you use slice or array , how to iterate over it, its the job of your program and logic. to do the task. Rule engine just "tell" which of the function to call".

That means, that your Fact, should contain a function or method to call. To call by the rule engine. Inside that function, you can do what ever you want to do.

I hope you understand what I mean. This is the basic of Rule Engine. What you're asking is to trying to make the rule engine do everything for you.