Search in sources :

Example 21 with StockTick

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

the class CepEspTest method testEventExpiration4.

@Test(timeout = 10000)
public void testEventExpiration4() throws Exception {
    // read in the source
    final KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration4.drl");
    final KieSessionConfiguration sconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sconf.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = createKnowledgeSession(kbase, sconf);
    EntryPoint eventStream = ksession.getEntryPoint("Event Stream");
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    EventFactHandle handle1 = (EventFactHandle) eventStream.insert(new StockTick(1, "ACME", 50, System.currentTimeMillis(), 3));
    ksession.fireAllRules();
    clock.advanceTime(11, TimeUnit.SECONDS);
    /**
     * clock.advance() will put the event expiration in the queue to be executed,
     *            but it has to wait for a "thread" to do that
     *            so we fire rules again here to get that
     *            alternative could run fireUntilHalt() *
     */
    ksession.fireAllRules();
    assertTrue(results.size() == 1);
    assertTrue(handle1.isExpired());
    assertFalse(ksession.getFactHandles().contains(handle1));
}
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) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) 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 22 with StockTick

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

the class CepEspTest method testComplexOperator.

@Test(timeout = 10000)
public void testComplexOperator() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_ComplexOperator.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);
    final PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    clock.setStartupTime(1000);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    StockTickInterface tick1 = new StockTick(1, "DROO", 50, 0, 3);
    StockTickInterface tick2 = new StockTick(2, "ACME", 10, 4, 3);
    StockTickInterface tick3 = new StockTick(3, "ACME", 10, 8, 3);
    StockTickInterface tick4 = new StockTick(4, "DROO", 50, 12, 5);
    StockTickInterface tick5 = new StockTick(5, "ACME", 10, 12, 5);
    StockTickInterface tick6 = new StockTick(6, "ACME", 10, 13, 3);
    StockTickInterface tick7 = new StockTick(7, "ACME", 10, 13, 5);
    StockTickInterface tick8 = new StockTick(8, "ACME", 10, 15, 3);
    ksession.insert(tick1);
    ksession.insert(tick2);
    ksession.insert(tick3);
    ksession.insert(tick4);
    ksession.insert(tick5);
    ksession.insert(tick6);
    ksession.insert(tick7);
    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) 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) 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 23 with StockTick

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

the class CepEspTest method testIdleTime.

// @Test(timeout=10000) @Ignore
// public void testTransactionCorrelation() throws Exception {
// // read in the source
// final Reader reader = new InputStreamReader( getClass().getResourceAsStream( "test_TransactionCorrelation.drl" ) );
// final RuleBase ruleBase = loadRuleBase( reader );
// 
// final WorkingMemory wm = ruleBase.newStatefulSession();
// final List results = new ArrayList();
// 
// wm.setGlobal( "results",
// results );
// 
// 
// }
@Test(timeout = 10000)
public void testIdleTime() throws Exception {
    // read in the source
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    StatefulKnowledgeSessionImpl session = (StatefulKnowledgeSessionImpl) createKnowledgeSession(kbase, conf);
    SessionPseudoClock clock = (SessionPseudoClock) session.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);
    assertEquals(0, session.getIdleTime());
    InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(10000, session.getIdleTime());
    InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
    assertEquals(0, session.getIdleTime());
    clock.advanceTime(15, TimeUnit.SECONDS);
    assertEquals(15000, session.getIdleTime());
    clock.advanceTime(15, TimeUnit.SECONDS);
    assertEquals(30000, session.getIdleTime());
    InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
    assertEquals(0, session.getIdleTime());
    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());
    assertEquals(10000, session.getIdleTime());
    session.fireAllRules();
    assertEquals(0, session.getIdleTime());
    assertEquals(2, ((List) session.getGlobal("results")).size());
}
Also used : StockTickInterface(org.drools.compiler.StockTickInterface) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StockTick(org.drools.compiler.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 24 with StockTick

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

the class CepEspTest method testSerializationWithWindowLengthAndLiaSharing.

@Test
public void testSerializationWithWindowLengthAndLiaSharing() 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\n" + "\n" + "rule ReportEventInserted when\n" + "   $e : StockTick()\n" + "then\n" + "   System.out.println(\"Event Insert : \" + $e);\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));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("JBPM", list.get(0));
    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(0, list.size());
}
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)

Example 25 with StockTick

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

the class ConstraintsTest method testConnectorsAndOperators.

@Test
public void testConnectorsAndOperators() throws IOException, ClassNotFoundException {
    final KieBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_ConstraintConnectorsAndOperators.drl"));
    final KieSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new StockTick(1, "RHT", 10, 1000));
    ksession.insert(new StockTick(2, "IBM", 10, 1100));
    final int fired = ksession.fireAllRules();
    assertEquals(1, fired);
}
Also used : StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) 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