Search in sources :

Example 11 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class TimerAndCalendarTest method testCalendarsWithCronAndStartAndEnd.

@Test(timeout = 10000)
public void testCalendarsWithCronAndStartAndEnd() 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" + "  date-expires \"6-JAN-2010\"\n" + "  calendars \"cal1\"\n" + "  timer (cron: 0 0 0 * * ?) " + "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(3, 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 12 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class TimerAndCalendarTest method testCalendarNormalRuleMultipleCalendars.

@Test(timeout = 10000)
public void testCalendarNormalRuleMultipleCalendars() 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";
    str += "when \n";
    str += "  String()\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);
    Calendar calFalse = new Calendar() {

        public boolean isTimeIncluded(long timestamp) {
            return false;
        }
    };
    Calendar calTrue = new Calendar() {

        public boolean isTimeIncluded(long timestamp) {
            return true;
        }
    };
    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");
    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());
}
Also used : 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) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Date(java.util.Date) Test(org.junit.Test)

Example 13 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class TimerAndCalendarTest method testSharedTimers.

@Test
public void testSharedTimers() throws Exception {
    // DROOLS-451
    String str = "package org.simple \n" + "global java.util.List list \n" + "rule R1\n" + "  timer (int:30s 10s) " + "when \n" + "  $i: Integer()\n" + "then \n" + "  System.out.println(\"1\");\n" + "  list.add(\"1\"); \n" + "end  \n" + "rule R2\n" + "  timer (int:30s 10s) " + "when \n" + "  $i: Integer()\n" + "then \n" + "  System.out.println(\"2\");\n" + "  list.add(\"2\"); \n" + "end  \n";
    KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    conf.setOption(ClockTypeOption.get("pseudo"));
    conf.setOption(TimedRuleExecutionOption.YES);
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = createKnowledgeSession(kbase, conf);
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    SessionClock clock = ksession.getSessionClock();
    SessionPseudoClock pseudoClock = (SessionPseudoClock) clock;
    ksession.insert(1);
    ksession.fireAllRules();
    pseudoClock.advanceTime(35, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("1", "2")));
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) SessionClock(org.kie.api.time.SessionClock) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 14 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class TimerAndCalendarTest method testIntervalTimerExpressionWithOr.

@Test(timeout = 10000)
public void testIntervalTimerExpressionWithOr() throws Exception {
    String text = "package org.kie.test\n" + "global java.util.List list\n" + "import " + FactA.class.getCanonicalName() + "\n" + "import " + Foo.class.getCanonicalName() + "\n" + "import " + Pet.class.getCanonicalName() + "\n" + "rule r1 timer (expr: f1.field2, f1.field2; repeat-limit=3)\n" + "when\n" + "    foo: Foo()\n" + "    ( Pet()  and f1 : FactA( field1 == 'f1') ) or \n" + "    f1 : FactA(field1 == 'f2') \n" + "then\n" + "    list.add( f1 );\n" + "    foo.setId( 'xxx' );\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);
    ksession.insert(new Foo(null, null));
    ksession.insert(new Pet(null));
    FactA fact1 = new FactA();
    fact1.setField1("f1");
    fact1.setField2(250);
    FactA fact3 = new FactA();
    fact3.setField1("f2");
    fact3.setField2(1000);
    ksession.insert(fact1);
    ksession.insert(fact3);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(fact1, list.get(0));
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertEquals(fact1, list.get(1));
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // did not change, repeat-limit kicked in
    assertEquals(2, list.size());
    timeService.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    assertEquals(fact3, list.get(2));
    timeService.advanceTime(1000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    assertEquals(4, list.size());
    assertEquals(fact3, list.get(3));
    timeService.advanceTime(1000, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    // did not change, repeat-limit kicked in
    assertEquals(4, list.size());
}
Also used : KieBase(org.kie.api.KieBase) Foo(org.drools.compiler.Foo) 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) Pet(org.drools.compiler.Pet) Test(org.junit.Test)

Example 15 with KieSessionConfiguration

use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.

the class TimerAndCalendarTest method testIntervalTimerWithLongExpressions.

@Test(timeout = 10000)
public void testIntervalTimerWithLongExpressions() throws Exception {
    String str = "package org.simple;\n" + "global java.util.List list;\n" + "\n" + "declare Bean\n" + "  delay   : long = 30000\n" + "  period  : long = 10000\n" + "end\n" + "\n" + "rule init \n" + "when \n" + "then \n" + " insert( new Bean() );\n" + "end \n" + "\n" + "rule xxx\n" + "  salience ($d) \n" + "  timer( expr: $d, $p; start=3-JAN-2010 )\n" + "when\n" + "  Bean( $d : delay, $p : period )\n" + "then\n" + "  list.add( \"fired\" );\n" + "end";
    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();
    timeService.setStartupTime(DateUtils.parseDate("3-JAN-2010").getTime());
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(15, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(3, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(2, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
}
Also used : 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) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Test(org.junit.Test)

Aggregations

KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)188 KieSession (org.kie.api.runtime.KieSession)166 Test (org.junit.Test)157 KieBase (org.kie.api.KieBase)122 ArrayList (java.util.ArrayList)96 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)71 KieHelper (org.kie.internal.utils.KieHelper)68 List (java.util.List)59 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)57 StockTick (org.drools.compiler.StockTick)40 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)39 EntryPoint (org.kie.api.runtime.rule.EntryPoint)28 Arrays.asList (java.util.Arrays.asList)27 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)25 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)22 Date (java.util.Date)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 StockTickInterface (org.drools.compiler.StockTickInterface)18 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)16 SessionConfiguration (org.drools.core.SessionConfiguration)15