Search in sources :

Example 11 with KieFileSystem

use of org.kie.api.builder.KieFileSystem 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)

Example 12 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class IncrementalCompilationTest method testRemoveAndReaddJavaClass.

@Test
public void testRemoveAndReaddJavaClass() {
    // DROOLS-1704
    String javaSource = "package org.drools.test;\n" + "public class Person { }\n";
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    ReleaseId releaseId3 = ks.newReleaseId("org.kie", "test-upgrade", "1.2.0");
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(releaseId1);
    KieModuleModel kModuleModel = ks.newKieModuleModel();
    kfs.writeKModuleXML(kModuleModel.toXML());
    kfs.write("src/main/java/org/drools/test/Person.java", javaSource);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kContainer = ks.newKieContainer(releaseId1);
    try {
        Class.forName("org.drools.test.Person", true, kContainer.getClassLoader());
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    kContainer.getKieBase();
    kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(releaseId2);
    kModuleModel = ks.newKieModuleModel();
    kfs.writeKModuleXML(kModuleModel.toXML());
    ks.newKieBuilder(kfs).buildAll();
    kContainer.updateToVersion(releaseId2);
    kContainer.getKieBase();
    kfs = ks.newKieFileSystem();
    kfs.generateAndWritePomXML(releaseId3);
    kModuleModel = ks.newKieModuleModel();
    kfs.writeKModuleXML(kModuleModel.toXML());
    kfs.write("src/main/java/org/drools/test/Person.java", javaSource);
    ks.newKieBuilder(kfs).buildAll();
    kContainer.updateToVersion(releaseId3);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 13 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class IncrementalCompilationTest method testIncrementalCompilationWithDuplicatedRuleInSameDRL.

@Test
public void testIncrementalCompilationWithDuplicatedRuleInSameDRL() throws Exception {
    String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n" + "rule R1 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);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertFalse(kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).isEmpty());
}
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) Test(org.junit.Test)

Example 14 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class RuleUnitTest method testRuleUnitFromKieContainer.

@Test
public void testRuleUnitFromKieContainer() {
    String drl = "package org.drools.compiler.integrationtests\n" + "unit " + getCanonicalSimpleName(AdultUnitWithSingleItem.class) + "\n" + "import " + Person.class.getCanonicalName() + "\n" + "rule Adult when\n" + "    $p : /person[age >= adultAge]\n" + "then\n" + "    System.out.println($p.getName() + \" is adult\");\n" + "end";
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "    <kbase name=\"unit\" equalsBehavior=\"equality\" eventProcessingMode=\"stream\">\n" + "        <ksession name=\"unit-rules\" default=\"true\" type=\"stateful\" clockType=\"pseudo\"/>\n" + "    </kbase>\n" + "</kmodule>";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl).writeKModuleXML(kmodule);
    ks.newKieBuilder(kfs).buildAll();
    KieContainer kcontainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    RuleUnitExecutor executor = kcontainer.newRuleUnitExecutor();
    assertTrue(executor.getKieSession().getSessionClock() instanceof SessionPseudoClock);
    RuleUnit adultUnit = new AdultUnitWithSingleItem(new Person("Mario", 42));
    assertEquals(1, executor.run(adultUnit));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieServices(org.kie.api.KieServices) RuleUnit(org.kie.api.runtime.rule.RuleUnit) Person(org.drools.compiler.Person) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 15 with KieFileSystem

use of org.kie.api.builder.KieFileSystem in project drools by kiegroup.

the class RuleUnitTest method testNotExistingDataSource.

@Test
public void testNotExistingDataSource() throws Exception {
    String drl1 = "import " + Person.class.getCanonicalName() + "\n" + "import " + AdultUnit.class.getCanonicalName() + "\n" + "rule Adult @Unit( AdultUnit.class ) when\n" + "    Person(age >= 18, $name : name) from adults\n" + "then\n" + "    System.out.println($name + \" is adult\");\n" + "end";
    KieServices ks = KieServices.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1);
    Results results = ks.newKieBuilder(kfs).buildAll().getResults();
    assertFalse(results.getMessages().isEmpty());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) Test(org.junit.Test)

Aggregations

KieFileSystem (org.kie.api.builder.KieFileSystem)297 KieServices (org.kie.api.KieServices)241 Test (org.junit.Test)211 KieBuilder (org.kie.api.builder.KieBuilder)179 KieSession (org.kie.api.runtime.KieSession)101 KieModuleModel (org.kie.api.builder.model.KieModuleModel)79 KieContainer (org.kie.api.runtime.KieContainer)74 ReleaseId (org.kie.api.builder.ReleaseId)64 Results (org.kie.api.builder.Results)60 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)53 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)39 KieBase (org.kie.api.KieBase)38 IncrementalResults (org.kie.internal.builder.IncrementalResults)36 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)32 KieBaseModel (org.kie.api.builder.model.KieBaseModel)31 ArrayList (java.util.ArrayList)30 Message (org.kie.api.builder.Message)26 KieModule (org.kie.api.builder.KieModule)22 Message (org.drools.compiler.Message)20 Resource (org.kie.api.io.Resource)18