Search in sources :

Example 91 with ReleaseId

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

the class KieBuilderTest method testNoProjectXml.

@Test
public void testNoProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0-SNAPSHOT");
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML(kfs, releaseId);
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 92 with ReleaseId

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

the class KieBuilderTest method testInvalidProjectXml.

@Test
public void testInvalidProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    KieModuleModel kProj = createKieProject(namespace);
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0-SNAPSHOT");
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML(kfs, releaseId);
    kfs.writeKModuleXML("xxxx");
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    KieBuilder kieBuilder = createKieBuilder(kfs);
    kieBuilder.buildAll();
    assertTrue(kieBuilder.getResults().hasMessages(Level.ERROR));
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 93 with ReleaseId

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

the class KieBuilderTest method testNoPomAndProjectXml.

@Test
public void testNoPomAndProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    ReleaseId releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 94 with ReleaseId

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

the class KieBuilderTest method testSetPomModelReuse.

@Test
public void testSetPomModelReuse() throws IOException {
    String namespace = "org.kie.test";
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "pomModelReuse", "1.0-SNAPSHOT");
    String pom = KieBuilderImpl.generatePomXml(releaseId);
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    kfs.writePomXML(pom);
    // Create a KieBuilder instance
    KieBuilder kieBuilder1 = createKieBuilder(kfs);
    kieBuilder1.buildAll();
    // Get PomModel to re-use in second KieBuilder instance
    PomModel pomModel = ((KieBuilderImpl) kieBuilder1).getPomModel();
    kfs.writePomXML(pom);
    // Create another KieBuilder instance with the same KieFileSystem, setting PomModel
    KieBuilder kieBuilder2 = createKieBuilder(kfs);
    ((KieBuilderImpl) kieBuilder2).setPomModel(pomModel);
    kieBuilder2.buildAll();
    // Read pom.xml from first KieBuilder's KieModule
    InternalKieModule kieModule1 = (InternalKieModule) ((KieBuilderImpl) kieBuilder1).getKieModuleIgnoringErrors();
    final Reader reader1 = kieModule1.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
    int charCode;
    String readPom1 = "";
    while ((charCode = reader1.read()) != -1) {
        readPom1 = readPom1 + (char) charCode;
    }
    reader1.close();
    assertEquals(pom, readPom1);
    // Read pom.xml from second KieBuilder's KieModule
    InternalKieModule kieModule2 = (InternalKieModule) ((KieBuilderImpl) kieBuilder2).getKieModuleIgnoringErrors();
    final Reader reader2 = kieModule2.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
    String readPom2 = "";
    while ((charCode = reader2.read()) != -1) {
        readPom2 = readPom2 + (char) charCode;
    }
    reader1.close();
    assertEquals(pom, readPom2);
}
Also used : KieBuilderImpl(org.drools.compiler.kie.builder.impl.KieBuilderImpl) KieFileSystem(org.kie.api.builder.KieFileSystem) PomModel(org.appformer.maven.support.PomModel) Reader(java.io.Reader) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) Test(org.junit.Test)

Example 95 with ReleaseId

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

the class ClassLoaderTest method testClassLoaderFromPojo.

@Test
public void testClassLoaderFromPojo() throws IOException {
    // DROOLS-1108
    final String source = "package org.drools.testdrl;\n" + "public class MyPojo {\n" + "    public String getUrlPath() {" + "        return getClass().getClassLoader().getResource(\"META-INF/foo.xml\").getPath();\n" + "    }\n" + "}\n";
    final String drl1 = "package org.drools.testdrl;\n" + "import org.drools.testdrl.MyPojo;\n" + "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + "   insert(new MyPojo());\n" + "end\n" + "rule R2 when\n" + "    $m : MyPojo()\n" + "then\n" + "   list.add($m.getUrlPath());\n" + "end\n";
    final String foo = "<xyz/>\n";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-cl", "1.0.0");
    final KieFileSystem kfs = ks.newKieFileSystem();
    final KieModuleModel kproj = ks.newKieModuleModel();
    final KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("testKbase").setDefault(true).addPackage("org.drools.testdrl");
    final KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("testKsession").setDefault(true);
    kfs.writeKModuleXML(kproj.toXML());
    kfs.write("src/main/resources/org/drools/testdrl/rules1.drl", drl1);
    kfs.write("src/main/java/org/drools/testdrl/MyPojo.java", source);
    kfs.write("src/main/resources/META-INF/foo.xml", foo);
    kfs.generateAndWritePomXML(releaseId);
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
    final KieContainer kieContainer = ks.newKieContainer(releaseId);
    final ClassLoader classLoader = kieContainer.getClassLoader();
    final URL url = classLoader.getResource("META-INF/foo.xml");
    assertNotNull(url);
    final KieSession ksession = kieContainer.newKieSession("testKsession");
    final List<URL> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(url.getPath(), list.get(0));
    ksession.dispose();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) URL(java.net.URL) KieSessionModel(org.kie.api.builder.model.KieSessionModel) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

ReleaseId (org.kie.api.builder.ReleaseId)273 Test (org.junit.Test)219 KieServices (org.kie.api.KieServices)214 KieContainer (org.kie.api.runtime.KieContainer)176 KieSession (org.kie.api.runtime.KieSession)137 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)117 KieModule (org.kie.api.builder.KieModule)84 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)73 KieFileSystem (org.kie.api.builder.KieFileSystem)64 KieModuleModel (org.kie.api.builder.model.KieModuleModel)40 KieBuilder (org.kie.api.builder.KieBuilder)34 ArrayList (java.util.ArrayList)32 Message (org.drools.compiler.Message)32 KieMavenRepository.getKieMavenRepository (org.kie.scanner.KieMavenRepository.getKieMavenRepository)27 Resource (org.kie.api.io.Resource)23 Results (org.kie.api.builder.Results)20 KieBaseModel (org.kie.api.builder.model.KieBaseModel)20 InternalKieScanner (org.drools.compiler.kie.builder.impl.InternalKieScanner)18 KieScanner (org.kie.api.builder.KieScanner)18 IncrementalResults (org.kie.internal.builder.IncrementalResults)18