Search in sources :

Example 56 with StockTick

use of org.drools.compiler.StockTick 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 57 with StockTick

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

the class StreamsTest method testEntryPointWithAccumulateAndMVEL.

@Test(timeout = 10000)
public void testEntryPointWithAccumulateAndMVEL() throws Exception {
    String str = "package org.drools.compiler\n" + "rule R1 dialect 'mvel'\n" + "    when\n" + "        $n : Number() from accumulate( \n" + "                 StockTick() from entry-point ep1,\n" + "                 count(1))" + "    then\n" + "end\n";
    // read in the source
    KieBase kbase = loadKnowledgeBaseFromString((KieBaseConfiguration) null, str);
    KieSession ksession = createKnowledgeSession(kbase);
    org.kie.api.event.rule.AgendaEventListener ael = mock(org.kie.api.event.rule.AgendaEventListener.class);
    ksession.addEventListener(ael);
    EntryPoint ep1 = ksession.getEntryPoint("ep1");
    ep1.insert(new StockTick(1, "RHT", 10, 1000));
    int rulesFired = ksession.fireAllRules();
    assertEquals(1, rulesFired);
    ArgumentCaptor<org.kie.api.event.rule.AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(org.kie.api.event.rule.AfterMatchFiredEvent.class);
    verify(ael, times(1)).afterMatchFired(captor.capture());
    List<org.kie.api.event.rule.AfterMatchFiredEvent> aafe = captor.getAllValues();
    assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
}
Also used : AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) EntryPoint(org.kie.api.runtime.rule.EntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 58 with StockTick

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

the class StreamsTest method testModifyRetracOnEntryPointFacts.

@Test(timeout = 10000)
public void testModifyRetracOnEntryPointFacts() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_modifyRetractEntryPoint.drl");
    KieSession session = kbase.newKieSession();
    final List<? extends Number> results = new ArrayList<Number>();
    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();
    System.out.println(results);
    assertEquals(2, results.size());
    assertEquals(30, ((Number) results.get(0)).intValue());
    assertEquals(110, ((Number) results.get(1)).intValue());
    // the 3 non-matched facts continue to exist in the entry point
    assertEquals(3, entry.getObjects().size());
    // but no fact was inserted into the main session
    assertEquals(0, session.getObjects().size());
}
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 59 with StockTick

use of org.drools.compiler.StockTick 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 60 with StockTick

use of org.drools.compiler.StockTick 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)

Aggregations

StockTick (org.drools.compiler.StockTick)64 Test (org.junit.Test)61 KieSession (org.kie.api.runtime.KieSession)60 KieBase (org.kie.api.KieBase)47 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)40 ArrayList (java.util.ArrayList)37 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)31 EntryPoint (org.kie.api.runtime.rule.EntryPoint)27 StockTickInterface (org.drools.compiler.StockTickInterface)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)16 List (java.util.List)15 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)13 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)11 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)9 EventFactHandle (org.drools.core.common.EventFactHandle)8 IOException (java.io.IOException)7 ParseException (java.text.ParseException)7