Search in sources :

Example 76 with KiePackage

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();
}
Also used : PersonInterface(org.drools.compiler.PersonInterface) KiePackage(org.kie.api.definition.KiePackage) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 77 with KiePackage

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());
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 78 with KiePackage

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));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) Precondition(org.drools.compiler.Precondition) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 79 with KiePackage

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());
}
Also used : StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 80 with KiePackage

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));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7