Search in sources :

Example 71 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class DeclarativeAgendaTest method testIterativeUpdate.

@Test(timeout = 10000)
public void testIterativeUpdate() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule0 \n";
    str += "when \n";
    str += "     $s : String( this == 'rule0' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name ); \n";
    str += "end \n";
    str += "rule rule1 \n";
    str += "when \n";
    str += "     $s : String( this == 'rule1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name ); \n";
    str += "end \n";
    str += "rule rule2 \n";
    str += "when \n";
    str += "     $s : String( this == 'rule2' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules1 @activationListener('direct') \n";
    str += "when \n";
    str += "     $l : List( ) \n";
    str += "     $i : Match( rule.name == $l[0] ) \n";
    str += "then \n";
    // str += "   System.out.println( kcontext.rule.name  + ':' + $i ); \n";
    str += "    list.add( 'block:' + $i.rule.name  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    FactHandle rule0 = ksession.insert("rule0");
    FactHandle rule1 = ksession.insert("rule1");
    FactHandle rule2 = ksession.insert("rule2");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertTrue(list.contains("rule0"));
    assertTrue(list.contains("rule1"));
    assertTrue(list.contains("rule2"));
    list.clear();
    ArrayList l = new ArrayList();
    l.add("rule0");
    FactHandle lh = ksession.insert(l);
    ksession.update(rule0, "rule0");
    ksession.update(rule1, "rule1");
    ksession.update(rule2, "rule2");
    ksession.fireAllRules();
    assertEquals(4, list.size());
    assertTrue(list.contains("block:rule0"));
    assertTrue(list.contains("rule1"));
    assertTrue(list.contains("rule2"));
    assertFalse(list.contains("rule0"));
    list.clear();
    ksession.update(rule0, "rule0");
    ksession.update(rule1, "rule1");
    ksession.update(rule2, "rule2");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertTrue(list.contains("block:rule0"));
    assertTrue(list.contains("rule1"));
    assertTrue(list.contains("rule2"));
    assertFalse(list.contains("rule0"));
    list.clear();
    l.set(0, "rule1");
    ksession.update(lh, l);
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(2, list.size());
    assertTrue(list.contains("rule0"));
    assertTrue(list.contains("block:rule1"));
    list.clear();
    ksession.update(rule0, "rule0");
    ksession.update(rule1, "rule1");
    ksession.update(rule2, "rule2");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertTrue(list.contains("block:rule1"));
    assertTrue(list.contains("rule0"));
    assertTrue(list.contains("rule2"));
    assertFalse(list.contains("rule1"));
    list.clear();
    l.set(0, "rule2");
    ksession.update(lh, l);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains("rule1"));
    assertTrue(list.contains("block:rule2"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 72 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class DeclarativeAgendaTest method testCancelMultipleActivations.

@Test(timeout = 10000)
public void testCancelMultipleActivations() {
    String str = "package org.domain.test\n" + "import " + Match.class.getName() + "\n" + "global java.util.List list\n" + "rule sales1 @department('sales')\n" + "when\n" + "    String( this == 'fireRules' )\n" + "then\n" + "    list.add(\"sales1\");\n" + "end\n" + "\n" + "rule sales2 @department('sales') \n" + "when\n" + "    String( this == 'fireRules' )\n" + "then\n" + "    list.add(\"sales2\");\n" + "end\n" + "\n" + "rule salesCancel @activationListener('direct')\n" + "when\n" + "    $i : Match( department == 'sales' )\n" + "then\n" + "    kcontext.cancelMatch($i);\n" + "end";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("fireRules");
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(0, list.size());
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 73 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class DeclarativeAgendaTest method testCancelActivationOnInsertAndUpdate.

@Test(timeout = 10000)
public void testCancelActivationOnInsertAndUpdate() {
    String str = "package org.domain.test\n" + "import " + Match.class.getName() + "\n" + "global java.util.List list\n" + "rule sales1 @department('sales') @category('special')\n" + "salience 10\n" + "when\n" + "    String( this == 'fireRules' )\n" + "then\n" + "    list.add(\"sales1\");\n" + "end\n" + "\n" + "rule sales2 @department('sales') \n" + "when\n" + "    String( this == 'fireRules' )\n" + "then\n" + "    list.add(\"sales2\");\n" + "end\n" + "\n" + "rule salesCancel @activationListener('direct')\n" + "when\n" + "    String(this == 'fireCancelRule')\n" + "    $i : Match( department == 'sales', category == 'special' )\n" + "then\n" + "    kcontext.cancelMatch($i);\n" + "end";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    FactHandle fireRules = ksession.insert("fireRules");
    FactHandle fireCancelRule = ksession.insert("fireCancelRule");
    ksession.fireAllRules();
    assertEquals(1, list.size());
    ksession.update(fireRules, "fireRules");
    ksession.update(fireCancelRule, "fireCancelRule");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 74 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class DeclarativeAgendaTest method testSimpleBlockingUsingForall.

@Test(timeout = 10000)
public void testSimpleBlockingUsingForall() {
    String str = "";
    str += "package org.domain.test \n";
    str += "import " + Match.class.getName() + "\n";
    str += "global java.util.List list \n";
    str += "dialect 'mvel' \n";
    str += "rule rule1 @department(sales) salience -100 \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule rule2 salience 200\n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "     exists  Match( department == 'sales' ) \n";
    str += "     forall ( $a : Match( department == 'sales' ) Match( this == $a, active == false ) ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(DeclarativeAgendaOption.ENABLED);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
    KieSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert("go1");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.contains("rule1:go1"));
    assertTrue(list.contains("rule2:go1"));
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test)

Example 75 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration 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)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)124 Test (org.junit.Test)94 KieSession (org.kie.api.runtime.KieSession)77 KieBase (org.kie.api.KieBase)69 ArrayList (java.util.ArrayList)54 List (java.util.List)38 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)25 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 FactHandle (org.kie.api.runtime.rule.FactHandle)21 EntryPoint (org.kie.api.runtime.rule.EntryPoint)18 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 StockTick (org.drools.compiler.StockTick)11 KieContainer (org.kie.api.runtime.KieContainer)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 Match (org.kie.api.runtime.rule.Match)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7