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());
}
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();
}
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());
}
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());
}
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());
}
Aggregations