use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWithSubSubPath.
@Test
public void testEagerEvaluationWithSubSubPath() throws Exception {
String str = "package org.simple \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " exists( Boolean() and not(not(Integer()) and not(Double())) )\n" + "then \n" + "end \n";
KieServices ks = KieServices.Factory.get();
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
final List list = new ArrayList();
AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert(Boolean.TRUE);
assertEquals(0, list.size());
ksession.insert("test");
assertEquals(0, list.size());
ksession.insert(1);
assertEquals(1, list.size());
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class Misc2Test method testListnersOnStatlessKieSession.
@Test
public void testListnersOnStatlessKieSession() {
// DROOLS-141
// BZ-999491
String str = "rule R when\n" + " String()\n" + "then\n" + "end\n";
KieBase kbase = loadKnowledgeBaseFromString(str);
StatelessKieSession ksession = kbase.newStatelessKieSession();
final List<String> firings = new ArrayList<String>();
AgendaEventListener agendaEventListener = new AgendaEventListener() {
public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
}
public void matchCancelled(org.kie.api.event.rule.MatchCancelledEvent event) {
}
public void beforeMatchFired(org.kie.api.event.rule.BeforeMatchFiredEvent event) {
}
public void afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent event) {
firings.add("Fired!");
}
public void agendaGroupPopped(org.kie.api.event.rule.AgendaGroupPoppedEvent event) {
}
public void agendaGroupPushed(org.kie.api.event.rule.AgendaGroupPushedEvent event) {
}
public void beforeRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
}
public void afterRuleFlowGroupActivated(org.kie.api.event.rule.RuleFlowGroupActivatedEvent event) {
}
public void beforeRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
}
public void afterRuleFlowGroupDeactivated(org.kie.api.event.rule.RuleFlowGroupDeactivatedEvent event) {
}
};
ksession.addEventListener(agendaEventListener);
ksession.execute("1");
ksession.execute("2");
assertEquals(2, firings.size());
ksession.removeEventListener(agendaEventListener);
ksession.execute("3");
assertEquals(2, firings.size());
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class StreamsTest method testWindowDeclaration.
public void testWindowDeclaration() throws Exception {
String drl = "package org.drools.compiler\n" + "declare StockTick\n" + " @role(event)\n" + "end\n" + "declare window RedHatTicks\n" + " StockTick( company == 'RHT' )\n" + " over window:length(5)\n" + " from entry-point ticks\n" + "end\n" + "rule X\n" + "when\n" + " accumulate( $s : StockTick( price > 20 ) from window RedHatTicks,\n" + " $sum : sum( $s.getPrice() ),\n" + " $cnt : count( $s ) )\n" + "then\n" + "end\n";
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("ticks");
// not in the window
ep.insert(new StockTick(1, "ACME", 20, 1000));
// not > 20
ep.insert(new StockTick(2, "RHT", 20, 1000));
ep.insert(new StockTick(3, "RHT", 30, 1000));
// not in the window
ep.insert(new StockTick(4, "ACME", 30, 1000));
ep.insert(new StockTick(5, "RHT", 25, 1000));
// not in the window
ep.insert(new StockTick(6, "ACME", 10, 1000));
// not > 20
ep.insert(new StockTick(7, "RHT", 10, 1000));
ep.insert(new StockTick(8, "RHT", 40, 1000));
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(95));
assertThat(((Number) aafe.getMatch().getDeclarationValue("$cnt")).intValue(), is(3));
}
use of org.kie.api.event.rule.AgendaEventListener 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.event.rule.AgendaEventListener in project drools by kiegroup.
the class StreamsTest method testEventExpirationSetToZero.
@Test(timeout = 10000)
public void testEventExpirationSetToZero() throws Exception {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase("test_EventExpirationSetToZero.drl", kconf);
KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(ksessionConfig, null);
RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
ksession.addEventListener(wml);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
final StockTickInterface st1 = new StockTick(1, "RHT", 100, 1000);
final StockTickInterface 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(), hasItems((Object) st1, st2));
clock.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertThat(ksession.getObjects().size(), equalTo(0));
}
Aggregations