use of org.drools.testcoverage.common.model.OrderEvent in project drools by kiegroup.
the class CepEspTest method testTimeAndLengthWindowConflict.
@Test(timeout = 10000)
public void testTimeAndLengthWindowConflict() {
// JBRULES-3671
final String drl = "package org.drools.compiler;\n" + "import java.util.List\n" + "import " + OrderEvent.class.getCanonicalName() + "\n" + "\n" + "global List timeResults;\n" + "global List lengthResults;\n" + "\n" + "declare OrderEvent\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " $list : List( empty == false ) from collect(\n" + " $o : OrderEvent() over window:time(30s) )\n" + "then\n" + " timeResults.add( $list.size() );\n" + "end\n" + "\n" + "rule \"collect with length window\"\n" + "when\n" + " accumulate (\n" + " $o : OrderEvent( $tot : total ) over window:length(3)," + " $avg : average( $tot ) )\n" + "then\n" + " lengthResults.add( $avg );\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 List<Number> timeResults = new ArrayList<>();
final List<Number> lengthResults = new ArrayList<>();
ksession.setGlobal("timeResults", timeResults);
ksession.setGlobal("lengthResults", lengthResults);
final SessionPseudoClock clock = ksession.getSessionClock();
// 5 seconds
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("1", "customer A", 70));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(70.0));
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("2", "customer A", 60));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(65.0));
// Third interaction: advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("3", "customer A", 50));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(60.0));
// Fourth interaction: advance clock and assert new data
// 60 seconds
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("4", "customer A", 25));
ksession.fireAllRules();
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.OrderEvent in project drools by kiegroup.
the class CepEspTest method testSimpleTimeWindow.
@Test(timeout = 10000)
public void testSimpleTimeWindow() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleTimeWindow.drl");
final KieSession wm = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final List results = new ArrayList();
wm.setGlobal("results", results);
final SessionPseudoClock clock = wm.getSessionClock();
// 5 seconds
clock.advanceTime(5, TimeUnit.SECONDS);
final EventFactHandle handle1 = (EventFactHandle) wm.insert(new OrderEvent("1", "customer A", 70));
assertEquals(5000, handle1.getStartTimestamp());
assertEquals(0, handle1.getDuration());
wm.fireAllRules();
assertEquals(1, results.size());
assertEquals(70, ((Number) results.get(0)).intValue());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
final EventFactHandle handle2 = (EventFactHandle) wm.insert(new OrderEvent("2", "customer A", 60));
assertEquals(15000, handle2.getStartTimestamp());
assertEquals(0, handle2.getDuration());
wm.fireAllRules();
assertEquals(2, results.size());
assertEquals(65, ((Number) results.get(1)).intValue());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
final EventFactHandle handle3 = (EventFactHandle) wm.insert(new OrderEvent("3", "customer A", 50));
assertEquals(25000, handle3.getStartTimestamp());
assertEquals(0, handle3.getDuration());
wm.fireAllRules();
assertEquals(3, results.size());
assertEquals(60, ((Number) results.get(2)).intValue());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
final EventFactHandle handle4 = (EventFactHandle) wm.insert(new OrderEvent("4", "customer A", 25));
assertEquals(35000, handle4.getStartTimestamp());
assertEquals(0, handle4.getDuration());
wm.fireAllRules();
// first event should have expired, making average under the rule threshold, so no additional rule fire
assertEquals(3, results.size());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
final EventFactHandle handle5 = (EventFactHandle) wm.insert(new OrderEvent("5", "customer A", 70));
assertEquals(45000, handle5.getStartTimestamp());
assertEquals(0, handle5.getDuration());
wm.fireAllRules();
// still under the threshold, so no fire
assertEquals(3, results.size());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
final EventFactHandle handle6 = (EventFactHandle) wm.insert(new OrderEvent("6", "customer A", 115));
assertEquals(55000, handle6.getStartTimestamp());
assertEquals(0, handle6.getDuration());
wm.fireAllRules();
assertEquals(4, results.size());
assertEquals(70, ((Number) results.get(3)).intValue());
} finally {
wm.dispose();
}
}
use of org.drools.testcoverage.common.model.OrderEvent in project drools by kiegroup.
the class CepEspTest method testSimpleLengthWindowWithQueue.
@Test(timeout = 10000)
public void testSimpleLengthWindowWithQueue() throws Exception {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleLengthWindow.drl");
KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final List results = new ArrayList();
ksession.setGlobal("results", results);
ksession.insert(new OrderEvent("1", "customer A", 80));
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
// assert new data
ksession.insert(new OrderEvent("2", "customer A", 70));
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
// assert new data
ksession.insert(new OrderEvent("3", "customer A", 60));
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
// assert new data
ksession.insert(new OrderEvent("4", "customer A", 50));
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(1, results.size());
assertEquals(60, ((Number) results.get(0)).intValue());
// assert new data
ksession.insert(new OrderEvent("5", "customer A", 10));
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(1, results.size());
ksession.insert(new OrderEvent("6", "customer A", 90));
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
ksession.fireAllRules();
assertEquals(2, results.size());
assertEquals(50, ((Number) results.get(1)).intValue());
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.OrderEvent in project drools by kiegroup.
the class CepEspTest method testSimpleLengthWindow.
@Test(timeout = 10000)
public void testSimpleLengthWindow() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleLengthWindow.drl");
final KieSession wm = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final List results = new ArrayList();
wm.setGlobal("results", results);
wm.insert(new OrderEvent("1", "customer A", 70));
wm.fireAllRules();
assertEquals(1, results.size());
assertEquals(70, ((Number) results.get(0)).intValue());
// assert new data
wm.insert(new OrderEvent("2", "customer A", 60));
wm.fireAllRules();
assertEquals(2, results.size());
assertEquals(65, ((Number) results.get(1)).intValue());
// assert new data
wm.insert(new OrderEvent("3", "customer A", 50));
wm.fireAllRules();
assertEquals(3, results.size());
assertEquals(60, ((Number) results.get(2)).intValue());
// assert new data
wm.insert(new OrderEvent("4", "customer A", 25));
wm.fireAllRules();
// first event should have expired, making average under the rule threshold, so no additional rule fire
assertEquals(3, results.size());
// assert new data
wm.insert(new OrderEvent("5", "customer A", 70));
wm.fireAllRules();
// still under the threshold, so no fire
assertEquals(3, results.size());
// assert new data
wm.insert(new OrderEvent("6", "customer A", 115));
wm.fireAllRules();
assertEquals(4, results.size());
assertEquals(70, ((Number) results.get(3)).intValue());
} finally {
wm.dispose();
}
}
use of org.drools.testcoverage.common.model.OrderEvent in project drools by kiegroup.
the class CepEspTest method testIdleTimeAndTimeToNextJob.
@Test(timeout = 10000)
public void testIdleTimeAndTimeToNextJob() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_SimpleTimeWindow.drl");
final StatefulKnowledgeSessionImpl wm = (StatefulKnowledgeSessionImpl) kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final WorkingMemoryFileLogger logger = new WorkingMemoryFileLogger((WorkingMemory) wm);
final File testTmpDir = new File("target/test-tmp/");
testTmpDir.mkdirs();
logger.setFileName("target/test-tmp/testIdleTimeAndTimeToNextJob-audit");
try {
final List results = new ArrayList();
wm.setGlobal("results", results);
// how to initialize the clock?
// how to configure the clock?
final SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
// 5 seconds
clock.advanceTime(5, TimeUnit.SECONDS);
// there is no next job, so returns -1
assertEquals(-1, wm.getTimeToNextJob());
wm.insert(new OrderEvent("1", "customer A", 70));
wm.fireAllRules();
assertEquals(0, wm.getIdleTime());
// now, there is a next job in 30 seconds: expire the event
assertEquals(30000, wm.getTimeToNextJob());
wm.fireAllRules();
assertEquals(1, results.size());
assertEquals(70, ((Number) results.get(0)).intValue());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
// next job is in 20 seconds: expire the event
assertEquals(20000, wm.getTimeToNextJob());
wm.insert(new OrderEvent("2", "customer A", 60));
wm.fireAllRules();
assertEquals(2, results.size());
assertEquals(65, ((Number) results.get(1)).intValue());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
// next job is in 10 seconds: expire the event
assertEquals(10000, wm.getTimeToNextJob());
wm.insert(new OrderEvent("3", "customer A", 50));
wm.fireAllRules();
assertEquals(3, results.size());
assertEquals(60, ((Number) results.get(2)).intValue());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
// advancing clock time will cause events to expire
assertEquals(0, wm.getIdleTime());
// next job is in 10 seconds: expire another event
// assertEquals( 10000, iwm.getTimeToNextJob());
wm.insert(new OrderEvent("4", "customer A", 25));
wm.fireAllRules();
// first event should have expired, making average under the rule threshold, so no additional rule fire
assertEquals(3, results.size());
// advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
wm.insert(new OrderEvent("5", "customer A", 70));
assertEquals(0, wm.getIdleTime());
// wm = SerializationHelper.serializeObject(wm);
wm.fireAllRules();
// still under the threshold, so no fire
assertEquals(3, results.size());
} finally {
logger.writeToDisk();
}
} finally {
wm.dispose();
}
}
Aggregations