Search in sources :

Example 46 with StockTick

use of org.drools.compiler.StockTick 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 47 with StockTick

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

the class CepEspTest method testTemporalOperators2.

@Test(timeout = 10000)
public void testTemporalOperators2() throws Exception {
    // read in the source
    final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
    kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators2.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);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    EntryPoint ep = ksession.getEntryPoint("X");
    clock.advanceTime(1000, TimeUnit.SECONDS);
    ep.insert(new StockTick(1, "A", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    ep.insert(new StockTick(2, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    ep.insert(new StockTick(3, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    int rules = ksession.fireAllRules();
// assertEquals( 2,
// rules );
// assertEquals( 1, list.size() );
// StockTick[] stocks = ( StockTick[] ) list.get(0);
// assertSame( tick4, stocks[0]);
// assertSame( tick2, stocks[1]);
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 48 with StockTick

use of org.drools.compiler.StockTick 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 49 with StockTick

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

Example 50 with StockTick

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

the class CepEspTest method testDuplicateFiring1.

@Test
public void testDuplicateFiring1() throws InterruptedException {
    String drl = "package org.test;\n" + "import org.drools.compiler.StockTick;\n " + "" + "global java.util.List list \n" + "" + "declare StockTick @role(event) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + "  accumulate ( $e: StockTick() over window:time(300ms) from entry-point SensorEventStream, " + "              $n : count( $e );" + "              $n > 0 )\n" + "then\n" + "  list.add( $n ); \n" + "  System.out.println( \"Events in last 3 seconds: \" + $n );\n" + "end" + "" + "\n" + "rule \"timerRuleAfterAllEvents\"\n" + "        timer ( int: 2s )\n" + "when\n" + "        $room : String( )\n" + "then\n" + "  list.add( -1 ); \n" + "  System.out.println(\"2sec after room was modified\");\n" + "end " + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    // Check the builder for errors
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    // configure knowledge base
    KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    baseConfig.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    // init session clock
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get("pseudo"));
    // init stateful knowledge session
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    SessionPseudoClock clock = ksession.getSessionClock();
    ArrayList list = new ArrayList();
    ksession.setGlobal("list", list);
    // entry point for sensor events
    EntryPoint sensorEventStream = ksession.getEntryPoint("SensorEventStream");
    ksession.insert("Go");
    System.out.println("1. fireAllRules()");
    // insert events
    for (int i = 2; i < 8; i++) {
        StockTick event = new StockTick((i - 1), "XXX", 1.0, 0);
        sensorEventStream.insert(event);
        System.out.println(i + ". fireAllRules()");
        ksession.fireAllRules();
        clock.advanceTime(105, TimeUnit.MILLISECONDS);
    }
    // let thread sleep for another 1m to see if dereffered rules fire (timers, (not) after rules)
    clock.advanceTime(100 * 40 * 1, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(Arrays.asList(1L, 2L, 3L, 3L, 3L, 3L, -1), list);
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Aggregations

StockTick (org.drools.compiler.StockTick)64 Test (org.junit.Test)61 KieSession (org.kie.api.runtime.KieSession)60 KieBase (org.kie.api.KieBase)47 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)40 ArrayList (java.util.ArrayList)37 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)31 EntryPoint (org.kie.api.runtime.rule.EntryPoint)27 StockTickInterface (org.drools.compiler.StockTickInterface)22 InternalFactHandle (org.drools.core.common.InternalFactHandle)16 List (java.util.List)15 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)13 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)11 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)9 EventFactHandle (org.drools.core.common.EventFactHandle)8 IOException (java.io.IOException)7 ParseException (java.text.ParseException)7