Search in sources :

Example 6 with KieSessionConfiguration

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

the class FactHandleMarshallingTest method createWorkingMemory.

private StatefulKnowledgeSessionImpl createWorkingMemory(InternalKnowledgeBase kBase) {
    // WorkingMemoryEntryPoint
    KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    SessionConfiguration sessionConf = ((SessionConfiguration) ksconf);
    StatefulKnowledgeSessionImpl wm = new StatefulKnowledgeSessionImpl(1L, kBase, true, sessionConf, EnvironmentFactory.newEnvironment());
    return wm;
}
Also used : StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration)

Example 7 with KieSessionConfiguration

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

the class MarshallingTest method testMarshallWithTimedRule.

@Test
public void testMarshallWithTimedRule() {
    // DROOLS-795
    String drl = "rule \"Rule A Timeout\"\n" + "when\n" + "    String( this == \"ATrigger\" )\n" + "then\n" + "   insert (new String( \"A-Timer\") );\n" + "end\n" + "\n" + "rule \"Timer For rule A Timeout\"\n" + "    timer ( int: 5s )\n" + "when\n" + "   String( this == \"A-Timer\")\n" + "then\n" + "   delete ( \"A-Timer\" );\n" + "   delete ( \"ATrigger\" );\n" + "end\n";
    KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(EqualityBehaviorOption.EQUALITY, DeclarativeAgendaOption.ENABLED, EventProcessingOption.STREAM);
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    ksession.insert("ATrigger");
    assertEquals(1, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(2, ksession.getFactCount());
    SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(4, TimeUnit.SECONDS);
    assertEquals(2, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(2, ksession.getFactCount());
    ksession = marshallAndUnmarshall(kbase, ksession, sessionConfig);
    clock = ksession.getSessionClock();
    clock.advanceTime(4, TimeUnit.SECONDS);
    assertEquals(2, ksession.getFactCount());
    ksession.fireAllRules();
    assertEquals(0, ksession.getFactCount());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 8 with KieSessionConfiguration

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

the class TimerAndCalendarTest method testIntervalTimer.

@Test(timeout = 10000)
public void testIntervalTimer() throws Exception {
    String str = "";
    str += "package org.simple \n";
    str += "global java.util.List list \n";
    str += "rule xxx \n";
    str += "  timer (int:30s 10s) ";
    str += "when \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);
    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(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) Date(java.util.Date) Test(org.junit.Test)

Example 9 with KieSessionConfiguration

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

the class TimerAndCalendarTest method testCronTimer.

@Test(timeout = 10000)
public void testCronTimer() throws Exception {
    String str = "";
    str += "package org.simple \n";
    str += "global java.util.List list \n";
    str += "rule xxx \n";
    str += "  timer (cron:15 * * * * ?) ";
    str += "when \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);
    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");
    timeService.advanceTime(date.getTime(), TimeUnit.MILLISECONDS);
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, list.size());
    timeService.advanceTime(10, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(30, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    timeService.advanceTime(30, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(2, list.size());
}
Also used : KieBase(org.kie.api.KieBase) 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 10 with KieSessionConfiguration

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

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