Search in sources :

Example 11 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler 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 12 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler 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 13 with PseudoClockScheduler

use of org.drools.core.time.impl.PseudoClockScheduler 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)

Example 14 with PseudoClockScheduler

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

the class TimerAndCalendarTest method testCalendarNormalRuleSingleCalendar.

@Test(timeout = 10000)
public void testCalendarNormalRuleSingleCalendar() throws Exception {
    String str = "";
    str += "package org.simple \n";
    str += "global java.util.List list \n";
    str += "rule xxx \n";
    str += "  calendars \"cal1\"\n";
    str += "when \n";
    str += "  String()\n";
    str += "then \n";
    str += "  list.add(\"fired\"); \n";
    str += "end  \n";
    Calendar calFalse = new Calendar() {

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

        public boolean isTimeIncluded(long timestamp) {
            return true;
        }
    };
    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");
    ksession.getCalendars().set("cal1", calTrue);
    timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
    ksession.setGlobal("list", list);
    ksession.insert("o1");
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    ksession.insert("o2");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    ksession.getCalendars().set("cal1", calFalse);
    timeService.advanceTime(10, TimeUnit.SECONDS);
    ksession.insert("o3");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    ksession.getCalendars().set("cal1", calTrue);
    timeService.advanceTime(30, TimeUnit.SECONDS);
    ksession.insert("o4");
    ksession.fireAllRules();
    assertEquals(3, 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 15 with PseudoClockScheduler

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

the class TimerAndCalendarTest method checkIntervalTimerWithStringExpressions.

private void checkIntervalTimerWithStringExpressions(boolean useExprForStart, String startTime) throws Exception {
    String str = "package org.simple;\n" + "global java.util.List list;\n" + "\n" + "declare Bean\n" + "  delay   : String = \"30s\"\n" + "  period  : long = 60000\n" + "  start   : String = \"3-JAN-2010\"\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=" + (useExprForStart ? "$s" : "3-JAN-2010") + " )\n" + "when\n" + "  Bean( $d : delay, $p : period, $s : start )\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(startTime).getTime());
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(40, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    timeService.advanceTime(60, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(3, list.size());
    // simulate a pause in the use of the engine by advancing the system clock
    timeService.setStartupTime(DateUtils.parseDate("3-MAR-2010").getTime());
    list.clear();
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    // fires once to recover from missing activation
    assertEquals(1, list.size());
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    timeService.advanceTime(40, 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) 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)

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