Search in sources :

Example 1 with SessionPseudoClock

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

the class PseudoClockRunner method updateKieSessionTime.

private void updateKieSessionTime(long timeNow, long distance, KieSession ksession) {
    SessionPseudoClock clock = ksession.getSessionClock();
    if (clock.getCurrentTime() != timeNow) {
        long newTime = startTime + distance;
        long currentTime = clock.getCurrentTime();
        clock.advanceTime(newTime - currentTime, TimeUnit.MILLISECONDS);
    }
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock)

Example 2 with SessionPseudoClock

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

the class IncrementalCompilationTest method testAlphaNodeSharingIsOK.

@Test
public void testAlphaNodeSharingIsOK() throws Exception {
    // inspired by drools-usage Fmt9wZUFi8g
    // check timer -scheduled activations are preserved if rule untouched by incremental compilation even with alpha node sharing.
    StringBuffer drl = new StringBuffer();
    drl.append("package org.drools.compiler\n");
    drl.append("global java.util.List list;\n");
    drl.append("global java.util.List list2;\n");
    drl.append("rule R1\n");
    drl.append(" timer (int: 3s)\n");
    drl.append(" when\n");
    drl.append("   $m : String()\n");
    drl.append(" then\n");
    drl.append("   list.add( $m );\n");
    drl.append("   retract( $m );\n");
    drl.append("end\n");
    drl.append("rule RS\n");
    drl.append(" timer (int: 3s)\n");
    drl.append(" salience 1\n");
    drl.append(" when\n");
    drl.append("   $i : Integer()\n");
    drl.append("   $m : String()\n");
    drl.append(" then\n");
    drl.append("   System.out.println($i + \" \"+ $m);");
    drl.append("   list2.add($i + \" \"+ $m);\n");
    drl.append("end\n");
    StringBuffer drl2 = new StringBuffer();
    drl2.append("package org.drools.compiler\n");
    drl2.append("global java.util.List list;\n");
    drl2.append("global java.util.List list2;\n");
    drl2.append("rule R1\n");
    drl2.append(" timer (int: 3s)\n");
    drl2.append(" when\n");
    drl2.append("   $m : String()\n");
    drl2.append(" then\n");
    drl2.append("   list.add( $m );\n");
    drl2.append("   list.add( $m );\n");
    drl2.append("   retract( $m );\n");
    drl2.append("end\n");
    drl2.append("rule RS\n");
    drl2.append(" timer (int: 3s)\n");
    drl2.append(" salience 1\n");
    drl2.append(" when\n");
    drl2.append("   $i : Integer()\n");
    drl2.append("   $m : String()\n");
    drl2.append(" then\n");
    drl2.append("   System.out.println($i + \" \"+ $m);");
    drl2.append("   list2.add($i + \" \"+ $m);\n");
    drl2.append("end\n");
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJarInStreamMode(ks, releaseId1, drl.toString());
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSessionConfiguration ksconf = ks.newKieSessionConfiguration();
    ksconf.setOption(TimedRuleExecutionOption.YES);
    ksconf.setOption(TimerJobFactoryOption.get("trackable"));
    ksconf.setOption(ClockTypeOption.get("pseudo"));
    KieSession ksession = kc.newKieSession(ksconf);
    SessionPseudoClock timeService = ksession.getSessionClock();
    timeService.advanceTime(new Date().getTime(), TimeUnit.MILLISECONDS);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    List list2 = new ArrayList();
    ksession.setGlobal("list2", list2);
    ksession.insert(new String("A"));
    ksession.insert(1);
    ksession.fireAllRules();
    assertEquals("1. Initial run: no message expected after rule fired immediately after fireAllRules due to duration of 5 sec", 0, list.size());
    assertEquals("1. Initial run: no message expected after rule fired immediately after fireAllRules due to duration of 5 sec", 0, list2.size());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.1");
    km = createAndDeployJarInStreamMode(ks, releaseId2, drl2.toString());
    kc.updateToVersion(releaseId2);
    timeService.advanceTime(3200, TimeUnit.MILLISECONDS);
    assertEquals("1. R1 is NOT preserved", 0, list.size());
    assertEquals("1. RS is preserved", 1, list2.size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Date(java.util.Date) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 3 with SessionPseudoClock

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

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

the class RuleUnitTest method testRuleUnitFromKieContainer.

@Test
public void testRuleUnitFromKieContainer() {
    String drl = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithSingleItem.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /person[age >= adultAge]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\n" + "end";
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "    <kbase name=\"unit\" equalsBehavior=\"equality\" eventProcessingMode=\"stream\">\n" + "        <ksession name=\"unit-rules\" default=\"true\" type=\"stateful\" clockType=\"pseudo\"/>\n" + "    </kbase>\n" + "</kmodule>";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl).writeKModuleXML(kmodule);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    RuleUnitExecutor executor = kcontainer.newRuleUnitExecutor();
    assertTrue(executor.getKieSession().getSessionClock() instanceof SessionPseudoClock);
    RuleUnit adultUnit = new AdultUnitWithSingleItem(new Person("Mario", 42));
    assertEquals(1, executor.run(adultUnit));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieServices(org.kie.api.KieServices) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 5 with SessionPseudoClock

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

Aggregations

SessionPseudoClock (org.kie.api.time.SessionPseudoClock)74 Test (org.junit.Test)66 KieSession (org.kie.api.runtime.KieSession)52 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)39 KieBase (org.kie.api.KieBase)37 ArrayList (java.util.ArrayList)30 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)20 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)15 EntryPoint (org.kie.api.runtime.rule.EntryPoint)15 StockTick (org.drools.compiler.StockTick)14 Message (org.drools.testcoverage.common.model.Message)14 List (java.util.List)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 StockTick (org.drools.modelcompiler.domain.StockTick)10 OrderEvent (org.drools.compiler.OrderEvent)6 QueryResults (org.kie.api.runtime.rule.QueryResults)4 KieHelper (org.kie.internal.utils.KieHelper)4 StockTickInterface (org.drools.compiler.StockTickInterface)3