Search in sources :

Example 21 with IncrementalResults

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

the class IncrementalCompilationTest method testIncrementalCompilationWithModuleOverride.

@Test
public void testIncrementalCompilationWithModuleOverride() {
    final 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 " + "";
    final 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 " + "";
    final 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 " + "";
    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();
    final List<String> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.insert("X");
    ksession.fireAllRules();
    assertTrue(list.contains("AX"));
    final KieFileSystem kfs2 = ks.newKieFileSystem();
    final KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
    kfs2.generateAndWritePomXML(id);
    kfs2.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drla.drl"));
    kieBuilder2.buildAll(DrlProject.class);
    final KieContainer kc2 = ks.newKieContainer(id);
    final 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"));
    final 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 22 with IncrementalResults

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

the class MessageImplTests method testMessageWithIncrementalBuild.

@Test
public void testMessageWithIncrementalBuild() throws Exception {
    // Some suitably duff DSL to generate errors
    String dsl1 = "bananna\n";
    // Some suitably valid DRL
    String drl1 = "import org.drools.compiler.Person;\n" + "rule R1\n" + "when\n" + "There is a Person\n" + "then\n" + "end\n";
    // Some suitably valid DRL
    String drl2 = "rule R2\n" + "when\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/dsl.dsl", dsl1).write("src/main/resources/drl.dslr", drl1);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    Results fullBuildResults = kieBuilder.getResults();
    assertEquals(3, fullBuildResults.getMessages().size());
    kfs.write("src/main/resources/r2.drl", drl2);
    IncrementalResults incrementalBuildResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(0, incrementalBuildResults.getAddedMessages().size());
    assertEquals(0, incrementalBuildResults.getRemovedMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) IncrementalResults(org.kie.internal.builder.IncrementalResults) 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 23 with IncrementalResults

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

the class MultipleModelTest method testIncrementalBuilding.

@Test
public void testIncrementalBuilding() throws Exception {
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId("org.test", "test", "1.0.0-SNAPSHOT");
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    KieBuilder kb = ks.newKieBuilder(kfs);
    kfs.generateAndWritePomXML(releaseId1);
    kfs.write(ResourceFactory.newClassPathResource(source1).setResourceType(ResourceType.PMML));
    kb.buildAll();
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession kSession = kc.newKieSession();
    setKbase(kSession.getKieBase());
    kSession.fireAllRules();
    kfs.write(ResourceFactory.newClassPathResource(source2).setResourceType(ResourceType.PMML));
    IncrementalResults results = ((InternalKieBuilder) kb).incrementalBuild();
    kc.updateToVersion(releaseId1);
    kSession.fireAllRules();
    assertEquals(1, kSession.getObjects(new ClassObjectFilter(ModelMarker.class)).size());
    assertEquals(9, kSession.getObjects(new ClassObjectFilter(kSession.getKieBase().getFactType(packageName, "Synapse").getFactClass())).size());
    kSession.dispose();
    checkGeneratedRules();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) ModelMarker(org.drools.pmml.pmml_4_2.ModelMarker) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieContainer(org.kie.api.runtime.KieContainer) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 24 with IncrementalResults

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

the class RuleTemplateModelIncrementalCompilationTests method testRuleTemplateIncrementalCompilationAddValidUpdateWithInvalid.

@Test
public void testRuleTemplateIncrementalCompilationAddValidUpdateWithInvalid() throws Exception {
    // DROOLS-360
    // Valid
    // package org.mortgages;
    // rule "t1_0"
    // when
    // Applicant( age == 22 )
    // then
    // end
    String drl1_1 = "<rule>\n" + "  <name>t1</name>\n" + "  <modelVersion>1.0</modelVersion>\n" + "  <attributes/>\n" + "  <metadataList/>\n" + "  <lhs>\n" + "    <fact>\n" + "      <constraintList>\n" + "        <constraints>\n" + "          <fieldConstraint>\n" + "            <value>$default</value>\n" + "            <operator>==</operator>\n" + "            <constraintValueType>7</constraintValueType>\n" + "            <expression>\n" + "              <parts/>\n" + "            </expression>\n" + "            <parameters/>\n" + "            <factType>Applicant</factType>\n" + "            <fieldName>age</fieldName>\n" + "            <fieldType>Integer</fieldType>\n" + "          </fieldConstraint>\n" + "        </constraints>\n" + "      </constraintList>\n" + "      <factType>Applicant</factType>\n" + "      <isNegated>false</isNegated>\n" + "      <window>\n" + "        <parameters/>\n" + "      </window>\n" + "    </fact>\n" + "  </lhs>\n" + "  <rhs/>\n" + "  <imports>\n" + "    <imports/>\n" + "  </imports>\n" + "  <packageName>org.drools.workbench.models.guided.template.backend</packageName>\n" + "  <isNegated>false</isNegated>\n" + "  <table>\n" + "    <entry>\n" + "      <string>__ID_KOL_NAME__</string>\n" + "      <list>\n" + "        <string>1</string>\n" + "      </list>\n" + "    </entry>\n" + "    <entry>\n" + "      <string>$default</string>\n" + "      <list>\n" + "        <string>22</string>\n" + "      </list>\n" + "    </entry>\n" + "  </table>\n" + "  <idCol>1</idCol>\n" + "  <rowsCount>1</rowsCount>\n" + "</rule>";
    // Smurf is unknown
    // package org.mortgages;
    // rule "t1_0"
    // when
    // Applicant( age == 22 )
    // Smurf()
    // then
    // end
    String drl1_2 = "<rule>\n" + "  <name>t1</name>\n" + "  <modelVersion>1.0</modelVersion>\n" + "  <attributes/>\n" + "  <metadataList/>\n" + "  <lhs>\n" + "    <fact>\n" + "      <constraintList>\n" + "        <constraints>\n" + "          <fieldConstraint>\n" + "            <value>$default</value>\n" + "            <operator>==</operator>\n" + "            <constraintValueType>7</constraintValueType>\n" + "            <expression>\n" + "              <parts/>\n" + "            </expression>\n" + "            <parameters/>\n" + "            <factType>Applicant</factType>\n" + "            <fieldName>age</fieldName>\n" + "            <fieldType>Integer</fieldType>\n" + "          </fieldConstraint>\n" + "        </constraints>\n" + "      </constraintList>\n" + "      <factType>Applicant</factType>\n" + "      <isNegated>false</isNegated>\n" + "      <window>\n" + "        <parameters/>\n" + "      </window>\n" + "    </fact>\n" + "    <freeForm>\n" + "      <text>Smurf()</text>\n" + "    </freeForm>\n" + "  </lhs>\n" + "  <rhs/>\n" + "  <imports>\n" + "    <imports/>\n" + "  </imports>\n" + "  <packageName>org.drools.workbench.models.guided.template.backend</packageName>\n" + "  <isNegated>false</isNegated>\n" + "  <table>\n" + "    <entry>\n" + "      <string>__ID_KOL_NAME__</string>\n" + "      <list>\n" + "        <string>1</string>\n" + "      </list>\n" + "    </entry>\n" + "    <entry>\n" + "      <string>$default</string>\n" + "      <list>\n" + "        <string>22</string>\n" + "      </list>\n" + "    </entry>\n" + "  </table>\n" + "  <idCol>1</idCol>\n" + "  <rowsCount>1</rowsCount>\n" + "</rule>";
    KieServices ks = KieServices.Factory.get();
    // Add valid definition
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.template", drl1_1);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    List<Message> messages = kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR);
    assertEquals(0, messages.size());
    // Update with invalid definition - expect 1 "added" error message
    kfs.write("src/main/resources/r1.template", drl1_2);
    IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r1.template").build();
    assertEquals(1, addResults.getAddedMessages().size());
    assertEquals(0, addResults.getRemovedMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) 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 25 with IncrementalResults

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

the class RuleTemplateModelIncrementalCompilationTests method testRuleTemplateIncrementalCompilationAddInvalidUpdateWithValid.

@Test
public void testRuleTemplateIncrementalCompilationAddInvalidUpdateWithValid() throws Exception {
    // Smurf is unknown
    // package org.mortgages;
    // rule "t1_0"
    // when
    // Applicant( age == 22 )
    // Smurf()
    // then
    // end
    String drl1_1 = "<rule>\n" + "  <name>t1</name>\n" + "  <modelVersion>1.0</modelVersion>\n" + "  <attributes/>\n" + "  <metadataList/>\n" + "  <lhs>\n" + "    <fact>\n" + "      <constraintList>\n" + "        <constraints>\n" + "          <fieldConstraint>\n" + "            <value>$default</value>\n" + "            <operator>==</operator>\n" + "            <constraintValueType>7</constraintValueType>\n" + "            <expression>\n" + "              <parts/>\n" + "            </expression>\n" + "            <parameters/>\n" + "            <factType>Applicant</factType>\n" + "            <fieldName>age</fieldName>\n" + "            <fieldType>Integer</fieldType>\n" + "          </fieldConstraint>\n" + "        </constraints>\n" + "      </constraintList>\n" + "      <factType>Applicant</factType>\n" + "      <isNegated>false</isNegated>\n" + "      <window>\n" + "        <parameters/>\n" + "      </window>\n" + "    </fact>\n" + "    <freeForm>\n" + "      <text>Smurf()</text>\n" + "    </freeForm>\n" + "  </lhs>\n" + "  <rhs/>\n" + "  <imports>\n" + "    <imports/>\n" + "  </imports>\n" + "  <packageName>org.drools.workbench.models.guided.template.backend</packageName>\n" + "  <isNegated>false</isNegated>\n" + "  <table>\n" + "    <entry>\n" + "      <string>__ID_KOL_NAME__</string>\n" + "      <list>\n" + "        <string>1</string>\n" + "      </list>\n" + "    </entry>\n" + "    <entry>\n" + "      <string>$default</string>\n" + "      <list>\n" + "        <string>22</string>\n" + "      </list>\n" + "    </entry>\n" + "  </table>\n" + "  <idCol>1</idCol>\n" + "  <rowsCount>1</rowsCount>\n" + "</rule>";
    // Valid
    // package org.mortgages;
    // rule "t1_0"
    // when
    // Applicant( age == 22 )
    // then
    // end
    String drl1_2 = "<rule>\n" + "  <name>t1</name>\n" + "  <modelVersion>1.0</modelVersion>\n" + "  <attributes/>\n" + "  <metadataList/>\n" + "  <lhs>\n" + "    <fact>\n" + "      <constraintList>\n" + "        <constraints>\n" + "          <fieldConstraint>\n" + "            <value>$default</value>\n" + "            <operator>==</operator>\n" + "            <constraintValueType>7</constraintValueType>\n" + "            <expression>\n" + "              <parts/>\n" + "            </expression>\n" + "            <parameters/>\n" + "            <factType>Applicant</factType>\n" + "            <fieldName>age</fieldName>\n" + "            <fieldType>Integer</fieldType>\n" + "          </fieldConstraint>\n" + "        </constraints>\n" + "      </constraintList>\n" + "      <factType>Applicant</factType>\n" + "      <isNegated>false</isNegated>\n" + "      <window>\n" + "        <parameters/>\n" + "      </window>\n" + "    </fact>\n" + "  </lhs>\n" + "  <rhs/>\n" + "  <imports>\n" + "    <imports/>\n" + "  </imports>\n" + "  <packageName>org.drools.workbench.models.guided.template.backend</packageName>\n" + "  <isNegated>false</isNegated>\n" + "  <table>\n" + "    <entry>\n" + "      <string>__ID_KOL_NAME__</string>\n" + "      <list>\n" + "        <string>1</string>\n" + "      </list>\n" + "    </entry>\n" + "    <entry>\n" + "      <string>$default</string>\n" + "      <list>\n" + "        <string>22</string>\n" + "      </list>\n" + "    </entry>\n" + "  </table>\n" + "  <idCol>1</idCol>\n" + "  <rowsCount>1</rowsCount>\n" + "</rule>";
    KieServices ks = KieServices.Factory.get();
    // Add invalid definition
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.template", drl1_1);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(1, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    // Update file with valid definition - expect 1 "removed" error message
    kfs.write("src/main/resources/r1.template", drl1_2);
    IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r1.template").build();
    assertEquals(0, addResults.getAddedMessages().size());
    assertEquals(1, addResults.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