Search in sources :

Example 1 with SessionClock

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

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

the class TimerAndCalendarTest method testRaceConditionWithTimedRuleExectionOption.

@Test(timeout = 10000)
@Ignore("the listener callback holds some locks so blocking in it is not safe")
public void testRaceConditionWithTimedRuleExectionOption() throws Exception {
    // BZ-1073880
    String str = "package org.simple \n" + "global java.util.List list \n" + "rule xxx @Propagation(EAGER)\n" + "  timer (int:30s 10s) " + "when \n" + "  $s: String()\n" + "then \n" + "  list.add($s); \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);
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final AtomicBoolean aBool = new AtomicBoolean(true);
    AgendaEventListener agendaEventListener = new DefaultAgendaEventListener() {

        public void afterMatchFired(org.kie.api.event.rule.AfterMatchFiredEvent event) {
            try {
                if (aBool.get()) {
                    barrier.await();
                    aBool.set(false);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    ksession.addEventListener(agendaEventListener);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    // Using the Pseudo Clock.
    SessionClock clock = ksession.getSessionClock();
    SessionPseudoClock pseudoClock = (SessionPseudoClock) clock;
    // Insert the event.
    String eventOne = "one";
    ksession.insert(eventOne);
    // Advance the time .... so the timer will fire.
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    // Rule doesn't fire in PHREAK. This is because you need to call 'fireAllRules' after you've inserted the fact, otherwise the timer
    // job is not created.
    ksession.fireAllRules();
    // Rule still doesn't fire, because the DefaultTimerJob is created now, and now we need to advance the timer again.
    pseudoClock.advanceTime(30000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    String eventTwo = "two";
    ksession.insert(eventTwo);
    ksession.fireAllRules();
    // 60
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    // 70
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    // From here, the second rule should fire.
    // phaser.register();
    pseudoClock.advanceTime(10000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    aBool.set(true);
    // Now 2 rules have fired, and those will now fire every 10 seconds.
    pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
    barrier.await();
    barrier.reset();
    pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
    aBool.set(true);
    barrier.await();
    barrier.reset();
    pseudoClock.advanceTime(20000, TimeUnit.MILLISECONDS);
    aBool.set(true);
    barrier.await();
    barrier.reset();
    ksession.destroy();
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) SessionClock(org.kie.api.time.SessionClock) CyclicBarrier(java.util.concurrent.CyclicBarrier) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with SessionClock

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

the class TimerAndCalendarTest method testTimerRuleAfterCronReloadSession.

@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterCronReloadSession() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = createSession(kbase);
    // must advance time or it won't save.
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // if we do not call 'ksession.fireAllRules()', this test will run successfully.
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // build timer rule, if the rule is fired, the list size will increase every 300ms
    String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + "   timer (cron: * * * * * ?) \n" + "when \n" + "then \n" + "        list.add(list.size()); \n" + " end";
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addPackages(kpackages);
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    ksession.setGlobal("list", list);
    ksession.setGlobal("list", list);
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    Assert.assertEquals(1, list.size());
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(3, TimeUnit.SECONDS);
    Assert.assertEquals(4, list.size());
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(2, TimeUnit.SECONDS);
    // if the rule is fired, the list size will greater than one.
    Assert.assertEquals(6, list.size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) SessionClock(org.kie.api.time.SessionClock) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with SessionClock

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

the class TimerAndCalendarTest method testTimerRuleAfterIntReloadSession.

@Test
@Ignore("beta4 phreak")
public void testTimerRuleAfterIntReloadSession() throws Exception {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KieSession ksession = createSession(kbase);
    // must advance time or it won't save.
    SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // if we do not call 'ksession.fireAllRules()', this test will run successfully.
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(300, TimeUnit.MILLISECONDS);
    // build timer rule, if the rule is fired, the list size will increase every 500ms
    String timerRule = "package org.drools.test\n" + "global java.util.List list \n" + "rule TimerRule \n" + "   timer (int:1000 500) \n" + "when \n" + "then \n" + "        list.add(list.size()); \n" + " end";
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addPackages(kpackages);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession.fireAllRules();
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    ksession = disposeAndReloadSession(ksession, kbase);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(10, TimeUnit.MILLISECONDS);
    List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>());
    ksession.setGlobal("list", list);
    Assert.assertEquals(0, list.size());
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1700, TimeUnit.MILLISECONDS);
    Assert.assertEquals(2, list.size());
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.setGlobal("list", list);
    clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
    clock.advanceTime(1000, TimeUnit.MILLISECONDS);
    // if the rule is fired, the list size will greater than one.
    Assert.assertEquals(4, list.size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) SessionClock(org.kie.api.time.SessionClock) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with SessionClock

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

the class MarshallingTest method testMarshallEntryPointsWithSlidingTimeWindow.

@Test
@Ignore("beta4 phreak")
public void testMarshallEntryPointsWithSlidingTimeWindow() throws Exception {
    String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "import java.util.List\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + "   $l : List() from collect( A()  over window:time(30s) from entry-point 'a-ep') \n" + "then\n" + "   list.add( $l );" + "end\n";
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(conf, str);
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    KieSession ksession = createKnowledgeSession(kbase, ksconf);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    EntryPoint aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(2, ((List) list.get(0)).size());
    PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(15, TimeUnit.SECONDS);
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    aep = ksession.getEntryPoint("a-ep");
    aep.insert(new A());
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    ksession = marsallStatefulKnowledgeSession(ksession);
    assertEquals(4, ((List) list.get(0)).size());
    timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
    timeService.advanceTime(20, TimeUnit.SECONDS);
    ksession = marsallStatefulKnowledgeSession(ksession);
    list.clear();
    ksession.fireAllRules();
    assertEquals(2, ((List) list.get(0)).size());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) FactA(org.drools.compiler.FactA) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) SessionClock(org.kie.api.time.SessionClock) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

KieSession (org.kie.api.runtime.KieSession)6 SessionClock (org.kie.api.time.SessionClock)6 Test (org.junit.Test)5 Ignore (org.junit.Ignore)4 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)4 ArrayList (java.util.ArrayList)3 KieBase (org.kie.api.KieBase)3 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)3 List (java.util.List)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 KiePackage (org.kie.api.definition.KiePackage)2 Resource (org.kie.api.io.Resource)2 Arrays.asList (java.util.Arrays.asList)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 FactA (org.drools.compiler.FactA)1 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1 ExecutionResultImpl (org.drools.core.runtime.impl.ExecutionResultImpl)1 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)1