use of org.kie.api.event.rule.MatchCancelledEvent 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.MatchCancelledEvent in project jbpm by kiegroup.
the class ProcessFlowControlTest method testRuleFlowClear.
@Test
public void testRuleFlowClear() throws Exception {
builder.addPackageFromDrl(new InputStreamReader(getClass().getResourceAsStream("test_ruleflowClear.drl")));
builder.addRuleFlow(new InputStreamReader(getClass().getResourceAsStream("test_ruleflowClear.rfm")));
KieSession workingMemory = createKieSession(true, builder.getPackages());
final List<String> list = new ArrayList<String>();
workingMemory.setGlobal("list", list);
final List<Match> activations = new ArrayList<Match>();
AgendaEventListener listener = new DefaultAgendaEventListener() {
public void matchCancelled(MatchCancelledEvent event) {
activations.add(event.getMatch());
}
};
workingMemory.addEventListener(listener);
InternalAgenda agenda = (InternalAgenda) workingMemory.getAgenda();
// assertEquals( 0,
// agenda.getRuleFlowGroup( "flowgroup-1" ).size() );
// We need to call fireAllRules here to get the InitialFact into the system, to the eval(true)'s kick in
workingMemory.fireAllRules();
agenda.evaluateEagerList();
// Now we have 4 in the RuleFlow, but not yet in the agenda
assertEquals(4, agenda.sizeOfRuleFlowGroup("flowgroup-1"));
// Check they aren't in the Agenda
assertEquals(0, agenda.getAgendaGroup("MAIN").size());
// Check we have 0 activation cancellation events
assertEquals(0, activations.size());
((InternalAgenda) workingMemory.getAgenda()).clearAndCancelRuleFlowGroup("flowgroup-1");
// Check the AgendaGroup and RuleFlowGroup are now empty
assertEquals(0, agenda.getAgendaGroup("MAIN").size());
assertEquals(0, agenda.sizeOfRuleFlowGroup("flowgroup-1"));
// Check we have four activation cancellation events
assertEquals(4, activations.size());
}
use of org.kie.api.event.rule.MatchCancelledEvent in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testEagerEvaluationWith2Paths.
@Test
public void testEagerEvaluationWith2Paths() {
final String drl = "package org.simple \n" + "rule xxx \n" + "when \n" + " $s : String()\n" + " $i : Integer()\n" + "then \n" + "end \n" + "rule yyy \n" + "when \n" + " $s : String()\n" + " $i : Integer()\n" + "then \n" + "end \n";
final KieSession ksession = getSessionWithEagerActivation(drl);
try {
final List<String> list = new ArrayList<>();
final AgendaEventListener agendaEventListener = new org.kie.api.event.rule.DefaultAgendaEventListener() {
@Override
public void matchCreated(final org.kie.api.event.rule.MatchCreatedEvent event) {
list.add("activated");
}
@Override
public void matchCancelled(final MatchCancelledEvent event) {
list.add("cancelled");
}
};
ksession.addEventListener(agendaEventListener);
ksession.insert("test");
assertEquals(0, list.size());
final FactHandle fh = ksession.insert(1);
assertEquals(2, list.size());
assertEquals("activated", list.get(0));
assertEquals("activated", list.get(1));
list.clear();
ksession.delete(fh);
assertEquals(2, list.size());
assertEquals("cancelled", list.get(0));
assertEquals("cancelled", list.get(1));
} finally {
ksession.dispose();
}
}
use of org.kie.api.event.rule.MatchCancelledEvent 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.mvel.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "lock-on-active true\n" + "salience 1 \n" + "when\n" + " $booking: TradeBooking()\n" + " $trade: TradeHeader() @watch(*) from $booking.getTrade()\n" + "then\n" + " $trade.setAction(\"New\");\n" + " modify($booking) {}\n" + "end;\n" + "\n" + "rule \"Rule2\"\n" + "when\n" + " $booking: TradeBooking( )\n" + " $trade: Object( ) @watch(*) from $booking.getTrade()\n" + "then\n" + "end";
KieBase kb = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, 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.MatchCancelledEvent 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.mvel.integrationtests.Misc2Test.TradeBooking;\n" + "import org.drools.mvel.integrationtests.Misc2Test.TradeHeader;\n" + "rule \"Rule1\" \n" + "salience 1 \n" + "when\n" + " $booking: TradeBooking()\n" + " $trade: TradeHeader() @watch(*) 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( )\n" + " $trade: Object( ) @watch(*) from $booking.getTrade()\n" + "then\n" + "end";
KieBase kb = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, 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));
}
Aggregations