use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovals.
@Test(timeout = 10000)
public void testDynamicRuleRemovals() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) SerializationHelper.serializeObject(loadKnowledgeBase("test_Dynamic1.drl", "test_Dynamic3.drl", "test_Dynamic4.drl"));
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic2.drl"));
kbase.addPackages(kpkgs);
KieSession wm = createKnowledgeSession(kbase);
// AgendaEventListener ael = mock( AgendaEventListener.class );
// wm.addEventListener( ael );
final List<?> list = new ArrayList<Object>();
wm.setGlobal("list", list);
final PersonInterface bob = new Person("bob", "stilton");
bob.setStatus("Not evaluated");
FactHandle fh0 = wm.insert(bob);
final Cheese stilton1 = new Cheese("stilton", 5);
FactHandle fh1 = wm.insert(stilton1);
final Cheese stilton2 = new Cheese("stilton", 3);
FactHandle fh2 = wm.insert(stilton2);
final Cheese stilton3 = new Cheese("stilton", 1);
FactHandle fh3 = wm.insert(stilton3);
final Cheese cheddar = new Cheese("cheddar", 5);
FactHandle fh4 = wm.insert(cheddar);
wm.fireAllRules();
assertEquals(15, list.size());
list.clear();
kbase.removeRule("org.drools.compiler.test", "Who likes Stilton");
wm.update(fh0, bob);
wm.update(fh1, stilton1);
wm.update(fh2, stilton2);
wm.update(fh3, stilton3);
wm.update(fh4, cheddar);
wm.fireAllRules();
assertEquals(12, list.size());
list.clear();
kbase.removeRule("org.drools.compiler.test", "like cheese");
wm.update(fh0, bob);
wm.update(fh1, stilton1);
wm.update(fh2, stilton2);
wm.update(fh3, stilton3);
wm.update(fh4, cheddar);
wm.fireAllRules();
assertEquals(8, list.size());
list.clear();
final Cheese muzzarela = new Cheese("muzzarela", 5);
wm.insert(muzzarela);
wm.fireAllRules();
assertEquals(1, list.size());
list.clear();
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testSharedLIANodeRemoval.
@Test(timeout = 10000)
public void testSharedLIANodeRemoval() throws Exception {
// it's not a true share, but the liaNode will have two sinks, due to subnetwork.
String str = "global java.util.List list;\n";
str += "rule \"test\"\n";
str += "when\n";
str += " exists(eval(true))\n";
str += "then\n";
str += " list.add(\"fired\");\n";
str += "end\n";
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackagesFromString(str));
// Add once ...
kbase.addPackages(kpkgs);
// This one works
List list = new ArrayList();
KieSession session = createKnowledgeSession(kbase);
session.setGlobal("list", list);
session.fireAllRules();
assertEquals(1, list.size());
list.clear();
// ... remove ...
KiePackage kpkg = (KiePackage) kpkgs.toArray()[0];
kbase.removeKiePackage(kpkg.getName());
kbase.addPackages(kpkgs);
session = createKnowledgeSession(kbase);
session.setGlobal("list", list);
session.fireAllRules();
assertEquals(1, list.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testRemovePackage.
@Test(timeout = 10000)
public void testRemovePackage() throws Exception {
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_RemovePackage.drl"));
final String packageName = kpkgs.iterator().next().getName();
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
KieSession session = createKnowledgeSession(kbase);
session.insert(new Precondition("genericcode", "genericvalue"));
session.fireAllRules();
InternalKnowledgeBase ruleBaseWM = (InternalKnowledgeBase) session.getKieBase();
ruleBaseWM.removeKiePackage(packageName);
kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_RemovePackage.drl"));
ruleBaseWM.addPackages(kpkgs);
ruleBaseWM = SerializationHelper.serializeObject(ruleBaseWM);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
ruleBaseWM.removeKiePackage(packageName);
ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
ruleBaseWM.removeKiePackage(packageName);
ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleAdditionsWithEntryPoints.
@Test(timeout = 10000)
public void testDynamicRuleAdditionsWithEntryPoints() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicWithEntryPoint.drl");
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
KieSession ksession = createKnowledgeSession(kbase);
// now lets add some knowledge to the kbase
kbase.addPackages(kpkgs);
List<StockTick> results = new ArrayList<StockTick>();
ksession.setGlobal("results", results);
EntryPoint ep = ksession.getEntryPoint("in-channel");
ep.insert(new StockTick(1, "RHT", 20, 10000));
ep.insert(new StockTick(2, "RHT", 21, 15000));
ep.insert(new StockTick(3, "RHT", 22, 20000));
ksession.fireAllRules();
assertEquals(3, results.size());
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovalsSubNetworkAndNot.
@Test(timeout = 10000)
public void testDynamicRuleRemovalsSubNetworkAndNot() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase("test_DynamicRulesWithNotSubnetwork.drl");
KieSession ksession = createKnowledgeSession(kbase);
final AgendaEventListener alistener = mock(AgendaEventListener.class);
ksession.addEventListener(alistener);
// pattern does not match, so do not activate
ksession.insert(new Person("toni"));
ksession.fireAllRules();
verify(alistener, never()).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
// pattern matches, so create activation
ksession.insert(new Person("bob"));
ksession.fireAllRules();
verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
// already active, so no new activation should be created
ksession.insert(new Person("mark"));
ksession.fireAllRules();
verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
kbase.removeKiePackage("org.drools.compiler");
assertEquals(0, kbase.getKiePackages().size());
// lets re-compile and add it again
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicRulesWithNotSubnetwork.drl");
kbase.addPackages(kpkgs);
ksession.fireAllRules();
// rule should be reactivated, since data is still in the session
verify(alistener, times(2)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
}
Aggregations