Search in sources :

Example 21 with StockTickInterface

use of org.drools.compiler.StockTickInterface 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 22 with StockTickInterface

use of org.drools.compiler.StockTickInterface in project drools by kiegroup.

the class StreamsTest method testEntryPointReference.

// (timeout=10000)
@Test
public void testEntryPointReference() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_EntryPointReference.drl");
    KieSession session = kbase.newKieSession();
    final List<StockTick> results = new ArrayList<StockTick>();
    session.setGlobal("results", results);
    StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
    StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
    EntryPoint entry = session.getEntryPoint("stream1");
    InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
    InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
    InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
    InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
    assertNotNull(handle5);
    assertNotNull(handle6);
    assertNotNull(handle7);
    assertNotNull(handle8);
    assertTrue(handle5.isEvent());
    assertTrue(handle6.isEvent());
    assertTrue(handle7.isEvent());
    assertTrue(handle8.isEvent());
    session.fireAllRules();
    assertEquals(1, results.size());
    assertSame(tick7, results.get(0));
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 23 with StockTickInterface

use of org.drools.compiler.StockTickInterface in project drools by kiegroup.

the class PseudoClockEventsTest method processStocks.

private int processStocks(int stockCount, AgendaEventListener agendaEventListener, String drlContentString) throws DroolsParserException, IOException, Exception {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drlContentString);
    KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
    ksessionConfig.setProperty("keep.reference", "true");
    final KieSession ksession = kbase.newKieSession(ksessionConfig, null);
    ksession.addEventListener(agendaEventListener);
    PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    Runnable fireUntilHaltRunnable = new Runnable() {

        public void run() {
            ksession.fireUntilHalt();
        }
    };
    Thread fireUntilHaltThread = new Thread(fireUntilHaltRunnable, "Engine's thread");
    fireUntilHaltThread.start();
    Thread.currentThread().setName("Feeding thread");
    for (int stIndex = 1; stIndex <= stockCount; stIndex++) {
        clock.advanceTime(20, TimeUnit.SECONDS);
        Thread.sleep(100);
        final StockTickInterface st = new StockTick(stIndex, "RHT", 100 * stIndex, 100 * stIndex);
        ksession.insert(st);
        Thread.sleep(100);
    }
    Thread.sleep(100);
    ksession.halt();
    fireUntilHaltThread.join(5000);
    return stockCount;
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) 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)

Aggregations

StockTickInterface (org.drools.compiler.StockTickInterface)23 StockTick (org.drools.compiler.StockTick)22 Test (org.junit.Test)22 KieSession (org.kie.api.runtime.KieSession)22 KieBase (org.kie.api.KieBase)21 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)18 ArrayList (java.util.ArrayList)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)17 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)13 List (java.util.List)11 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)7 EventFactHandle (org.drools.core.common.EventFactHandle)5 EntryPoint (org.kie.api.runtime.rule.EntryPoint)5 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)4 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)4 DebugAgendaEventListener (org.kie.api.event.rule.DebugAgendaEventListener)3 DefaultAgendaEventListener (org.kie.api.event.rule.DefaultAgendaEventListener)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2