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));
}
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));
}
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));
}
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());
}
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();
}
Aggregations