j-easy / easy-rules

The simple, stupid rules engine for Java
https://github.com/j-easy/easy-rules/wiki
MIT License
4.83k stars 1.04k forks source link

How to apply rule for each object in a list in one session and return the result from action? #408

Open ashwiniakshay04 opened 1 year ago

ashwiniakshay04 commented 1 year ago

As far as I can tell, we can fire multiple facts at once, and the rules that match the fact's value will be executed. What if we want to evaluate multiple objects representing the same fact?

We cannot enter two facts with the same key because the Facts object is a hashmap. So I'm thinking we can make lists for each object type and then apply the rules to each object in the list. But how can rules be applied to each object in that list, where the list is a fact?

List dataBall = new ArrayList<>(); dataBall.add(new Ball("Red", 7, "B")); dataBall.add(new Ball("Blue", 3, "A"));

List dataBat = new ArrayList<>(); dataBat.add(new Bat("Green", 8, "B")); dataBat.add(new Bat("Red", 5, "A")); dataBat.add(new Bat("Green", 6, "B"));

Facts fact = new Facts(); fact.put("ball", dataBall); fact.put("bat", dataBat);

I want to execute two different rules based on the facts. As an example: For the fact "ball," I'd like to run the ball rule for each ball in the dataBall list. Similarly, for bat. All of this in one execution/ fire.

Siktik commented 45 minutes ago

hey, the way i can think of achieving this is by implementing a method for both the dataBall and the dataBat class with the same name, which returns a bool. in fact.put() you should use the same name for both classes, f.e. "myClass". you can then call this method in the condition where you check which class is calling an evaluate according to this. For the action u also define a method on the class level where you can implement anything.