Search in sources :

Example 21 with KieServices

use of org.kie.api.KieServices 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());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) 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 22 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class IncrementalCompilationTest method testSegmentMergeOnRuleRemovalWithNotExistingSegment.

@Test
public void testSegmentMergeOnRuleRemovalWithNotExistingSegment() throws Exception {
    // DROOLS-950
    String drl1 = "rule R1 when\n" + "    $i : Integer()\n" + "    $s : String( length == $i )\n" + "then\n" + "end\n" + "rule R2 when\n" + "    $i : Integer()\n" + "    $l : Long( this > $i )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.1");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    kc.updateToVersion(releaseId1);
    ksession.insert("Test");
    ksession.insert(4L);
    ksession.fireAllRules();
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.2");
    km = createAndDeployJar(ks, releaseId2);
    kc.updateToVersion(releaseId2);
}
Also used : 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) Test(org.junit.Test)

Example 23 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class IncrementalCompilationTest method testIncrementalCompilationChangeingParentRule.

@Test
public void testIncrementalCompilationChangeingParentRule() throws Exception {
    // DROOLS-1031
    String drl1_1 = "rule R1 when\n" + "   $s : String( this == \"s1\" )\n" + "then\n" + "end\n";
    String drl1_2 = "rule R1 when\n" + "   $s : String( this == \"s2\" )\n" + "then\n" + "end\n";
    String drl2 = "rule R2 extends R1 when\n" + "   $i : Integer()\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-extends", "1.1.1");
    KieModule km = createAndDeployJar(ks, releaseId1, drl1_1 + drl2);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieSession ksession = kc.newKieSession();
    ksession.insert(1);
    ksession.insert("s2");
    assertEquals(0, ksession.fireAllRules());
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-extends", "1.1.2");
    km = createAndDeployJar(ks, releaseId2, drl1_2 + drl2);
    kc.updateToVersion(releaseId2);
    assertEquals(2, ksession.fireAllRules());
}
Also used : 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) Test(org.junit.Test)

Example 24 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class IncrementalCompilationTest method testGlobalRemovedAndAdded.

@Test
public void testGlobalRemovedAndAdded() throws Exception {
    // RHDM-311
    String drlAWithGlobal = "package org.x.a\nglobal Boolean globalBool\n";
    String drlANoGlobal = "package org.x.a\n";
    String drlBWithGlobal = "package org.x.b\nglobal Boolean globalBool\n";
    String drlBNoGlobal = "package org.x.b\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, drlAWithGlobal, drlBNoGlobal);
    KieContainer kc = ks.newKieContainer(releaseId1);
    KieSession ksession = kc.newKieSession();
    ksession.setGlobal("globalBool", Boolean.FALSE);
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    createAndDeployJar(ks, releaseId2, drlANoGlobal, drlBWithGlobal);
    kc.updateToVersion(releaseId2);
    ksession.setGlobal("globalBool", Boolean.TRUE);
}
Also used : 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) Test(org.junit.Test)

Example 25 with KieServices

use of org.kie.api.KieServices in project drools by kiegroup.

the class IncrementalCompilationTest method testIncrementalCompilationAddErrorThenEmptyWithoutError.

@Test
public void testIncrementalCompilationAddErrorThenEmptyWithoutError() throws Exception {
    // BZ-1009369
    // Invalid. Type "Smurf" is unknown
    String drl1 = "Smurf";
    // Valid
    String drl2 = "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();
    // Add file with error - expect 2 build messages
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(2, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    // Add empty file - expect no "added" messages and no "removed" messages
    kfs.write("src/main/resources/r2.drl", "");
    IncrementalResults addResults1 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(0, addResults1.getAddedMessages().size());
    assertEquals(0, addResults1.getRemovedMessages().size());
    // Update file with no errors - expect no "added" messages and no "removed" messages
    kfs.write("src/main/resources/r2.drl", drl2);
    IncrementalResults addResults2 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(0, addResults2.getAddedMessages().size());
    assertEquals(0, addResults2.getRemovedMessages().size());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) 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)

Aggregations

KieServices (org.kie.api.KieServices)479 Test (org.junit.Test)385 KieContainer (org.kie.api.runtime.KieContainer)249 KieFileSystem (org.kie.api.builder.KieFileSystem)242 KieSession (org.kie.api.runtime.KieSession)239 ReleaseId (org.kie.api.builder.ReleaseId)214 KieBuilder (org.kie.api.builder.KieBuilder)146 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)119 KieModule (org.kie.api.builder.KieModule)104 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)99 KieModuleModel (org.kie.api.builder.model.KieModuleModel)78 ArrayList (java.util.ArrayList)76 Results (org.kie.api.builder.Results)73 KieBase (org.kie.api.KieBase)59 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)49 KieBaseModel (org.kie.api.builder.model.KieBaseModel)45 IncrementalResults (org.kie.internal.builder.IncrementalResults)44 Message (org.drools.compiler.Message)43 Resource (org.kie.api.io.Resource)42 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)28