Search in sources :

Example 41 with KieContainer

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

the class IncrementalCompilationTest method testChangedPackage.

@Test
public void testChangedPackage() {
    // DROOLS-1742
    String drl1 = "package org.a\n" + "rule \"RG_1\"\n" + "    when\n" + "        Boolean()\n" + "        Integer()\n" + "    then\n" + "        System.out.println(\"RG_1\");" + "end\n";
    String drl2 = "package org.b\n" + "rule \"RG_2\"\n" + "    when\n" + "        Boolean()\n" + "        String()\n" + "    then\n" + "        System.out.println(\"RG_2\");" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    createAndDeployJar(ks, releaseId1, drl2, drl1);
    KieContainer kieContainer = ks.newKieContainer(releaseId1);
    KieSession kieSession = kieContainer.newKieSession();
    kieSession.insert("test");
    assertEquals(0, kieSession.fireAllRules());
    createAndDeployJar(ks, releaseId2, drl1);
    kieContainer.updateToVersion(releaseId2);
    assertEquals(0, kieSession.fireAllRules());
}
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) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 42 with KieContainer

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

the class IncrementalCompilationTest method testIncrementalCompilationWithAddedError.

@Test
public void testIncrementalCompilationWithAddedError() throws Exception {
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    String drl2_1 = "package org.drools.compiler\n" + "rule R2_1 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    String drl2_2 = "package org.drools.compiler\n" + "rule R2_2 when\n" + "   $m : Message( nonExistentField == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1).write("src/main/resources/r2.drl", drl2_1);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieSession ksession = kieContainer.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    kfs.write("src/main/resources/r2.drl", drl2_2);
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(1, results.getAddedMessages().size());
    assertEquals(0, results.getRemovedMessages().size());
    kieContainer.updateToVersion(ks.getRepository().getDefaultReleaseId());
    ksession = kieContainer.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 43 with KieContainer

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

the class IncrementalCompilationTest method testIncrementalCompilationWithModuleOverride.

@Test
public void testIncrementalCompilationWithModuleOverride() {
    String drl1 = "package org.test.compiler; " + "global java.util.List list; " + "rule A when $s : String() then System.out.println( 'AAA' + $s ); list.add( 'A' + $s ); end " + "";
    String drl2 = "package totally.unrelated.pack; " + "global java.util.List list; " + "rule B when $s : String() then System.out.println( 'BBB' + $s ); list.add( 'B' + $s ); end " + "";
    String drl3 = "package totally.unrelated.pack; " + "global java.util.List list; " + "rule C when $s : String() then System.out.println( 'CCC' + $s ); list.add( 'C' + $s ); end " + "";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kfs.generateAndWritePomXML(id);
    kfs.write(ks.getResources().newReaderResource(new StringReader(drl1)).setResourceType(ResourceType.DRL).setSourcePath("drl1.drl"));
    kieBuilder.buildAll();
    KieContainer kc = ks.newKieContainer(id);
    KieSession ksession = kc.newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert("X");
    ksession.fireAllRules();
    assertTrue(list.contains("AX"));
    KieFileSystem kfs2 = ks.newKieFileSystem();
    KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
    kfs2.generateAndWritePomXML(id);
    kfs2.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drla.drl"));
    kieBuilder2.buildAll();
    KieContainer kc2 = ks.newKieContainer(id);
    KieSession ksession2 = kc2.newKieSession();
    ksession2.setGlobal("list", list);
    ksession2.insert("X");
    ksession2.fireAllRules();
    kfs2.write(ks.getResources().newReaderResource(new StringReader(drl3)).setResourceType(ResourceType.DRL).setSourcePath("drlb.drl"));
    IncrementalResults results = ((InternalKieBuilder) kieBuilder2).incrementalBuild();
    assertEquals(0, results.getAddedMessages().size());
    kc2.updateToVersion(id);
    ksession2.fireAllRules();
    assertEquals(Arrays.asList("AX", "BX", "CX"), list);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) StringReader(java.io.StringReader) 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) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 44 with KieContainer

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

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace2_usingSingleQuote.

@Test
public void testKJarUpgradeDRLWithSpace2_usingSingleQuote() throws Exception {
    // DROOLS-1399 bis (using single quote)
    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 is the only change in this other version.
    "   $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();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, 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(0, 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 45 with KieContainer

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

the class IncrementalCompilationTest method testJavaClassRedefinition.

@Test
public void testJavaClassRedefinition() {
    // DROOLS-1402
    String JAVA1 = "package org.test;" + "    public class MyBean {\n" + "        private String firstName;\n" + "        public MyBean() { /* empty constructor */ }\n" + "        public MyBean(String firstName) { this.firstName = firstName; }\n" + "        public String getFirstName() { return firstName; }\n" + "        public void setFirstName(String firstName) { this.firstName = firstName; }\n" + "    }";
    String DRL1 = "package org.test;\n" + "\n" + "//from row number: 1\n" + "rule \"Row 1 HelloRules\"\n" + "    when\n" + "        $b : MyBean( firstName == null )\n" + "    then\n" + "        System.out.println($b);" + "end";
    String INIT_DRL = "package org.test; rule RINIT when eval(true) then insert(new MyBean()); end";
    String INIT_DRL_2 = "package org.test; rule RINIT when eval(1==1) then insert(new MyBean()); end";
    String JAVA2 = "package org.test;" + "    public class MyBean {\n" + "        private String firstName;\n" + "        private String lastName;\n" + "        public MyBean() { /* empty constructor */ }\n" + "        public MyBean(String firstName) { this.firstName = firstName; }\n" + "        public MyBean(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }\n" + "        public String getFirstName() { return firstName; }\n" + "        public void setFirstName(String firstName) { this.firstName = firstName; }\n" + "        public String getLastName() { return lastName; }\n" + "        public void setLastName(String lastName) { this.lastName = lastName; }\n" + "    }";
    String DRL2 = "package org.test;\n" + "\n" + "//from row number: 1\n" + "rule \"Row 1 HelloRules\"\n" + "    when\n" + "        $b : MyBean( firstName == null , lastName == null )\n" + "    then\n" + "        System.out.println($b);" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    ReleaseId id = ks.newReleaseId("org.test", "myTest", "1.0.0");
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kfs.generateAndWritePomXML(id);
    kfs.write("src/main/java/org/test/MyBean.java", ks.getResources().newReaderResource(new StringReader(JAVA1)));
    kfs.write(ks.getResources().newReaderResource(new StringReader(DRL1)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
    kfs.write(ks.getResources().newReaderResource(new StringReader(INIT_DRL)).setResourceType(ResourceType.DRL).setSourcePath("INIT_DRL.drl"));
    kieBuilder.buildAll();
    KieContainer kc = ks.newKieContainer(id);
    KieSession ksession = kc.newKieSession();
    int fired = ksession.fireAllRules();
    assertEquals(2, fired);
    ReleaseId id2 = ks.newReleaseId("org.test", "myTest", "2.0.0");
    KieFileSystem kfs2 = ks.newKieFileSystem();
    KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
    kfs2.generateAndWritePomXML(id2);
    kfs2.write("src/main/java/org/test/MyBean.java", ks.getResources().newReaderResource(new StringReader(JAVA2)));
    kfs2.write(ks.getResources().newReaderResource(new StringReader(DRL2)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
    kfs2.write(ks.getResources().newReaderResource(new StringReader(INIT_DRL_2)).setResourceType(ResourceType.DRL).setSourcePath("INIT_DRL.drl"));
    kieBuilder2.buildAll();
    Results updateResults = kc.updateToVersion(id2);
    assertFalse(updateResults.hasMessages(Level.ERROR));
    fired = ksession.fireAllRules();
    assertEquals(2, fired);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) 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