use of org.kie.api.event.rule.AgendaEventListener in project jbpm by kiegroup.
the class ProcessFlowControlTest method testRuleFlowClear.
@Test
public void testRuleFlowClear() throws Exception {
builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream("test_ruleflowClear.drl")));
builder.addRuleFlow(new InputStreamReader(getClass().getResourceAsStream("test_ruleflowClear.rfm")));
KieSession workingMemory = createKieSession(true, builder.getPackages());
final List<String> list = new ArrayList<String>();
workingMemory.setGlobal("list", list);
final List<Match> activations = new ArrayList<Match>();
AgendaEventListener listener = new DefaultAgendaEventListener() {
public void matchCancelled(MatchCancelledEvent event) {
activations.add(event.getMatch());
}
};
workingMemory.addEventListener(listener);
InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
// assertEquals( 0,
// agenda.getRuleFlowGroup( "flowgroup-1" ).size() );
// We need to call fireAllRules here to get the InitialFact into the system, to the eval(true)'s kick in
workingMemory.fireAllRules();
agenda.evaluateEagerList();
// Now we have 4 in the RuleFlow, but not yet in the agenda
assertEquals(4, agenda.sizeOfRuleFlowGroup("flowgroup-1"));
// Check they aren't in the Agenda
assertEquals(0, agenda.getAgendaGroup("MAIN").size());
// Check we have 0 activation cancellation events
assertEquals(0, activations.size());
((InternalAgenda) workingMemory.getAgenda()).clearAndCancelRuleFlowGroup("flowgroup-1");
// Check the AgendaGroup and RuleFlowGroup are now empty
assertEquals(0, agenda.getAgendaGroup("MAIN").size());
assertEquals(0, agenda.sizeOfRuleFlowGroup("flowgroup-1"));
// Check we have four activation cancellation events
assertEquals(4, activations.size());
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class KieLoggersTest method testKieConsoleLoggerStateless.
@Test
public void testKieConsoleLoggerStateless() throws Exception {
String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + " when\n" + " m : Message( myMessage : message )\n" + " then\n" + "end";
// get the resource
Resource dt = ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/integrationtests/hello.drl");
// create the builder
StatelessKieSession ksession = getStatelessKieSession(dt);
KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newConsoleLogger(ksession);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
ksession.execute(new Message("Hello World"));
verify(ael).afterMatchFired(any(AfterMatchFiredEvent.class));
logger.close();
}
use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.
the class AccumulateTest method testImportAccumulateFunctionWithDeclaration.
@Test
public void testImportAccumulateFunctionWithDeclaration() throws Exception {
// DROOLS-750
String drl = "package org.foo.bar\n" + "import accumulate " + TestFunction.class.getCanonicalName() + " f;\n" + "import " + Person.class.getCanonicalName() + ";\n" + "declare Person \n" + " @propertyReactive\n" + "end\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 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.AgendaEventListener 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();
}
Aggregations