use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class ConsequenceTest method testMVELConsequenceWithoutSemiColon1.
@Test
public void testMVELConsequenceWithoutSemiColon1() throws Exception {
String drl = "";
drl += "package test\n";
drl += "import org.drools.compiler.Person\n";
drl += "import org.drools.compiler.Pet\n";
drl += "rule test dialect 'mvel'\n";
drl += "when\n";
drl += " $person:Person()\n";
drl += " $pet:Pet()\n";
drl += "then\n";
drl += " delete($person) // some comment\n";
drl += " delete($pet) // another comment\n";
drl += "end\n";
final KieBase kbase = loadKnowledgeBaseFromString(drl);
final KieSession ksession = createKnowledgeSession(kbase);
// create working memory mock listener
final RuleRuntimeEventListener wml = Mockito.mock(RuleRuntimeEventListener.class);
ksession.addEventListener(wml);
final FactHandle personFH = ksession.insert(new Person("Toni"));
final FactHandle petFH = ksession.insert(new Pet("Toni"));
final int fired = ksession.fireAllRules();
assertEquals(1, fired);
// capture the arguments and check that the retracts happened
final ArgumentCaptor<ObjectDeletedEvent> retracts = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
verify(wml, times(2)).objectDeleted(retracts.capture());
final List<ObjectDeletedEvent> values = retracts.getAllValues();
assertThat(values.get(0).getFactHandle(), is(personFH));
assertThat(values.get(1).getFactHandle(), is(petFH));
}
use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class RuleRuntimeEventTest method testEventModel.
@Test
public void testEventModel() throws Exception {
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_EventModel.drl"));
final KieSession wm = createKnowledgeSession(kbase);
final RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
wm.addEventListener(wmel);
final Cheese stilton = new Cheese("stilton", 15);
final FactHandle stiltonHandle = wm.insert(stilton);
final ArgumentCaptor<ObjectInsertedEvent> oic = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectInsertedEvent.class);
verify(wmel).objectInserted(oic.capture());
assertSame(stiltonHandle, oic.getValue().getFactHandle());
wm.update(stiltonHandle, stilton);
final ArgumentCaptor<org.kie.api.event.rule.ObjectUpdatedEvent> ouc = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectUpdatedEvent.class);
verify(wmel).objectUpdated(ouc.capture());
assertSame(stiltonHandle, ouc.getValue().getFactHandle());
wm.delete(stiltonHandle);
final ArgumentCaptor<ObjectDeletedEvent> orc = ArgumentCaptor.forClass(ObjectDeletedEvent.class);
verify(wmel).objectDeleted(orc.capture());
assertSame(stiltonHandle, orc.getValue().getFactHandle());
}
use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class RuleChainingTest method testRuleChainingWithLogicalInserts.
@Test
public void testRuleChainingWithLogicalInserts() throws Exception {
final KieBase kbase = loadKnowledgeBase("test_RuleChaining.drl");
final KieSession ksession = createKnowledgeSession(kbase);
// create working memory mock listener
final RuleRuntimeEventListener wml = Mockito.mock(RuleRuntimeEventListener.class);
final org.kie.api.event.rule.AgendaEventListener ael = Mockito.mock(org.kie.api.event.rule.AgendaEventListener.class);
ksession.addEventListener(wml);
ksession.addEventListener(ael);
final int fired = ksession.fireAllRules();
assertEquals(3, fired);
// capture the arguments and check that the rules fired in the proper sequence
final ArgumentCaptor<AfterMatchFiredEvent> actvs = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
verify(ael, times(3)).afterMatchFired(actvs.capture());
final List<AfterMatchFiredEvent> values = actvs.getAllValues();
assertThat(values.get(0).getMatch().getRule().getName(), is("init"));
assertThat(values.get(1).getMatch().getRule().getName(), is("r1"));
assertThat(values.get(2).getMatch().getRule().getName(), is("r2"));
verify(ael, never()).matchCancelled(any(org.kie.api.event.rule.MatchCancelledEvent.class));
verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
verify(wml, never()).objectDeleted(any(ObjectDeletedEvent.class));
}
use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class TraitTest method testTraitdeleteOrder.
@Test(timeout = 10000)
public void testTraitdeleteOrder() {
String drl = "" + "package org.drools.core.factmodel.traits.test;\n" + "\n" + "import org.drools.core.factmodel.traits.*;\n" + "import java.util.*;\n" + "\n" + "declare trait A end \n" + "declare trait B extends A end \n" + "declare trait C end \n" + "\n" + "rule \"don\"\n" + "when \n" + " $e : Entity() \n" + "then\n" + " don( $e, A.class ); \n" + " don( $e, C.class ); \n" + " don( $e, B.class ); \n" + "end\n" + "";
KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession();
TraitFactory.setMode(mode, ksession.getKieBase());
FactHandle handle = ksession.insert(new Entity());
ksession.fireAllRules();
final ArrayList list = new ArrayList();
ksession.addEventListener(new RuleRuntimeEventListener() {
public void objectInserted(org.kie.api.event.rule.ObjectInsertedEvent objectInsertedEvent) {
}
public void objectUpdated(org.kie.api.event.rule.ObjectUpdatedEvent objectUpdatedEvent) {
}
public void objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent objectRetractedEvent) {
Object o = objectRetractedEvent.getOldObject();
if (o instanceof TraitProxy) {
String traitName = ((TraitProxy) o)._getTraitName();
list.add(traitName.substring(traitName.lastIndexOf(".") + 1));
}
}
});
ksession.delete(handle);
ksession.fireAllRules();
System.out.println(list);
assertEquals(Arrays.asList("B", "C", "A"), list);
}
use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class RuleRuntimeEventSupportTest method testRuleRuntimeEventListener.
@Test
public void testRuleRuntimeEventListener() {
final KieBase rb = KnowledgeBaseFactory.newKnowledgeBase();
final KieSession wm = rb.newKieSession();
final List wmList = new ArrayList();
final RuleRuntimeEventListener workingMemoryListener = new RuleRuntimeEventListener() {
public void objectInserted(ObjectInsertedEvent event) {
wmList.add(event);
}
public void objectUpdated(ObjectUpdatedEvent event) {
wmList.add(event);
}
public void objectDeleted(ObjectDeletedEvent event) {
wmList.add(event);
}
};
wm.addEventListener(workingMemoryListener);
assertEquals(1, wm.getRuleRuntimeEventListeners().size());
final Cheese stilton = new Cheese("stilton", 15);
final Cheese cheddar = new Cheese("cheddar", 17);
final FactHandle stiltonHandle = wm.insert(stilton);
ObjectInsertedEvent oae = (ObjectInsertedEvent) wmList.get(0);
assertSame(stiltonHandle, oae.getFactHandle());
wm.update(stiltonHandle, cheddar);
final ObjectUpdatedEvent ome = (ObjectUpdatedEvent) wmList.get(1);
assertSame(stiltonHandle, ome.getFactHandle());
assertEquals(cheddar, ome.getObject());
assertEquals(stilton, ome.getOldObject());
wm.retract(stiltonHandle);
final ObjectDeletedEvent ore = (ObjectDeletedEvent) wmList.get(2);
assertSame(stiltonHandle, ore.getFactHandle());
final FactHandle cheddarHandle = wm.insert(cheddar);
oae = (ObjectInsertedEvent) wmList.get(3);
assertSame(cheddarHandle, oae.getFactHandle());
}
Aggregations