Search in sources :

Example 46 with PseudoClockScheduler

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

the class ExpirationTest method testBetaLeftExpired2.

@Test
public void testBetaLeftExpired2() {
    // DROOLS-1329
    String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "import " + C.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(31ms) end\n" + "declare B @role( event ) @expires(11ms) end\n" + "declare C @role( event ) @expires(31ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + "  $a: A( $Aid: id > 0 )\n" + "  $b: B( $Bid: id == $Aid )\n" + "  $c: C( id == $Bid )\n" + "then\n" + "  System.out.println(\"[\" + $a + \",\" + $b + \",\" + $c + \"]\");" + "  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();
    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal("counter", counter);
    ksession.insert(new A(1));
    ksession.insert(new B(1));
    sessionClock.advanceTime(20, TimeUnit.MILLISECONDS);
    ksession.insert(new C(1));
    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 47 with PseudoClockScheduler

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

the class CepEspTest method test2NotsWithTemporalConstraints.

@Test
public void test2NotsWithTemporalConstraints() {
    // BZ-1122738 DROOLS-479
    String drl = "import " + SimpleEvent.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "\n" + "declare OtherFact\n" + "    @role( event )\n" + "end\n" + "\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( dateEvt )\n" + "end\n" + "\n" + "\n" + "rule R\n" + "    when\n" + "        $e : SimpleEvent()\n" + "        not OtherFact( this after[0, 1h] $e )\n" + "        not OtherFact( this after[0, 1h] $e )\n" + "    then\n" + "        $e.setCode(\"code2\");\n" + "    end\n " + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    baseConfig.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    final KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler clock = ksession.getSessionClock();
    clock.setStartupTime(System.currentTimeMillis());
    SimpleEvent event = new SimpleEvent("code1");
    event.setDateEvt(System.currentTimeMillis() - (2 * 60 * 60 * 1000));
    ksession.insert(event);
    ksession.fireAllRules();
    assertEquals("code2", event.getCode());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 48 with PseudoClockScheduler

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

the class CepEspTest method test2TimersWithNamedCons.

@Test
public void test2TimersWithNamedCons() 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 'Viol' when " + "    $trigger : Msg( 'Alice' ; )\n" + "    not Msg( 'Bob' ; this after[0, 100ms] $trigger ) do[t1]\n" + "    not Msg( 'Charles' ; this after[0, 200ms] $trigger )\n" + "then\n" + "  list.add( 0 );\n" + "then[t1]\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 49 with PseudoClockScheduler

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

the class CepEspTest method testExpireUnusedDeclaredTypeClass.

@Test
public void testExpireUnusedDeclaredTypeClass() throws Exception {
    // DROOLS-1524
    String drl = "rule R when\n" + "then\n" + "  System.out.println(\"fired\");\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);
    PseudoClockScheduler sessionClock = ksession.getSessionClock();
    ksession.insert(new EventWithoutRule());
    ksession.fireAllRules();
    sessionClock.advanceTime(2, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, ksession.getFactCount());
    ksession.dispose();
}
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 50 with PseudoClockScheduler

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

the class CepEspTest method checkNoExpiration.

private void checkNoExpiration(String lhs) {
    String drl = "import " + SimpleEvent.class.getCanonicalName() + "\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @expires( -1 )\n" + "    @timestamp( dateEvt )\n" + "end\n" + "\n" + "rule R when\n" + lhs + "then\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 clock = ksession.getSessionClock();
    SimpleEvent event1 = new SimpleEvent("1", 0L);
    ksession.insert(event1);
    ksession.fireAllRules();
    // Session should only contain the fact we just inserted.
    assertEquals(1, ksession.getFactCount());
    clock.advanceTime(60000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // We've disabled expiration, so fact should still be in WorkingMemory.
    assertEquals(1, ksession.getFactCount());
    ksession.dispose();
}
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)

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