Search in sources :

Example 71 with KieContainer

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

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

the class IncrementalCompilationTest method testRuleRemovalAfterUpdate.

@Test
public void testRuleRemovalAfterUpdate() {
    // DROOLS-801
    String drl = "rule Rule1\n" + "  when\n" + "    Integer()\n" + "    String()\n" + "    Long()\n" + "    not (Double())\n" + "  then \n" + "end\n" + "\n" + "rule Rule2\n" + "  when\n" + "    Integer()\n" + "    String()\n" + "  then \n" + "end";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert("test");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    createAndDeployJar(ks, releaseId2, drl);
    kc.updateToVersion(releaseId2);
    FactHandle handle = ksession.insert(1);
    ksession.fireAllRules();
    ksession.update(handle, 1);
    ksession.fireAllRules();
    ReleaseId releaseId3 = ks.newReleaseId("org.kie", "test-upgrade", "1.2.0");
    createAndDeployJar(ks, releaseId3);
    kc.updateToVersion(releaseId3);
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) 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 73 with KieContainer

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

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

the class SecurityPolicyTest method testAccumulateFunctionMVEL.

@Test
public void testAccumulateFunctionMVEL() throws Exception {
    String drl = "package org.foo.bar\n" + "import " + MaliciousExitHelper.class.getName().replace('$', '.') + " \n" + "rule testRule dialect \"mvel\" \n" + "    when\n" + "        Number() from accumulate(Object(), " + "               sum(MaliciousExitHelper.exit()))\n" + "    then\n" + "end";
    try {
        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
        ks.newKieBuilder(kfs).buildAll();
        ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
        KieContainer kc = ks.newKieContainer(releaseId);
        KieSession ksession = kc.newKieSession();
        ksession.insert("foo");
        ksession.fireAllRules();
        Assert.fail("The security policy for the rule should have prevented this from executing...");
    } catch (PropertyAccessException e) {
        // weak way of testing but couldn't find a better way
        if (e.toString().contains("The security policy should have prevented")) {
            Assert.fail("The security policy for the rule should have prevented this from executing...");
        } else {
        // test succeeded
        }
    } catch (Exception e) {
        if (e.toString().contains("access denied (\"java.lang.RuntimePermission\" \"exitVM.0\")")) {
        // test succeeded
        } else {
            throw e;
        }
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) PropertyAccessException(org.mvel2.PropertyAccessException) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) PropertyAccessException(org.mvel2.PropertyAccessException) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 75 with KieContainer

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

the class SecurityPolicyTest method testCustomAccumulateMVEL.

@Test
public void testCustomAccumulateMVEL() throws Exception {
    String drl = "package org.foo.bar\n" + "rule testRule dialect \"mvel\" \n" + "    when\n" + "        Number() from accumulate(Object(), " + "               init(System.exit(-1);), " + "               action(System.exit(-1);), " + "               reverse(System.exit(-1);), " + "               result(0))\n" + "    then\n" + "end";
    try {
        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes()).setSourcePath("org/foo/bar/r1.drl"));
        ks.newKieBuilder(kfs).buildAll();
        ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
        KieContainer kc = ks.newKieContainer(releaseId);
        KieSession ksession = kc.newKieSession();
        ksession.fireAllRules();
        Assert.fail("The security policy for the rule should have prevented this from executing...");
    } catch (PropertyAccessException e) {
        // weak way of testing but couldn't find a better way
        if (e.toString().contains("The security policy should have prevented")) {
            Assert.fail("The security policy for the rule should have prevented this from executing...");
        } else {
        // test succeeded
        }
    } catch (Exception e) {
        if (e.toString().contains("access denied (\"java.lang.RuntimePermission\" \"exitVM.-1\")")) {
        // test succeeded
        } else {
            throw e;
        }
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) PropertyAccessException(org.mvel2.PropertyAccessException) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) ConsequenceException(org.kie.api.runtime.rule.ConsequenceException) PropertyAccessException(org.mvel2.PropertyAccessException) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieContainer (org.kie.api.runtime.KieContainer)357 KieServices (org.kie.api.KieServices)271 Test (org.junit.Test)246 KieSession (org.kie.api.runtime.KieSession)191 ReleaseId (org.kie.api.builder.ReleaseId)185 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)108 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)90 KieFileSystem (org.kie.api.builder.KieFileSystem)81 KieModule (org.kie.api.builder.KieModule)75 KieBuilder (org.kie.api.builder.KieBuilder)56 KieModuleModel (org.kie.api.builder.model.KieModuleModel)54 KieBase (org.kie.api.KieBase)49 ArrayList (java.util.ArrayList)44 Results (org.kie.api.builder.Results)40 Resource (org.kie.api.io.Resource)29 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)29 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)27 Message (org.drools.compiler.Message)26 KieBaseModel (org.kie.api.builder.model.KieBaseModel)26 InternalKieContainer (org.drools.core.impl.InternalKieContainer)25