use of org.kie.internal.builder.InternalKieBuilder in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithAmbiguousRedeclares.
@Test
public void testIncrementalCompilationWithAmbiguousRedeclares() {
String drl1 = "package domestic; " + "import foreign.*; " + "declare foreign.Score " + " id : String " + "end ";
String drl2 = "\n" + "package domestic; " + "import foreign.*; " + "declare foreign.Score " + " id : String " + "end\n" + "declare Score " + " value : double " + "end " + "";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kfs.generateAndWritePomXML(id);
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();
ksession.fireAllRules();
kfs.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drl2.drl"));
IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
System.out.println(results.getAddedMessages());
assertEquals(0, results.getAddedMessages().size());
Results updateResults = kc.updateToVersion(id);
assertEquals(0, updateResults.getMessages().size());
}
use of org.kie.internal.builder.InternalKieBuilder in project drools by kiegroup.
the class IncrementalCompilationTest method testIncremenatalCompilationAddingFieldToDeclaredType.
@Test
public void testIncremenatalCompilationAddingFieldToDeclaredType() {
// DROOLS-2197
String declares1 = "declare Address\n" + " streetName : String\n" + " city : String\n" + "end";
String declares2 = "declare Address\n" + " streetName : String\n" + " city : String\n" + " flg: String\n" + "end";
String rules1 = "rule R when\n" + " a : Address( city == \"Antibes\" )\n" + "then\n" + " a.setStreetName(\"Av. Jean Medecin\");\n" + "end";
String rules2 = "rule R when\n" + " a : Address( city == \" Paris \" )\n" + "then\n" + " a.setStreetName(\" Champs Elisees \");\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
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();
KieContainer kc = ks.newKieContainer(id);
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"));
IncrementalResults results = ((InternalKieBuilder) kieBuilder).incrementalBuild();
System.out.println(results.getAddedMessages());
assertEquals(0, results.getAddedMessages().size());
Results updateResults = kc.updateToVersion(id);
assertEquals(0, updateResults.getMessages().size());
}
use of org.kie.internal.builder.InternalKieBuilder in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithAddedError.
@Test
public void testIncrementalCompilationWithAddedError() throws Exception {
String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
String drl2_1 = "package org.drools.compiler\n" + "rule R2_1 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
String drl2_2 = "package org.drools.compiler\n" + "rule R2_2 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).write("src/main/resources/r2.drl", drl2_1);
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
KieSession ksession = kieContainer.newKieSession();
ksession.insert(new Message("Hello World"));
assertEquals(1, ksession.fireAllRules());
kfs.write("src/main/resources/r2.drl", drl2_2);
IncrementalResults results = ((InternalKieBuilder) kieBuilder).createFileSet("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(new Message("Hello World"));
assertEquals(1, ksession.fireAllRules());
}
use of org.kie.internal.builder.InternalKieBuilder in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithModuleOverride.
@Test
public void testIncrementalCompilationWithModuleOverride() {
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 " + "";
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 " + "";
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 " + "";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId id = ks.newReleaseId("org.test", "foo", "1.0-SNAPSHOT");
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kfs.generateAndWritePomXML(id);
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();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("X");
ksession.fireAllRules();
assertTrue(list.contains("AX"));
KieFileSystem kfs2 = ks.newKieFileSystem();
KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
kfs2.generateAndWritePomXML(id);
kfs2.write(ks.getResources().newReaderResource(new StringReader(drl2)).setResourceType(ResourceType.DRL).setSourcePath("drla.drl"));
kieBuilder2.buildAll();
KieContainer kc2 = ks.newKieContainer(id);
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"));
IncrementalResults results = ((InternalKieBuilder) kieBuilder2).incrementalBuild();
assertEquals(0, results.getAddedMessages().size());
kc2.updateToVersion(id);
ksession2.fireAllRules();
assertEquals(Arrays.asList("AX", "BX", "CX"), list);
}
use of org.kie.internal.builder.InternalKieBuilder in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationAddErrorThenRemoveIt.
@Test
public void testIncrementalCompilationAddErrorThenRemoveIt() throws Exception {
// Fact Type is unknown ("NonExistentClass" not "Message")
String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : NonExistentClass()\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";
// Valid
String drl2_2 = "package org.drools.compiler\n" + "rule R2_2 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1);
// Initial file contains errors
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/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());
// 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);
IncrementalResults removeResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
assertEquals(0, removeResults.getAddedMessages().size());
assertEquals(1, removeResults.getRemovedMessages().size());
}
Aggregations