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