Search in sources :

Example 96 with KieBuilder

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

the class KieRepositoryScannerTest method createKieJarWithJavaClass.

private InternalKieModule createKieJarWithJavaClass(KieServices ks, ReleaseId releaseId, String kbaseName, int factor) throws IOException {
    String drl = "package org.kie.test.drl\n" + "import org.kie.test.Bean\n" + "global java.util.List list\n" + "rule R1\n" + "when\n" + "   $b : Bean( value > 0 )\n" + "then\n" + "   list.add( $b.getValue() );\n" + "end\n";
    KieFileSystem kfs = createKieFileSystemWithKProject(ks, false, kbaseName, "KSession1");
    kfs.writePomXML(getPom(releaseId));
    kfs.write("src/main/resources/KBase1/rule1.drl", drl).write("src/main/java/org/kie/test/Bean.java", createJavaSource(factor));
    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 97 with KieBuilder

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

the class KieRepositoryScannerTest method testMissingDependency.

@Test
public void testMissingDependency() throws Exception {
    KieServices ks = KieServices.Factory.get();
    MavenRepository repository = getMavenRepository();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "scanner-test", "1.0");
    ReleaseId missingDep = ks.newReleaseId("org.kie", "missing-dep", "1.0");
    KieFileSystem kfs = createKieFileSystemWithKProject(ks, false);
    kfs.writePomXML(getPom(releaseId, missingDep));
    kfs.write("src/main/resources/KBase1/rule1.drl", createDRLWithTypeDeclaration(1, 1));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    List<Message> messages = kieBuilder.buildAll().getResults().getMessages();
    assertEquals(1, messages.size());
    assertTrue(messages.get(0).toString().contains("missing-dep"));
    ks.getRepository().removeKieModule(releaseId);
}
Also used : MavenRepository.getMavenRepository(org.appformer.maven.integration.MavenRepository.getMavenRepository) KieMavenRepository.getKieMavenRepository(org.kie.scanner.KieMavenRepository.getKieMavenRepository) MavenRepository(org.appformer.maven.integration.MavenRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 98 with KieBuilder

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

the class KieRepositoryScannerTest method createKieJarWithType.

private InternalKieModule createKieJarWithType(KieServices ks, ReleaseId releaseId) throws IOException {
    KieFileSystem kfs = createKieFileSystemWithKProject(ks);
    kfs.write("src/main/resources/KBase1/r1.drl", "package org.kie.test\n" + getDRLWithType());
    kfs.writePomXML(getPom(releaseId));
    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 99 with KieBuilder

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

the class KieScannerIncrementalCompilationTest method createKieJarWithClass.

private InternalKieModule createKieJarWithClass(KieServices ks, ReleaseId releaseId, int value, boolean useJavaInDrl) throws IOException {
    KieFileSystem kfs = createKieFileSystemWithKProject(ks, false);
    kfs.writePomXML(getPom(releaseId));
    if (useJavaInDrl) {
        kfs.write("src/main/resources/KBase1/rule1.drl", createDRLForJavaSource());
    } else {
        kfs.write("src/main/resources/KBase1/rule1.drl", createDRLNotUsingJava(value));
    }
    kfs.write("src/main/java/org/kie/test/Value.java", createJavaSource(value));
    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 100 with KieBuilder

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

the class KieModuleDeploymentHelperImpl method internalCreateKieJar.

/**
 * Create a KJar for deployment;
 *
 * @param releaseId Release (deployment) id.
 * @param resourceFilePaths List of resource file paths
 * @param kbaseName The name of the {@link KieBase}
 * @param ksessionName The name of the {@link KieSession}.
 * @param dependencies List of dependencies to add
 *
 * @return The {@link InternalKieModule} which represents the KJar.
 */
private synchronized KieModule internalCreateKieJar(ReleaseId releaseId, String kbaseName, String ksessionName, List<String> resourceFilePaths, List<Class<?>> classes, List<String> dependencies) {
    ReleaseId[] releaseIds = {};
    if (dependencies != null && dependencies.size() > 0) {
        List<ReleaseId> depReleaseIds = new ArrayList<ReleaseId>();
        for (String dep : dependencies) {
            String[] gav = dep.split(":");
            if (gav.length != 3) {
                throw new IllegalArgumentException("Dependendency id '" + dep + "' does not conform to the format <groupId>:<artifactId>:<version> (Classifiers are not accepted).");
            }
            depReleaseIds.add(new ReleaseIdImpl(gav[0], gav[1], gav[2]));
        }
        releaseIds = depReleaseIds.toArray(new ReleaseId[depReleaseIds.size()]);
    }
    config.pomText = getPomText(releaseId, releaseIds);
    KieFileSystem kfs = createKieFileSystemWithKProject(kbaseName, ksessionName);
    kfs.writePomXML(this.config.pomText);
    List<KJarResource> resourceFiles = loadResources(resourceFilePaths);
    for (KJarResource resource : resourceFiles) {
        kfs.write("src/main/resources/" + kbaseName + "/" + resource.name, resource.content);
    }
    if (classes != null) {
        for (Class<?> userClass : classes) {
            addClass(userClass, kfs);
        }
    }
    KieBuilder kieBuilder = config.getKieServicesInstance().newKieBuilder(kfs);
    int buildMsgs = 0;
    for (Message buildMsg : kieBuilder.buildAll().getResults().getMessages()) {
        System.out.println(buildMsg.getPath() + " : " + buildMsg.getText());
        ++buildMsgs;
    }
    if (buildMsgs > 0) {
        throw new RuntimeException("Unable to build KieModule, see the " + buildMsgs + " messages above.");
    }
    return (InternalKieModule) kieBuilder.getKieModule();
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.kie.api.builder.Message) ArrayList(java.util.ArrayList) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule)

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