Search in sources :

Example 61 with Results

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());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) 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 62 with Results

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")));
}
Also used : IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 63 with Results

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());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) 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) KieContainer(org.kie.api.runtime.KieContainer) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 64 with Results

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")));
}
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 65 with Results

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();
    }
}
Also used : IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Results (org.kie.api.builder.Results)92 Test (org.junit.Test)81 KieServices (org.kie.api.KieServices)72 KieFileSystem (org.kie.api.builder.KieFileSystem)60 KieContainer (org.kie.api.runtime.KieContainer)36 KieBuilder (org.kie.api.builder.KieBuilder)32 KieSession (org.kie.api.runtime.KieSession)25 IncrementalResults (org.kie.internal.builder.IncrementalResults)20 ReleaseId (org.kie.api.builder.ReleaseId)19 QueryResults (org.kie.api.runtime.rule.QueryResults)17 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)15 KieBase (org.kie.api.KieBase)14 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)13 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)11 KieHelper (org.kie.internal.utils.KieHelper)11 KieModuleModel (org.kie.api.builder.model.KieModuleModel)10 FactType (org.kie.api.definition.type.FactType)10 ArrayList (java.util.ArrayList)9 FlatQueryResults (org.drools.core.runtime.rule.impl.FlatQueryResults)9 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)7