Search in sources :

Example 6 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class FirstOrderLogicTest method testOrWithVariableResolution2.

// JBRULES-2526
@Test
public void testOrWithVariableResolution2() throws Exception {
    // KieBase kbase = loadKnowledgeBase( "test_OrCEFollowedByMultipleEval.drl");
    // KieSession workingMemory = createKnowledgeSession(kbase);
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("test_OrCEFollowedByMultipleEval2.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(8)).afterMatchFired(any(AfterMatchFiredEvent.class));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) FactA(org.drools.compiler.FactA) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactB(org.drools.compiler.FactB) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) FactC(org.drools.compiler.FactC) Test(org.junit.Test)

Example 7 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class DynamicRulesTest method testJBRULES_2206.

@Test(timeout = 10000)
public void testJBRULES_2206() {
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    ((RuleBaseConfiguration) config).setRuleBaseUpdateHandler(null);
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase(config);
    KieSession session = createKnowledgeSession(kbase);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    session.addEventListener(ael);
    for (int i = 0; i < 5; i++) {
        session.insert(new Cheese());
    }
    kbase.addPackages(loadKnowledgePackages("test_JBRULES_2206_1.drl"));
    ((InternalAgenda) session.getAgenda()).evaluateEagerList();
    // two matching rules were added, so 2 activations should have been created
    verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
    int fireCount = session.fireAllRules();
    // both should have fired
    assertEquals(2, fireCount);
    kbase.addPackages(loadKnowledgePackages("test_JBRULES_2206_2.drl"));
    ((InternalAgenda) session.getAgenda()).evaluateEagerList();
    // one rule was overridden and should activate
    verify(ael, times(3)).matchCreated(any(MatchCreatedEvent.class));
    fireCount = session.fireAllRules();
    // that rule should fire again
    assertEquals(1, fireCount);
    session.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) InternalAgenda(org.drools.core.common.InternalAgenda) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 8 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class ActivationIteratorTest method testFilteredEagerEvaluation.

@Test(timeout = 10000)
public void testFilteredEagerEvaluation() throws Exception {
    String str = "package org.simple \n" + "rule xxx @Propagation(EAGER) \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n" + "rule yyy @Propagation(EAGER) \n" + "when \n" + "  $s : String()\n" + "then \n" + "end  \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(new ForceEagerActivationOption.FILTERED(new ForceEagerActivationFilter() {

        @Override
        public boolean accept(Rule rule) {
            return rule.getName().equals("xxx");
        }
    }));
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    final List list = new ArrayList();
    AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {

        public void matchCreated(org.kie.api.event.rule.MatchCreatedEvent event) {
            list.add("activated");
        }
    };
    ksession.addEventListener(agendaEventListener);
    ksession.insert("test");
    ((InternalWorkingMemory) ksession).flushPropagations();
    assertEquals(1, list.size());
}
Also used : ForceEagerActivationOption(org.kie.internal.runtime.conf.ForceEagerActivationOption) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) ForceEagerActivationFilter(org.kie.internal.runtime.conf.ForceEagerActivationFilter) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 9 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class TraitTest method testIsAOperator.

@Test(timeout = 10000)
public void testIsAOperator() {
    String source = "org/drools/compiler/factmodel/traits/testTraitIsA2.drl";
    KieSession ksession = getSession(source);
    TraitFactory.setMode(mode, ksession.getKieBase());
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    Person student = new Person("student", 18);
    ksession.insert(student);
    ksession.fireAllRules();
    ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael, times(3)).afterMatchFired(cap.capture());
    List<AfterMatchFiredEvent> values = cap.getAllValues();
    assertThat(values.get(0).getMatch().getRule().getName(), is("create student"));
    assertThat(values.get(1).getMatch().getRule().getName(), is("print student"));
    assertThat(values.get(2).getMatch().getRule().getName(), is("print school"));
}
Also used : DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 10 with AgendaEventListener

use of org.kie.api.event.rule.AgendaEventListener in project drools by kiegroup.

the class CepEspTest method testSalienceWithEventsPseudoClock.

@Test(timeout = 10000)
public void testSalienceWithEventsPseudoClock() throws IOException, ClassNotFoundException {
    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.PSEUDO_CLOCK.getId()));
    KieSession ksession = kbase.newKieSession(ksconf, null);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1000000, TimeUnit.MILLISECONDS);
    ksession.insert(new StockTick(1, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(2, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(3, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(4, "ACME", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    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) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) 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

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