Search in sources :

Example 41 with StockTick

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

the class CepEspTest method testExpireEventOnEndTimestamp.

@Test(timeout = 10000)
public void testExpireEventOnEndTimestamp() throws Exception {
    // DROOLS-40
    String str = "package org.drools.compiler;\n" + "\n" + "import org.drools.compiler.StockTick;\n" + "\n" + "global java.util.List resultsAfter;\n" + "\n" + "declare StockTick\n" + "    @role( event )\n" + "    @duration( duration )\n" + "end\n" + "\n" + "rule \"after[60,80]\"\n" + "when\n" + "$a : StockTick( company == \"DROO\" )\n" + "$b : StockTick( company == \"ACME\", this after[60,80] $a )\n" + "then\n" + "       resultsAfter.add( $b );\n" + "end";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = kbase.newKieSession(conf, null);
    PseudoClockScheduler clock = (PseudoClockScheduler) ksession.getSessionClock();
    List<StockTick> resultsAfter = new ArrayList<StockTick>();
    ksession.setGlobal("resultsAfter", resultsAfter);
    // inserting new StockTick with duration 30 at time 0 => rule
    // after[60,80] should fire when ACME lasts at 100-120
    ksession.insert(new StockTick(1, "DROO", 0, 0, 30));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.insert(new StockTick(2, "ACME", 0, 0, 20));
    ksession.fireAllRules();
    assertEquals(1, resultsAfter.size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 42 with StockTick

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

the class CepEspTest method testBeforeOnArbitraryDates.

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

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

the class CepEspTest method testTemporalOperators.

@Test(timeout = 10000)
public void testTemporalOperators() throws Exception {
    // read in the source
    final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
    kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators.drl");
    KieSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new StockTick(1, "A", 10, 1000));
}
Also used : RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 44 with StockTick

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

the class DynamicRulesTest method testDynamicRuleAdditionsWithEntryPoints.

@Test(timeout = 10000)
public void testDynamicRuleAdditionsWithEntryPoints() throws Exception {
    Collection<KiePackage> kpkgs = loadKnowledgePackages("test_DynamicWithEntryPoint.drl");
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
    KieSession ksession = createKnowledgeSession(kbase);
    // now lets add some knowledge to the kbase
    kbase.addPackages(kpkgs);
    List<StockTick> results = new ArrayList<StockTick>();
    ksession.setGlobal("results", results);
    EntryPoint ep = ksession.getEntryPoint("in-channel");
    ep.insert(new StockTick(1, "RHT", 20, 10000));
    ep.insert(new StockTick(2, "RHT", 21, 15000));
    ep.insert(new StockTick(3, "RHT", 22, 20000));
    ksession.fireAllRules();
    assertEquals(3, results.size());
}
Also used : StockTick(org.drools.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 45 with StockTick

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

the class CepEspTest method testTimeWindowWithPastEvents.

@Test(timeout = 10000)
public void testTimeWindowWithPastEvents() throws Exception {
    // JBRULES-2258
    String drl = "package org.drools.compiler;\n" + "\n" + "import java.util.List\n" + "\n" + "global List timeResults;\n" + "\n" + "declare StockTick\n" + " @role( event )\n" + " @timestamp( time ) \n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " accumulate(\n" + " $o : StockTick() over window:time(10ms)," + " $tot : count( $o );" + " $tot > 0 )\n" + "then\n" + " System.out.println( $tot ); \n" + " timeResults.add( $tot );\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>();
    ksession.setGlobal("timeResults", timeResults);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    int count = 0;
    StockTick tick1 = new StockTick(count++, "X", 0.0, 1);
    StockTick tick2 = new StockTick(count++, "X", 0.0, 3);
    StockTick tick3 = new StockTick(count++, "X", 0.0, 7);
    StockTick tick4 = new StockTick(count++, "X", 0.0, 9);
    StockTick tick5 = new StockTick(count++, "X", 0.0, 15);
    clock.advanceTime(30, TimeUnit.MILLISECONDS);
    ksession.insert(tick1);
    ksession.insert(tick2);
    ksession.insert(tick3);
    ksession.insert(tick4);
    ksession.insert(tick5);
    ksession.fireAllRules();
    System.out.println(timeResults);
    assertTrue(timeResults.isEmpty());
    clock.advanceTime(0, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertTrue(timeResults.isEmpty());
    clock.advanceTime(3, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertTrue(timeResults.isEmpty());
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertTrue(timeResults.isEmpty());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) 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) 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