Search in sources :

Example 11 with StockTickInterface

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

the class CepEspTest method testDelayingNot.

@Test(timeout = 10000)
public void testDelayingNot() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_DelayingNot.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final RuleImpl rule = (RuleImpl) kbase.getRule("org.drools.compiler", "Delaying Not");
    assertEquals(10000, ((DurationTimer) rule.getTimer()).getDuration());
    final List results = new ArrayList();
    wm.setGlobal("results", results);
    SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
    clock.advanceTime(10, TimeUnit.SECONDS);
    StockTickInterface st1O = new StockTick(1, "DROO", 100, clock.getCurrentTime());
    EventFactHandle st1 = (EventFactHandle) wm.insert(st1O);
    wm.fireAllRules();
    // should not fire, because it must wait 10 seconds
    assertEquals(0, results.size());
    clock.advanceTime(5, TimeUnit.SECONDS);
    EventFactHandle st2 = (EventFactHandle) wm.insert(new StockTick(1, "DROO", 80, clock.getCurrentTime()));
    wm.fireAllRules();
    // should still not fire, because it must wait 5 more seconds, and st2 has lower price (80)
    assertEquals(0, results.size());
    // assert new data
    wm.fireAllRules();
    clock.advanceTime(6, TimeUnit.SECONDS);
    wm.fireAllRules();
    // should fire, because waited for 10 seconds and no other event arrived with a price increase
    assertEquals(1, results.size());
    assertEquals(st1O, results.get(0));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 12 with StockTickInterface

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

the class CepEspTest method testBeforeOnArbitraryDates.

@Test(timeout = 10000)
public void testBeforeOnArbitraryDates() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperatorDates.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final List<?> results = new ArrayList<Object>();
    wm.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, // arbitrary timestamp
    104000, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 4 seconds after DROO
    100000, 3);
    InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    // wm  = SerializationHelper.serializeObject(wm);
    wm.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(tick1, results.get(0));
    assertEquals(tick2, results.get(1));
    assertEquals(tick1, results.get(2));
    assertEquals(tick2, results.get(3));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 13 with StockTickInterface

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

the class CepEspTest method testBeforeOperator.

@Test(timeout = 10000)
public void testBeforeOperator() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_BeforeOperator.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, sconf);
    final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    clock.setStartupTime(1000);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
    StockTickInterface tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
    StockTickInterface tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
    ksession.insert(tick1);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    ksession.insert(tick2);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    ksession.insert(tick3);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    ksession.insert(tick4);
    ksession.insert(tick5);
    clock.advanceTime(1, TimeUnit.MILLISECONDS);
    ksession.insert(tick6);
    ksession.insert(tick7);
    clock.advanceTime(2, TimeUnit.MILLISECONDS);
    ksession.insert(tick8);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    StockTick[] stocks = (StockTick[]) list.get(0);
    assertSame(tick4, stocks[0]);
    assertSame(tick2, stocks[1]);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 14 with StockTickInterface

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

the class CepEspTest method testMetByOperator.

@Test(timeout = 10000)
public void testMetByOperator() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_MetByOperator.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, sconf);
    final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<PseudoClockScheduler>getSessionClock();
    clock.setStartupTime(1000);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis(), 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis(), 5);
    StockTickInterface tick5 = new StockTick(5, "ACME", 10, System.currentTimeMillis(), 5);
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis(), 3);
    StockTickInterface tick7 = new StockTick(7, "ACME", 10, System.currentTimeMillis(), 5);
    StockTickInterface tick8 = new StockTick(8, "ACME", 10, System.currentTimeMillis(), 3);
    InternalFactHandle fh1 = (InternalFactHandle) ksession.insert(tick1);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    InternalFactHandle fh2 = (InternalFactHandle) ksession.insert(tick2);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    ksession.insert(tick3);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    ksession.insert(tick4);
    ksession.insert(tick5);
    clock.advanceTime(1, TimeUnit.MILLISECONDS);
    ksession.insert(tick6);
    ksession.insert(tick7);
    clock.advanceTime(2, TimeUnit.MILLISECONDS);
    ksession.insert(tick8);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    StockTick[] stocks = (StockTick[]) list.get(0);
    assertSame(tick1, stocks[0]);
    assertSame(tick2, stocks[1]);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 15 with StockTickInterface

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

the class CepEspTest method testEqualityAssertBehaviorOnEntryPoints.

@Test(timeout = 10000)
public void testEqualityAssertBehaviorOnEntryPoints() throws IOException, ClassNotFoundException {
    StockTickInterface st1 = new StockTick(1, "RHT", 10, 10);
    StockTickInterface st2 = new StockTick(1, "RHT", 10, 10);
    StockTickInterface st3 = new StockTick(2, "RHT", 15, 20);
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    kbconf.setOption(EqualityBehaviorOption.EQUALITY);
    final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
    final KieSession ksession1 = kbase1.newKieSession();
    AgendaEventListener ael1 = mock(AgendaEventListener.class);
    ksession1.addEventListener(ael1);
    EntryPoint ep1 = ksession1.getEntryPoint("stocktick stream");
    FactHandle fh1 = ep1.insert(st1);
    FactHandle fh1_2 = ep1.insert(st1);
    FactHandle fh2 = ep1.insert(st2);
    FactHandle fh3 = ep1.insert(st3);
    assertSame(fh1, fh1_2);
    assertSame(fh1, fh2);
    assertNotSame(fh1, fh3);
    ksession1.fireAllRules();
    // must have fired 2 times, one for each event equality
    verify(ael1, times(2)).afterMatchFired(any(AfterMatchFiredEvent.class));
    ksession1.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) InternalFactHandle(org.drools.core.common.InternalFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

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