Please have a look at the two code settings below.
Setting 1:
public class DroolsAnotherTest {
private static KieSession kSession;
private static Building building;
private static FactHandle buildingFact;
public static final void main(String[] args) {
try {
// load up the knowledge base
KieServices ks = KieServices.Factory.get();
KieContainer kContainer = ks.getKieClasspathContainer();
kSession = kContainer.newKieSession("ksession-rules");
building = new Building();
building.setRooms(2);
buildingFact = kSession.insert(building);
kSession.fireAllRules();
} catch (Throwable t) {
t.printStackTrace();
}
}
public static void fireAgain(){
System.out.println("inside fireAgain method");
kSession.fireAllRules(); // firing the rules second time
}
public static class Building {
public int rooms;
public int getRooms() {
return this.rooms;
}
public void setRooms(int rooms) {
this.rooms = rooms;
}
}
}
And the .drl file
package com.sample
import com.sample.DroolsAnotherTest.Building;
import com.sample.DroolsAnotherTest;
rule "Building"
when
Building( rooms <3 )
then
System.out.println( "Building rule fired" );
DroolsAnotherTest.firingAgain();
end
Upon running, the output is : Building rule fired
inside fireAgain method
Setting 2: Here I just changed the fireAgain() method to:
public static void fireAgain(){
System.out.println("inside fireAgain method");
kSession.delete(buildingFact);
building.setRooms(4);
kSession.insert(building);
kSession.fireAllRules(); // firing the rule second time
}
everything else is same. Upon running, the output is same again : Building rule fired
inside fireAgain method
As per my understanding,
in Setting 1, the rule did not get fired second time because the fact has not changed in the working memory.
in Setting 2, the rule did not get fired second time because now the fact has updated and it does not match with the rule condition.
My question is, does Drools generate any event for the setting 2 ( the rule fired once but now is not fired again because fact has updated ?) In that way I can distinguish between : a rule did not get fired because the fact are unchanged and it did not get fired because now the fact does not match with rule condition ?