Search in sources :

Example 6 with Calendar

use of org.kie.api.time.Calendar 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 7 with Calendar

use of org.kie.api.time.Calendar 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 8 with Calendar

use of org.kie.api.time.Calendar in project drools by kiegroup.

the class TimerAndCalendarTest method testCalendarsWithIntervalsAndStartAndEnd.

@Test(timeout = 10000)
public void testCalendarsWithIntervalsAndStartAndEnd() 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, end=5-JAN-2010) ";
    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)

Aggregations

DateFormat (java.text.DateFormat)8 SimpleDateFormat (java.text.SimpleDateFormat)8 ArrayList (java.util.ArrayList)8 Arrays.asList (java.util.Arrays.asList)8 Date (java.util.Date)8 List (java.util.List)8 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)8 Test (org.junit.Test)8 KieBase (org.kie.api.KieBase)8 KieSession (org.kie.api.runtime.KieSession)8 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)8 Calendar (org.kie.api.time.Calendar)8 Locale (java.util.Locale)2