Search in sources :

Example 86 with KieSession

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

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace5.

@Test
public void testKJarUpgradeDRLWithSpace5() throws Exception {
    // DROOLS-1399 quinquies
    String drl_1 = "package org.drools.compiler\n" + "rule Rx when\n" + "   $m : Message( message == \"Hello'  World\" )\n" + "then\n" + "end\n";
    String drl_2 = "package org.drools.compiler\n" + "rule Rx when\n" + // <<- notice the EXTRA SPACE typo was removed
    "   $m : Message( message == \"Hello' World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl_1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    // <<- notice the ' character
    ksession.insert(new Message("Hello' World"));
    assertEquals(0, ksession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl_2);
    kc.updateToVersion(releaseId2);
    // rule Rx is UNchanged and should NOT fire again
    assertEquals(1, ksession.fireAllRules());
}
Also used : Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 87 with KieSession

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

the class IncrementalCompilationTest method testDrlRenamingWithEvents.

@Test
public void testDrlRenamingWithEvents() throws Exception {
    // DROOLS-965
    String drl1 = "import " + SimpleEvent.class.getCanonicalName() + ";\n" + "\n" + "global java.util.concurrent.atomic.AtomicInteger counter1;\n" + "global java.util.concurrent.atomic.AtomicInteger counter2;\n" + "\n" + "declare SimpleEvent\n" + "    @role( event )\n" + "    @timestamp( timestamp )\n" + "    @expires( 2d )\n" + "end\n" + "\n" + "rule R1 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\")\n" + "then\n" + "    counter1.incrementAndGet();\n" + "end\n" + "\n" + "rule R2 when\n" + "    $s:SimpleEvent(code==\"MY_CODE\")\n" + "    not SimpleEvent(this != $s, this after [0,10s] $s)\n" + "then\n" + "    counter2.incrementAndGet();\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").setDefault(true).setEventProcessingMode(EventProcessingOption.STREAM);
    KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("KSession1").setDefault(true).setType(KieSessionModel.KieSessionType.STATEFUL).setClockType(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
    KieModule km = deployJar(ks, createKJar(ks, kproj, releaseId1, null, drl1));
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    PseudoClockScheduler clock = ksession.getSessionClock();
    AtomicInteger counter1 = new AtomicInteger(0);
    AtomicInteger counter2 = new AtomicInteger(0);
    ksession.setGlobal("counter1", counter1);
    ksession.setGlobal("counter2", counter2);
    ksession.insert(new SimpleEvent("1", "MY_CODE", 0));
    ksession.fireAllRules();
    clock.advanceTime(5, TimeUnit.SECONDS);
    ksession.insert(new SimpleEvent("2", "MY_CODE", 5));
    ksession.fireAllRules();
    assertEquals(2, counter1.get());
    assertEquals(0, counter2.get());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
    // the null drl placeholder is used to have the same drl with a different file name
    // this causes the removal and readdition of both rules
    km = deployJar(ks, createKJar(ks, kproj, releaseId2, null, (String) null, drl1));
    kc.updateToVersion(releaseId2);
    clock = ksession.getSessionClock();
    clock.advanceTime(16, TimeUnit.SECONDS);
    ksession.insert(new SimpleEvent("3", "MY_CODE", 21));
    ksession.fireAllRules();
    assertEquals(5, counter1.get());
    assertEquals(1, counter2.get());
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 88 with KieSession

use of org.kie.api.runtime.KieSession 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 89 with KieSession

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

the class TestUtil method buildSessionInSteps.

public static KieSession buildSessionInSteps(final boolean reuseKieBaseWhenAddingRules, final String... drls) {
    if (drls == null || drls.length == 0) {
        return KnowledgeBaseFactory.newKnowledgeBase().newKieSession();
    } else {
        String drl = drls[0];
        final KnowledgeBuilder kbuilder = createKnowledgeBuilder(null, drl);
        final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addPackages(kbuilder.getKnowledgePackages());
        final KieSession kSession = kbase.newKieSession();
        for (int i = 1; i < drls.length; i++) {
            drl = drls[i];
            final KnowledgeBuilder kbuilder2;
            if (reuseKieBaseWhenAddingRules) {
                kbuilder2 = createKnowledgeBuilder(kSession.getKieBase(), drl);
            } else {
                kbuilder2 = createKnowledgeBuilder(null, drl);
            }
            ((InternalKnowledgeBase) kSession.getKieBase()).addPackages(kbuilder2.getKnowledgePackages());
        }
        return kSession;
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 90 with KieSession

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

Aggregations

KieSession (org.kie.api.runtime.KieSession)5328 Test (org.junit.Test)4824 KieBase (org.kie.api.KieBase)2414 ArrayList (java.util.ArrayList)2317 List (java.util.List)1105 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)769 FactHandle (org.kie.api.runtime.rule.FactHandle)598 Person (org.drools.modelcompiler.domain.Person)519 HashMap (java.util.HashMap)416 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)415 KieServices (org.kie.api.KieServices)382 KieHelper (org.kie.internal.utils.KieHelper)355 KieContainer (org.kie.api.runtime.KieContainer)298 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)265 InternalFactHandle (org.drools.core.common.InternalFactHandle)259 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)234 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)234 ReleaseId (org.kie.api.builder.ReleaseId)232 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)229 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)207