Search in sources :

Example 21 with AgendaEventListener

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());
}
Also used : InternalAgenda(org.drools.core.common.InternalAgenda) InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DefaultAgendaEventListener(org.drools.core.event.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Match(org.kie.api.runtime.rule.Match) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 22 with AgendaEventListener

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();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 23 with AgendaEventListener

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)));
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 24 with AgendaEventListener

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));
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match)

Example 25 with AgendaEventListener

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();
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) Person(org.drools.compiler.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match)

Aggregations

AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)50 Test (org.junit.Test)45 KieSession (org.kie.api.runtime.KieSession)37 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)32 ArrayList (java.util.ArrayList)21 KieBase (org.kie.api.KieBase)20 List (java.util.List)15 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)15 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)15 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)13 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)13 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)11 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)10 StockTick (org.drools.compiler.StockTick)9 KieServices (org.kie.api.KieServices)9 FactHandle (org.kie.api.runtime.rule.FactHandle)9 EntryPoint (org.kie.api.runtime.rule.EntryPoint)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 Person (org.drools.compiler.Person)6 KieHelper (org.kie.internal.utils.KieHelper)6