use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testIdleTime.
@Test(timeout = 10000)
public void testIdleTime() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleEventAssertion.drl");
final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final SessionPseudoClock clock = session.getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
final StockTick tick1 = new StockTick(1, "DROO", 50, 10000);
final StockTick tick2 = new StockTick(2, "ACME", 10, 10010);
final StockTick tick3 = new StockTick(3, "ACME", 10, 10100);
final StockTick tick4 = new StockTick(4, "DROO", 50, 11000);
assertEquals(0, ((InternalWorkingMemory) session).getIdleTime());
final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
assertEquals(10000, ((InternalWorkingMemory) session).getIdleTime());
final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
assertEquals(0, ((InternalWorkingMemory) session).getIdleTime());
clock.advanceTime(15, TimeUnit.SECONDS);
assertEquals(15000, ((InternalWorkingMemory) session).getIdleTime());
clock.advanceTime(15, TimeUnit.SECONDS);
assertEquals(30000, ((InternalWorkingMemory) session).getIdleTime());
final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
assertEquals(0, ((InternalWorkingMemory) session).getIdleTime());
clock.advanceTime(20, TimeUnit.SECONDS);
final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
assertEquals(10000, ((InternalWorkingMemory) session).getIdleTime());
session.fireAllRules();
assertEquals(0, ((InternalWorkingMemory) session).getIdleTime());
assertEquals(2, ((List) session.getGlobal("results")).size());
} finally {
session.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testAnnotatedEventAssertion.
@Test(timeout = 10000)
public void testAnnotatedEventAssertion() {
final String drl = "package org.drools.compiler.test;\n" + "\n" + "import " + StockTickEvent.class.getCanonicalName() + ";\n" + "\n" + "global java.util.List results;\n" + "\n" + "rule \"Check event\"\n" + "when\n" + " $st : StockTickEvent( company == \"ACME\" )\n" + "then\n" + " results.add( $st );\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final SessionPseudoClock clock = session.getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
final StockTick tick1 = new StockTickEvent(1, "DROO", 50, 10000);
final StockTick tick2 = new StockTickEvent(2, "ACME", 10, 10010);
final StockTick tick3 = new StockTickEvent(3, "ACME", 10, 10100);
final StockTick tick4 = new StockTickEvent(4, "DROO", 50, 11000);
final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
clock.advanceTime(30, TimeUnit.SECONDS);
final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
clock.advanceTime(20, TimeUnit.SECONDS);
final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(2, ((List) session.getGlobal("results")).size());
} finally {
session.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testEventAssertion.
@Test(timeout = 10000)
public void testEventAssertion() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleEventAssertion.drl");
final KieSession session = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final SessionPseudoClock clock = session.getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
final StockTick tick1 = new StockTick(1, "DROO", 50, 10000);
final StockTick tick2 = new StockTick(2, "ACME", 10, 10010);
final StockTick tick3 = new StockTick(3, "ACME", 10, 10100);
final StockTick tick4 = new StockTick(4, "DROO", 50, 11000);
final InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
final InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
clock.advanceTime(30, TimeUnit.SECONDS);
final InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
clock.advanceTime(20, TimeUnit.SECONDS);
final InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(2, ((List) session.getGlobal("results")).size());
} finally {
session.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testSalienceWithEventsPseudoClock.
@Test(timeout = 10000)
public void testSalienceWithEventsPseudoClock() {
final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getName() + "\n" + "declare StockTick\n" + " @role ( event )\n" + "end\n" + "rule R1 salience 1000\n" + " when\n" + " $s1 : StockTick( company == 'RHT' )\n" + " $s2 : StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + " then\n" + "end\n" + "rule R2 salience 1000\n" + " when\n" + " $s1 : StockTick( company == 'RHT' )\n" + " not StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + " then\n" + "end\n" + "rule R3 salience 100\n" + " when\n" + " $s2 : StockTick( company == 'ACME' )\n" + " then\n" + "end\n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(1000000, TimeUnit.MILLISECONDS);
ksession.insert(new StockTick(1, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(2, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(3, "RHT", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new StockTick(4, "ACME", 10, 1000));
clock.advanceTime(5, TimeUnit.SECONDS);
final int rulesFired = ksession.fireAllRules();
assertEquals(4, rulesFired);
final ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(4)).afterMatchFired(captor.capture());
final List<AfterMatchFiredEvent> aafe = captor.getAllValues();
assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(1).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(2).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(3).getMatch().getRule().getName(), is("R3"));
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testDeleteExpiredEventWithTimestampAndEqualityKey.
@Test
public void testDeleteExpiredEventWithTimestampAndEqualityKey() {
// DROOLS-1017
final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + " @role( event )\n" + " @timestamp( time )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "when\n" + " $st1 : StockTick( company == \"ACME\" )\n" + " not ( StockTick( this != $st1, this after[0s, 1s] $st1) )\n" + "then\n" + " delete($st1);\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final PseudoClockScheduler clock = ksession.getSessionClock();
clock.setStartupTime(5000L);
final EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50, 0L));
clock.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
} finally {
ksession.dispose();
}
}
Aggregations