use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class AccumulateTest method testImportAccumulateFunction.
@Test
public void testImportAccumulateFunction() throws Exception {
String drl = "package org.foo.bar\n" + "import accumulate " + TestFunction.class.getCanonicalName() + " f\n" + "rule X when\n" + " accumulate( $s : String(),\n" + " $v : f( $s ) )\n" + "then\n" + "end\n";
ReleaseId releaseId = new ReleaseIdImpl("foo", "bar", "1.0");
KieServices ks = KieServices.Factory.get();
createAndDeployJar(ks, releaseId, drl);
KieContainer kc = ks.newKieContainer(releaseId);
KieSession ksession = kc.newKieSession();
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.insert("x");
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> ac = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael).afterMatchFired(ac.capture());
assertThat((Integer) ac.getValue().getMatch().getDeclarationValue("$v"), is(Integer.valueOf(1)));
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class ExecutionFlowControlTest method testUpdateActivationCreationNoLoop.
@Test
public void testUpdateActivationCreationNoLoop() throws Exception {
// JBRULES-787, no-loop blocks all dependant tuples for update
KieBase kbase = loadKnowledgeBase("test_UpdateActivationCreationNoLoop.drl");
KieSession ksession = kbase.newKieSession();
final List created = new ArrayList();
final List cancelled = new ArrayList();
final AgendaEventListener l = new DefaultAgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
created.add(event);
}
@Override
public void matchCancelled(MatchCancelledEvent event) {
cancelled.add(event);
}
};
ksession.addEventListener(l);
final Cheese stilton = new Cheese("stilton", 15);
final FactHandle stiltonHandle = ksession.insert(stilton);
final Person p1 = new Person("p1");
p1.setCheese(stilton);
ksession.insert(p1);
final Person p2 = new Person("p2");
p2.setCheese(stilton);
ksession.insert(p2);
final Person p3 = new Person("p3");
p3.setCheese(stilton);
ksession.insert(p3);
ksession.fireAllRules();
assertEquals(3, created.size());
assertEquals(0, cancelled.size());
// simulate a modify inside a consequence
ksession.update(stiltonHandle, stilton);
// with true modify, no reactivations should be triggered
assertEquals(3, created.size());
assertEquals(0, cancelled.size());
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class FirstOrderLogicTest method testOrWithVariableResolution.
// JBRULES-2482
@Test
public void testOrWithVariableResolution() throws Exception {
// KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl");
// KieSession workingMemory = createKnowledgeSession(kbase);
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_OrCEFollowedByMultipleEval.drl", FirstOrderLogicTest.class), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final AgendaEventListener al = mock(AgendaEventListener.class);
ksession.addEventListener(al);
ksession.insert(new FactA("a"));
ksession.insert(new FactB("b"));
ksession.insert(new FactC("c"));
ksession.fireAllRules();
verify(al, times(6)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class CepEspTest method testSalienceWithEventsRealtimeClock.
@Test(timeout = 10000)
public void testSalienceWithEventsRealtimeClock() throws IOException, ClassNotFoundException, InterruptedException {
String str = "package org.drools.compiler\n" + "import " + StockTick.class.getName() + "\n" + "declare StockTick\n" + " @role ( event )\n" + "end\n" + "rule R1 salience 1000\n" + " when\n" + " $s1 : StockTick( company == 'RHT' )\n" + " $s2 : StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + " then\n" + "end\n" + "rule R2 salience 1000\n" + " when\n" + " $s1 : StockTick( company == 'RHT' )\n" + " not StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + " then\n" + "end\n" + "rule R3 salience 100\n" + " when\n" + " $s2 : StockTick( company == 'ACME' )\n" + " then\n" + "end\n";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.REALTIME_CLOCK.getId()));
KieSession ksession = kbase.newKieSession(ksconf, null);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.insert(new StockTick(1, "RHT", 10, 1000));
ksession.insert(new StockTick(2, "RHT", 10, 1000));
ksession.insert(new StockTick(3, "RHT", 10, 1000));
// sleep for 2 secs
Thread.currentThread().sleep(2000);
ksession.insert(new StockTick(4, "ACME", 10, 1000));
// sleep for 1 sec
Thread.currentThread().sleep(1000);
int rulesFired = ksession.fireAllRules();
assertEquals(4, rulesFired);
ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael, times(4)).afterMatchFired(captor.capture());
List<AfterMatchFiredEvent> aafe = captor.getAllValues();
assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(1).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(2).getMatch().getRule().getName(), is("R1"));
assertThat(aafe.get(3).getMatch().getRule().getName(), is("R3"));
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class CepEspTest method testStreamModeNoSerialization.
@Test(timeout = 10000)
public void testStreamModeNoSerialization() throws IOException, ClassNotFoundException {
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_StreamMode.drl");
KieBase kbase2 = (KieBase) DroolsStreamUtils.streamIn(DroolsStreamUtils.streamOut(kbase1), null);
final KieSession ksession1 = kbase1.newKieSession();
AgendaEventListener ael1 = mock(AgendaEventListener.class);
ksession1.addEventListener(ael1);
final KieSession ksession2 = kbase2.newKieSession();
AgendaEventListener ael2 = mock(AgendaEventListener.class);
ksession2.addEventListener(ael2);
// -------------
// first, check the non-serialized session
// -------------
ksession1.insert(new Sensor(10, 10));
ksession1.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> aafe1 = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
verify(ael1, times(1)).afterMatchFired(aafe1.capture());
List<AfterMatchFiredEvent> events1 = aafe1.getAllValues();
assertThat(events1.get(0).getMatch().getDeclarationValue("$avg"), is((Object) 10));
ksession1.insert(new Sensor(20, 20));
ksession1.fireAllRules();
verify(ael1, times(2)).afterMatchFired(aafe1.capture());
events1 = aafe1.getAllValues();
assertThat(events1.get(1).getMatch().getDeclarationValue("$avg"), is((Object) 15));
ksession1.insert(new Sensor(30, 30));
ksession1.fireAllRules();
verify(ael1, times(3)).afterMatchFired(aafe1.capture());
assertThat(events1.get(2).getMatch().getDeclarationValue("$avg"), is((Object) 25));
ksession1.dispose();
// -------------
// now we check the serialized session
// -------------
ArgumentCaptor<AfterMatchFiredEvent> aafe2 = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
ksession2.insert(new Sensor(10, 10));
ksession2.fireAllRules();
verify(ael2, times(1)).afterMatchFired(aafe2.capture());
List<AfterMatchFiredEvent> events2 = aafe2.getAllValues();
assertThat(events2.get(0).getMatch().getDeclarationValue("$avg"), is((Object) 10));
ksession2.insert(new Sensor(20, 20));
ksession2.fireAllRules();
verify(ael2, times(2)).afterMatchFired(aafe2.capture());
events2 = aafe2.getAllValues();
assertThat(events2.get(1).getMatch().getDeclarationValue("$avg"), is((Object) 15));
ksession2.insert(new Sensor(30, 30));
ksession2.fireAllRules();
verify(ael2, times(3)).afterMatchFired(aafe2.capture());
assertThat(events2.get(2).getMatch().getDeclarationValue("$avg"), is((Object) 25));
ksession2.dispose();
}
Aggregations