Search in sources :

Example 36 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration 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 37 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testCloudModeExpiration.

@Test(timeout = 10000)
public void testCloudModeExpiration() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, InterruptedException {
    String str = "package org.drools.cloud\n" + "import org.drools.compiler.*\n" + "declare Event\n" + "        @role ( event )\n" + "        name : String\n" + "        value : Object\n" + "end\n" + "declare AnotherEvent\n" + "        @role ( event )\n" + "        message : String\n" + "        type : String\n" + "end\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule \"two events\"\n" + "    when\n" + "        Event( value != null ) from entry-point X\n" + "        StockTick( company != null ) from entry-point X\n" + "    then\n" + "end";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.CLOUD);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSession ksession = createKnowledgeSession(kbase);
    EntryPoint ep = ksession.getEntryPoint("X");
    ep.insert(new StockTick(1, "RHT", 10, 1000));
    int rulesFired = ksession.fireAllRules();
    assertEquals(0, rulesFired);
    org.kie.api.definition.type.FactType event = kbase.getFactType("org.drools.cloud", "Event");
    Object e1 = event.newInstance();
    event.set(e1, "name", "someKey");
    event.set(e1, "value", "someValue");
    ep.insert(e1);
    rulesFired = ksession.fireAllRules();
    assertEquals(1, rulesFired);
    // let some time be spent
    Thread.currentThread().sleep(1000);
    // check both events are still in memory as we are running in CLOUD mode
    assertEquals(2, ep.getFactCount());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 38 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testSimpleTimeWindow.

@Test(timeout = 10000)
public void testSimpleTimeWindow() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_SimpleTimeWindow.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession wm = createKnowledgeSession(kbase, sconf);
    List results = new ArrayList();
    wm.setGlobal("results", results);
    // how to initialize the clock?
    // how to configure the clock?
    SessionPseudoClock clock = (SessionPseudoClock) wm.getSessionClock();
    clock.advanceTime(5, // 5 seconds
    TimeUnit.SECONDS);
    EventFactHandle handle1 = (EventFactHandle) wm.insert(new OrderEvent("1", "customer A", 70));
    assertEquals(5000, handle1.getStartTimestamp());
    assertEquals(0, handle1.getDuration());
    // wm  = SerializationHelper.getSerialisedStatefulSession( wm );
    // results = (List) wm.getGlobal( "results" );
    // clock = (SessionPseudoClock) wm.getSessionClock();
    wm.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(70, ((Number) results.get(0)).intValue());
    // advance clock and assert new data
    clock.advanceTime(10, // 10 seconds
    TimeUnit.SECONDS);
    EventFactHandle handle2 = (EventFactHandle) wm.insert(new OrderEvent("2", "customer A", 60));
    assertEquals(15000, handle2.getStartTimestamp());
    assertEquals(0, handle2.getDuration());
    wm.fireAllRules();
    assertEquals(2, results.size());
    assertEquals(65, ((Number) results.get(1)).intValue());
    // advance clock and assert new data
    clock.advanceTime(10, // 10 seconds
    TimeUnit.SECONDS);
    EventFactHandle handle3 = (EventFactHandle) wm.insert(new OrderEvent("3", "customer A", 50));
    assertEquals(25000, handle3.getStartTimestamp());
    assertEquals(0, handle3.getDuration());
    wm.fireAllRules();
    assertEquals(3, results.size());
    assertEquals(60, ((Number) results.get(2)).intValue());
    // advance clock and assert new data
    clock.advanceTime(10, // 10 seconds
    TimeUnit.SECONDS);
    EventFactHandle handle4 = (EventFactHandle) wm.insert(new OrderEvent("4", "customer A", 25));
    assertEquals(35000, handle4.getStartTimestamp());
    assertEquals(0, handle4.getDuration());
    wm.fireAllRules();
    // first event should have expired, making average under the rule threshold, so no additional rule fire
    assertEquals(3, results.size());
    // advance clock and assert new data
    clock.advanceTime(10, // 10 seconds
    TimeUnit.SECONDS);
    EventFactHandle handle5 = (EventFactHandle) wm.insert(new OrderEvent("5", "customer A", 70));
    assertEquals(45000, handle5.getStartTimestamp());
    assertEquals(0, handle5.getDuration());
    // wm  = SerializationHelper.serializeObject(wm);
    wm.fireAllRules();
    // still under the threshold, so no fire
    assertEquals(3, results.size());
    // advance clock and assert new data
    clock.advanceTime(10, // 10 seconds
    TimeUnit.SECONDS);
    EventFactHandle handle6 = (EventFactHandle) wm.insert(new OrderEvent("6", "customer A", 115));
    assertEquals(55000, handle6.getStartTimestamp());
    assertEquals(0, handle6.getDuration());
    wm.fireAllRules();
    assertEquals(4, results.size());
    assertEquals(70, ((Number) results.get(3)).intValue());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) OrderEvent(org.drools.compiler.OrderEvent) ArrayList(java.util.ArrayList) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 39 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration 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 40 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class CepEspTest method testTimeAndLengthWindowConflict.

@Test(timeout = 10000)
public void testTimeAndLengthWindowConflict() throws Exception {
    // JBRULES-3671
    String drl = "package org.drools.compiler;\n" + "\n" + "import java.util.List\n" + "\n" + "global List timeResults;\n" + "global List lengthResults;\n" + "\n" + "declare OrderEvent\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " $list : List( empty == false ) from collect(\n" + " $o : OrderEvent() over window:time(30s) )\n" + "then\n" + " timeResults.add( $list.size() );\n" + "end\n" + "\n" + "rule \"collect with length window\"\n" + "when\n" + " accumulate (\n" + " $o : OrderEvent( $tot : total ) over window:length(3)," + " $avg : average( $tot ) )\n" + "then\n" + " lengthResults.add( $avg );\n" + "end\n";
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(kbconf, drl);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    List<Number> timeResults = new ArrayList<Number>();
    List<Number> lengthResults = new ArrayList<Number>();
    ksession.setGlobal("timeResults", timeResults);
    ksession.setGlobal("lengthResults", lengthResults);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    // 5 seconds
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new OrderEvent("1", "customer A", 70));
    ksession.fireAllRules();
    System.out.println(lengthResults);
    assertTrue(lengthResults.contains(70.0));
    // 10 seconds
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.insert(new OrderEvent("2", "customer A", 60));
    ksession.fireAllRules();
    System.out.println(lengthResults);
    assertTrue(lengthResults.contains(65.0));
    // Third interaction: advance clock and assert new data
    // 10 seconds
    clock.advanceTime(10, TimeUnit.SECONDS);
    ksession.insert(new OrderEvent("3", "customer A", 50));
    ksession.fireAllRules();
    System.out.println(lengthResults);
    assertTrue(lengthResults.contains(60.0));
    // Fourth interaction: advance clock and assert new data
    // 60 seconds
    clock.advanceTime(60, TimeUnit.SECONDS);
    ksession.insert(new OrderEvent("4", "customer A", 25));
    ksession.fireAllRules();
    System.out.println(lengthResults);
// assertTrue( lengthResults.contains( 45 ) );
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) OrderEvent(org.drools.compiler.OrderEvent) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)151 Test (org.junit.Test)131 KieSession (org.kie.api.runtime.KieSession)116 KieBase (org.kie.api.KieBase)99 ArrayList (java.util.ArrayList)76 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)57 List (java.util.List)50 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)39 StockTick (org.drools.compiler.StockTick)33 EntryPoint (org.kie.api.runtime.rule.EntryPoint)31 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)24 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)21 FactHandle (org.kie.api.runtime.rule.FactHandle)20 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)20 InternalFactHandle (org.drools.core.common.InternalFactHandle)19 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)17 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)15 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)14 StockTickInterface (org.drools.compiler.StockTickInterface)13 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)13