Search in sources :

Example 71 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DynamicRulesTest method testRemovePackageSubNetwork.

@Test(timeout = 10000)
public void testRemovePackageSubNetwork() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_DynamicRulesWithSubnetwork.drl");
    String packageName = kbase.getKiePackages().iterator().next().getName();
    KieSession workingMemory = createKnowledgeSession(kbase);
    List<?> results = new ArrayList<Object>();
    workingMemory.setGlobal("results", results);
    Order order = new Order();
    OrderItem item1 = new OrderItem(order, 1, "Adventure Guide Brazil", OrderItem.TYPE_BOOK, 24);
    OrderItem item2 = new OrderItem(order, 2, "Prehistoric Britain", OrderItem.TYPE_BOOK, 15);
    OrderItem item3 = new OrderItem(order, 3, "Holiday Music", OrderItem.TYPE_CD, 9);
    OrderItem item4 = new OrderItem(order, 4, "Very Best of Mick Jagger", OrderItem.TYPE_CD, 11);
    OrderItem item5 = new OrderItem(order, 5, "The Master and Margarita", OrderItem.TYPE_BOOK, 29);
    order.addItem(item1);
    order.addItem(item2);
    order.addItem(item3);
    order.addItem(item4);
    order.addItem(item5);
    workingMemory.insert(order);
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    InternalKnowledgeBase ruleBaseWM = (InternalKnowledgeBase) workingMemory.getKieBase();
    ruleBaseWM.removeKiePackage(packageName);
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicRulesWithSubnetwork.drl");
    ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
    workingMemory.fireAllRules();
    results = (List) workingMemory.getGlobal("results");
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    ruleBaseWM.removeKiePackage(packageName);
    ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    ruleBaseWM.removeKiePackage(packageName);
    ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
}
Also used : Order(org.drools.compiler.Order) KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) OrderItem(org.drools.compiler.OrderItem) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 72 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DynamicRulesTest method testDynamicRules.

@Test(timeout = 10000)
public void testDynamicRules() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
    KieSession session = createKnowledgeSession(kbase);
    final Cheese a = new Cheese("stilton", 10);
    final Cheese b = new Cheese("stilton", 15);
    final Cheese c = new Cheese("stilton", 20);
    session.insert(a);
    session.insert(b);
    session.insert(c);
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRules.drl"));
    kbase.addPackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 73 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DynamicRulesTest method testIsolatedClassLoaderWithEnumsPkgBuilder.

@Test(timeout = 10000)
public void testIsolatedClassLoaderWithEnumsPkgBuilder() throws Exception {
    try {
        // Creates first class loader and use it to load fact classes
        ClassLoader loader1 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
        loader1.loadClass("org.drools.Primitives");
        loader1.loadClass("org.drools.TestEnum");
        // create a builder with the given classloader
        KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader1);
        Collection<KiePackage> kpkgs = loadKnowledgePackages(conf, "test_EnumSerialization.drl");
        // serialize out
        byte[] out = DroolsStreamUtils.streamOut(kpkgs);
        // adding original packages to a kbase just to make sure they are fine
        KieBaseConfiguration kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader1);
        InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase(kbaseConf);
        kbase.addPackages(kpkgs);
        KieSession ksession = createKnowledgeSession(kbase);
        List list = new ArrayList();
        ksession.setGlobal("list", list);
        assertEquals(1, ksession.fireAllRules());
        assertEquals(1, list.size());
        // now, create another classloader and make sure it has access to the classes
        ClassLoader loader2 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
        loader2.loadClass("org.drools.Primitives");
        loader2.loadClass("org.drools.TestEnum");
        ClassLoader ccl = Thread.currentThread().getContextClassLoader();
        Collection<KiePackage> kpkgs2 = null;
        try {
            Thread.currentThread().setContextClassLoader(loader2);
            kpkgs2 = (Collection<KiePackage>) DroolsStreamUtils.streamIn(out);
        } finally {
            Thread.currentThread().setContextClassLoader(ccl);
        }
        // create another kbase
        KieBaseConfiguration kbaseConf2 = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader2);
        InternalKnowledgeBase kbase2 = (InternalKnowledgeBase) getKnowledgeBase(kbaseConf2);
        kbase2.addPackages(kpkgs2);
        ksession = createKnowledgeSession(kbase2);
        list = new ArrayList();
        ksession.setGlobal("list", list);
        assertEquals(1, ksession.fireAllRules());
        assertEquals(1, list.size());
    } catch (ClassCastException cce) {
        cce.printStackTrace();
        fail("No ClassCastException should be raised.");
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 74 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DynamicRulesTest method testIsolatedClassLoaderWithEnumsContextClassloader.

@Test(timeout = 10000)
public void testIsolatedClassLoaderWithEnumsContextClassloader() throws Exception {
    try {
        // Creates first class loader and use it to load fact classes
        ClassLoader loader1 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
        loader1.loadClass("org.drools.Primitives");
        loader1.loadClass("org.drools.TestEnum");
        byte[] out = null;
        // Build it using the current context
        ClassLoader ccl = Thread.currentThread().getContextClassLoader();
        Collection<KiePackage> kpkgs2 = null;
        try {
            Thread.currentThread().setContextClassLoader(loader1);
            // create a builder with the given classloader
            Collection<KiePackage> kpkgs = loadKnowledgePackages("test_EnumSerialization.drl");
            // serialize out
            out = DroolsStreamUtils.streamOut(kpkgs);
            // adding original packages to a kbase just to make sure they are fine
            InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
            kbase.addPackages(kpkgs);
            KieSession ksession = createKnowledgeSession(kbase);
            List list = new ArrayList();
            ksession.setGlobal("list", list);
            assertEquals(1, ksession.fireAllRules());
            assertEquals(1, list.size());
        } finally {
            Thread.currentThread().setContextClassLoader(ccl);
        }
        // now, create another classloader and make sure it has access to the classes
        ClassLoader loader2 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
        loader2.loadClass("org.drools.Primitives");
        loader2.loadClass("org.drools.TestEnum");
        // set context classloader and use it
        ccl = Thread.currentThread().getContextClassLoader();
        kpkgs2 = null;
        try {
            Thread.currentThread().setContextClassLoader(loader2);
            kpkgs2 = (Collection<KiePackage>) DroolsStreamUtils.streamIn(out);
            // create another kbase
            InternalKnowledgeBase kbase2 = (InternalKnowledgeBase) getKnowledgeBase();
            kbase2.addPackages(kpkgs2);
            KieSession ksession = createKnowledgeSession(kbase2);
            List list = new ArrayList();
            ksession.setGlobal("list", list);
            assertEquals(1, ksession.fireAllRules());
            assertEquals(1, list.size());
        } finally {
            Thread.currentThread().setContextClassLoader(ccl);
        }
    } catch (ClassCastException cce) {
        cce.printStackTrace();
        fail("No ClassCastException should be raised.");
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) URLClassLoader(java.net.URLClassLoader) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 75 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class DynamicRulesTest method testRuleBaseAddRemove.

@Test(timeout = 10000)
public void testRuleBaseAddRemove() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
    // add and remove
    Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic1.drl"));
    String pkgName = kpkgs.iterator().next().getName();
    kbase.addPackages(kpkgs);
    kbase.removeKiePackage(pkgName);
    kbase = SerializationHelper.serializeObject(kbase);
    // add and remove again
    kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic1.drl"));
    pkgName = kpkgs.iterator().next().getName();
    kbase.addPackages(kpkgs);
    kbase.removeKiePackage(pkgName);
    kbase = SerializationHelper.serializeObject(kbase);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) 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