Search in sources :

Example 86 with KieBuilder

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

the class JavaBeansEventRoleTest method testBeanAndDeclarativeTypeInSamePackage.

@Test
public void testBeanAndDeclarativeTypeInSamePackage() throws Exception {
    String declaration = "package org.drools.compiler;\n" + "public class Bean {}";
    String drl = "package org.drools.compiler;\n" + "declare Bean\n" + "  @role(event)\n" + "end";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/java/org/drools/compiler/Bean.java", declaration).write("src/main/resources/bean1.drl", drl);
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    final KieModule kieModule = kieBuilder.buildAll().getKieModule();
    final KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
    final String packageName = "org.drools.compiler";
    final String className = "Bean";
    final Class clazz = kieModuleMetaData.getClass(packageName, className);
    final TypeMetaInfo typeMetaInfo = kieModuleMetaData.getTypeMetaInfo(clazz);
    assertTrue(typeMetaInfo.isEvent());
}
Also used : TypeMetaInfo(org.drools.core.rule.TypeMetaInfo) KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) KieModuleMetaData(org.kie.scanner.KieModuleMetaData) Test(org.junit.Test)

Example 87 with KieBuilder

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

the class AbstractKieCiTest method createKieJarWithDependencies.

protected InternalKieModule createKieJarWithDependencies(KieServices ks, ReleaseId releaseId, boolean isdefault, String rule, Dependency... dependencies) {
    KieFileSystem kfs = createKieFileSystemWithKProject(ks, isdefault);
    kfs.writePomXML(pomWithMvnDeps(dependencyWithScope(releaseId.getGroupId(), releaseId.getArtifactId(), releaseId.getVersion(), ""), dependencies));
    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 88 with KieBuilder

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

the class KieModuleBuilderTest method testKieModuleUsingPOMMissingKBaseDefinition.

@Test
public void testKieModuleUsingPOMMissingKBaseDefinition() throws Exception {
    KieServices ks = KieServices.Factory.get();
    // Build a KieModule jar, deploy it into local Maven repository
    ReleaseId releaseId = ks.newReleaseId("org.kie", "metadata-test2", "1.0-SNAPSHOT");
    String pomText = getPom(releaseId);
    File pomFile = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile);
        fos.write(pomText.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writePomXML(getPom(releaseId));
    kfs.write("src/main/java/org/kie/test/Bean.java", createJavaSourceInPackage());
    KieBuilder kieBuilder1 = ks.newKieBuilder(kfs);
    Assert.assertTrue(kieBuilder1.buildAll().getResults().getMessages().isEmpty());
    InternalKieModule kieModule = (InternalKieModule) kieBuilder1.getKieModule();
    KieMavenRepository.getKieMavenRepository().installArtifact(releaseId, kieModule, pomFile);
    // Build a second KieModule, depends on the first KieModule jar which we have deployed into Maven
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "metadata-test-using-pom", "1.0-SNAPSHOT");
    String pomText2 = getPom(releaseId2, releaseId);
    File pomFile2 = new File(System.getProperty("java.io.tmpdir"), MavenRepository.toFileName(releaseId2, null) + ".pom");
    try {
        FileOutputStream fos = new FileOutputStream(pomFile2);
        fos.write(pomText2.getBytes());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    // Try building the second KieModule
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    InputStream is = new FileInputStream(pomFile2);
    KieModuleModel kproj2 = ks.newKieModuleModel();
    kieFileSystem.write("pom.xml", KieServices.Factory.get().getResources().newInputStreamResource(is));
    kieFileSystem.writeKModuleXML(kproj2.toXML());
    kieFileSystem.write("src/main/resources/rule.drl", createDRLWithImport("rule1"));
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();
    assertTrue(kieBuilder.getResults().getMessages().isEmpty());
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) KieModuleModel(org.kie.api.builder.model.KieModuleModel) FileOutputStream(java.io.FileOutputStream) KieBuilder(org.kie.api.builder.KieBuilder) File(java.io.File) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 89 with KieBuilder

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

the class KieModuleIncrementalCompilationTest method testCheckMetaDataAfterIncrementalDelete.

@Test
public void testCheckMetaDataAfterIncrementalDelete() throws Exception {
    String drl1 = "package org.kie.scanner\n" + "rule R1 when\n" + "   String()\n" + "then\n" + "end\n";
    String drl2 = "package org.kie.scanner\n" + "rule R2_2 when\n" + "   String( )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl1).write("src/main/resources/r2.drl", drl2);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(2, getRuleNames(kieBuilder).get("org.kie.scanner").size());
    kfs.delete("src/main/resources/r2.drl");
    IncrementalResults addResults = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(1, getRuleNames(kieBuilder).get("org.kie.scanner").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 90 with KieBuilder

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

the class KieModuleIncrementalCompilationTest method checkIncrementalCompilationWithRuleThenFunction.

@Test
public void checkIncrementalCompilationWithRuleThenFunction() throws Exception {
    String rule = "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);
    KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(1, 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(1, addResults1.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)

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