Search in sources :

Example 91 with KieBuilder

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

the class KieModuleIncrementalCompilationTest method checkIncrementalCompilationWithMultipleKieBases.

@Test
public void checkIncrementalCompilationWithMultipleKieBases() throws Exception {
    String rule = "package org.kie.scanner\n" + "rule R1 when\n" + "then\n" + "end\n";
    String invalidRule = "package org.kie.scanner\n" + "rule R2 when\n" + // missing import
    "   Cheese()\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = createKieFileSystemWithTwoKBases(ks);
    kfs.write("src/main/resources/org/kie/scanner/rule.drl", rule);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, kieBuilder.getResults().getMessages().size());
    kfs.write("src/main/resources/org/kie/scanner/invalidRule.drl", invalidRule);
    IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/org/kie/scanner/invalidRule.drl").build();
    assertEquals(2, addResults.getAddedMessages().size());
    addResults.getAddedMessages().stream().map(m -> (MessageImpl) m).forEach(m -> assertNotNull(m.getKieBaseName()));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBuilder(org.kie.api.builder.KieBuilder) EventProcessingOption(org.kie.api.conf.EventProcessingOption) MessageImpl(org.drools.compiler.kie.builder.impl.MessageImpl) Collection(java.util.Collection) EqualityBehaviorOption(org.kie.api.conf.EqualityBehaviorOption) HashMap(java.util.HashMap) Test(org.junit.Test) IncrementalResults(org.kie.internal.builder.IncrementalResults) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) Assert(org.junit.Assert) KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) MessageImpl(org.drools.compiler.kie.builder.impl.MessageImpl) IncrementalResults(org.kie.internal.builder.IncrementalResults) Test(org.junit.Test)

Example 92 with KieBuilder

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

the class KieModuleIncrementalCompilationTest method checkIncrementalCompilationWithRuleFunctionRule.

@Test
public void checkIncrementalCompilationWithRuleFunctionRule() throws Exception {
    String rule_1 = "package org.kie.scanner\n" + "rule R1 when\n" + "   String()\n" + "then\n" + "end\n";
    String rule_2 = "package org.kie.scanner\n" + "rule R1 when\n" + "   String()\n" + "then\n" + "   System.out.println(MyFunction());\n" + "end\n";
    String function = "package org.kie.scanner\n" + "function int MyFunction() {\n" + "   return 1;\n" + "}\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/org/kie/scanner/rule.drl", rule_1);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, kieBuilder.getResults().getMessages(org.kie.api.builder.Message.Level.ERROR).size());
    kfs.write("src/main/resources/org/kie/scanner/function.drl", function);
    IncrementalResults addResults1 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/org/kie/scanner/function.drl").build();
    assertEquals(0, addResults1.getAddedMessages().size());
    assertEquals(0, addResults1.getRemovedMessages().size());
    kfs.write("src/main/resources/org/kie/scanner/rule.drl", rule_2);
    IncrementalResults addResults2 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/org/kie/scanner/rule.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) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Test(org.junit.Test)

Example 93 with KieBuilder

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

the class KieModuleMavenTest method createKieJarWithProperties.

protected InternalKieModule createKieJarWithProperties(KieServices ks, ReleaseId releaseId, boolean isdefault, String droolsVersion, ReleaseId[] dependencies, String... rules) throws IOException {
    KieFileSystem kfs = createKieFileSystemWithKProject(ks, isdefault);
    kfs.writePomXML(generatePomXmlWithProperties(releaseId, droolsVersion, dependencies));
    for (String rule : rules) {
        String file = "org/test/" + rule + ".drl";
        kfs.write("src/main/resources/KBase1/" + file, createDRL(rule));
    }
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
    return (InternalKieModule) kieBuilder.getKieModule();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

Example 94 with KieBuilder

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

the class KieModuleMetaDataTest method testGetPackageNames.

@Test
public void testGetPackageNames() {
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("src/main/resources/test.drl", "package org.test declare Bean end");
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    final List<Message> messages = kieBuilder.buildAll().getResults().getMessages();
    assertTrue(messages.isEmpty());
    final KieModule kieModule = kieBuilder.getKieModule();
    final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
    assertFalse(kieModuleMetaData.getPackages().isEmpty());
    assertTrue(kieModuleMetaData.getPackages().contains("org.test"));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 95 with KieBuilder

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

the class KieModuleMetaDataTest method testKieMavenPluginEmptyProject.

@Test
public void testKieMavenPluginEmptyProject() {
    // According to https://bugzilla.redhat.com/show_bug.cgi?id=1049674#c2 the below is the minimal POM required to use KieMavenPlugin.
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write("pom.xml", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<project xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\" xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + "  <modelVersion>4.0.0</modelVersion>" + "  <groupId>org.kie</groupId>" + "  <artifactId>plugin-test</artifactId>" + "  <version>1.0</version>" + "  <packaging>kjar</packaging>" + "  <build>" + "    <plugins>" + "      <plugin>" + "        <groupId>org.kie</groupId>" + "        <artifactId>kie-maven-plugin</artifactId>" + "        <version>the-test-does-not-need-proper-version-here</version>" + "        <extensions>true</extensions>" + "      </plugin>" + "    </plugins>" + "  </build>" + "</project>");
    kfs.write("/src/main/resources/META-INF/kmodule.xml", "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>");
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    final List<Message> messages = kieBuilder.buildAll().getResults().getMessages();
    assertTrue(messages.isEmpty());
    final KieModule kieModule = kieBuilder.getKieModule();
    final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
    boolean fail = false;
    for (final String packageName : kieModuleMetaData.getPackages()) {
        for (final String className : kieModuleMetaData.getClasses(packageName)) {
            try {
                kieModuleMetaData.getClass(packageName, className);
            } catch (Throwable e) {
                fail = true;
                System.out.println(e);
            }
        }
    }
    if (fail) {
        fail("See console for details.");
    }
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Aggregations

KieBuilder (org.kie.api.builder.KieBuilder)211 KieFileSystem (org.kie.api.builder.KieFileSystem)179 KieServices (org.kie.api.KieServices)145 Test (org.junit.Test)134 KieSession (org.kie.api.runtime.KieSession)59 KieModuleModel (org.kie.api.builder.model.KieModuleModel)57 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)54 KieContainer (org.kie.api.runtime.KieContainer)48 IncrementalResults (org.kie.internal.builder.IncrementalResults)35 ReleaseId (org.kie.api.builder.ReleaseId)34 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)33 Results (org.kie.api.builder.Results)32 Message (org.kie.api.builder.Message)30 KieBase (org.kie.api.KieBase)29 ArrayList (java.util.ArrayList)22 KieBaseModel (org.kie.api.builder.model.KieBaseModel)22 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)21 Resource (org.kie.api.io.Resource)19 FactType (org.kie.api.definition.type.FactType)16 KieModule (org.kie.api.builder.KieModule)12