Search in sources :

Example 1 with StockTickInterface

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

the class CepEspTest method testIdentityAssertBehaviorOnEntryPoints.

@Test(timeout = 10000)
public void testIdentityAssertBehaviorOnEntryPoints() 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.IDENTITY);
    final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
    final KieSession ksession = kbase1.newKieSession();
    AgendaEventListener ael1 = mock(AgendaEventListener.class);
    ksession.addEventListener(ael1);
    EntryPoint ep1 = ksession.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);
    assertNotSame(fh1, fh2);
    assertNotSame(fh1, fh3);
    assertNotSame(fh2, fh3);
    ksession.fireAllRules();
    // must have fired 3 times, one for each event identity
    verify(ael1, times(3)).afterMatchFired(any(AfterMatchFiredEvent.class));
    ksession.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)

Example 2 with StockTickInterface

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

the class CepEspTest method testTimeRelationalOperators.

@Test(timeout = 10000)
public void testTimeRelationalOperators() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_TimeRelationalOperators.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    final PseudoClockScheduler clock = (PseudoClockScheduler) wm.getSessionClock();
    clock.setStartupTime(1000);
    final List results_coincides = new ArrayList();
    final List results_before = new ArrayList();
    final List results_after = new ArrayList();
    final List results_meets = new ArrayList();
    final List results_met_by = new ArrayList();
    final List results_overlaps = new ArrayList();
    final List results_overlapped_by = new ArrayList();
    final List results_during = new ArrayList();
    final List results_includes = new ArrayList();
    final List results_starts = new ArrayList();
    final List results_started_by = new ArrayList();
    final List results_finishes = new ArrayList();
    final List results_finished_by = new ArrayList();
    wm.setGlobal("results_coincides", results_coincides);
    wm.setGlobal("results_before", results_before);
    wm.setGlobal("results_after", results_after);
    wm.setGlobal("results_meets", results_meets);
    wm.setGlobal("results_met_by", results_met_by);
    wm.setGlobal("results_overlaps", results_overlaps);
    wm.setGlobal("results_overlapped_by", results_overlapped_by);
    wm.setGlobal("results_during", results_during);
    wm.setGlobal("results_includes", results_includes);
    wm.setGlobal("results_starts", results_starts);
    wm.setGlobal("results_started_by", results_started_by);
    wm.setGlobal("results_finishes", results_finishes);
    wm.setGlobal("results_finished_by", results_finished_by);
    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 handle1 = (InternalFactHandle) wm.insert(tick1);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    InternalFactHandle handle2 = (InternalFactHandle) wm.insert(tick2);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    InternalFactHandle handle3 = (InternalFactHandle) wm.insert(tick3);
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    InternalFactHandle handle4 = (InternalFactHandle) wm.insert(tick4);
    InternalFactHandle handle5 = (InternalFactHandle) wm.insert(tick5);
    clock.advanceTime(1, TimeUnit.MILLISECONDS);
    InternalFactHandle handle6 = (InternalFactHandle) wm.insert(tick6);
    InternalFactHandle handle7 = (InternalFactHandle) wm.insert(tick7);
    clock.advanceTime(2, TimeUnit.MILLISECONDS);
    InternalFactHandle handle8 = (InternalFactHandle) wm.insert(tick8);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertNotNull(handle3);
    assertNotNull(handle4);
    assertNotNull(handle5);
    assertNotNull(handle6);
    assertNotNull(handle7);
    assertNotNull(handle8);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    assertTrue(handle3.isEvent());
    assertTrue(handle4.isEvent());
    assertTrue(handle6.isEvent());
    assertTrue(handle7.isEvent());
    assertTrue(handle8.isEvent());
    // wm  = SerializationHelper.serializeObject(wm);
    wm.fireAllRules();
    assertEquals(1, results_coincides.size());
    assertEquals(tick5, results_coincides.get(0));
    assertEquals(1, results_before.size());
    assertEquals(tick2, results_before.get(0));
    assertEquals(1, results_after.size());
    assertEquals(tick3, results_after.get(0));
    assertEquals(1, results_meets.size());
    assertEquals(tick3, results_meets.get(0));
    assertEquals(1, results_met_by.size());
    assertEquals(tick2, results_met_by.get(0));
    assertEquals(1, results_met_by.size());
    assertEquals(tick2, results_met_by.get(0));
    assertEquals(1, results_overlaps.size());
    assertEquals(tick4, results_overlaps.get(0));
    assertEquals(1, results_overlapped_by.size());
    assertEquals(tick8, results_overlapped_by.get(0));
    assertEquals(1, results_during.size());
    assertEquals(tick6, results_during.get(0));
    assertEquals(1, results_includes.size());
    assertEquals(tick4, results_includes.get(0));
    assertEquals(1, results_starts.size());
    assertEquals(tick6, results_starts.get(0));
    assertEquals(1, results_started_by.size());
    assertEquals(tick7, results_started_by.get(0));
    assertEquals(1, results_finishes.size());
    assertEquals(tick8, results_finishes.get(0));
    assertEquals(1, results_finished_by.size());
    assertEquals(tick7, results_finished_by.get(0));
}
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 3 with StockTickInterface

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

the class CepEspTest method testEventAssertion.

@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
    KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertion.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 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);
    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) StockTick(org.drools.compiler.StockTick) 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 4 with StockTickInterface

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

the class CepEspTest method testPackageSerializationWithEvents.

@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testPackageSerializationWithEvents() throws Exception {
    // read in the source
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
    // get the package
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    assertEquals(2, pkgs.size());
    // serialize the package
    byte[] serializedPkg = DroolsStreamUtils.streamOut(pkgs);
    pkgs = (Collection<KiePackage>) DroolsStreamUtils.streamIn(serializedPkg);
    // create the kbase
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    // create the session
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    KieSession session = createKnowledgeSession(kbase, conf);
    final List<StockTick> results = new ArrayList<StockTick>();
    session.setGlobal("results", results);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    assertNotNull(handle1);
    assertNotNull(handle2);
    assertTrue(handle1.isEvent());
    assertTrue(handle2.isEvent());
    session.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(tick2, results.get(0));
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 5 with StockTickInterface

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

the class CepEspTest method testComplexOperator.

@Test(timeout = 10000)
public void testComplexOperator() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_ComplexOperator.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, sconf);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    clock.setStartupTime(1000);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 0, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 4, 3);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, 8, 3);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, 12, 5);
    StockTickInterface tick5 = new StockTick(5, "ACME", 10, 12, 5);
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, 13, 3);
    StockTickInterface tick7 = new StockTick(7, "ACME", 10, 13, 5);
    StockTickInterface tick8 = new StockTick(8, "ACME", 10, 15, 3);
    ksession.insert(tick1);
    ksession.insert(tick2);
    ksession.insert(tick3);
    ksession.insert(tick4);
    ksession.insert(tick5);
    ksession.insert(tick6);
    ksession.insert(tick7);
    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) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) 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)

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