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")));
}
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());
}
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();
}
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());
}
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());
}
Aggregations