Search in sources :

Example 6 with AfterMatchFiredEvent

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

the class Misc2Test method testLockOnActive1.

@Test
public void testLockOnActive1() {
    // the modify changes the hashcode of TradeHeader
    // this forces the 'from' to think it's new. This results in an insert and a delete propagation from the 'from'
    // With Property Reactivity enabled by default this also required adding a @watch(*) annotation
    String drl = "" + "package org.drools.test; \n" + "import org.drools.compiler.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.compiler.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "salience 1 \n" + "when\n" + "  $booking: TradeBooking() @watch(*) \n" + "  $trade: TradeHeader() from $booking.getTrade()\n" + "  not String()\n" + "then\n" + "  $trade.setAction(\"New\");\n" + "  modify($booking) {}\n" + "  insert (\"run\");\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "lock-on-active true\n" + "when\n" + "  $booking: TradeBooking( ) @watch(*) \n" + "  $trade: Object( ) from $booking.getTrade()\n" + "then\n" + "end";
    KieBase kb = loadKnowledgeBaseFromString(drl);
    KieSession ks = kb.newKieSession();
    ReteDumper.dumpRete(kb);
    final List created = new ArrayList();
    final List cancelled = new ArrayList();
    final List fired = new ArrayList();
    ks.addEventListener(new DefaultAgendaEventListener() {

        public void matchCreated(MatchCreatedEvent event) {
            created.add(event.getMatch().getRule().getName());
        }

        public void matchCancelled(MatchCancelledEvent event) {
            cancelled.add(event.getMatch().getRule().getName());
        }

        public void afterMatchFired(AfterMatchFiredEvent event) {
            fired.add(event.getMatch().getRule().getName());
        }
    });
    ks.fireAllRules();
    TradeBooking tb = new TradeBookingImpl(new TradeHeaderImpl());
    ks.insert(tb);
    assertEquals(1, ks.fireAllRules());
    assertEquals(3, created.size());
    assertEquals(2, cancelled.size());
    assertEquals(1, fired.size());
    assertEquals("Rule2", created.get(0));
    assertEquals("Rule1", created.get(1));
    assertEquals("Rule2", created.get(2));
    assertEquals("Rule2", cancelled.get(0));
    assertEquals("Rule2", cancelled.get(1));
    assertEquals("Rule1", fired.get(0));
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) MatchCancelledEvent(org.kie.api.event.rule.MatchCancelledEvent) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 7 with AfterMatchFiredEvent

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

the class MapConstraintTest method testMapNullConstraint.

@Test
public void testMapNullConstraint() throws Exception {
    final KieBase kbase = loadKnowledgeBase("test_mapNullConstraints.drl");
    final KieSession ksession = createKnowledgeSession(kbase);
    final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    new WorkingMemoryConsoleLogger((WorkingMemory) ksession);
    final Map addresses = new HashMap();
    addresses.put("home", new Address("home street"));
    final Person bob = new Person("Bob");
    bob.setNamedAddresses(addresses);
    ksession.insert(bob);
    ksession.fireAllRules();
    final ArgumentCaptor<AfterMatchFiredEvent> arg = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(4)).afterMatchFired(arg.capture());
    org.kie.api.event.rule.AfterMatchFiredEvent aaf = arg.getAllValues().get(0);
    assertThat(aaf.getMatch().getRule().getName(), is("1. home != null"));
    aaf = arg.getAllValues().get(1);
    assertThat(aaf.getMatch().getRule().getName(), is("2. not home == null"));
    aaf = arg.getAllValues().get(2);
    assertThat(aaf.getMatch().getRule().getName(), is("7. work == null"));
    aaf = arg.getAllValues().get(3);
    assertThat(aaf.getMatch().getRule().getName(), is("8. not work != null"));
}
Also used : Address(org.drools.compiler.Address) HashMap(java.util.HashMap) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) WorkingMemoryConsoleLogger(org.drools.core.audit.WorkingMemoryConsoleLogger) KieBase(org.kie.api.KieBase) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieSession(org.kie.api.runtime.KieSession) HashMap(java.util.HashMap) Map(java.util.Map) Person(org.drools.compiler.Person) Test(org.junit.Test)

Example 8 with AfterMatchFiredEvent

use of org.kie.api.event.rule.AfterMatchFiredEvent 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 9 with AfterMatchFiredEvent

use of org.kie.api.event.rule.AfterMatchFiredEvent 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)

Example 10 with AfterMatchFiredEvent

use of org.kie.api.event.rule.AfterMatchFiredEvent 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)

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