Search in sources :

Example 6 with StockTickInterface

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

the class CepEspTest method testIdleTime.

// @Test(timeout=10000) @Ignore
// public void testTransactionCorrelation() throws Exception {
// // read in the source
// final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_TransactionCorrelation.drl" ) );
// final RuleBase ruleBase = loadRuleBase( reader );
// 
// final WorkingMemory wm = ruleBase.newStatefulSession();
// final List results = new ArrayList();
// 
// wm.setGlobal( "results",
// results );
// 
// 
// }
@Test(timeout = 10000)
public void testIdleTime() throws Exception {
    // read in the source
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) createKnowledgeSession(kbase, conf);
    SessionPseudoClock clock = (SessionPseudoClock) session.getSessionClock();
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
    assertEquals(0, session.getIdleTime());
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(10000, session.getIdleTime());
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    assertEquals(0, session.getIdleTime());
    clock.advanceTime(15, TimeUnit.SECONDS);
    assertEquals(15000, session.getIdleTime());
    clock.advanceTime(15, TimeUnit.SECONDS);
    assertEquals(30000, session.getIdleTime());
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    assertEquals(0, session.getIdleTime());
    clock.advanceTime(20, TimeUnit.SECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    assertEquals(10000, session.getIdleTime());
    session.fireAllRules();
    assertEquals(0, session.getIdleTime());
    assertEquals(2, ((List) session.getGlobal("results")).size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 7 with StockTickInterface

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

the class CepEspTest method testAnnotatedEventAssertion.

@Test(timeout = 10000)
public void testAnnotatedEventAssertion() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleAnnotatedEventAssertion.drl");
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieSession session = createKnowledgeSession(kbase, conf);
    SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
    final List results = new ArrayList();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTickEvent(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTickEvent(2, "ACME", 10, 10010);
    StockTickInterface tick3 = new StockTickEvent(3, "ACME", 10, 10100);
    StockTickInterface tick4 = new StockTickEvent(4, "DROO", 50, 11000);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    clock.advanceTime(10, TimeUnit.SECONDS);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    clock.advanceTime(30, TimeUnit.SECONDS);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    clock.advanceTime(20, TimeUnit.SECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    session.fireAllRules();
    assertEquals(2, ((List) session.getGlobal("results")).size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTickEvent(org.drools.compiler.StockTickEvent) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) 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) Test(org.junit.Test)

Example 8 with StockTickInterface

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

the class CepEspTest method testEventAssertionWithDuration.

@Test(timeout = 10000)
public void testEventAssertionWithDuration() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertionWithDuration.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());
    assertEquals(tick1.getDuration(), eh1.getDuration());
    assertEquals(tick2.getDuration(), eh2.getDuration());
    assertEquals(tick3.getDuration(), eh3.getDuration());
    assertEquals(tick4.getDuration(), eh4.getDuration());
    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 9 with StockTickInterface

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

the class CepEspTest method testCoincidesOnArbitraryDates.

@Test(timeout = 10000)
public void testCoincidesOnArbitraryDates() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_CoincidesOperatorDates.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, // 50 milliseconds after DROO
    100050, 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 10 with StockTickInterface

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

the class CepEspTest method testEventDeclarationForInterfaces.

@Test(timeout = 10000)
public void testEventDeclarationForInterfaces() throws Exception {
    // read in the source
    final KieBase kbase = loadKnowledgeBase("test_CEP_EventInterfaces.drl");
    KieSession session = createKnowledgeSession(kbase);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) 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