use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class DeclarativeAgendaTest method testCancelActivation.
@Test(timeout = 10000)
public void testCancelActivation() {
String str = "";
str += "package org.domain.test \n";
str += "import " + Match.class.getName() + "\n";
str += "global java.util.List list \n";
str += "dialect 'mvel' \n";
str += "rule rule1 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule blockerAllSalesRules @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " kcontext.cancelMatch( $i ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
final List cancelled = new ArrayList();
ksession.addEventListener(new AgendaEventListener() {
public void beforeMatchFired(BeforeMatchFiredEvent event) {
}
public void agendaGroupPushed(AgendaGroupPushedEvent event) {
}
public void agendaGroupPopped(AgendaGroupPoppedEvent event) {
}
public void afterMatchFired(AfterMatchFiredEvent event) {
}
public void matchCreated(MatchCreatedEvent event) {
}
public void beforeRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
public void afterRuleFlowGroupActivated(RuleFlowGroupActivatedEvent event) {
}
public void beforeRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
public void afterRuleFlowGroupDeactivated(RuleFlowGroupDeactivatedEvent event) {
}
public void matchCancelled(MatchCancelledEvent event) {
cancelled.add(event);
}
});
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("go1");
FactHandle go2 = ksession.insert("go2");
ksession.fireAllRules();
assertEquals(0, list.size());
assertEquals(1, cancelled.size());
assertEquals("rule1", ((MatchCancelledEvent) cancelled.get(0)).getMatch().getRule().getName());
ksession.dispose();
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRulesWithTypeDeclarations.
@Test(timeout = 10000)
public void testDynamicRulesWithTypeDeclarations() {
String type = "package com.sample\n" + "declare type Foo\n" + " id : int\n" + "end\n";
String r1 = "package com.sample\n" + "rule R1 when\n" + " not Foo()\n" + "then\n" + " insert( new Foo(1) );\n" + "end\n";
String r2 = "package com.sample\n" + "rule R2 when\n" + " $f : Foo()\n" + "then\n" + " $f.setId( 2 );\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(type.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.fireAllRules();
verify(ael, never()).afterMatchFired(any(AfterMatchFiredEvent.class));
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbase);
kbuilder.add(ResourceFactory.newByteArrayResource(r1.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> capt = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(1)).afterMatchFired(capt.capture());
assertThat("R1", is(capt.getValue().getMatch().getRule().getName()));
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbase);
kbuilder.add(ResourceFactory.newByteArrayResource(r2.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
ksession.fireAllRules();
verify(ael, times(2)).afterMatchFired(capt.capture());
assertThat("R2", is(capt.getAllValues().get(2).getMatch().getRule().getName()));
ksession.dispose();
}
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 CepEspTest method testMultipleSlidingWindows.
@Test(timeout = 10000)
public void testMultipleSlidingWindows() throws IOException, ClassNotFoundException {
String str = "declare A\n" + " @role( event )\n" + " id : int\n" + "end\n" + "declare B\n" + " @role( event )\n" + " id : int\n" + "end\n" + "rule launch\n" + "when\n" + "then\n" + " insert( new A( 1 ) );\n" + " insert( new A( 2 ) );\n" + " insert( new B( 1 ) );\n" + " insert( new A( 3 ) );\n" + " insert( new B( 2 ) );\n" + "end\n" + "rule \"ab\" \n" + "when\n" + " A( $a : id ) over window:length( 1 )\n" + " B( $b : id ) over window:length( 1 )\n" + "then\n" + " System.out.println(\"AB: ( \"+$a+\", \"+$b+\" )\");\n" + "end\n" + "rule \"ba\" salience 10\n" + "when\n" + " B( $b : id ) over window:length( 1 )\n" + " A( $a : id ) over window:length( 1 )\n" + "then\n" + " System.out.println(\"BA: ( \"+$b+\", \"+$a+\" )\");\n" + "end";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSession ksession = createKnowledgeSession(kbase);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(3)).afterMatchFired(captor.capture());
List<AfterMatchFiredEvent> values = captor.getAllValues();
// first rule
Match act = values.get(0).getMatch();
assertThat(act.getRule().getName(), is("launch"));
// second rule
act = values.get(1).getMatch();
assertThat(act.getRule().getName(), is("ba"));
assertThat(((Number) act.getDeclarationValue("$a")).intValue(), is(3));
assertThat(((Number) act.getDeclarationValue("$b")).intValue(), is(2));
// third rule
act = values.get(2).getMatch();
assertThat(act.getRule().getName(), is("ab"));
assertThat(((Number) act.getDeclarationValue("$a")).intValue(), is(3));
assertThat(((Number) act.getDeclarationValue("$b")).intValue(), is(2));
}
Aggregations