use of org.kie.api.event.rule.AfterMatchFiredEvent in project drools by kiegroup.
the class AccumulateTest method execTestAccumulateMultipleFunctions.
public void execTestAccumulateMultipleFunctions(String fileName) throws Exception {
KieSession ksession = getKieSessionFromResources(fileName);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 15), new Cheese("brie", 17), new Cheese("provolone", 8) };
final Person bob = new Person("Bob", "stilton");
final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
for (int i = 0; i < cheese.length; i++) {
cheeseHandles[i] = (FactHandle) ksession.insert(cheese[i]);
}
final FactHandle bobHandle = (FactHandle) ksession.insert(bob);
// ---------------- 1st scenario
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
Mockito.verify(ael).afterMatchFired(cap.capture());
Match activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
Mockito.reset(ael);
// ---------------- 2nd scenario
final int index = 1;
cheese[index].setPrice(9);
ksession.update(cheeseHandles[index], cheese[index]);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(24));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(5));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(8));
Mockito.reset(ael);
// ---------------- 3rd scenario
bob.setLikes("brie");
ksession.update(bobHandle, bob);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(32));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(15));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(16));
Mockito.reset(ael);
// ---------------- 4th scenario
ksession.delete(cheeseHandles[3]);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(17));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(17));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(17));
}
use of org.kie.api.event.rule.AfterMatchFiredEvent in project drools by kiegroup.
the class AccumulateTest method execTestAccumulateMultipleFunctionsConstraint.
public void execTestAccumulateMultipleFunctionsConstraint(String fileName) throws Exception {
KieSession ksession = getKieSessionFromResources(fileName);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
final Cheese[] cheese = new Cheese[] { new Cheese("stilton", 10), new Cheese("stilton", 3), new Cheese("stilton", 5), new Cheese("brie", 3), new Cheese("brie", 17), new Cheese("provolone", 8) };
final Person bob = new Person("Bob", "stilton");
final FactHandle[] cheeseHandles = new FactHandle[cheese.length];
for (int i = 0; i < cheese.length; i++) {
cheeseHandles[i] = (FactHandle) ksession.insert(cheese[i]);
}
final FactHandle bobHandle = (FactHandle) ksession.insert(bob);
// ---------------- 1st scenario
ksession.fireAllRules();
ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
Mockito.verify(ael).afterMatchFired(cap.capture());
Match activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(18));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(6));
Mockito.reset(ael);
// ---------------- 2nd scenario
final int index = 1;
cheese[index].setPrice(9);
ksession.update(cheeseHandles[index], cheese[index]);
ksession.fireAllRules();
Mockito.verify(ael, Mockito.never()).afterMatchFired(Mockito.any(AfterMatchFiredEvent.class));
Mockito.reset(ael);
// ---------------- 3rd scenario
bob.setLikes("brie");
ksession.update(bobHandle, bob);
ksession.fireAllRules();
Mockito.verify(ael).afterMatchFired(cap.capture());
activation = cap.getValue().getMatch();
assertThat(((Number) activation.getDeclarationValue("$sum")).intValue(), is(20));
assertThat(((Number) activation.getDeclarationValue("$min")).intValue(), is(3));
assertThat(((Number) activation.getDeclarationValue("$avg")).intValue(), is(10));
ksession.dispose();
}
use of org.kie.api.event.rule.AfterMatchFiredEvent 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.AfterMatchFiredEvent in project drools by kiegroup.
the class ExecutionFlowControlTest method testRuleFlowGroupInActiveMode.
@Test(timeout = 10000)
public void testRuleFlowGroupInActiveMode() throws Exception {
KieBase kbase = loadKnowledgeBase("ruleflowgroup.drl");
final KieSession ksession = kbase.newKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
final AtomicBoolean fired = new AtomicBoolean(false);
ksession.addEventListener(new org.kie.api.event.rule.DefaultAgendaEventListener() {
@Override
public void afterMatchFired(AfterMatchFiredEvent event) {
synchronized (fired) {
fired.set(true);
fired.notifyAll();
}
}
});
new Thread(new Runnable() {
public void run() {
ksession.fireUntilHalt();
}
}).start();
ksession.insert("Test");
assertEquals(0, list.size());
((InternalAgenda) ksession.getAgenda()).activateRuleFlowGroup("Group1");
synchronized (fired) {
if (!fired.get()) {
fired.wait();
}
}
assertEquals(1, list.size());
ksession.halt();
}
use of org.kie.api.event.rule.AfterMatchFiredEvent 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"));
}
Aggregations