Search in sources :

Example 26 with KieSessionConfiguration

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

the class ExpirationTest method testSoftExpirationWithDeclaration.

@Test
public void testSoftExpirationWithDeclaration() {
    // DROOLS-1483
    String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "import " + C.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(10s) end\n" + "declare B @role( event ) @expires(value = 30s, policy = TIME_SOFT) end\n" + "declare C @role( event ) @expires(value = 30s, policy = TIME_SOFT) end\n" + "rule Ra when\n" + "  $e : A() over window:time(20s)\n" + "then end\n " + "rule Rb when\n" + "  $e : B() over window:time(20s)\n" + "then end\n " + "rule Rc when\n" + "  $e : C()\n" + "then 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 clock = ksession.getSessionClock();
    ksession.insert(new A(1));
    ksession.insert(new B(2));
    ksession.insert(new C(3));
    ksession.fireAllRules();
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(A.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(B.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(C.class)).size());
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // t=15 -> hard expiration of A
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(A.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(B.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(C.class)).size());
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // t=25 -> implicit expiration of B
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(A.class)).size());
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(B.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(C.class)).size());
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // t=35 -> soft expiration of C
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(A.class)).size());
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(B.class)).size());
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(C.class)).size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) 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 27 with KieSessionConfiguration

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

the class ExpirationTest method testEventsExpiredInThePast.

private void testEventsExpiredInThePast(final String drl) {
    final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    final KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    final KieBase kieBase = helper.build(EventProcessingOption.STREAM);
    final KieSession kieSession = kieBase.newKieSession(sessionConfig, null);
    PseudoClockScheduler clock = kieSession.getSessionClock();
    final long currentTime = clock.getCurrentTime();
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    kieSession.insert(new BasicEvent(new Date(currentTime + 20), 10L, "20ms-30ms"));
    clock.advanceTime(1, TimeUnit.MILLISECONDS);
    kieSession.insert(new BasicEvent(new Date(currentTime + 20), 20L, "20ms-40ms"));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    Assertions.assertThat(kieSession.fireAllRules()).isEqualTo(1);
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    Assertions.assertThat(kieSession.getObjects()).isEmpty();
}
Also used : KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) BasicEvent(org.drools.compiler.integrationtests.facts.BasicEvent) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date)

Example 28 with KieSessionConfiguration

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

the class ExpirationTest method testSoftExpiration.

@Test
public void testSoftExpiration() {
    // DROOLS-1483
    String drl = "import " + ExpiringEventA.class.getCanonicalName() + "\n" + "import " + ExpiringEventB.class.getCanonicalName() + "\n" + "import " + ExpiringEventC.class.getCanonicalName() + "\n" + "rule Ra when\n" + "  $e : ExpiringEventA() over window:time(20s)\n" + "then end\n " + "rule Rb when\n" + "  $e : ExpiringEventB() over window:time(20s)\n" + "then end\n " + "rule Rc when\n" + "  $e : ExpiringEventC()\n" + "then 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 clock = ksession.getSessionClock();
    ksession.insert(new ExpiringEventA());
    ksession.insert(new ExpiringEventB());
    ksession.insert(new ExpiringEventC());
    ksession.fireAllRules();
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // t=15 -> hard expiration of A
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // t=25 -> implicit expiration of B
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
    assertEquals(1, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // t=35 -> soft expiration of C
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventA.class)).size());
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventB.class)).size());
    assertEquals(0, ksession.getObjects(new ClassObjectFilter(ExpiringEventC.class)).size());
}
Also used : ClassObjectFilter(org.drools.core.ClassObjectFilter) 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 29 with KieSessionConfiguration

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

the class ExpirationTest method testAlpha.

@Test
public void testAlpha() {
    String drl = "import " + A.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "global java.util.concurrent.atomic.AtomicInteger counter;\n" + "rule R0 when\n" + "  $a: A( $Aid: id > 0 )\n" + "then\n" + "  System.out.println(\"[\" + $a + \"]\");" + "  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));
    sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.insert(new A(2));
    sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(2, 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 30 with KieSessionConfiguration

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

the class ExpirationTest method testBeta.

@Test
public void testBeta() {
    // DROOLS-1329
    String drl = "import " + A.class.getCanonicalName() + "\n" + "import " + B.class.getCanonicalName() + "\n" + "declare A @role( event ) @expires(11ms) end\n" + "declare B @role( event ) @expires(11ms) 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) && (id > ($Aid - 1 )))\n" + "then\n" + "  System.out.println(\"[\" + $a + \",\" + $b + \"]\");" + "  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(10, TimeUnit.MILLISECONDS);
    ksession.insert(new A(2));
    ksession.insert(new B(2));
    sessionClock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(2, 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)

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