KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession kSession = createKnowledgeSession(kbase);
kSession.setGlobal("totalHolder", new TotalHolder());
Father abraham = new Father("abraham", null, 100);
Father homer = new Father("homer", null, 20);
Father bart = new Father("bart", null, 3);
org.kie.api.runtime.rule.FactHandle abrahamHandle = kSession.insert(abraham);
org.kie.api.runtime.rule.FactHandle bartHandle = kSession.insert(bart);
kSession.fireAllRules();
assertEquals(0, ((TotalHolder) kSession.getGlobal("totalHolder")).getTotal());
bart.setFather(abraham);
kSession.update(bartHandle, bart);
kSession.fireAllRules();
assertEquals(100, ((TotalHolder) kSession.getGlobal("totalHolder")).getTotal());
bart.setFather(null);
kSession.update(bartHandle, bart);
kSession.fireAllRules();
assertEquals(0, ((TotalHolder) kSession.getGlobal("totalHolder")).getTotal());
bart.setFather(abraham);
kSession.update(bartHandle, bart);
kSession.fireAllRules();
assertEquals(100, ((TotalHolder) kSession.getGlobal("totalHolder")).getTotal());
org.kie.api.runtime.rule.FactHandle homerHandle = kSession.insert(homer);
homer.setFather(abraham);
kSession.update(homerHandle, homer);
bart.setFather(homer);
kSession.update(bartHandle, bart);
kSession.fireAllRules();
assertEquals(120, ((TotalHolder) kSession.getGlobal("totalHolder")).getTotal());
}