Search in sources :

Example 41 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class CepEspTest method testUseMapAsEvent.

@Test
public void testUseMapAsEvent() {
    // DROOLS-753
    String drl = "import java.util.Map\n " + "declare Map \n" + "  @role(event)\n" + "end\n" + "rule \"sliding window time map\" \n" + "when \n" + "   $m:Map()\n" + "   accumulate(Map() over window:time( 1m ); $count:count(); $count>1 )\n" + "then \n" + "    System.out.println(\"alarm!!!!\");  \n" + "end \n";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    ksession.insert(new HashMap<String, Object>());
    ksession.fireAllRules();
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 42 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class CepEspTest method testDelayingNotWithPreEpochClock.

@Test(timeout = 10000)
public void testDelayingNotWithPreEpochClock() throws Exception {
    String str = "package org.drools.compiler\n" + "declare A @role(event) symbol : String end\n" + "declare B @role(event) symbol : String end\n" + "rule Setup when\n" + "then\n" + "    insert( new A() );\n" + "end\n" + "rule X\n" + "when\n" + "    $a : A() and not( B( this after $a ) )\n" + "then\n" + "end\n";
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(conf, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    // Getting a pre-epoch date (i.e., before 1970)
    Calendar ts = Calendar.getInstance();
    ts.set(1900, 1, 1);
    // Initializing the clock to that date
    SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(ts.getTimeInMillis(), TimeUnit.MILLISECONDS);
    // rule X should not be delayed as the delay would be infinite
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) Calendar(java.util.Calendar) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 43 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class CepEspTest method testCancelActivationWithExpiredEvent.

@Test
public void testCancelActivationWithExpiredEvent() {
    // RHBRMS-2463
    String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "import " + AtomicInteger.class.getCanonicalName() + "\n" + "declare MyEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 10ms )\n" + "end\n" + "\n" + "rule R\n" + "    timer (int: 0 1; start=$startTime, repeat-limit=0 )\n" + "    when\n" + "       $event: MyEvent ($startTime : timestamp)\n" + "       $counter : AtomicInteger(get() > 0)\n" + "    then\n" + "        System.out.println(\"RG_TEST_TIMER WITH \" + $event + \" AND \" + $counter);\n" + "        modify($counter){\n" + "            decrementAndGet()\n" + "        }\n" + "end";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    long now = System.currentTimeMillis();
    PseudoClockScheduler sessionClock = ksession.getSessionClock();
    sessionClock.setStartupTime(now - 10000);
    AtomicInteger counter = new AtomicInteger(1);
    MyEvent event1 = new MyEvent(now - 8000);
    MyEvent event2 = new MyEvent(now - 7000);
    MyEvent event3 = new MyEvent(now - 6000);
    ksession.insert(counter);
    ksession.insert(event1);
    ksession.insert(event2);
    ksession.insert(event3);
    // Nothing Happens
    ksession.fireAllRules();
    assertEquals(1, counter.get());
    sessionClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(0, counter.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 44 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class CepEspTest method testExpireLogicallyInsertedEvent.

@Test
public void testExpireLogicallyInsertedEvent() {
    // RHBRMS-2515
    String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "declare MyEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 10ms )\n" + "end\n" + "\n" + "rule R when\n" + "  $e : MyEvent()\n" + "then\n" + "  insertLogical($e.toString());\n" + "end";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler sessionClock = ksession.getSessionClock();
    sessionClock.setStartupTime(0);
    ksession.insert(new MyEvent(0));
    assertEquals(1L, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(2L, ksession.getFactCount());
    sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(0L, ksession.getFactCount());
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 45 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class CepEspTest method testCEPWith2NamedConsAndEagerRule.

@Test
public void testCEPWith2NamedConsAndEagerRule() throws InterruptedException {
    String drl = "package org.drools " + "global java.util.List list; " + "declare  Msg " + "    @role( event ) " + "    sender : String  @key " + "end " + "rule Init1 " + "when " + "  $s : String() " + "then " + "  insert( new Msg( $s ) ); " + "end " + "rule Init2 " + "when " + "  Msg( 'Alice' ; )\n" + "then " + "  insert( 42 ); " + "end " + "rule 'Viol' @Propagation(EAGER) when " + "    $trigger : Msg( 'Alice' ; )\n" + "    not Msg( 'Bob' ; this after[0, 100ms] $trigger ) do[t1]" + "    Integer( ) do[t2]\n" + "then\n" + "  list.add( 0 );\n" + "then[t1]\n" + "  list.add( 1 );\n" + "then[t2]\n" + "  list.add( 2 );\n" + "end\n";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieSession ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert("Alice");
    ksession.fireAllRules();
    assertTrue(list.isEmpty());
    ((PseudoClockScheduler) ksession.getSessionClock()).advanceTime(150, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(3, list.size());
    assertTrue(list.contains(0));
    assertTrue(list.contains(1));
    assertTrue(list.contains(2));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Aggregations

KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)188 KieSession (org.kie.api.runtime.KieSession)166 Test (org.junit.Test)157 KieBase (org.kie.api.KieBase)122 ArrayList (java.util.ArrayList)96 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)71 KieHelper (org.kie.internal.utils.KieHelper)68 List (java.util.List)59 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)57 StockTick (org.drools.compiler.StockTick)40 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)39 EntryPoint (org.kie.api.runtime.rule.EntryPoint)28 Arrays.asList (java.util.Arrays.asList)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)25 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)22 Date (java.util.Date)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 StockTickInterface (org.drools.compiler.StockTickInterface)18 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)16 SessionConfiguration (org.drools.core.SessionConfiguration)15