Search in sources :

Example 31 with InternalKieBuilder

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

Example 32 with InternalKieBuilder

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

the class KieHelper method createJar.

public static byte[] createJar(KieServices ks, ReleaseId releaseId, Resource... resources) {
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId);
    for (int i = 0; i < resources.length; i++) {
        if (resources[i] != null) {
            kfs.write(resources[i]);
        }
    }
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    ((InternalKieBuilder) kieBuilder).buildAll(o -> true);
    Results results = kieBuilder.getResults();
    if (results.hasMessages(Message.Level.ERROR)) {
        throw new IllegalStateException(results.getMessages(Message.Level.ERROR).toString());
    }
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    return jar;
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) Results(org.kie.api.builder.Results) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 33 with InternalKieBuilder

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

the class KieModuleIncrementalCompilationTest method testIncrementalCompilationFirstBuildHasErrors.

@Test
public void testIncrementalCompilationFirstBuildHasErrors() throws Exception {
    KieServices ks = KieServices.Factory.get();
    // Malformed POM - No Version information
    ReleaseId releaseId = ks.newReleaseId("org.kie", "incremental-test-with-invalid pom", "");
    KieFileSystem kfs = createKieFileSystemWithKProject(ks);
    kfs.writePomXML(getPom(releaseId));
    // Valid
    String drl1 = "rule R1 when\n" + "   $s : String()\n" + "then\n" + "end\n";
    // Invalid
    String drl2 = "rule R2 when\n" + "   $s : Strin( )\n" + "then\n" + "end\n";
    // Write Rule 1 - No DRL errors, but POM is in error
    kfs.write("src/main/resources/KBase1/r1.drl", drl1);
    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/KBase1/r2.drl", drl2);
    IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/KBase1/r2.drl").build();
    assertEquals(1, addResults.getAddedMessages().size());
    assertEquals(0, addResults.getRemovedMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Test(org.junit.Test)

Example 34 with InternalKieBuilder

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

the class KieModuleIncrementalCompilationTest method checkIncrementalCompilationWithFunctionThenRule.

@Test
public void checkIncrementalCompilationWithFunctionThenRule() throws Exception {
    String rule = "package org.kie.scanner\n" + "rule R1 when\n" + "   String()\n" + "then\n" + "   System.out.println(MyFunction());\n" + "end\n";
    String function = "package org.kie.scanner\n" + "function int MyFunction() {\n" + "   return 1;\n" + "}\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/org/kie/scanner/function.drl", function);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    kfs.write("src/main/resources/org/kie/scanner/rule.drl", rule);
    IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/org/kie/scanner/rule.drl").build();
    assertEquals(0, addResults.getAddedMessages().size());
    assertEquals(0, addResults.getRemovedMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) 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