Search in sources :

Example 16 with StockTick

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

the class CepEspTest method testDeserializationWithCompositeTrigger.

@Test
public void testDeserializationWithCompositeTrigger() throws InterruptedException {
    String drl = "package org.drools.test;\n" + "import org.drools.compiler.StockTick; \n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + "  @role( event )\n" + "  @expires( 1s )\n" + "end\n" + "\n" + "rule \"One\"\n" + "when\n" + "  $event : StockTick( )\n" + "  not StockTick( company == \"BBB\", this after[0,96h] $event )\n" + "  not StockTick( company == \"CCC\", this after[0,96h] $event )\n" + "then\n" + "end";
    KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    KieSessionConfiguration knowledgeSessionConfiguration = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    knowledgeSessionConfiguration.setOption(TimerJobFactoryOption.get("trackable"));
    KieBase kb = loadKnowledgeBaseFromString(kbconf, drl);
    KieSession ks = kb.newKieSession(knowledgeSessionConfiguration, null);
    ks.insert(new StockTick(2, "AAA", 1.0, 0));
    try {
        ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true, false);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 17 with StockTick

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

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

the class CepEspTest method testDeleteExpiredEventWithTimestampAndEqualityKey.

@Test
public void testDeleteExpiredEventWithTimestampAndEqualityKey() throws Exception {
    // DROOLS-1017
    String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + "    @role( event )\n" + "    @timestamp( time )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "when\n" + "  $st1 : StockTick( company == \"ACME\" )\n" + "  not ( StockTick( this != $st1, this after[0s, 1s] $st1) )\n" + "then\n" + "  delete($st1);\n" + "end";
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    KieBase kbase = helper.build(EventProcessingOption.STREAM, EqualityBehaviorOption.EQUALITY);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler clock = ksession.getSessionClock();
    clock.setStartupTime(5000L);
    EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50, 0L));
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertTrue(handle1.isExpired());
    assertFalse(ksession.getFactHandles().contains(handle1));
}
Also used : StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) EventFactHandle(org.drools.core.common.EventFactHandle) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Example 19 with StockTick

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

the class CepEspTest method testPastEventExipration.

@Test
public void testPastEventExipration() throws InterruptedException {
    // DROOLS-257
    String drl = "package org.test;\n" + "import org.drools.compiler.StockTick;\n " + "" + "global java.util.List list; \n" + "" + "declare StockTick @role(event) @timestamp( time ) @expires( 200ms ) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + "  accumulate ( $e: StockTick() over window:length(10), $n : count($e) )\n" + "then\n" + "  list.add( $n ); \n" + "  System.out.println( \"Events in last X seconds: \" + $n );\n" + "end" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    baseConfig.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    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);
    long now = 0;
    StockTick event1 = new StockTick(1, "XXX", 1.0, now);
    StockTick event2 = new StockTick(2, "XXX", 1.0, now + 240);
    StockTick event3 = new StockTick(2, "XXX", 1.0, now + 380);
    StockTick event4 = new StockTick(2, "XXX", 1.0, now + 500);
    ksession.insert(event1);
    ksession.insert(event2);
    ksession.insert(event3);
    ksession.insert(event4);
    clock.advanceTime(220, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock.advanceTime(400, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(Arrays.asList(3L, 1L), list);
}
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) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 20 with StockTick

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

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