Search in sources :

Example 16 with IncrementalResults

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

the class IncrementalCompilationTest method testIncrementalCompilationWithAmbiguousRedeclares.

@Test
public void testIncrementalCompilationWithAmbiguousRedeclares() {
    final String drl1 = "package domestic; " + "import foreign.*; " + "declare foreign.Score " + "    id       : String " + "end ";
    final String drl2 = "\n" + "package domestic; " + "import foreign.*; " + "declare foreign.Score " + "    id       : String " + "end\n" + "declare Score " + "    value : double " + "end " + "";
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    final ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kfs.generateAndWritePomXML(id);
    kfs.write(ks.getResources().newReaderResource(new StringReader(drl1)).setResourceType(ResourceType.DRL).setSourcePath("drl1.drl"));
    kieBuilder.buildAll(DrlProject.class);
    final KieContainer kc = ks.newKieContainer(id);
    final KieSession ksession = kc.newKieSession();
    ksession.fireAllRules();
    kfs.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drl2.drl"));
    final IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    System.out.println(results.getAddedMessages());
    assertEquals(0, results.getAddedMessages().size());
    final 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 17 with IncrementalResults

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

the class IncrementalCompilationTest method testIncremenatalCompilationAddingFieldToDeclaredType.

@Test
public void testIncremenatalCompilationAddingFieldToDeclaredType() {
    // DROOLS-2197
    final String declares1 = "declare Address\n" + "   streetName : String\n" + "   city : String\n" + "end";
    final String declares2 = "declare Address\n" + "   streetName : String\n" + "   city : String\n" + "   flg: String\n" + "end";
    final String rules1 = "rule R when\n" + "    a : Address( city == \"Antibes\" )\n" + "then\n" + "    a.setStreetName(\"Av. Jean Medecin\");\n" + "end";
    final String rules2 = "rule R when\n" + "    a : Address( city == \" Paris \" )\n" + "then\n" + "    a.setStreetName(\" Champs Elisees \");\n" + "end";
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    final ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
    final 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(DrlProject.class);
    final KieContainer kc = ks.newKieContainer(id);
    final 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"));
    final IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    System.out.println(results.getAddedMessages());
    assertEquals(0, results.getAddedMessages().size());
    final 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 18 with IncrementalResults

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

the class IncrementalCompilationTest method testIncrementalCompilationAddErrorThenEmptyWithoutError.

@Test
public void testIncrementalCompilationAddErrorThenEmptyWithoutError() {
    // BZ-1009369
    // Invalid. Type "Smurf" is unknown
    final String drl1 = "Smurf";
    // Valid
    final String drl2 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    // Add file with error - expect 2 build messages
    final KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1);
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(DrlProject.class);
    assertEquals(2, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    // Add empty file - expect no "added" messages and no "removed" messages
    kfs.write("src/main/resources/r2.drl", "");
    final IncrementalResults addResults1 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(0, addResults1.getAddedMessages().size());
    assertEquals(0, addResults1.getRemovedMessages().size());
    // Update file with no errors - expect no "added" messages and no "removed" messages
    kfs.write("src/main/resources/r2.drl", drl2);
    final IncrementalResults addResults2 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(0, addResults2.getAddedMessages().size());
    assertEquals(0, addResults2.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)

Example 19 with IncrementalResults

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

the class IncrementalCompilationTest method testIncrementalCompilationWithErrorFromEmptyProject.

@Test
public void testIncrementalCompilationWithErrorFromEmptyProject() {
    // DROOLS-5584
    final String drl_KO = "package org.drools.test;\n" + "global java.util.List list;\n" + "rule \"test1\" when Strinf() then end\n";
    final String drl_OK = "package org.drools.test;\n" + "global java.util.List list;\n" + "rule \"test1\" when String() then end\n";
    final KieServices ks = KieServices.Factory.get();
    ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(id);
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(DrlProject.class);
    assertEquals(0, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    kfs.write("src/main/resources/r1.drl", drl_KO);
    final IncrementalResults addResults1 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r1.drl").build();
    assertEquals(1, addResults1.getAddedMessages().size());
    assertEquals(0, addResults1.getRemovedMessages().size());
    kfs.write("src/main/resources/r1.drl", drl_OK);
    final IncrementalResults addResults2 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r1.drl").build();
    assertEquals(0, addResults2.getAddedMessages().size());
    assertEquals(1, addResults2.getRemovedMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Test(org.junit.Test)

Example 20 with IncrementalResults

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

the class IncrementalCompilationTest method testIncrementalCompilationAddErrorThenRemoveIt.

@Test
public void testIncrementalCompilationAddErrorThenRemoveIt() {
    // Fact Type is unknown ("NonExistentClass" not "Message")
    final String drl1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R1 when\n" + "   $m : NonExistentClass()\n" + "then\n" + "end\n";
    // Field is unknown ("nonExistentField" not "message")
    final String drl2_1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_1 when\n" + "   $m : Message( nonExistentField == \"Hello World\" )\n" + "then\n" + "end\n";
    // Valid
    final String drl2_2 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1);
    // Initial file contains errors
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(DrlProject.class);
    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);
    final 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);
    final 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

IncrementalResults (org.kie.internal.builder.IncrementalResults)44 Test (org.junit.Test)42 KieServices (org.kie.api.KieServices)42 KieFileSystem (org.kie.api.builder.KieFileSystem)42 KieBuilder (org.kie.api.builder.KieBuilder)39 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)39 KieContainer (org.kie.api.runtime.KieContainer)14 ReleaseId (org.kie.api.builder.ReleaseId)13 KieSession (org.kie.api.runtime.KieSession)10 Results (org.kie.api.builder.Results)8 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)8 Message (org.kie.api.builder.Message)5 ArrayList (java.util.ArrayList)4 StringReader (java.io.StringReader)3 Message (org.drools.testcoverage.common.model.Message)3 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)3 KieBuilderSetImpl (org.drools.compiler.kie.builder.impl.KieBuilderSetImpl)2 KieModuleModel (org.kie.api.builder.model.KieModuleModel)2 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)2 InputStream (java.io.InputStream)1