Search in sources :

Example 1 with InternalKieBuilder

use of org.kie.internal.builder.InternalKieBuilder in project drools by kiegroup.

the class IncrementalCompilationTest method testIncrementalCompilationWithAmbiguousRedeclares.

@Test
public void testIncrementalCompilationWithAmbiguousRedeclares() {
    String drl1 = "package domestic; " + "import foreign.*; " + "declare foreign.Score " + "    id       : String " + "end ";
    String drl2 = "\n" + "package domestic; " + "import foreign.*; " + "declare foreign.Score " + "    id       : String " + "end\n" + "declare Score " + "    value : double " + "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();
    ksession.fireAllRules();
    kfs.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drl2.drl"));
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    System.out.println(results.getAddedMessages());
    assertEquals(0, results.getAddedMessages().size());
    Results updateResults = kc.updateToVersion(id);
    assertEquals(0, updateResults.getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) 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) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 2 with InternalKieBuilder

use of org.kie.internal.builder.InternalKieBuilder in project drools by kiegroup.

the class IncrementalCompilationTest method testIncremenatalCompilationAddingFieldToDeclaredType.

@Test
public void testIncremenatalCompilationAddingFieldToDeclaredType() {
    // DROOLS-2197
    String declares1 = "declare Address\n" + "   streetName : String\n" + "   city : String\n" + "end";
    String declares2 = "declare Address\n" + "   streetName : String\n" + "   city : String\n" + "   flg: String\n" + "end";
    String rules1 = "rule R when\n" + "    a : Address( city == \"Antibes\" )\n" + "then\n" + "    a.setStreetName(\"Av. Jean Medecin\");\n" + "end";
    String rules2 = "rule R when\n" + "    a : Address( city == \" Paris \" )\n" + "then\n" + "    a.setStreetName(\" Champs Elisees \");\n" + "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(declares1)).setResourceType(ResourceType.DRL).setSourcePath("declares.drl"));
    kfs.write(ks.getResources().newReaderResource(new StringReader(rules1)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
    kieBuilder.buildAll();
    KieContainer kc = ks.newKieContainer(id);
    KieSession ksession = kc.newKieSession();
    ksession.fireAllRules();
    kfs.write(ks.getResources().newReaderResource(new StringReader(declares2)).setResourceType(ResourceType.DRL).setSourcePath("declares.drl"));
    kfs.write(ks.getResources().newReaderResource(new StringReader(rules2)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    System.out.println(results.getAddedMessages());
    assertEquals(0, results.getAddedMessages().size());
    Results updateResults = kc.updateToVersion(id);
    assertEquals(0, updateResults.getMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) 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) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 3 with InternalKieBuilder

use of org.kie.internal.builder.InternalKieBuilder 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 4 with InternalKieBuilder

use of org.kie.internal.builder.InternalKieBuilder 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 5 with InternalKieBuilder

use of org.kie.internal.builder.InternalKieBuilder in project drools by kiegroup.

the class IncrementalCompilationTest method testIncrementalCompilationAddErrorThenRemoveIt.

@Test
public void testIncrementalCompilationAddErrorThenRemoveIt() throws Exception {
    // Fact Type is unknown ("NonExistentClass" not "Message")
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : NonExistentClass()\n" + "then\n" + "end\n";
    // Field is unknown ("nonExistentField" not "message")
    String drl2_1 = "package org.drools.compiler\n" + "rule R2_1 when\n" + "   $m : Message( nonExistentField == \"Hello World\" )\n" + "then\n" + "end\n";
    // Valid
    String drl2_2 = "package org.drools.compiler\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1);
    // Initial file contains errors
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(1, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    // Add file with error - expect 1 "added" error message
    kfs.write("src/main/resources/r2.drl", drl2_1);
    IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(1, addResults.getAddedMessages().size());
    assertEquals(0, addResults.getRemovedMessages().size());
    // Update flawed file with correct version - expect 0 "added" error messages and removal of 1 previous error relating to updated file
    kfs.write("src/main/resources/r2.drl", drl2_2);
    IncrementalResults removeResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(0, removeResults.getAddedMessages().size());
    assertEquals(1, removeResults.getRemovedMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Test(org.junit.Test)

Aggregations

KieBuilder (org.kie.api.builder.KieBuilder)34 KieFileSystem (org.kie.api.builder.KieFileSystem)34 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)34 KieServices (org.kie.api.KieServices)32 Test (org.junit.Test)31 IncrementalResults (org.kie.internal.builder.IncrementalResults)31 ReleaseId (org.kie.api.builder.ReleaseId)10 KieContainer (org.kie.api.runtime.KieContainer)10 KieSession (org.kie.api.runtime.KieSession)10 Results (org.kie.api.builder.Results)9 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)8 StringReader (java.io.StringReader)4 Message (org.kie.api.builder.Message)4 ArrayList (java.util.ArrayList)2 Message (org.drools.compiler.Message)2 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)2 KieModuleModel (org.kie.api.builder.model.KieModuleModel)2 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)2 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1