use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testDeserializationWithCompositeTrigger.
@Test
public void testDeserializationWithCompositeTrigger() {
final String drl = "package org.drools.test;\n" + "import " + StockTick.class.getCanonicalName() + "; \n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + " @role( event )\n" + " @expires( 1s )\n" + "end\n" + "\n" + "rule \"One\"\n" + "when\n" + " $event : StockTick( )\n" + " not StockTick( company == \"BBB\", this after[0,96h] $event )\n" + " not StockTick( company == \"CCC\", this after[0,96h] $event )\n" + "then\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSessionConfiguration kieSessionConfiguration = KieSessionTestConfiguration.STATEFUL_REALTIME.getKieSessionConfiguration();
kieSessionConfiguration.setOption(TimerJobFactoryOption.get("trackable"));
KieSession ks = kbase.newKieSession(kieSessionConfiguration, null);
try {
ks.insert(new StockTick(2, "AAA", 1.0, 0));
try {
ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
} catch (final Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
} finally {
ks.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testPastEventExipration.
@Test
public void testPastEventExipration() {
// DROOLS-257
final String drl = "package org.test;\n" + "import " + StockTick.class.getCanonicalName() + ";\n " + "" + "global java.util.List list; \n" + "" + "declare StockTick @role(event) @timestamp( time ) @expires( 200ms ) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + " accumulate ( $e: StockTick() over window:length(10), $n : count($e) )\n" + "then\n" + " list.add( $n ); \n" + " System.out.println( \"Events in last X seconds: \" + $n );\n" + "end" + "";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final SessionPseudoClock clock = ksession.getSessionClock();
final ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
final long now = 0;
final StockTick event1 = new StockTick(1, "XXX", 1.0, now);
final StockTick event2 = new StockTick(2, "XXX", 1.0, now + 240);
final StockTick event3 = new StockTick(2, "XXX", 1.0, now + 380);
final StockTick event4 = new StockTick(2, "XXX", 1.0, now + 500);
ksession.insert(event1);
ksession.insert(event2);
ksession.insert(event3);
ksession.insert(event4);
clock.advanceTime(220, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock.advanceTime(400, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(Arrays.asList(3L, 1L), list);
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testSerializationWithWindowLengthAndLiaSharing.
@Test
public void testSerializationWithWindowLengthAndLiaSharing() {
// DROOLS-953
final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "rule ReportLastEvent when\n" + " $e : StockTick() over window:length(1)\n" + "then\n" + " list.add($e.getCompany());\n" + "end\n" + "\n" + "rule ReportEventInserted when\n" + " $e : StockTick()\n" + "then\n" + " System.out.println(\"Event Insert : \" + $e);\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.insert(new StockTick(1, "ACME", 50));
ksession.insert(new StockTick(2, "DROO", 50));
ksession.insert(new StockTick(3, "JBPM", 50));
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("JBPM", list.get(0));
try {
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
} catch (final Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(0, list.size());
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testComplexOperator.
@Test(timeout = 10000)
public void testComplexOperator() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_ComplexOperator.drl");
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final List list = new ArrayList();
ksession.setGlobal("list", list);
final PseudoClockScheduler clock = ksession.getSessionClock();
clock.setStartupTime(1000);
final AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final StockTick tick1 = new StockTick(1, "DROO", 50, 0, 3);
final StockTick tick2 = new StockTick(2, "ACME", 10, 4, 3);
final StockTick tick3 = new StockTick(3, "ACME", 10, 8, 3);
final StockTick tick4 = new StockTick(4, "DROO", 50, 12, 5);
final StockTick tick5 = new StockTick(5, "ACME", 10, 12, 5);
final StockTick tick6 = new StockTick(6, "ACME", 10, 13, 3);
final StockTick tick7 = new StockTick(7, "ACME", 10, 13, 5);
final StockTick tick8 = new StockTick(8, "ACME", 10, 15, 3);
ksession.insert(tick1);
ksession.insert(tick2);
ksession.insert(tick3);
ksession.insert(tick4);
ksession.insert(tick5);
ksession.insert(tick6);
ksession.insert(tick7);
ksession.insert(tick8);
ksession.fireAllRules();
assertEquals(1, list.size());
final StockTick[] stocks = (StockTick[]) list.get(0);
assertSame(tick4, stocks[0]);
assertSame(tick2, stocks[1]);
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testEventExpiration4.
@Test(timeout = 10000)
public void testEventExpiration4() {
final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "declare StockTick \n" + " @role( event )\n" + " @expires( 10s )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "no-loop\n" + "when\n" + " $st1 : StockTick( company == \"ACME\" ) over window:time( 10s ) from entry-point \"Event Stream\"\n" + "then\n" + " results.add( $st1 );\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 EntryPoint eventStream = ksession.getEntryPoint("Event Stream");
final SessionPseudoClock clock = ksession.getSessionClock();
final List results = new ArrayList();
ksession.setGlobal("results", results);
final EventFactHandle handle1 = (EventFactHandle) eventStream.insert(new StockTick(1, "ACME", 50, System.currentTimeMillis(), 3));
ksession.fireAllRules();
clock.advanceTime(11, TimeUnit.SECONDS);
// clock.advance() will put the event expiration in the queue to be executed,
// but it has to wait for a "thread" to do that
// so we fire rules again here to get that
// alternative could run fireUntilHalt()
ksession.fireAllRules();
assertEquals(1, results.size());
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
} finally {
ksession.dispose();
}
}
Aggregations