use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class StreamsTest method testEventExpirationSetToZero.
@Test(timeout = 10000)
public void testEventExpirationSetToZero() {
final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick\n" + " @role( event )\n" + " @expires( 0 )\n" + "end\n" + "\n" + "rule X\n" + "when\n" + " StockTick()\n" + "then \n" + " // no-op\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("stream-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
ksession.addEventListener(wml);
final AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final PseudoClockScheduler clock = ksession.getSessionClock();
final StockTick st1 = new StockTick(1, "RHT", 100, 1000);
final StockTick st2 = new StockTick(2, "RHT", 100, 1000);
ksession.insert(st1);
ksession.insert(st2);
assertThat(ksession.fireAllRules(), equalTo(2));
verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
assertThat(ksession.getObjects().size(), equalTo(2));
assertThat((Collection<Object>) ksession.getObjects(), CoreMatchers.hasItems(st1, st2));
clock.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertThat(ksession.getObjects().size(), equalTo(0));
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testTemporalOperators.
@Test(timeout = 10000)
public void testTemporalOperators() {
final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick \n" + " @role( event )\n" + " @timestamp( time )\n" + "end\n" + "\n" + "rule \"2 operators\"\n" + "when\n" + " $a : StockTick( company == \"A\" )\n" + " not( StockTick( company == \"B\", this after[0,20s] $a ) )\n" + " not( StockTick( company == \"C\", this after[0,20s] $a ) )\n" + "then\n" + " // do something\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
ksession.insert(new StockTick(1, "A", 10, 1000));
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testDuplicateFiring1.
@Test
public void testDuplicateFiring1() {
final String drl = "package org.test;\n" + "import " + StockTick.class.getCanonicalName() + ";\n " + "" + "global java.util.List list \n" + "" + "declare StockTick @role(event) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + " accumulate ( $e: StockTick() over window:time(300ms) from entry-point SensorEventStream, " + " $n : count( $e );" + " $n > 0 )\n" + "then\n" + " list.add( $n ); \n" + " System.out.println( \"Events in last 3 seconds: \" + $n );\n" + "end" + "" + "\n" + "rule \"timerRuleAfterAllEvents\"\n" + " timer ( int: 2s )\n" + "when\n" + " $room : String( )\n" + "then\n" + " list.add( -1 ); \n" + " System.out.println(\"2sec after room was modified\");\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);
// entry point for sensor events
final EntryPoint sensorEventStream = ksession.getEntryPoint("SensorEventStream");
ksession.insert("Go");
// insert events
for (int i = 2; i < 8; i++) {
final StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
sensorEventStream.insert(event);
ksession.fireAllRules();
clock.advanceTime(105, TimeUnit.MILLISECONDS);
}
// let thread sleep for another 1m to see if dereffered rules fire (timers, (not) after rules)
clock.advanceTime(100 * 40, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(Arrays.asList(1L, 2L, 3L, 3L, 3L, 3L, -1), list);
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testSlidingWindowsAccumulateExternalJoin.
@Test(timeout = 10000)
public void testSlidingWindowsAccumulateExternalJoin() {
// DROOLS-106
// The logic may not be optimal, but was used to detect a WM corruption
final String drl = "package testing2;\n" + "\n" + "import java.util.*;\n" + "import " + StockTick.class.getCanonicalName() + "\n" + "" + "global List list;\n" + "" + "declare StockTick\n" + " @role( event )\n" + " @duration( duration )\n" + "end\n" + "\n" + "rule test\n" + "when\n" + " $primary : StockTick( $name : company ) over window:length(1)\n" + " accumulate ( " + " $tick : StockTick( company == $name ) , " + " $num : count( $tick ) )\n" + "then\n" + " list.add( $num.intValue() ); \n" + "end\n" + "";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
int seq = 0;
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new StockTick(seq++, "AAA", 10.0, 10L));
ksession.fireAllRules();
assertEquals(list, Collections.singletonList(1));
ksession.insert(new StockTick(seq++, "AAA", 15.0, 10L));
ksession.fireAllRules();
assertEquals(list, Arrays.asList(1, 2));
ksession.insert(new StockTick(seq++, "CCC", 10.0, 10L));
ksession.fireAllRules();
assertEquals(list, Arrays.asList(1, 2, 1));
ksession.insert(new StockTick(seq++, "DDD", 13.0, 20L));
ksession.fireAllRules();
assertEquals(list, Arrays.asList(1, 2, 1, 1));
ksession.insert(new StockTick(seq, "AAA", 11.0, 20L));
ksession.fireAllRules();
assertEquals(list, Arrays.asList(1, 2, 1, 1, 3));
// NPE Here
ksession.fireAllRules();
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testBeforeOperator.
@Test(timeout = 10000)
public void testBeforeOperator() {
final KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources("cep-esp-test", kieBaseTestConfiguration, "org/drools/compiler/integrationtests/test_CEP_BeforeOperator.drl");
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final PseudoClockScheduler clock = ksession.getSessionClock();
clock.setStartupTime(1000);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final StockTick tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
final StockTick tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
final StockTick tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
final StockTick tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
final StockTick tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
final StockTick tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
final StockTick tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
final StockTick tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
ksession.insert(tick1);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick2);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick3);
clock.advanceTime(4, TimeUnit.MILLISECONDS);
ksession.insert(tick4);
ksession.insert(tick5);
clock.advanceTime(1, TimeUnit.MILLISECONDS);
ksession.insert(tick6);
ksession.insert(tick7);
clock.advanceTime(2, TimeUnit.MILLISECONDS);
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();
}
}
Aggregations