Search in sources :

Example 21 with AfterMatchFiredEvent

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

the class StreamsTest method testWindowDeclaration.

public void testWindowDeclaration() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "declare window RedHatTicks\n" + "    StockTick( company == 'RHT' )\n" + "               over window:length(5)\n" + "               from entry-point ticks\n" + "end\n" + "rule X\n" + "when\n" + "    accumulate( $s : StockTick( price > 20 ) from window RedHatTicks,\n" + "                $sum : sum( $s.getPrice() ),\n" + "                $cnt : count( $s ) )\n" + "then\n" + "end\n";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep = ksession.getEntryPoint("ticks");
    // not in the window
    ep.insert(new StockTick(1, "ACME", 20, 1000));
    // not > 20
    ep.insert(new StockTick(2, "RHT", 20, 1000));
    ep.insert(new StockTick(3, "RHT", 30, 1000));
    // not in the window
    ep.insert(new StockTick(4, "ACME", 30, 1000));
    ep.insert(new StockTick(5, "RHT", 25, 1000));
    // not in the window
    ep.insert(new StockTick(6, "ACME", 10, 1000));
    // not > 20
    ep.insert(new StockTick(7, "RHT", 10, 1000));
    ep.insert(new StockTick(8, "RHT", 40, 1000));
    ksession.fireAllRules();
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    AfterMatchFiredEvent aafe = captor.getValue();
    assertThat(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(95));
    assertThat(((Number) aafe.getMatch().getDeclarationValue("$cnt")).intValue(), is(3));
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession)

Example 22 with AfterMatchFiredEvent

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

the class StreamsTest method testWindowDeclaration2.

@Test(timeout = 10000)
public void testWindowDeclaration2() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare Double\n" + "    @role(event)\n" + "end\n" + "declare window Streem\n" + "    Double() over window:length( 10 ) from entry-point data\n" + "end\n" + "rule \"See\"\n" + "when\n" + "    $sum : Double() from accumulate (\n" + "        $d: Double()\n" + "            from window Streem,\n" + "        sum( $d )\n" + "    )\n" + "then\n" + "end";
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep = ksession.getEntryPoint("data");
    ep.insert(Double.valueOf(10));
    ep.insert(Double.valueOf(11));
    ep.insert(Double.valueOf(12));
    ksession.fireAllRules();
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    AfterMatchFiredEvent aafe = captor.getValue();
    assertThat(((Number) aafe.getMatch().getDeclarationValue("$sum")).intValue(), is(33));
}
Also used : EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 23 with AfterMatchFiredEvent

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

the class Misc2Test method testLockOnActive2.

@Test
public void testLockOnActive2() {
    // 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" + "lock-on-active true\n" + "salience 1 \n" + "when\n" + "  $booking: TradeBooking() @watch(*) \n" + "  $trade: TradeHeader() from $booking.getTrade()\n" + "then\n" + "  $trade.setAction(\"New\");\n" + "  modify($booking) {}\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "when\n" + "  $booking: TradeBooking( ) @watch(*) \n" + "  $trade: Object( ) from $booking.getTrade()\n" + "then\n" + "end";
    KieBase kb = loadKnowledgeBaseFromString(drl);
    KieSession ks = kb.newKieSession();
    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(2, ks.fireAllRules());
    assertEquals(3, created.size());
    assertEquals(1, cancelled.size());
    assertEquals(2, fired.size());
    assertEquals("Rule1", created.get(0));
    assertEquals("Rule1", created.get(1));
    assertEquals("Rule2", created.get(2));
    assertEquals("Rule1", cancelled.get(0));
    assertEquals("Rule1", fired.get(0));
    assertEquals("Rule2", fired.get(1));
}
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 24 with AfterMatchFiredEvent

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

the class RuleCoverageListenerTest method testCoverage.

@Test
public void testCoverage() throws Exception {
    // configuring mock event
    AfterMatchFiredEvent amfe = mock(AfterMatchFiredEvent.class);
    Match match = mock(Match.class);
    Rule rule = mock(Rule.class);
    when(amfe.getMatch()).thenReturn(match);
    when(match.getRule()).thenReturn(rule);
    when(rule.getName()).thenReturn("rule1").thenReturn("rule2").thenReturn("rule3");
    HashSet<String> rules = new HashSet<String>();
    rules.add("rule1");
    rules.add("rule2");
    rules.add("rule3");
    RuleCoverageListener ls = new RuleCoverageListener(rules);
    Assert.assertEquals(3, ls.rules.size());
    Assert.assertEquals(0, ls.getPercentCovered());
    ls.afterMatchFired(amfe);
    Assert.assertEquals(2, ls.rules.size());
    assertTrue(ls.rules.contains("rule2"));
    assertTrue(ls.rules.contains("rule3"));
    assertFalse(ls.rules.contains("rule1"));
    Assert.assertEquals(33, ls.getPercentCovered());
    ls.afterMatchFired(amfe);
    Assert.assertEquals(1, ls.rules.size());
    assertFalse(ls.rules.contains("rule2"));
    assertFalse(ls.rules.contains("rule1"));
    assertTrue(ls.rules.contains("rule3"));
    Assert.assertEquals(66, ls.getPercentCovered());
    ls.afterMatchFired(amfe);
    Assert.assertEquals(0, ls.rules.size());
    assertFalse(ls.rules.contains("rule2"));
    assertFalse(ls.rules.contains("rule1"));
    assertFalse(ls.rules.contains("rule3"));
    Assert.assertEquals(100, ls.getPercentCovered());
}
Also used : Rule(org.kie.api.definition.rule.Rule) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Match(org.kie.api.runtime.rule.Match) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 25 with AfterMatchFiredEvent

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

the class NestedAccessorsTest method testNestedAccessors2.

@Test
public void testNestedAccessors2() throws Exception {
    final String rule = "package org.drools.compiler\n" + "rule 'rule1'" + "    salience 10\n" + "when\n" + "    Cheesery( typedCheeses[0].type == 'stilton' );\n" + "then\n" + "end\n" + "rule 'rule2'\n" + "when\n" + "    Cheesery( typedCheeses[0].price == 10 );\n" + "then\n" + "end";
    final KieBase kbase = loadKnowledgeBaseFromString(rule);
    final KieSession ksession = createKnowledgeSession(kbase);
    final org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    final Cheesery c1 = new Cheesery();
    c1.addCheese(new Cheese("stilton", 20));
    final Cheesery c2 = new Cheesery();
    c2.addCheese(new Cheese("brie", 10));
    final Cheesery c3 = new Cheesery();
    c3.addCheese(new Cheese("muzzarella", 30));
    ksession.insert(c1);
    ksession.insert(c2);
    ksession.insert(c3);
    ksession.fireAllRules();
    final ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(2)).afterMatchFired(captor.capture());
    final List<org.kie.api.event.rule.AfterMatchFiredEvent> values = captor.getAllValues();
    assertThat(values.get(0).getMatch().getObjects().get(0), is(c1));
    assertThat(values.get(1).getMatch().getObjects().get(0), is(c2));
    ksession.dispose();
}
Also used : AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) 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