use of org.kie.api.event.rule.MatchCreatedEvent 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.MatchCreatedEvent in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndUpdate.
@Test
public void testActivateOnMatchAndUpdate() throws Exception {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Alarm.class.getCanonicalName() + " \n";
str += "import " + Sensor.class.getCanonicalName() + " \n";
str += "rule StringRule @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
str += " when \n";
str += " $a : Alarm() \n";
str += " $s : Sensor() \n";
str += " then \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
for (FactHandle alarm : alarms) {
event.getKieRuntime().update(alarm, new Alarm());
}
}
});
// go !
Alarm alarm = new Alarm();
alarm.setMessage("test");
alarm.setNumber(123);
ksession.insert(alarm);
Sensor sensor = new Sensor();
sensor.setPressure(1);
sensor.setTemperature(25);
ksession.insert(sensor);
}
use of org.kie.api.event.rule.MatchCreatedEvent in project drools by kiegroup.
the class ActivateAndDeleteOnListenerTest method testActivateOnMatchAndDelete.
@Test
public void testActivateOnMatchAndDelete() throws Exception {
String str = "";
str += "package org.drools.compiler.integrationtests \n";
str += "import " + Alarm.class.getCanonicalName() + " \n";
str += "import " + Sensor.class.getCanonicalName() + " \n";
str += "rule StringRule @Propagation(EAGER) ruleflow-group \"DROOLS_SYSTEM\"\n";
str += " when \n";
str += " $a : Alarm() \n";
str += " $s : Sensor() \n";
str += " then \n";
str += "end \n";
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ForceEagerActivationOption.YES);
KieSession ksession = new KieHelper().addContent(str, ResourceType.DRL).build().newKieSession(conf, null);
ksession.addEventListener(new DefaultAgendaEventListener() {
@Override
public void matchCreated(MatchCreatedEvent event) {
Collection<? extends FactHandle> alarms = event.getKieRuntime().getFactHandles(new ClassObjectFilter(Alarm.class));
for (FactHandle alarm : alarms) {
event.getKieRuntime().delete(alarm);
}
}
});
// go !
Alarm alarm = new Alarm();
alarm.setMessage("test");
alarm.setNumber(123);
ksession.insert(alarm);
Sensor sensor = new Sensor();
sensor.setPressure(1);
sensor.setTemperature(25);
ksession.insert(sensor);
ksession.fireAllRules();
}
use of org.kie.api.event.rule.MatchCreatedEvent 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.MatchCreatedEvent 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