Search in sources :

Example 11 with AfterMatchFiredEvent

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));
}
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 12 with AfterMatchFiredEvent

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();
}
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 13 with AfterMatchFiredEvent

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)));
}
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 14 with AfterMatchFiredEvent

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();
}
Also used : ArrayList(java.util.ArrayList) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) InternalAgenda(org.drools.core.common.InternalAgenda) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) Test(org.junit.Test)

Example 15 with AfterMatchFiredEvent

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"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Aggregations

AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)29 KieSession (org.kie.api.runtime.KieSession)26 Test (org.junit.Test)25 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)18 KieBase (org.kie.api.KieBase)16 ArrayList (java.util.ArrayList)8 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)8 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)8 List (java.util.List)6 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)6 StockTick (org.drools.compiler.StockTick)5 KieServices (org.kie.api.KieServices)5 ReleaseId (org.kie.api.builder.ReleaseId)5 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)5 MatchCancelledEvent (org.kie.api.event.rule.MatchCancelledEvent)5 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)5 KieContainer (org.kie.api.runtime.KieContainer)5 EntryPoint (org.kie.api.runtime.rule.EntryPoint)5 Match (org.kie.api.runtime.rule.Match)5 Person (org.drools.compiler.Person)4