Search in sources :

Example 6 with StockTick

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

the class CepEspTest method testWindowExpireActionDeserialization.

@Test
public void testWindowExpireActionDeserialization() 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" + "end\n" + "\n" + "rule \"One\"\n" + "when\n" + "  StockTick( $id : seq, company == \"BBB\" ) over window:time( 1s )\n" + "then\n" + "  list.add( $id );\n" + "end\n" + "\n" + "";
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    KieBase kb = loadKnowledgeBaseFromString(kbconf, drl);
    KieSession ks = kb.newKieSession();
    ks.insert(new StockTick(2, "BBB", 1.0, 0));
    Thread.sleep(1500);
    try {
        ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true, false);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    ArrayList list = new ArrayList();
    ks.setGlobal("list", list);
    ks.fireAllRules();
    ks.insert(new StockTick(3, "BBB", 1.0, 0));
    ks.fireAllRules();
    System.out.print(list);
    assertEquals(1, list.size());
    assertEquals(Arrays.asList(3L), list);
}
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) IOException(java.io.IOException) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 7 with StockTick

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

the class CepEspTest method testDeleteExpiredEvent.

@Test
public void testDeleteExpiredEvent() throws Exception {
    // BZ-1274696
    String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "no-loop\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);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler clock = ksession.getSessionClock();
    EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50));
    ksession.fireAllRules();
    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 8 with StockTick

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

the class CepEspTest method testTemporalOperatorsInfinity.

@Test(timeout = 10000)
public void testTemporalOperatorsInfinity() throws Exception {
    // read in the source
    final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
    kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators3.drl");
    KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = kbase.newKieSession(sconf, null);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    EntryPoint ep = ksession.getEntryPoint("X");
    clock.advanceTime(1000, TimeUnit.SECONDS);
    int rules = 0;
    ep.insert(new StockTick(1, "A", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    // int rules = ksession.fireAllRules();
    System.out.println(list);
    ep.insert(new StockTick(2, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    // rules = ksession.fireAllRules();
    System.out.println(list);
    ep.insert(new StockTick(3, "B", 10, clock.getCurrentTime()));
    clock.advanceTime(8, TimeUnit.SECONDS);
    rules = ksession.fireAllRules();
    System.out.println(list);
    assertEquals(3, rules);
}
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 9 with StockTick

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

the class CepEspTest method testSalienceWithEventsPseudoClock.

@Test(timeout = 10000)
public void testSalienceWithEventsPseudoClock() throws IOException, ClassNotFoundException {
    String str = "package org.drools.compiler\n" + "import " + StockTick.class.getName() + "\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule R1 salience 1000\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT' )\n" + "        $s2 : StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + "    then\n" + "end\n" + "rule R2 salience 1000\n" + "    when\n" + "        $s1 : StockTick( company == 'RHT' )\n" + "        not StockTick( company == 'ACME', this after[0s,1m] $s1 )\n" + "    then\n" + "end\n" + "rule R3 salience 100\n" + "    when\n" + "        $s2 : StockTick( company == 'ACME' )\n" + "    then\n" + "end\n";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = kbase.newKieSession(ksconf, null);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1000000, TimeUnit.MILLISECONDS);
    ksession.insert(new StockTick(1, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(2, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(3, "RHT", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new StockTick(4, "ACME", 10, 1000));
    clock.advanceTime(5, TimeUnit.SECONDS);
    int rulesFired = ksession.fireAllRules();
    assertEquals(4, rulesFired);
    ArgumentCaptor<AfterMatchFiredEvent> captor = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael, times(4)).afterMatchFired(captor.capture());
    List<AfterMatchFiredEvent> aafe = captor.getAllValues();
    assertThat(aafe.get(0).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(1).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(2).getMatch().getRule().getName(), is("R1"));
    assertThat(aafe.get(3).getMatch().getRule().getName(), is("R3"));
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) 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) 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) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 10 with StockTick

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

the class CepEspTest method testSerializationBeforeFireWithWindowLength.

@Test
public void testSerializationBeforeFireWithWindowLength() throws Exception {
    // DROOLS-953
    String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "declare StockTick\n" + "    @role( event )\n" + "end\n" + "\n" + "rule ReportLastEvent when\n" + "    $e : StockTick() over window:length(1)\n" + "then\n" + "    list.add($e.getCompany());\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);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    PseudoClockScheduler clock = ksession.getSessionClock();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new StockTick(1, "ACME", 50));
    ksession.insert(new StockTick(2, "DROO", 50));
    ksession.insert(new StockTick(3, "JBPM", 50));
    try {
        ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    System.out.println(list);
    assertEquals(1, list.size());
    assertEquals("JBPM", list.get(0));
}
Also used : StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) IOException(java.io.IOException) ParseException(java.text.ParseException) 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