use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testSalienceWithEventsRealtimeClock.
@Test(timeout = 10000)
public void testSalienceWithEventsRealtimeClock() throws IOException, ClassNotFoundException, InterruptedException {
String str = "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";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.REALTIME_CLOCK.getId()));
KieSession ksession = kbase.newKieSession(ksconf, null);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.insert(new StockTick(1, "RHT", 10, 1000));
ksession.insert(new StockTick(2, "RHT", 10, 1000));
ksession.insert(new StockTick(3, "RHT", 10, 1000));
// sleep for 2 secs
Thread.currentThread().sleep(2000);
ksession.insert(new StockTick(4, "ACME", 10, 1000));
// sleep for 1 sec
Thread.currentThread().sleep(1000);
int rulesFired = ksession.fireAllRules();
assertEquals(4, rulesFired);
ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(4)).afterMatchFired(captor.capture());
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"));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testDeserializationWithExpiringEventAndAccumulate.
@Test
public void testDeserializationWithExpiringEventAndAccumulate() throws InterruptedException {
String drl = "package org.drools.test;\n" + "import org.drools.compiler.StockTick; \n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + " @role( event )\n" + " @expires( 1s )\n" + "end\n" + "\n" + "rule R\n" + "when\n" + " accumulate ( StockTick( company == \"BBB\", $p : price), " + " $sum : sum( $p );" + " $sum > 0 )\n" + "then\n" + " list.add( $sum ); \n" + "end";
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
KieSessionConfiguration knowledgeSessionConfiguration = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
KieBase kb = loadKnowledgeBaseFromString(kbconf, drl);
KieSession ks = kb.newKieSession(knowledgeSessionConfiguration, null);
ks.insert(new StockTick(1, "BBB", 1.0, 0));
Thread.sleep(1000);
ks.insert(new StockTick(2, "BBB", 2.0, 0));
Thread.sleep(100);
try {
ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true, false);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
List<Double> list = new ArrayList<Double>();
ks.setGlobal("list", list);
ks.fireAllRules();
ks.insert(new StockTick(3, "BBB", 3.0, 0));
ks.fireAllRules();
assertEquals(2, list.size());
assertEquals(Arrays.asList(2.0, 5.0), list);
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testEventAssertionWithDuration.
@Test(timeout = 10000)
public void testEventAssertionWithDuration() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertionWithDuration.drl");
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession session = createKnowledgeSession(kbase, conf);
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000, 5);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 11000, 10);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 12000, 8);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 13000, 7);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
EventFactHandle eh1 = (EventFactHandle) handle1;
EventFactHandle eh2 = (EventFactHandle) handle2;
EventFactHandle eh3 = (EventFactHandle) handle3;
EventFactHandle eh4 = (EventFactHandle) handle4;
assertEquals(tick1.getTime(), eh1.getStartTimestamp());
assertEquals(tick2.getTime(), eh2.getStartTimestamp());
assertEquals(tick3.getTime(), eh3.getStartTimestamp());
assertEquals(tick4.getTime(), eh4.getStartTimestamp());
assertEquals(tick1.getDuration(), eh1.getDuration());
assertEquals(tick2.getDuration(), eh2.getDuration());
assertEquals(tick3.getDuration(), eh3.getDuration());
assertEquals(tick4.getDuration(), eh4.getDuration());
session.fireAllRules();
assertEquals(2, results.size());
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testSlidingWindowsAccumulateExternalJoin.
@Test(timeout = 10000)
public void testSlidingWindowsAccumulateExternalJoin() throws Exception {
// DROOLS-106
// The logic may not be optimal, but was used to detect a WM corruption
String str = "package testing2;\n" + "\n" + "import java.util.*;\n" + "import org.drools.compiler.StockTick;\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" + " System.out.println(\"Found name: \" + $primary + \" with \" +$num );\n" + " list.add( $num.intValue() ); \n" + "end\n" + "";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = kbase.newKieSession(conf, null);
int seq = 0;
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new StockTick(seq++, "AAA", 10.0, 10L));
ksession.fireAllRules();
assertEquals(list, Arrays.asList(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));
System.out.println(" ___________________________________- ");
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();
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testDeserializationWithTrackableTimerJob.
@Test
public void testDeserializationWithTrackableTimerJob() throws InterruptedException {
String drl = "package org.drools.test;\n" + "import org.drools.compiler.StockTick; \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" + " StockTick( $id : seq, company == \"AAA\" ) over window:time( 1s )\n" + "then\n" + " list.add( $id ); \n" + "end\n" + "\n" + "rule \"Two\"\n" + "when\n" + " StockTick( $id : seq, company == \"BBB\" ) \n" + "then\n" + " System.out.println( $id ); \n" + " list.add( $id );\n" + "end";
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
KieSessionConfiguration knowledgeSessionConfiguration = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
knowledgeSessionConfiguration.setOption(TimerJobFactoryOption.get("trackable"));
KieBase kb = loadKnowledgeBaseFromString(kbconf, drl);
KieSession ks = kb.newKieSession(knowledgeSessionConfiguration, null);
ks.insert(new StockTick(2, "BBB", 1.0, 0));
Thread.sleep(1100);
try {
ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true, false);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
ks.addEventListener(new DebugAgendaEventListener());
ArrayList list = new ArrayList();
ks.setGlobal("list", list);
ks.fireAllRules();
ks.insert(new StockTick(3, "BBB", 1.0, 0));
ks.fireAllRules();
assertEquals(2, list.size());
assertEquals(Arrays.asList(2L, 3L), list);
}
Aggregations