Search in sources :

Example 21 with KieBaseConfiguration

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

the class Misc2Test method testBetaNodeInSubnetworkInStreamMode.

@Test
public void testBetaNodeInSubnetworkInStreamMode() {
    // BZ-995408
    String str = "import " + Foo.class.getCanonicalName() + "\n" + "\n" + "global java.util.List context;\n" + "\n" + "declare Foo\n" + "    @role( event )\n" + "end\n" + "\n" + "rule \"Rule A\"\n" + "when\n" + "    $f : Foo( )\n" + "    not ( Integer() from context )\n" + "then\n" + "    $f.setX( 2 );\n" + "end";
    KieBaseConfiguration kBaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kBaseConf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kBaseConf, str);
    KieSession ksession = kbase.newKieSession();
    ksession.setGlobal("context", new ArrayList() {

        {
            add(new Long(0));
        }
    });
    Foo foo = new Foo();
    foo.setX(1);
    ksession.insert(foo);
    ksession.fireAllRules();
    assertEquals(2, foo.getX());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Test(org.junit.Test)

Example 22 with KieBaseConfiguration

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

the class DeclarativeAgendaTest method getStatefulKnowledgeSession.

public KieSession getStatefulKnowledgeSession() {
    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) \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 blockerAllSalesRules @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( $i.rule.name + ':' + $s  ); \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);
    return ksession;
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Match(org.kie.api.runtime.rule.Match)

Example 23 with KieBaseConfiguration

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

the class DeclarativeAgendaTest method testBasicBlockOnAnnotation.

@Test(timeout = 10000)
public void testBasicBlockOnAnnotation() {
    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) \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 @department(sales) \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 rule3 @department(sales) \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 blockerAllSalesRules @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( $i.rule.name + ':' + $s  ); \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);
    ksession.insert("go1");
    FactHandle go2 = ksession.insert("go2");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertTrue(list.contains("rule1:go2"));
    assertTrue(list.contains("rule2:go2"));
    assertTrue(list.contains("rule3:go2"));
    list.clear();
    ksession.retract(go2);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertTrue(list.contains("rule1:go1"));
    assertTrue(list.contains("rule2:go1"));
    assertTrue(list.contains("rule3:go1"));
    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 24 with KieBaseConfiguration

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

the class DeclarativeAgendaTest method testMultipleBlockers.

@Test(timeout = 10000)
public void testMultipleBlockers() {
    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 @department(sales) \n";
    str += "when \n";
    str += "     $s : String( this == 'go0' ) \n";
    str += "then \n";
    str += "    list.add( kcontext.rule.name + ':' + $s ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules1 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go1' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules2 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( $i.rule.name + ':' + $s  ); \n";
    str += "    kcontext.blockMatch( $i ); \n";
    str += "end \n";
    str += "rule blockerAllSalesRules3 @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go3' ) \n";
    str += "     $i : Match( department == 'sales' ) \n";
    str += "then \n";
    str += "    list.add( $i.rule.name + ':' + $s  ); \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 go0 = ksession.insert("go0");
    FactHandle go1 = ksession.insert("go1");
    FactHandle go2 = ksession.insert("go2");
    FactHandle go3 = ksession.insert("go3");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertTrue(list.contains("rule0:go1"));
    assertTrue(list.contains("rule0:go2"));
    assertTrue(list.contains("rule0:go3"));
    list.clear();
    ksession.retract(go3);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    ksession.retract(go2);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    ksession.retract(go1);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertTrue(list.contains("rule0:go0"));
    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 25 with KieBaseConfiguration

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

the class DeclarativeAgendaTest method testActiveInActiveChanges.

@Test(timeout = 10000)
public void testActiveInActiveChanges() {
    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) \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 @department(sales) \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 rule3 @department(sales) \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 countActivateInActive @activationListener('direct') \n";
    str += "when \n";
    str += "     $s : String( this == 'go2' ) \n";
    str += "     $active : Number( this == 1 ) from accumulate( $a : Match( department == 'sales', active == true ), count( $a ) )\n";
    str += "     $inActive : Number( this == 2 ) from  accumulate( $a : Match( department == 'sales', active == false ), count( $a ) )\n";
    str += "then \n";
    str += "    list.add( $active + ':' + $inActive  ); \n";
    str += "    kcontext.halt( ); \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");
    FactHandle go2 = ksession.insert("go2");
    ksession.fireAllRules();
    assertEquals(3, list.size());
    System.out.println(list);
    assertTrue(list.contains("1:2"));
    assertTrue(list.contains("rule1:go1"));
    assertTrue(list.contains("rule2:go1"));
    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)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)151 Test (org.junit.Test)131 KieSession (org.kie.api.runtime.KieSession)116 KieBase (org.kie.api.KieBase)99 ArrayList (java.util.ArrayList)76 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)57 List (java.util.List)50 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 StockTick (org.drools.compiler.StockTick)33 EntryPoint (org.kie.api.runtime.rule.EntryPoint)31 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)24 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)21 FactHandle (org.kie.api.runtime.rule.FactHandle)20 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)20 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)17 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)15 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)14 StockTickInterface (org.drools.compiler.StockTickInterface)13 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)13