noolsjs / nools

Rete based rules engine written in javascript
http://noolsjs.com/
MIT License
951 stars 181 forks source link

Unable to retrieve facts in a loop #197

Open Hervinho opened 8 years ago

Hervinho commented 8 years ago

Hi! I am trying to retrieve facts in a for loop, coz Im sending differenct array objects to the rules, but when I retrieve, only the first object sent to the engine is retrieve. I dont know why. Here is my code:

for(var i = 0; i < rows.length; i++){
                            ev[i] = new Event(rows[i].id, rows[i].name, rows[i].code, rows[i].category_id, rows[i].inventory, rows[i].type,
                                rows[i].value, rows[i].total);
                                eventSentToRule = flow.getSession(ev[i]); //assert event
                                //Fire rules and retrieve facts

                                eventSentToRule.on("event", function (obj) { eve = obj.event;})
                                        .match().then(function (err) {
                                         if(err){
                                        console.error(err);
                                                res.redirect('/home');
                                    }else{
                                            if(eve){
                                                eventOutputFromNools[i] = eve;
                                                console.log("Type: " + eventOutputFromNools[i].type);
                                                //console.log(eve.type);
                                                eventSentToRule.dispose();
                                                res.redirect('/newsponsorship');
                                            }
                                        }
                                 });
                                 eventSentToRule.dispose();//this remains
                        }

And my nools rule engine is as follows:

rule TicketHospitalityTier2{
  when{
    e: Event e.code == "TICKETS_HOSPITALITY" && e.type == "T2";
  }
  then{
    var reach = e.inventory * 0.30;
    modify(e, function(){ this.total = reach * e.value;});
    this.emit("event", {event:e});
    this.assert({event:e});
    console.log("Category TICKETS_HOSPITALITY T2 - Total: " + e.total);
  }
}

So it seems like I can only get eventOutputFromNools[0] Thanks for the help

DevSide commented 8 years ago

Where eventOutputFromNools is declared ? Are you sure you can't accomplish this in another way ? Keep in mind that you should not have too much logic out of your rules..