Search in sources :

Example 11 with Calendar

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

the class KieContainerImpl method registerCalendars.

private void registerCalendars(KieSessionModel kSessionModel, KieSession kSession) {
    for (Map.Entry<String, String> entry : kSessionModel.getCalendars().entrySet()) {
        try {
            Calendar calendar = (Calendar) getClassLoader().loadClass(entry.getValue()).newInstance();
            kSession.getCalendars().set(entry.getKey(), calendar);
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            log.error("Cannot instance calendar " + entry.getKey(), e);
        }
    }
}
Also used : Calendar(org.kie.api.time.Calendar) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with Calendar

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

the class CepTest method testCalendarsWithCronAndStartAndEnd.

@Test
public void testCalendarsWithCronAndStartAndEnd() throws Exception {
    final Locale defaultLoc = Locale.getDefault();
    try {
        // Because of the date strings in the DRL, fixable with JBRULES-3444
        Locale.setDefault(Locale.UK);
        final String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + "  date-effective \"02-Jan-2010\"\n" + "  date-expires \"06-Jan-2010\"\n" + "  calendars \"cal1\"\n" + "  timer (cron: 0 0 0 * * ?) " + "when \n" + "then \n" + "  list.add(\"fired\"); \n" + "end  \n";
        KieSession ksession = getKieSession(getCepKieModuleModel(), str);
        try {
            final List list = new ArrayList();
            final PseudoClockScheduler timeService = ksession.getSessionClock();
            final Date date = convertDate("01-Jan-2010");
            final Calendar cal1 = timestamp -> true;
            final 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());
        } finally {
            ksession.dispose();
        }
    } finally {
        Locale.setDefault(defaultLoc);
    }
}
Also used : Locale(java.util.Locale) Date(java.util.Date) ZonedDateTime(java.time.ZonedDateTime) StockTick(org.drools.modelcompiler.domain.StockTick) LocalDateTime(java.time.LocalDateTime) ClockType(org.drools.core.ClockType) StockTickEx(org.drools.modelcompiler.domain.StockTickEx) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Properties(java.util.Properties) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Calendar(org.kie.api.time.Calendar) Timestamp(java.sql.Timestamp) DateTimeHolder(org.drools.modelcompiler.domain.DateTimeHolder) Test(org.junit.Test) FactType(org.kie.api.definition.type.FactType) EvaluationUtil.convertDate(org.drools.modelcompiler.util.EvaluationUtil.convertDate) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) ZoneId(java.time.ZoneId) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) SessionClock(org.kie.api.time.SessionClock) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) KieModuleModel(org.kie.api.builder.model.KieModuleModel) EventFactHandle(org.drools.core.common.EventFactHandle) Assert.assertEquals(org.junit.Assert.assertEquals) StockFact(org.drools.modelcompiler.domain.StockFact) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Calendar(org.kie.api.time.Calendar) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) EvaluationUtil.convertDate(org.drools.modelcompiler.util.EvaluationUtil.convertDate) Test(org.junit.Test)

Example 13 with Calendar

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

the class TimerAndCalendarWithPseudoTimeTest method testCalendarNormalRuleMultipleCalendars.

@Test(timeout = 10000)
public void testCalendarNormalRuleMultipleCalendars() throws Exception {
    final String drl = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + "  calendars \"cal1\", \"cal2\"\n" + "when \n" + "  String()\n" + "then \n" + "  list.add(\"fired\"); \n" + "end  \n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("timer-and-calendar-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final Calendar calFalse = timestamp -> false;
        final Calendar calTrue = timestamp -> true;
        final List list = new ArrayList();
        final PseudoClockScheduler timeService = ksession.getSessionClock();
        final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        final Date date = df.parse("2009-01-01T00:00:00.000-0000");
        ksession.getCalendars().set("cal1", calTrue);
        ksession.getCalendars().set("cal2", calTrue);
        timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
        ksession.setGlobal("list", list);
        ksession.insert("o1");
        ksession.fireAllRules();
        assertEquals(1, list.size());
        ksession.getCalendars().set("cal2", calFalse);
        timeService.advanceTime(10, TimeUnit.SECONDS);
        ksession.insert("o2");
        ksession.fireAllRules();
        assertEquals(1, list.size());
        ksession.getCalendars().set("cal1", calFalse);
        timeService.advanceTime(10, TimeUnit.SECONDS);
        ksession.insert("o3");
        ksession.fireAllRules();
        assertEquals(1, list.size());
        ksession.getCalendars().set("cal1", calTrue);
        ksession.getCalendars().set("cal2", calTrue);
        timeService.advanceTime(30, TimeUnit.SECONDS);
        ksession.insert("o4");
        ksession.fireAllRules();
        assertEquals(2, list.size());
    } finally {
        ksession.dispose();
    }
}
Also used : Pet(org.drools.testcoverage.common.model.Pet) Alarm(org.drools.testcoverage.common.model.Alarm) UndefinedCalendarExcption(org.drools.core.base.UndefinedCalendarExcption) Date(java.util.Date) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) KieSessionTestConfiguration(org.drools.testcoverage.common.util.KieSessionTestConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Locale(java.util.Locale) DateUtils(org.drools.core.util.DateUtils) Arrays.asList(java.util.Arrays.asList) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) DateFormat(java.text.DateFormat) FactA(org.drools.testcoverage.common.model.FactA) Parameterized(org.junit.runners.Parameterized) FactB(org.drools.testcoverage.common.model.FactB) CyclicBarrier(java.util.concurrent.CyclicBarrier) KieBuilder(org.kie.api.builder.KieBuilder) Calendar(org.kie.api.time.Calendar) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) Collection(java.util.Collection) Test(org.junit.Test) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) KieUtil(org.drools.testcoverage.common.util.KieUtil) SessionClock(org.kie.api.time.SessionClock) Ignore(org.junit.Ignore) StockTick(org.drools.testcoverage.common.model.StockTick) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) TimedRuleExecutionOption(org.kie.api.runtime.conf.TimedRuleExecutionOption) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieBase(org.kie.api.KieBase) Calendar(org.kie.api.time.Calendar) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) 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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Example 14 with Calendar

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

the class TimerAndCalendarWithPseudoTimeTest method testCalendarsWithIntervalsAndStartAndLimit.

@Test(timeout = 10000)
public void testCalendarsWithIntervalsAndStartAndLimit() throws Exception {
    final String drl = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + "  calendars \"cal1\"\n" + // int: protocol is assume
    "  timer (0d 1d; start=3-JAN-2010, repeat-limit=4) " + "when \n" + "then \n" + "  list.add(\"fired\"); \n" + "end  \n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("timer-and-calendar-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List list = new ArrayList();
        final PseudoClockScheduler timeService = ksession.getSessionClock();
        final DateFormat df = new SimpleDateFormat("dd-MMM-yyyy", Locale.UK);
        final Date date = df.parse("1-JAN-2010");
        final Calendar cal1 = timestamp -> true;
        final 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());
    } finally {
        ksession.dispose();
    }
}
Also used : Pet(org.drools.testcoverage.common.model.Pet) Alarm(org.drools.testcoverage.common.model.Alarm) UndefinedCalendarExcption(org.drools.core.base.UndefinedCalendarExcption) Date(java.util.Date) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) KieSessionTestConfiguration(org.drools.testcoverage.common.util.KieSessionTestConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Locale(java.util.Locale) DateUtils(org.drools.core.util.DateUtils) Arrays.asList(java.util.Arrays.asList) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) DateFormat(java.text.DateFormat) FactA(org.drools.testcoverage.common.model.FactA) Parameterized(org.junit.runners.Parameterized) FactB(org.drools.testcoverage.common.model.FactB) CyclicBarrier(java.util.concurrent.CyclicBarrier) KieBuilder(org.kie.api.builder.KieBuilder) Calendar(org.kie.api.time.Calendar) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) Collection(java.util.Collection) Test(org.junit.Test) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) KieUtil(org.drools.testcoverage.common.util.KieUtil) SessionClock(org.kie.api.time.SessionClock) Ignore(org.junit.Ignore) StockTick(org.drools.testcoverage.common.model.StockTick) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) TimedRuleExecutionOption(org.kie.api.runtime.conf.TimedRuleExecutionOption) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) EntryPoint(org.kie.api.runtime.rule.EntryPoint) 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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Example 15 with Calendar

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

the class TimerAndCalendarWithPseudoTimeTest method testCalendarsWithCron.

@Test(timeout = 10000)
public void testCalendarsWithCron() throws Exception {
    final String drl = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + "  calendars \"cal1\", \"cal2\"\n" + "  timer (cron:15 * * * * ?) " + "when \n" + "then \n" + "  list.add(\"fired\"); \n" + "end  \n";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("timer-and-calendar-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
    try {
        final List list = new ArrayList();
        final PseudoClockScheduler timeService = ksession.getSessionClock();
        final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        final 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));
        final Calendar cal1 = timestamp -> {
            if (timestamp == date1.getTime()) {
                return true;
            } else {
                return timestamp != date4.getTime();
            }
        };
        final Calendar cal2 = 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());
    } finally {
        ksession.dispose();
    }
}
Also used : Pet(org.drools.testcoverage.common.model.Pet) Alarm(org.drools.testcoverage.common.model.Alarm) UndefinedCalendarExcption(org.drools.core.base.UndefinedCalendarExcption) Date(java.util.Date) RunWith(org.junit.runner.RunWith) SimpleDateFormat(java.text.SimpleDateFormat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KieBaseTestConfiguration(org.drools.testcoverage.common.util.KieBaseTestConfiguration) ArrayList(java.util.ArrayList) KieSessionTestConfiguration(org.drools.testcoverage.common.util.KieSessionTestConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Locale(java.util.Locale) DateUtils(org.drools.core.util.DateUtils) Arrays.asList(java.util.Arrays.asList) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) TestParametersUtil(org.drools.testcoverage.common.util.TestParametersUtil) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) DateFormat(java.text.DateFormat) FactA(org.drools.testcoverage.common.model.FactA) Parameterized(org.junit.runners.Parameterized) FactB(org.drools.testcoverage.common.model.FactB) CyclicBarrier(java.util.concurrent.CyclicBarrier) KieBuilder(org.kie.api.builder.KieBuilder) Calendar(org.kie.api.time.Calendar) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) Collection(java.util.Collection) Test(org.junit.Test) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) KieUtil(org.drools.testcoverage.common.util.KieUtil) SessionClock(org.kie.api.time.SessionClock) Ignore(org.junit.Ignore) StockTick(org.drools.testcoverage.common.model.StockTick) KieBaseUtil(org.drools.testcoverage.common.util.KieBaseUtil) TimedRuleExecutionOption(org.kie.api.runtime.conf.TimedRuleExecutionOption) Assert(org.junit.Assert) Assert.assertEquals(org.junit.Assert.assertEquals) EntryPoint(org.kie.api.runtime.rule.EntryPoint) 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) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Calendar (org.kie.api.time.Calendar)19 ArrayList (java.util.ArrayList)18 Date (java.util.Date)18 List (java.util.List)18 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)18 Test (org.junit.Test)18 KieSession (org.kie.api.runtime.KieSession)18 DateFormat (java.text.DateFormat)16 SimpleDateFormat (java.text.SimpleDateFormat)16 Arrays.asList (java.util.Arrays.asList)16 KieBase (org.kie.api.KieBase)16 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)16 Locale (java.util.Locale)12 TimeUnit (java.util.concurrent.TimeUnit)10 Assert.assertEquals (org.junit.Assert.assertEquals)10 SessionClock (org.kie.api.time.SessionClock)10 Collection (java.util.Collection)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Assertions (org.assertj.core.api.Assertions)8