use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovalsSubNetworkAndNot.
@Test(timeout = 10000)
public void testDynamicRuleRemovalsSubNetworkAndNot() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase("test_DynamicRulesWithNotSubnetwork.drl");
KieSession ksession = createKnowledgeSession(kbase);
final AgendaEventListener alistener = mock(AgendaEventListener.class);
ksession.addEventListener(alistener);
// pattern does not match, so do not activate
ksession.insert(new Person("toni"));
ksession.fireAllRules();
verify(alistener, never()).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
// pattern matches, so create activation
ksession.insert(new Person("bob"));
ksession.fireAllRules();
verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
// already active, so no new activation should be created
ksession.insert(new Person("mark"));
ksession.fireAllRules();
verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
kbase.removeKiePackage("org.drools.compiler");
assertEquals(0, kbase.getKiePackages().size());
// lets re-compile and add it again
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicRulesWithNotSubnetwork.drl");
kbase.addPackages(kpkgs);
ksession.fireAllRules();
// rule should be reactivated, since data is still in the session
verify(alistener, times(2)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class CepEspTest method testEqualityAssertBehaviorOnEntryPoints.
@Test(timeout = 10000)
public void testEqualityAssertBehaviorOnEntryPoints() throws IOException, ClassNotFoundException {
StockTickInterface st1 = new StockTick(1, "RHT", 10, 10);
StockTickInterface st2 = new StockTick(1, "RHT", 10, 10);
StockTickInterface st3 = new StockTick(2, "RHT", 15, 20);
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
kbconf.setOption(EqualityBehaviorOption.EQUALITY);
final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
final KieSession ksession1 = kbase1.newKieSession();
AgendaEventListener ael1 = mock(AgendaEventListener.class);
ksession1.addEventListener(ael1);
EntryPoint ep1 = ksession1.getEntryPoint("stocktick stream");
FactHandle fh1 = ep1.insert(st1);
FactHandle fh1_2 = ep1.insert(st1);
FactHandle fh2 = ep1.insert(st2);
FactHandle fh3 = ep1.insert(st3);
assertSame(fh1, fh1_2);
assertSame(fh1, fh2);
assertNotSame(fh1, fh3);
ksession1.fireAllRules();
// must have fired 2 times, one for each event equality
verify(ael1, times(2)).afterMatchFired(any(AfterMatchFiredEvent.class));
ksession1.dispose();
}
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 PseudoClockEventsTest method testEvenFirePseudoClockRulesAB.
@Test(timeout = 60000)
public void testEvenFirePseudoClockRulesAB() throws Exception {
AgendaEventListener ael = mock(AgendaEventListener.class);
processStocks(evalFirePseudoClockStockCount, ael, evalFirePseudoClockDeclaration + evalFirePseudoClockRuleA + evalFirePseudoClockRuleB);
final int expectedActivationCount = evalFirePseudoClockStockCount * (evalFirePseudoClockStockCount - 1) / 2 + evalFirePseudoClockStockCount - 1;
verify(ael, times(expectedActivationCount)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class StarImportTest method starImportedFactAlsoDeclaredInDRL.
/**
* Tests that rule fires if given a fact that is imported using
* "star" import, while it is also declared in DRL.
*/
@Test
public void starImportedFactAlsoDeclaredInDRL() throws Exception {
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.insert(new TestEvent("event 1"));
ksession.fireAllRules();
// the rule should have fired exactly once
verify(ael, times(1)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
Aggregations