use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class BindTest method testFactBindings.
@Test
public void testFactBindings() throws Exception {
final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_FactBindings.drl"));
final KieSession ksession = createKnowledgeSession(kbase);
final RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
ksession.addEventListener(wmel);
final Person bigCheese = new Person("big cheese");
final Cheese cheddar = new Cheese("cheddar", 15);
bigCheese.setCheese(cheddar);
final FactHandle bigCheeseHandle = ksession.insert(bigCheese);
final FactHandle cheddarHandle = ksession.insert(cheddar);
ksession.fireAllRules();
final ArgumentCaptor<ObjectUpdatedEvent> arg = ArgumentCaptor.forClass(org.kie.api.event.rule.ObjectUpdatedEvent.class);
verify(wmel, times(2)).objectUpdated(arg.capture());
org.kie.api.event.rule.ObjectUpdatedEvent event = arg.getAllValues().get(0);
assertSame(cheddarHandle, event.getFactHandle());
assertSame(cheddar, event.getOldObject());
assertSame(cheddar, event.getObject());
event = arg.getAllValues().get(1);
assertSame(bigCheeseHandle, event.getFactHandle());
assertSame(bigCheese, event.getOldObject());
assertSame(bigCheese, event.getObject());
}
use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class StreamsTest method testEventDoesNotExpireIfNotInPattern.
@Test(timeout = 10000)
public void testEventDoesNotExpireIfNotInPattern() throws Exception {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase("test_EventExpiration.drl", kconf);
KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(ksessionConfig, null);
RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
ksession.addEventListener(wml);
PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
final StockTickInterface st1 = new StockTick(1, "RHT", 100, 1000);
final StockTickInterface st2 = new StockTick(2, "RHT", 100, 1000);
ksession.insert(st1);
ksession.insert(st2);
verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
assertThat(ksession.getObjects().size(), equalTo(2));
assertThat((Collection<Object>) ksession.getObjects(), hasItems((Object) st1, st2));
ksession.fireAllRules();
clock.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertThat(ksession.getObjects().size(), equalTo(0));
}
use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class StreamsTest method testEventExpirationSetToZero.
@Test(timeout = 10000)
public void testEventExpirationSetToZero() throws Exception {
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBase("test_EventExpirationSetToZero.drl", kconf);
KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kbase.newKieSession(ksessionConfig, null);
RuleRuntimeEventListener wml = mock(RuleRuntimeEventListener.class);
ksession.addEventListener(wml);
AgendaEventListener ael = mock(AgendaEventListener.class);
ksession.addEventListener(ael);
PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
final StockTickInterface st1 = new StockTick(1, "RHT", 100, 1000);
final StockTickInterface st2 = new StockTick(2, "RHT", 100, 1000);
ksession.insert(st1);
ksession.insert(st2);
assertThat(ksession.fireAllRules(), equalTo(2));
verify(wml, times(2)).objectInserted(any(org.kie.api.event.rule.ObjectInsertedEvent.class));
verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
assertThat(ksession.getObjects().size(), equalTo(2));
assertThat((Collection<Object>) ksession.getObjects(), hasItems((Object) st1, st2));
clock.advanceTime(3, TimeUnit.SECONDS);
ksession.fireAllRules();
assertThat(ksession.getObjects().size(), equalTo(0));
}
use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class TruthMaintenanceTest method testLogicalInsertOrder.
@Test(timeout = 10000)
public void testLogicalInsertOrder() throws Exception {
// JBRULES-1602
// "rule 1" is never logical inserted, as it's rule is unmatched prior to calling logical insert
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("test_LogicalInsertOrder.drl", getClass()), ResourceType.DRL);
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
kbase = SerializationHelper.serializeObject(kbase);
final KieSession session = createKnowledgeSession(kbase);
RuleRuntimeEventListener wmel = mock(RuleRuntimeEventListener.class);
session.addEventListener(wmel);
Person bob = new Person("bob");
bob.setStatus("hungry");
Person mark = new Person("mark");
mark.setStatus("thirsty");
session.insert(bob);
session.insert(mark);
int count = session.fireAllRules();
assertEquals(2, count);
assertEquals(2, session.getObjects().size());
TruthMaintenanceSystem tms = ((NamedEntryPoint) session.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())).getTruthMaintenanceSystem();
assertTrue(tms.getEqualityKeyMap().isEmpty());
}
use of org.kie.api.event.rule.RuleRuntimeEventListener in project drools by kiegroup.
the class RuleRuntimeEventSupportTest method testAddRuleRuntimeEventListener.
@Test
public void testAddRuleRuntimeEventListener() {
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = kbase.newKieSession();
final List wmList = new ArrayList();
final RuleRuntimeEventListener eventListener = 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);
}
};
ksession.addEventListener(eventListener);
final Cheese stilton = new Cheese("stilton", 15);
final Cheese cheddar = new Cheese("cheddar", 17);
final FactHandle stiltonHandle = ksession.insert(stilton);
final ObjectInsertedEvent oae = (ObjectInsertedEvent) wmList.get(0);
assertSame(stiltonHandle, oae.getFactHandle());
ksession.update(stiltonHandle, stilton);
final ObjectUpdatedEvent ome = (ObjectUpdatedEvent) wmList.get(1);
assertSame(stiltonHandle, ome.getFactHandle());
ksession.retract(stiltonHandle);
final ObjectDeletedEvent ore = (ObjectDeletedEvent) wmList.get(2);
assertSame(stiltonHandle, ore.getFactHandle());
ksession.insert(cheddar);
}
Aggregations