Search in sources :

Example 36 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testRightTupleExpiration.

@Test
public void testRightTupleExpiration() {
    // RHBRMS-2463
    String drl = "import " + MyEvent.class.getCanonicalName() + "\n" + "import " + AtomicInteger.class.getCanonicalName() + "\n" + "global AtomicInteger counter;\n" + "declare MyEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 10ms )\n" + "end\n" + "\n" + "rule R when\n" + "       String()\n" + "       MyEvent()\n" + "       Boolean()\n" + "       Integer()\n" + "    then\n" + "       counter.incrementAndGet();\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);
    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal("counter", counter);
    ksession.insert("test");
    ksession.insert(true);
    ksession.insert(new MyEvent(0));
    ksession.insert(new MyEvent(15));
    ksession.fireAllRules();
    assertEquals(0, counter.get());
    sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
    ksession.insert(1);
    // MyEvent is expired
    ksession.fireAllRules();
    assertEquals(1, 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 37 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testComplexOperator.

@Test(timeout = 10000)
public void testComplexOperator() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_ComplexOperator.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, sconf);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    clock.setStartupTime(1000);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 0, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 4, 3);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, 8, 3);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, 12, 5);
    StockTickInterface tick5 = new StockTick(5, "ACME", 10, 12, 5);
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, 13, 3);
    StockTickInterface tick7 = new StockTick(7, "ACME", 10, 13, 5);
    StockTickInterface tick8 = new StockTick(8, "ACME", 10, 15, 3);
    ksession.insert(tick1);
    ksession.insert(tick2);
    ksession.insert(tick3);
    ksession.insert(tick4);
    ksession.insert(tick5);
    ksession.insert(tick6);
    ksession.insert(tick7);
    ksession.insert(tick8);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    StockTick[] stocks = (StockTick[]) list.get(0);
    assertSame(tick4, stocks[0]);
    assertSame(tick2, stocks[1]);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 38 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testSerializationWithEventInPast.

@Test
public void testSerializationWithEventInPast() {
    // DROOLS-749
    String drl = "import " + Event1.class.getCanonicalName() + "\n" + "declare Event1\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 3h )\n" + "end\n" + "\n" + "rule R\n" + "    when\n" + "       $evt: Event1()\n" + "       not Event1(this != $evt, this after[0, 1h] $evt)\n" + "    then\n" + "       System.out.println($evt.getCode());\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);
    ksession.insert(new Event1("id1", 0));
    PseudoClockScheduler clock = ksession.getSessionClock();
    clock.advanceTime(2, TimeUnit.HOURS);
    ksession.fireAllRules();
    ksession = marshallAndUnmarshall(KieServices.Factory.get(), kbase, ksession, sessionConfig);
    ksession.insert(new Event1("id2", 0));
    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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 39 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method test2TimersWith2Rules.

@Test
public void test2TimersWith2Rules() throws InterruptedException {
    String drl = "package org.drools " + "global java.util.List list; " + "declare  Msg " + "    @role( event ) " + "    sender : String  @key " + "end " + "rule Init " + "when " + "  $s : String() " + "then " + "  insert( new Msg( $s ) ); " + "end " + "rule 'Viol1' when " + "    $trigger : Msg( 'Alice' ; )\n" + "    not Msg( 'Bob' ; this after[0, 100ms] $trigger ) \n" + "    not Msg( 'Charles' ; this after[0, 200ms] $trigger )\n" + "then\n" + "  list.add( 0 );\n" + "end\n" + "rule 'Viol2' when " + "    $trigger : Msg( 'Alice' ; )\n" + "    not Msg( 'Bob' ; this after[0, 100ms] $trigger ) \n" + "then\n" + "  list.add( 1 );\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();
    assertEquals(1, list.size());
    assertEquals(1, (int) list.get(0));
}
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)

Example 40 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class CepEspTest method testExpireUnusedDeclaredTypeEvent.

@Test
public void testExpireUnusedDeclaredTypeEvent() {
    // DROOLS-1524
    String drl = "declare String @role( event ) @expires( 1s ) end\n" + "\n" + "rule R when\n" + "then\n" + "    System.out.println(\"fired\");\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();
    ksession.insert("test");
    ksession.fireAllRules();
    assertEquals(1, ksession.getFactCount());
    sessionClock.advanceTime(2, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, 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)

Aggregations

PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)81 KieSession (org.kie.api.runtime.KieSession)76 Test (org.junit.Test)75 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)72 KieBase (org.kie.api.KieBase)60 ArrayList (java.util.ArrayList)39 KieHelper (org.kie.internal.utils.KieHelper)39 List (java.util.List)29 Date (java.util.Date)19 Arrays.asList (java.util.Arrays.asList)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14 StockTick (org.drools.compiler.StockTick)13 DateFormat (java.text.DateFormat)9 SimpleDateFormat (java.text.SimpleDateFormat)9 Calendar (org.kie.api.time.Calendar)8 StockTickInterface (org.drools.compiler.StockTickInterface)7 EntryPoint (org.kie.api.runtime.rule.EntryPoint)7 IOException (java.io.IOException)5 FactA (org.drools.compiler.FactA)5