Search in sources :

Example 26 with InternalKieBuilder

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

the class IncrementalCompilationTest method testIncrementalCompilationWithIncludes.

@Test
public void testIncrementalCompilationWithIncludes() throws Exception {
    // DROOLS-462
    String drl1 = "global java.util.List list\n" + "rule R1 when\n" + " $s : String() " + "then\n" + " list.add( \"a\" + $s );" + "end\n";
    String drl2 = "global java.util.List list\n" + "rule R1 when\n" + " $s : String() " + "then\n" + " list.add( \"b\" + $s );" + "end\n";
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId("org.test", "test", "1.0.0-SNAPSHOT");
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kproj = ks.newKieModuleModel();
    KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("KBase1").addPackage("org.pkg1");
    kieBaseModel1.newKieSessionModel("KSession1");
    KieBaseModel kieBaseModel2 = kproj.newKieBaseModel("KBase2").addPackage("org.pkg2").addInclude("KBase1");
    kieBaseModel2.newKieSessionModel("KSession2");
    KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).writeKModuleXML(kproj.toXML());
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    kieBuilder.buildAll();
    assertEquals(0, kieBuilder.getResults().getMessages().size());
    KieContainer kc = ks.newKieContainer(releaseId);
    KieSession ksession = kc.newKieSession("KSession2");
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert("Foo");
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("aFoo", list.get(0));
    list.clear();
    kfs.delete("src/main/resources/KBase1/org/pkg1/r1.drl");
    kfs.write("src/main/resources/KBase1/org/pkg1/r2.drl", drl2);
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    assertEquals(0, results.getAddedMessages().size());
    Results updateResults = kc.updateToVersion(releaseId);
    assertEquals(0, updateResults.getMessages().size());
    ksession.insert("Bar");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("bBar", "bFoo")));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IncrementalResults(org.kie.internal.builder.IncrementalResults) KieBaseModel(org.kie.api.builder.model.KieBaseModel) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) KieModuleModel(org.kie.api.builder.model.KieModuleModel) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 27 with InternalKieBuilder

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

the class IncrementalCompilationTest method testIncrementalCompilationRemovingParentRule.

@Test
public void testIncrementalCompilationRemovingParentRule() throws Exception {
    // DROOLS-1031
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $s : String()\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.compiler\n" + "rule R2 extends R1 when\n" + "   $i : Integer()\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);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    KieSession ksession = kieContainer.newKieSession();
    ksession.insert("test");
    ksession.insert(1);
    assertEquals(2, ksession.fireAllRules());
    kfs.delete("src/main/resources/r1.drl");
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r1.drl", "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("test");
    ksession.insert(1);
    assertEquals(2, ksession.fireAllRules());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) 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 28 with InternalKieBuilder

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

the class IncrementalCompilationTest method testIncrementalCompilationWithClassFieldReader.

@Test
public void testIncrementalCompilationWithClassFieldReader() {
    // BZ-1318532
    String personSrc = "package org.test;" + "import java.util.ArrayList;" + "import java.util.List;" + "public class Person {" + "    private String name;" + "    private List<String> addresses = new ArrayList<String>();" + "    public Person() {}" + "    public Person(final String name) { this.name = name; }" + "    public List<String> getAddresses() { return addresses; }" + "    public void setAddresses(List<String> addresses) { this.addresses = addresses; }" + "    public void addAddress(String address) { this.addresses.add(address); }" + "    public String getName() { return this.name; }" + "    public void setName(final String name) { this.name = name; }" + "}";
    String drl1 = "package org.drools.compiler\n" + "" + "import org.test.Person;\n" + "" + "rule R0 salience 100 when\n" + " String()" + "then\n" + " Person person = new Person(\"John\");" + " person.addAddress(\"A street\");" + " insert(person);" + "end\n" + "" + "rule R1 when\n" + " $p : Person($addresses : addresses) " + " $a : Address() from $addresses " + "then\n" + "end\n" + "";
    // just a trivial update
    String drl2 = drl1 + " ";
    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/Person.java", ks.getResources().newReaderResource(new StringReader(personSrc)));
    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();
    kfs.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath(// update the drl1.drl file
    "drl1.drl"));
    IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
    // don't call updateToVersion(). This test intends to prove that incrementalBuild doesn't do harm to existing ksession.
    ksession.insert("start");
    int fired = ksession.fireAllRules();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) 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 29 with InternalKieBuilder

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

the class IncrementalCompilationTest method testIncrementalCompilationAddErrorBuildAllMessages.

@Test
public void testIncrementalCompilationAddErrorBuildAllMessages() throws Exception {
    // Valid
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\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";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, 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());
    // Check errors on a full build
    assertEquals(1, ks.newKieBuilder(kfs).buildAll().getResults().getMessages().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 30 with InternalKieBuilder

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

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