use of org.kie.api.builder.KieFileSystem 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.api.builder.KieFileSystem 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.api.builder.KieFileSystem 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.api.builder.KieFileSystem in project drools by kiegroup.
the class IncrementalCompilationTest method testJavaClassRedefinition.
@Test
public void testJavaClassRedefinition() {
// DROOLS-1402
String JAVA1 = "package org.test;" + " public class MyBean {\n" + " private String firstName;\n" + " public MyBean() { /* empty constructor */ }\n" + " public MyBean(String firstName) { this.firstName = firstName; }\n" + " public String getFirstName() { return firstName; }\n" + " public void setFirstName(String firstName) { this.firstName = firstName; }\n" + " }";
String DRL1 = "package org.test;\n" + "\n" + "//from row number: 1\n" + "rule \"Row 1 HelloRules\"\n" + " when\n" + " $b : MyBean( firstName == null )\n" + " then\n" + " System.out.println($b);" + "end";
String INIT_DRL = "package org.test; rule RINIT when eval(true) then insert(new MyBean()); end";
String INIT_DRL_2 = "package org.test; rule RINIT when eval(1==1) then insert(new MyBean()); end";
String JAVA2 = "package org.test;" + " public class MyBean {\n" + " private String firstName;\n" + " private String lastName;\n" + " public MyBean() { /* empty constructor */ }\n" + " public MyBean(String firstName) { this.firstName = firstName; }\n" + " public MyBean(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; }\n" + " public String getFirstName() { return firstName; }\n" + " public void setFirstName(String firstName) { this.firstName = firstName; }\n" + " public String getLastName() { return lastName; }\n" + " public void setLastName(String lastName) { this.lastName = lastName; }\n" + " }";
String DRL2 = "package org.test;\n" + "\n" + "//from row number: 1\n" + "rule \"Row 1 HelloRules\"\n" + " when\n" + " $b : MyBean( firstName == null , lastName == null )\n" + " then\n" + " System.out.println($b);" + "end";
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/MyBean.java", ks.getResources().newReaderResource(new StringReader(JAVA1)));
kfs.write(ks.getResources().newReaderResource(new StringReader(DRL1)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
kfs.write(ks.getResources().newReaderResource(new StringReader(INIT_DRL)).setResourceType(ResourceType.DRL).setSourcePath("INIT_DRL.drl"));
kieBuilder.buildAll();
KieContainer kc = ks.newKieContainer(id);
KieSession ksession = kc.newKieSession();
int fired = ksession.fireAllRules();
assertEquals(2, fired);
ReleaseId id2 = ks.newReleaseId("org.test", "myTest", "2.0.0");
KieFileSystem kfs2 = ks.newKieFileSystem();
KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
kfs2.generateAndWritePomXML(id2);
kfs2.write("src/main/java/org/test/MyBean.java", ks.getResources().newReaderResource(new StringReader(JAVA2)));
kfs2.write(ks.getResources().newReaderResource(new StringReader(DRL2)).setResourceType(ResourceType.DRL).setSourcePath("rules.drl"));
kfs2.write(ks.getResources().newReaderResource(new StringReader(INIT_DRL_2)).setResourceType(ResourceType.DRL).setSourcePath("INIT_DRL.drl"));
kieBuilder2.buildAll();
Results updateResults = kc.updateToVersion(id2);
assertFalse(updateResults.hasMessages(Level.ERROR));
fired = ksession.fireAllRules();
assertEquals(2, fired);
}
use of org.kie.api.builder.KieFileSystem 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