Search in sources :

Example 26 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class IncrementalCompilationTest method testPropertyReactivityOfAnOriginallyUnknownClass.

@Test
public void testPropertyReactivityOfAnOriginallyUnknownClass() {
    // DROOLS-1684
    String drl1 = "import " + TypeA.class.getCanonicalName() + "\n" + "rule \"RULE_1\"\n" + "    when\n" + "        TypeA()" + "    then\n" + "end\n";
    String drl2 = "import " + TypeB.class.getCanonicalName() + "\n" + "rule \"RULE_2\"\n" + "    when\n" + "        $b : TypeB() @watch(!*)" + "    then\n" + "        modify($b) { setValue(0) } \n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    createAndDeployJar(ks, releaseId2, drl2);
    kc.updateToVersion(releaseId2);
    ksession.insert(new TypeB(1));
    int fired = ksession.fireAllRules(10);
    assertEquals(1, fired);
}
Also used : 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 27 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeSameSessionRemovingGlobal.

@Test
public void testKJarUpgradeSameSessionRemovingGlobal() throws Exception {
    // DROOLS-752
    String drl1 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "global java.lang.String bar\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.compiler\n" + "global java.lang.String foo\n" + "global java.lang.String baz\n" + "rule R2 when\n" + "   $m : Message( )\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, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.setGlobal("foo", "foo");
    ksession.setGlobal("bar", "bar");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, drl2);
    kc.updateToVersion(releaseId2);
    ksession.setGlobal("baz", "baz");
    Globals globals = ksession.getGlobals();
    assertEquals(2, globals.getGlobalKeys().size());
    assertEquals("foo", ksession.getGlobal("foo"));
    assertNull(ksession.getGlobal("bar"));
    assertEquals("baz", ksession.getGlobal("baz"));
}
Also used : Globals(org.kie.api.runtime.Globals) 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 28 with KieModule

use of org.kie.api.builder.KieModule 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 29 with KieModule

use of org.kie.api.builder.KieModule 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 30 with KieModule

use of org.kie.api.builder.KieModule in project drools by kiegroup.

the class KieModuleRepoTest method newerVersionDeployOverwritesTest.

// 2. simultaneous deploy requests
// - multitenant UI
// - duplicate REST requests
@Test(timeout = 5000)
public void newerVersionDeployOverwritesTest() throws Exception {
    // setup
    final ReleaseIdImpl releaseId = new ReleaseIdImpl("org", "deployTwiceAfterUpdateDependency", "1.0-SNAPSHOT");
    final InternalKieModule originalOldKieModule = new InternalKieModuleStub();
    final ReleaseId dependentReleaseid = new ReleaseIdImpl("org", "deployTwiceAfterUpdate", "1.0-SNAPSHOT");
    final KieContainerImpl kieContainer = createMockKieContainer(dependentReleaseid, kieModuleRepo);
    // 1. deploy
    kieModuleRepo.store(originalOldKieModule);
    // 2. another project is dependent on this project
    kieContainer.updateDependencyToVersion(releaseId, releaseId);
    final InternalKieModule newKieModule = mock(InternalKieModule.class);
    when(newKieModule.getReleaseId()).thenReturn(releaseId);
    when(newKieModule.getCreationTimestamp()).thenReturn(10l);
    final CyclicBarrier storeOperationBarrier = new CyclicBarrier(2);
    final CyclicBarrier storeSerializationBarrier = new CyclicBarrier(2);
    final CyclicBarrier threadsFinishedBarrier = new CyclicBarrier(3);
    final Runnable deployRunnable = () -> {
        waitFor(storeOperationBarrier);
        // Second thread waits with store until the first one finishes with it.
        if (Thread.currentThread().getName().equals("two")) {
            waitFor(storeSerializationBarrier);
        }
        kieModuleRepo.store(newKieModule);
        if (Thread.currentThread().getName().equals("one")) {
            waitFor(storeSerializationBarrier);
        }
        waitFor(threadsFinishedBarrier);
    };
    final ExecutorService executor = Executors.newFixedThreadPool(2);
    // 2. remove and redploy
    final Thread deployThread = new Thread(deployRunnable);
    deployThread.setName("one");
    executor.submit(deployThread);
    final Thread secondDeployThread = new Thread(deployRunnable);
    secondDeployThread.setName("two");
    executor.submit(secondDeployThread);
    try {
        waitFor(threadsFinishedBarrier);
    } finally {
        executor.shutdownNow();
    }
    // never gets this far, but this is a good check
    final KieModule oldKieModule = kieModuleRepo.oldKieModules.get(releaseId);
    final long oldKieModuleTimeStamp = ((InternalKieModule) oldKieModule).getCreationTimestamp();
    final long originalKieModuleTimestamp = originalOldKieModule.getCreationTimestamp();
    assertEquals("The old kie module in the repo is not the originally deployed module!", originalKieModuleTimestamp, oldKieModuleTimeStamp);
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) ExecutorService(java.util.concurrent.ExecutorService) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

KieModule (org.kie.api.builder.KieModule)124 KieServices (org.kie.api.KieServices)104 Test (org.junit.Test)94 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)84 ReleaseId (org.kie.api.builder.ReleaseId)84 KieContainer (org.kie.api.runtime.KieContainer)75 KieSession (org.kie.api.runtime.KieSession)64 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)53 Message (org.drools.compiler.Message)25 KieFileSystem (org.kie.api.builder.KieFileSystem)23 Resource (org.kie.api.io.Resource)20 ArrayList (java.util.ArrayList)15 KieModuleModel (org.kie.api.builder.model.KieModuleModel)15 KieBuilder (org.kie.api.builder.KieBuilder)13 KieRepository (org.kie.api.builder.KieRepository)10 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)7 KieBaseModel (org.kie.api.builder.model.KieBaseModel)7 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 Results (org.kie.api.builder.Results)5