use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class StreamsTest method testEntryPointWithAccumulateAndMVEL.
@Test(timeout = 10000)
public void testEntryPointWithAccumulateAndMVEL() throws Exception {
String str = "package org.drools.compiler\n" + "rule R1 dialect 'mvel'\n" + " when\n" + " $n : Number() from accumulate( \n" + " StockTick() from entry-point ep1,\n" + " count(1))" + " then\n" + "end\n";
// read in the source
KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, str);
KieSession ksession = createKnowledgeSession(kbase);
org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
ksession.addEventListener(ael);
EntryPoint ep1 = ksession.getEntryPoint("ep1");
ep1.insert(new StockTick(1, "RHT", 10, 1000));
int rulesFired = ksession.fireAllRules();
assertEquals(1, rulesFired);
ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(1)).afterMatchFired(captor.capture());
List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class StreamsTest method testModifyRetracOnEntryPointFacts.
@Test(timeout = 10000)
public void testModifyRetracOnEntryPointFacts() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_modifyRetractEntryPoint.drl");
KieSession session = kbase.newKieSession();
final List<? extends Number> results = new ArrayList<Number>();
session.setGlobal("results", results);
StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
EntryPoint entry = session.getEntryPoint("stream1");
InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
assertNotNull(handle5);
assertNotNull(handle6);
assertNotNull(handle7);
assertNotNull(handle8);
assertTrue(handle5.isEvent());
assertTrue(handle6.isEvent());
assertTrue(handle7.isEvent());
assertTrue(handle8.isEvent());
session.fireAllRules();
System.out.println(results);
assertEquals(2, results.size());
assertEquals(30, ((Number) results.get(0)).intValue());
assertEquals(110, ((Number) results.get(1)).intValue());
// the 3 non-matched facts continue to exist in the entry point
assertEquals(3, entry.getObjects().size());
// but no fact was inserted into the main session
assertEquals(0, session.getObjects().size());
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class StreamsTest method testWindowDeclaration2.
@Test(timeout = 10000)
public void testWindowDeclaration2() throws Exception {
String drl = "package org.drools.compiler\n" + "declare Double\n" + " @role(event)\n" + "end\n" + "declare window Streem\n" + " Double() over window:length( 10 ) from entry-point data\n" + "end\n" + "rule \"See\"\n" + "when\n" + " $sum : Double() from accumulate (\n" + " $d: Double()\n" + " from window Streem,\n" + " sum( $d )\n" + " )\n" + "then\n" + "end";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
KieSession ksession = kbase.newKieSession();
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
EntryPoint ep = ksession.getEntryPoint("data");
ep.insert(Double.valueOf(10));
ep.insert(Double.valueOf(11));
ep.insert(Double.valueOf(12));
ksession.fireAllRules();
ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(1)).afterMatchFired(captor.capture());
AfterMatchFiredEvent aafe = captor.getValue();
assertThat(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(33));
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class StreamsTest method testGetEntryPointList.
@Test(timeout = 10000)
public void testGetEntryPointList() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_EntryPointReference.drl");
KieSession session = kbase.newKieSession();
EntryPoint def = session.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId());
EntryPoint s1 = session.getEntryPoint("stream1");
EntryPoint s2 = session.getEntryPoint("stream2");
EntryPoint s3 = session.getEntryPoint("stream3");
Collection<? extends EntryPoint> eps = session.getEntryPoints();
assertEquals(4, eps.size());
assertTrue(eps.contains(def));
assertTrue(eps.contains(s1));
assertTrue(eps.contains(s2));
assertTrue(eps.contains(s3));
}
use of org.kie.api.runtime.rule.EntryPoint in project drools by kiegroup.
the class StreamsTest method testEntryPointReference.
// (timeout=10000)
@Test
public void testEntryPointReference() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_EntryPointReference.drl");
KieSession session = kbase.newKieSession();
final List<StockTick> results = new ArrayList<StockTick>();
session.setGlobal("results", results);
StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
EntryPoint entry = session.getEntryPoint("stream1");
InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
assertNotNull(handle5);
assertNotNull(handle6);
assertNotNull(handle7);
assertNotNull(handle8);
assertTrue(handle5.isEvent());
assertTrue(handle6.isEvent());
assertTrue(handle7.isEvent());
assertTrue(handle8.isEvent());
session.fireAllRules();
assertEquals(1, results.size());
assertSame(tick7, results.get(0));
}
Aggregations