Search in sources :

Example 96 with KieBase

use of org.kie.api.KieBase in project drools by kiegroup.

the class TimerAndCalendarTest method testIntervalTimerWithoutFire.

@Test(timeout = 10000)
public void testIntervalTimerWithoutFire() throws Exception {
    String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx \n" + "  timer (int:30s 10s) " + "when \n" + "then \n" + "  list.add(\"fired\"); \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 list = new ArrayList();
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(35, TimeUnit.SECONDS);
    assertEquals(1, list.size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(2, list.size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    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) Date(java.util.Date) Test(org.junit.Test)

Example 97 with KieBase

use of org.kie.api.KieBase 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 98 with KieBase

use of org.kie.api.KieBase 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 99 with KieBase

use of org.kie.api.KieBase 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 100 with KieBase

use of org.kie.api.KieBase 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)

Aggregations

KieBase (org.kie.api.KieBase)1272 Test (org.junit.Test)1191 KieSession (org.kie.api.runtime.KieSession)1011 ArrayList (java.util.ArrayList)592 List (java.util.List)392 Person (org.drools.compiler.Person)214 FactHandle (org.kie.api.runtime.rule.FactHandle)176 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)168 KieHelper (org.kie.internal.utils.KieHelper)156 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)154 Cheese (org.drools.compiler.Cheese)139 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)99 Arrays.asList (java.util.Arrays.asList)87 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)86 QueryResults (org.kie.api.runtime.rule.QueryResults)78 KieServices (org.kie.api.KieServices)74 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)67 Model (org.drools.model.Model)64 Rule (org.drools.model.Rule)64 ModelImpl (org.drools.model.impl.ModelImpl)64