Search in sources :

Example 86 with KieModule

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

the class IncrementalCompilationTest method testUpdateWithDeclarationPresent.

@Test
public void testUpdateWithDeclarationPresent() throws Exception {
    // DROOLS-560
    String header = "package org.drools.compiler\n" + "import org.drools.compiler.integrationtests.incrementalcompilation.IncrementalCompilationTest.FooEvent\n";
    String declaration = "declare FooEvent\n" + " @timestamp( mytime )\n" + " @role( event )\n" + "end\n";
    String rule1 = "rule R1 when\n" + " $e : FooEvent( )\n" + "then\n" + " insert(new Message(\"Hello R1\"));\n" + "end\n";
    String rule2 = "rule R1 when\n" + " $e : FooEvent( )\n" + "then\n" + " insert(new Message(\"Hello R2\"));\n" + "end\n";
    String file1 = header + declaration + rule1;
    String file2 = header + declaration + rule2;
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, file1);
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new FooEvent(0));
    assertEquals(1, ksession.fireAllRules());
    // Create a new jar for version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJar(ks, releaseId2, file2);
    // try to update the container to version 1.1.0
    Results results = kc.updateToVersion(releaseId2);
    assertFalse("Errors detected on updateToVersion: " + results.getMessages(org.kie.api.builder.Message.Level.ERROR), results.hasMessages(org.kie.api.builder.Message.Level.ERROR));
    // continue working with the session
    ksession.insert(new FooEvent(1));
    assertEquals(2, ksession.fireAllRules());
}
Also used : IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) 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 KieModule

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

the class IncrementalCompilationTest method testPropertyReactivityOfAKnownClass.

@Test
public void testPropertyReactivityOfAKnownClass() {
    String drl1 = "import " + TypeA.class.getCanonicalName() + "\n" + "import " + TypeB.class.getCanonicalName() + "\n" + "rule \"RULE_1\"\n" + "    when\n" + "        TypeA()" + "        TypeB()" + "    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 88 with KieModule

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

the class IncrementalCompilationTest method testMultipleIncrementalCompilationWithExistentialRules.

@Test
public void testMultipleIncrementalCompilationWithExistentialRules() {
    // DROOLS-988
    List<String> drls = new ArrayList<String>();
    drls.add(getExistenzialRule("R0", "> 10"));
    // Create a session with the above rule
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId, drls.toArray(new String[drls.size()]));
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    AtomicInteger counter = new AtomicInteger(0);
    ksession.setGlobal("counter", counter);
    ksession.insert(3);
    ksession.fireAllRules();
    assertEquals(0, counter.get());
    for (int i = 1; i < 11; i++) {
        ReleaseId newReleaseId = ks.newReleaseId("org.kie", "test-upgrade", "1.1." + i);
        drls.add(getExistenzialRule("R" + i, "< 10"));
        km = createAndDeployJar(ks, newReleaseId, drls.toArray(new String[drls.size()]));
        kc.updateToVersion(newReleaseId);
        ksession.fireAllRules();
        assertEquals(i, counter.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) 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 89 with KieModule

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

the class IncrementalCompilationTest method testKJarUpgradeWithDSL.

@Test
public void testKJarUpgradeWithDSL() throws Exception {
    // DROOLS-718
    String dsl = "[when][]There is a Message=Message()\n" + "[when][]-with message \"{factId}\"=message==\"{factId}\"\n" + "\n" + "[then][]Print \"{message}\"=System.out.println(\"{message}\");\n";
    String drl2_1 = "package org.drools.compiler\n" + "rule \"bla\"\n" + "when\n" + "\tThere is a Message\t   \n" + "\t-with message \"Hi Universe\"\n" + "then\n" + "\tPrint \"Found a Message Hi Universe.\"\n" + "end\n";
    String drl2_2 = "package org.drools.compiler\n" + "rule \"bla\"\n" + "when\n" + "\tThere is a Message\t   \n" + "\t-with message \"Hello World\"\n" + "then\n" + "\tPrint \"Found a Message Hello World.\"\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJarWithDSL(ks, releaseId1, dsl, drl2_1);
    // Create a session and fire rules
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(0, ksession.fireAllRules());
    ksession.dispose();
    // Create a new jar for version 1.1.0
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    km = createAndDeployJarWithDSL(ks, releaseId2, dsl, drl2_2);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    // create and use a new session
    ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    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 90 with KieModule

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

the class IncrementalCompilationTest method testSegmentSplitOnIncrementalCompilation.

@Test
public void testSegmentSplitOnIncrementalCompilation() throws Exception {
    // DROOLS-930
    String drl = "import " + Person.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "rule R1 when\n" + "  $s : String()" + "  Person( name == $s ) \n" + "then\n" + "  list.add(\"R1\");\n" + "end\n" + "rule R2 when\n" + "  $s : String()" + "  Person( name == $s ) \n" + "then\n" + "  list.add(\"R2\");\n" + "end\n" + "rule R3 when\n" + "  $s : String()" + "  Person( name != $s ) \n" + "then\n" + "  list.add(\"R3\");\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
    KieModule km = createAndDeployJar(ks, releaseId1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    kc.updateToVersion(releaseId1);
    ksession.insert(new Person("John", 26));
    ksession.insert("John");
    ksession.fireAllRules();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
    km = createAndDeployJar(ks, releaseId2, drl);
    kc.updateToVersion(releaseId2);
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("R1", "R2")));
}
Also used : ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) Person(org.drools.compiler.Person) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) 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