use of org.kie.api.builder.Results in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithInvalidDRL.
@Test
public void testIncrementalCompilationWithInvalidDRL() throws Exception {
String drl1 = "Smurf";
String drl2_1 = "package org.drools.compiler\n" + "rule R2\n" + "when\n" + " $m : NonExistentClass()\n" + "then\n" + "end\n";
String drl2_2 = "package org.drools.compiler\n" + "rule R2\n" + "when\n" + " $m : Message()\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
// First file contains errors
kfs.write("src/main/resources/r1.drl", drl1);
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
Results results1 = kieBuilder.getResults();
assertEquals(2, results1.getMessages().size());
// Second file also contains errors.. expect some added messages
kfs.write("src/main/resources/r2.drl", drl2_1);
IncrementalResults results2 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
assertEquals(1, results2.getAddedMessages().size());
assertEquals(0, results2.getRemovedMessages().size());
// Correct second file... expect original errors relating to the file to be removed
kfs.write("src/main/resources/r2.drl", drl2_2);
IncrementalResults results3 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
assertEquals(0, results3.getAddedMessages().size());
assertEquals(1, results3.getRemovedMessages().size());
// Remove first file... expect related errors to be removed
kfs.delete("src/main/resources/r1.drl");
IncrementalResults results4 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r1.drl").build();
assertEquals(0, results4.getAddedMessages().size());
assertEquals(2, results4.getRemovedMessages().size());
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class IncrementalCompilationTest method testRemovePackageFromKieBaseModel.
@Test
public void testRemovePackageFromKieBaseModel() throws Exception {
// DROOLS-1287
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-remove-pkg", "1.0");
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-remove-pkg", "1.1");
createKJarWIthPackages(ks, releaseId1, "pkg1", "pkg2");
KieContainer container = ks.newKieContainer(releaseId1);
KieSession ksession = container.newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("test");
ksession.fireAllRules();
assertEquals(2, list.size());
assertTrue(list.containsAll(asList("R1", "R2")));
createKJarWIthPackages(ks, releaseId2, "pkg2");
Results results = container.updateToVersion(releaseId2);
assertEquals(0, results.getMessages().size());
ksession = container.newKieSession();
list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.insert("test");
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.containsAll(asList("R2")));
}
use of org.kie.api.builder.Results in project drools by kiegroup.
the class IncrementalCompilationTest method testUpdateWithPojoExtensionDifferentPackages.
@Test
public void testUpdateWithPojoExtensionDifferentPackages() throws Exception {
// DROOLS-1491
String drlDeclare = "package org.drools.compiler.integrationtests\n" + "declare DroolsApplications extends " + BaseClass.class.getCanonicalName() + "\n" + " droolsAppName: String\n" + "end";
String drlRule = "package org.drools.compiler.test\n" + "rule R1 when\n" + " $fact : org.drools.compiler.integrationtests.DroolsApplications( droolsAppName == \"appName\" )\n" + "then\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
kfs.generateAndWritePomXML(releaseId1);
kfs.write(ks.getResources().newReaderResource(new StringReader(drlDeclare)).setResourceType(ResourceType.DRL).setSourcePath("drlDeclare.drl"));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
kieBuilder.buildAll();
KieContainer kc = ks.newKieContainer(releaseId1);
KieSession ksession = kc.newKieSession();
FactType factType = kc.getKieBase().getFactType("org.drools.compiler.integrationtests", "DroolsApplications");
Object fact = factType.newInstance();
factType.set(fact, "droolsAppName", "appName");
ksession.insert(fact);
assertEquals(0, ksession.fireAllRules());
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
KieFileSystem kfs2 = ks.newKieFileSystem();
kfs2.generateAndWritePomXML(releaseId2);
kfs2.write(ks.getResources().newReaderResource(new StringReader(drlDeclare)).setResourceType(ResourceType.DRL).setSourcePath("drlDeclare.drl"));
kfs2.write(ks.getResources().newReaderResource(new StringReader(drlRule)).setResourceType(ResourceType.DRL).setSourcePath("drlRule.drl"));
KieBuilder kieBuilder2 = ks.newKieBuilder(kfs2);
kieBuilder2.buildAll();
Results updateResults = kc.updateToVersion(releaseId2);
assertEquals(0, updateResults.getMessages().size());
assertEquals(1, ksession.fireAllRules());
}
use of org.kie.api.builder.Results 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.api.builder.Results in project drools by kiegroup.
the class IncrementalCompilationTest method testIncrementalCompilationWithFireUntilHalt.
@Test
@Ignore("this test takes too long and cannot be emulated with a pseudo clock")
public void testIncrementalCompilationWithFireUntilHalt() throws Exception {
// DROOLS-782
final String drl1 = getCronRule(3) + getCronRule(6);
final String drl2 = getCronRule(8) + getCronRule(10) + getCronRule(5);
final KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-fireUntilHalt", "1.0.0");
// KieModule km = createAndDeployJar( ks, releaseId1, testRuleAdd1, testRuleAdd2 );
KieModule km = createAndDeployJar(ks, releaseId1, drl1);
// Create a session and fire rules
final KieContainer kc = ks.newKieContainer(km.getReleaseId());
final KieSession kieSession = kc.newKieSession();
try {
new Thread(kieSession::fireUntilHalt).start();
Thread.sleep(10000);
// Create a new jar for version 1.1.0
final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-fireUntilHalt", "1.1.0");
km = createAndDeployJar(ks, releaseId2, drl2);
// try to update the container to version 1.1.0
final Results results = kc.updateToVersion(releaseId2);
assertFalse("Errors detected on updateToVersion: " + results.getMessages(org.kie.api.builder.Message.Level.ERROR), results.hasMessages(org.kie.api.builder.Message.Level.ERROR));
Thread.sleep(10000);
} finally {
kieSession.halt();
}
}
Aggregations