Search in sources :

Example 6 with RuleRuntimeEventListener

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());
}
Also used : RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.compiler.Cheese) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) Person(org.drools.compiler.Person) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) Test(org.junit.Test)

Example 7 with RuleRuntimeEventListener

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));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 8 with RuleRuntimeEventListener

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));
}
Also used : PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 9 with RuleRuntimeEventListener

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());
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) Test(org.junit.Test)

Example 10 with RuleRuntimeEventListener

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);
}
Also used : ObjectDeletedEvent(org.kie.api.event.rule.ObjectDeletedEvent) RuleRuntimeEventListener(org.kie.api.event.rule.RuleRuntimeEventListener) FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) ObjectUpdatedEvent(org.kie.api.event.rule.ObjectUpdatedEvent) Cheese(org.drools.core.test.model.Cheese) ObjectInsertedEvent(org.kie.api.event.rule.ObjectInsertedEvent) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 RuleRuntimeEventListener (org.kie.api.event.rule.RuleRuntimeEventListener)12 KieSession (org.kie.api.runtime.KieSession)11 KieBase (org.kie.api.KieBase)10 ObjectDeletedEvent (org.kie.api.event.rule.ObjectDeletedEvent)7 FactHandle (org.kie.api.runtime.rule.FactHandle)7 ArrayList (java.util.ArrayList)5 ObjectInsertedEvent (org.kie.api.event.rule.ObjectInsertedEvent)5 ObjectUpdatedEvent (org.kie.api.event.rule.ObjectUpdatedEvent)5 List (java.util.List)4 Person (org.drools.compiler.Person)3 Cheese (org.drools.core.test.model.Cheese)3 Cheese (org.drools.compiler.Cheese)2 StockTick (org.drools.compiler.StockTick)2 StockTickInterface (org.drools.compiler.StockTickInterface)2 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)2 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)2 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)2 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)2 MatchCreatedEvent (org.kie.api.event.rule.MatchCreatedEvent)2