Search in sources :

Example 16 with StockTickInterface

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

the class CepEspTest method testAfterOnArbitraryDates.

@Test(timeout = 10000)
public void testAfterOnArbitraryDates() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_AfterOperatorDates.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
    100000, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, // 4 seconds after DROO
    104000, 3);
    InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
    InternalFactHandle handle1 = (InternalFactHandle) wm.insert(tick1);
    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 17 with StockTickInterface

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

the class CepEspTest method testEventAssertionWithDateTimestamp.

@Test(timeout = 10000)
public void testEventAssertionWithDateTimestamp() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertionWithDateTimestamp.drl");
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession session = createKnowledgeSession(kbase, conf);
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000, 5);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 11000, 10);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, 12000, 8);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, 13000, 7);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    EventFactHandle eh1 = (EventFactHandle) handle1;
    EventFactHandle eh2 = (EventFactHandle) handle2;
    EventFactHandle eh3 = (EventFactHandle) handle3;
    EventFactHandle eh4 = (EventFactHandle) handle4;
    assertEquals(tick1.getTime(), eh1.getStartTimestamp());
    assertEquals(tick2.getTime(), eh2.getStartTimestamp());
    assertEquals(tick3.getTime(), eh3.getStartTimestamp());
    assertEquals(tick4.getTime(), eh4.getStartTimestamp());
    session.fireAllRules();
    assertEquals(2, results.size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) 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) Test(org.junit.Test)

Example 18 with StockTickInterface

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

the class StreamsTest method testEventAssertion.

@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
    // read in the source
    KieBase kbase = loadKnowledgeBase("test_EntryPoint.drl");
    // final RuleBase ruleBase = loadRuleBase( reader );
    KieSessionConfiguration conf = SessionConfiguration.newInstance();
    ((SessionConfiguration) conf).setClockType(ClockType.PSEUDO_CLOCK);
    KieSession session = kbase.newKieSession(conf, null);
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis());
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis());
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    session.fireAllRules();
    assertEquals(0, results.size());
    StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
    StockTickInterface tick7 = new StockTick(7, "ACME", 15, System.currentTimeMillis());
    StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
    EntryPoint entry = session.getEntryPoint("StockStream");
    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) List(java.util.List) ArrayList(java.util.ArrayList) InternalFactHandle(org.drools.core.common.InternalFactHandle) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) Test(org.junit.Test)

Example 19 with StockTickInterface

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

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

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