Search in sources :

Example 71 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class PseudoClockEventsTest method processStocks.

private int processStocks(int stockCount, AgendaEventListener agendaEventListener, String drlContentString) throws DroolsParserException, IOException, Exception {
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBaseFromString(kconf, drlContentString);
    KieSessionConfiguration ksessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksessionConfig.setOption(ClockTypeOption.get("pseudo"));
    ksessionConfig.setProperty("keep.reference", "true");
    final KieSession ksession = kbase.newKieSession(ksessionConfig, null);
    ksession.addEventListener(agendaEventListener);
    PseudoClockScheduler clock = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    Runnable fireUntilHaltRunnable = new Runnable() {

        public void run() {
            ksession.fireUntilHalt();
        }
    };
    Thread fireUntilHaltThread = new Thread(fireUntilHaltRunnable, "Engine's thread");
    fireUntilHaltThread.start();
    Thread.currentThread().setName("Feeding thread");
    for (int stIndex = 1; stIndex <= stockCount; stIndex++) {
        clock.advanceTime(20, TimeUnit.SECONDS);
        Thread.sleep(100);
        final StockTickInterface st = new StockTick(stIndex, "RHT", 100 * stIndex, 100 * stIndex);
        ksession.insert(st);
        Thread.sleep(100);
    }
    Thread.sleep(100);
    ksession.halt();
    fireUntilHaltThread.join(5000);
    return stockCount;
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler)

Example 72 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class TimerAndCalendarTest method testCalendarsWithIntervals.

@Test(timeout = 10000)
public void testCalendarsWithIntervals() throws Exception {
    String str = "";
    str += "package org.simple \n";
    str += "global java.util.List list \n";
    str += "rule xxx \n";
    str += "  calendars \"cal1\", \"cal2\"\n";
    // int: protocol is assumed
    str += "  timer (15s 60s) ";
    str += "when \n";
    str += "then \n";
    str += "  list.add(\"fired\"); \n";
    str += "end  \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    List list = new ArrayList();
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
    Date date = df.parse("2009-01-01T00:00:00.000-0000");
    timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
    final Date date1 = new Date(date.getTime() + (15 * 1000));
    final Date date2 = new Date(date1.getTime() + (60 * 1000));
    final Date date3 = new Date(date2.getTime() + (60 * 1000));
    final Date date4 = new Date(date3.getTime() + (60 * 1000));
    Calendar cal1 = new Calendar() {

        public boolean isTimeIncluded(long timestamp) {
            if (timestamp == date1.getTime()) {
                return true;
            } else if (timestamp == date4.getTime()) {
                return false;
            } else {
                return true;
            }
        }
    };
    Calendar cal2 = new Calendar() {

        public boolean isTimeIncluded(long timestamp) {
            if (timestamp == date2.getTime()) {
                return false;
            } else if (timestamp == date3.getTime()) {
                return true;
            } else {
                return true;
            }
        }
    };
    ksession.getCalendars().set("cal1", cal1);
    ksession.getCalendars().set("cal2", cal2);
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    timeService.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    timeService.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    timeService.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(4, list.size());
}
Also used : KieBase(org.kie.api.KieBase) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(org.kie.api.time.Calendar) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Example 73 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class TimerAndCalendarTest method testCalendarsWithCronAndStartAndLimit.

@Test(timeout = 10000)
public void testCalendarsWithCronAndStartAndLimit() throws Exception {
    Locale defaultLoc = Locale.getDefault();
    try {
        // Because of the date strings in the DRL, fixable with JBRULES-3444
        Locale.setDefault(Locale.UK);
        String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + "  date-effective \"2-JAN-2010\"\n" + "  calendars \"cal1\"\n" + // it is incremented regardless of the effective date
        "  timer (cron: 0 0 0 * * ?; repeat-limit=6) " + "when \n" + "then \n" + "  list.add(\"fired\"); \n" + "end  \n";
        KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
        conf.setOption(ClockTypeOption.get("pseudo"));
        KieBase kbase = loadKnowledgeBaseFromString(str);
        KieSession ksession = createKnowledgeSession(kbase, conf);
        List list = new ArrayList();
        PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
        DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
        Date date = df.parse("1-JAN-2010");
        Calendar cal1 = new Calendar() {

            public boolean isTimeIncluded(long timestamp) {
                return true;
            }
        };
        long oneDay = 60 * 60 * 24;
        ksession.getCalendars().set("cal1", cal1);
        ksession.setGlobal("list", list);
        timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
        ksession.fireAllRules();
        assertEquals(0, list.size());
        timeService.advanceTime(oneDay, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertEquals(0, list.size());
        // day 3
        timeService.advanceTime(oneDay, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertEquals(1, list.size());
        timeService.advanceTime(oneDay, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertEquals(2, list.size());
        // day 5
        timeService.advanceTime(oneDay, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertEquals(3, list.size());
        timeService.advanceTime(oneDay, TimeUnit.SECONDS);
        ksession.fireAllRules();
        assertEquals(4, list.size());
    } finally {
        Locale.setDefault(defaultLoc);
    }
}
Also used : Locale(java.util.Locale) Calendar(org.kie.api.time.Calendar) ArrayList(java.util.ArrayList) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) KieBase(org.kie.api.KieBase) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 74 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class TimerAndCalendarTest method testCalendarsWithIntervalsAndStartAndLimit.

@Test(timeout = 10000)
public void testCalendarsWithIntervalsAndStartAndLimit() throws Exception {
    String str = "";
    str += "package org.simple \n";
    str += "global java.util.List list \n";
    str += "rule xxx \n";
    str += "  calendars \"cal1\"\n";
    // int: protocol is assumed
    str += "  timer (0d 1d; start=3-JAN-2010, repeat-limit=4) ";
    str += "when \n";
    str += "then \n";
    str += "  list.add(\"fired\"); \n";
    str += "end  \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    List list = new ArrayList();
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
    Date date = df.parse("1-JAN-2010");
    Calendar cal1 = new Calendar() {

        public boolean isTimeIncluded(long timestamp) {
            return true;
        }
    };
    long oneDay = 60 * 60 * 24;
    ksession.getCalendars().set("cal1", cal1);
    ksession.setGlobal("list", list);
    timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    // day 3
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    // day 5
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    timeService.advanceTime(oneDay, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
}
Also used : KieBase(org.kie.api.KieBase) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) Calendar(org.kie.api.time.Calendar) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Example 75 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler in project drools by kiegroup.

the class TimerAndCalendarTest method testExprTimeRescheduled.

@Test(timeout = 10000)
public void testExprTimeRescheduled() throws Exception {
    String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "rule r1 timer (expr: f1.field2, f1.field4)\n" + "when\n" + "    f1 : FactA() \n" + "then\n" + "    list.add( f1 );\n" + "end\n" + "\n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    KieBase kbase = loadKnowledgeBaseFromString(text);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    FactA fact1 = new FactA();
    fact1.setField1("f1");
    fact1.setField2(500);
    fact1.setField4(1000);
    FactHandle fh = (FactHandle) ksession.insert(fact1);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(1100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(fact1, list.get(0));
    timeService.advanceTime(1100, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(fact1, list.get(1));
    timeService.advanceTime(400, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertEquals(fact1, list.get(2));
    list.clear();
    // the activation state of the rule is not changed so the timer isn't reset
    // since the timer alredy fired it will only use only the period that now will be set to 2000
    fact1.setField2(300);
    fact1.setField4(2000);
    ksession.update(fh, fact1);
    ksession.fireAllRules();
    // 100 has passed of the 1000, from the previous schedule
    // so that should be deducted from the 2000 period above, meaning
    // we only need to increment another 1950
    timeService.advanceTime(1950, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(fact1, list.get(0));
    list.clear();
    timeService.advanceTime(1000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(700, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) FactA(org.drools.compiler.FactA) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Aggregations

PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)81 KieSession (org.kie.api.runtime.KieSession)76 Test (org.junit.Test)75 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)72 KieBase (org.kie.api.KieBase)60 ArrayList (java.util.ArrayList)39 KieHelper (org.kie.internal.utils.KieHelper)39 List (java.util.List)29 Date (java.util.Date)19 Arrays.asList (java.util.Arrays.asList)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14 StockTick (org.drools.compiler.StockTick)13 DateFormat (java.text.DateFormat)9 SimpleDateFormat (java.text.SimpleDateFormat)9 Calendar (org.kie.api.time.Calendar)8 StockTickInterface (org.drools.compiler.StockTickInterface)7 EntryPoint (org.kie.api.runtime.rule.EntryPoint)7 IOException (java.io.IOException)5 FactA (org.drools.compiler.FactA)5