j-easy / easy-rules

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

Regarding list of Objects #386

Open baljatinder09 opened 2 years ago

baljatinder09 commented 2 years ago

is there any way i can process list of objects. for eg emp[ { "name":"a", "dep":"xyz", "flage":true/false. ///// need to set based on dep in rule. }, { "name":"a", "dep":"abc", "flage":true/false. ///// need to set based on dep in rule. }, .... ..... }] like based on the dep.. i want rule should process each emp and set some flag in fact. is is possible with easy-rule. any eg will be help to understand it.

dvgaba commented 2 years ago

you should process rules on object which are in list one by one, I am assuming your requirement is to set flag for each object based on object's state.

baljatinder09 commented 2 years ago

Yes , I want to process one by one. And action should set the Boolean flag ..is there any way I can do with rules ? Or I can use for each in conditions and are the flag and do nothing in action ?.... any other approach I can follow...

dvgaba commented 2 years ago

invoking rulesEngine.fire(rules, facts) in a for loop should work for you. Here fact should contain single object from your list

baljatinder09 commented 2 years ago

Is there any way we can make it dynamic . I have list of rules .. for each rule then I can not reuse the engine code...

dvgaba commented 2 years ago

If you can share some sample rule and sample data might be able to help.

baljatinder09 commented 2 years ago

Like this is my fact:- { "prac": { "contracts": [ { "net": "xyz", "selected":false },{ "net": "abc", "selected":false }, { "net": "abc2", "selected":false }, { "net": "abc3", "selected":false } ] } }

Rule:- [ { "name": "Business rule", "compositeRuleType": "ConditionalRuleGroup", "priority": 1, "composingRules":[ { "name": "Rule11", "description": "If net is xyz,abc3", "priority": 1, "condition": "contracts.net == \"xyz\" && contracts.net == \"abc3\"", "actions": [ "contracts.selected=true;" ] } ] } ] So i want the rule should process list of contracts and set the status selected=false as per the conditions the facts should list of contracts.