use of org.drools.compiler.StockTick in project drools by kiegroup.
the class PseudoClockEventsTest method processStocks.
private int processStocks(int stockCount, AgendaEventListener agendaEventListener, String drlContentString) throws DroolsParserException, IOException, Exception {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kconf, drlContentString);
KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
ksessionConfig.setProperty("keep.reference", "true");
final KieSession ksession = kbase.newKieSession(ksessionConfig, null);
ksession.addEventListener(agendaEventListener);
PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
Runnable fireUntilHaltRunnable = new Runnable() {
public void run() {
ksession.fireUntilHalt();
}
};
Thread fireUntilHaltThread = new Thread(fireUntilHaltRunnable, "Engine's thread");
fireUntilHaltThread.start();
Thread.currentThread().setName("Feeding thread");
for (int stIndex = 1; stIndex <= stockCount; stIndex++) {
clock.advanceTime(20, TimeUnit.SECONDS);
Thread.sleep(100);
final StockTickInterface st = new StockTick(stIndex, "RHT", 100 * stIndex, 100 * stIndex);
ksession.insert(st);
Thread.sleep(100);
}
Thread.sleep(100);
ksession.halt();
fireUntilHaltThread.join(5000);
return stockCount;
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class TimerAndCalendarTest method testExpiredPropagations.
@Test
public void testExpiredPropagations() throws InterruptedException {
// DROOLS-244
String drl = "package org.drools.test;\n" + "\n" + "import org.drools.compiler.StockTick;\n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + "\t@role( event )\n" + "\t@timestamp( time )\n" + "end\n" + "\n" + "declare window ATicks\n" + " StockTick( company == \"AAA\" ) over window:time( 1s ) " + " from entry-point \"AAA\"\n" + "end\n" + "\n" + "declare window BTicks\n" + " StockTick( company == \"BBB\" ) over window:time( 1s ) " + " from entry-point \"BBB\"\n" + "end\n" + "\n" + "rule Ticks \n" + " when\n" + " String()\n" + " accumulate( $x : StockTick() from window ATicks, $a : count( $x ) )\n" + " accumulate( $y : StockTick() from window BTicks, $b : count( $y ) )\n" + " accumulate( $z : StockTick() over window:time( 1s ), $c : count( $z ) )\n" + " then\n" + " list.add( $a );\n" + " list.add( $b );\n" + " list.add( $c );\n" + "end";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(conf, null);
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
SessionPseudoClock clock = (SessionPseudoClock) ksession.getSessionClock();
clock.advanceTime(1100, TimeUnit.MILLISECONDS);
StockTick tick = new StockTick(0, "AAA", 1.0, 0);
StockTick tock = new StockTick(1, "BBB", 1.0, 2500);
StockTick tack = new StockTick(1, "CCC", 1.0, 2700);
EntryPoint epa = ksession.getEntryPoint("AAA");
EntryPoint epb = ksession.getEntryPoint("BBB");
epa.insert(tick);
epb.insert(tock);
ksession.insert(tack);
FactHandle handle = ksession.insert("go1");
ksession.fireAllRules();
System.out.println("***** " + list + " *****");
assertEquals(asList(0L, 1L, 1L), list);
list.clear();
ksession.retract(handle);
clock.advanceTime(2550, TimeUnit.MILLISECONDS);
handle = ksession.insert("go2");
ksession.fireAllRules();
System.out.println("***** " + list + " *****");
assertEquals(asList(0L, 0L, 1L), list);
list.clear();
ksession.retract(handle);
clock.advanceTime(500, TimeUnit.MILLISECONDS);
handle = ksession.insert("go3");
ksession.fireAllRules();
System.out.println("***** " + list + " *****");
assertEquals(asList(0L, 0L, 0L), list);
list.clear();
ksession.retract(handle);
ksession.dispose();
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class DescrBuilderTest method testTopLevelAccumulate.
@Test
public void testTopLevelAccumulate() throws InstantiationException, IllegalAccessException {
PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("r1").lhs().accumulate().source().pattern("StockTick").constraint("company == \"RHT\"").bind("$p", "price", false).end().end().function("sum", "$sum", false, "$p").function("count", "$cnt", false, "$p").end().end().rhs("// some comment").end().getDescr();
KiePackage kpkg = compilePkgDescr(pkg);
assertEquals("org.drools.compiler", kpkg.getName());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Collections.singletonList(kpkg));
KieSession ksession = kbase.newKieSession();
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.insert(new StockTick(1, "RHT", 80, 1));
ksession.insert(new StockTick(2, "RHT", 100, 10));
int rules = ksession.fireAllRules();
assertEquals(1, rules);
ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael).afterMatchFired(cap.capture());
assertThat(((Number) cap.getValue().getMatch().getDeclarationValue("$sum")).intValue(), is(180));
assertThat(((Number) cap.getValue().getMatch().getDeclarationValue("$cnt")).intValue(), is(2));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class DescrBuilderTest method testRule.
@Test
public void testRule() throws InstantiationException, IllegalAccessException {
PackageDescrBuilder packBuilder = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("r1").lhs().and().or().pattern("StockTick").constraint("price > 100").end().pattern("StockTick").constraint("price < 10").end().end().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().rhs(" System.out.println(\"foo\");\n").end();
PackageDescr pkg = packBuilder.getDescr();
String drl = new DrlDumper().dump(packBuilder.getDescr());
System.out.println(drl);
KiePackage kpkg = compilePkgDescr(pkg);
assertEquals("org.drools.compiler", kpkg.getName());
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(Collections.singletonList(kpkg));
KieSession ksession = createKnowledgeSession(kbase);
ksession.insert(new StockTick(1, "RHT", 80, 1));
int rules = ksession.fireAllRules();
assertEquals(0, rules);
ksession = kbase.newKieSession();
ksession.insert(new StockTick(2, "RHT", 150, 1));
rules = ksession.fireAllRules();
assertEquals(1, rules);
}
Aggregations