Search in sources :

Example 66 with KieModule

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

the class KieBuilderTest method testJavaSourceFileAndDrlDeployWithClassFilter.

@Test
public void testJavaSourceFileAndDrlDeployWithClassFilter() {
    final String allowedJava = "package org.drools.compiler;\n" + "public class JavaSourceMessage { }\n";
    final String filteredJava = "package org.drools.compiler;\n" + "public class ClassCausingClassNotFoundException { non.existing.Type foo() { return null; } }\n";
    final String drl = "package org.drools.compiler;\n" + "import org.drools.compiler.JavaSourceMessage;" + "rule R1 when\n" + "   $m : JavaSourceMessage()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource allowedJavaResource = ResourceFactory.newByteArrayResource(allowedJava.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/JavaSourceMessage.java");
    final Resource filteredJavaResource = ResourceFactory.newByteArrayResource(filteredJava.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/ClassCausingClassNotFoundException.java");
    final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final Predicate<String> filter = fileName -> !fileName.endsWith("org/drools/compiler/ClassCausingClassNotFoundException.java");
    KieModule km = null;
    try {
        km = createAndDeployJar(ks, kmodule, filter, releaseId1, allowedJavaResource, filteredJavaResource, drlResource);
    } catch (final IllegalStateException ise) {
        if (ise.getMessage().contains("org/drools/compiler/ClassCausingClassNotFoundException.java")) {
            fail("Build failed because source file was not filtered out.");
        } else {
            throw ise;
        }
    }
    final KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
    try {
        final Class<?> messageClass = kieContainer.getClassLoader().loadClass("org.drools.compiler.JavaSourceMessage");
        assertNotNull(messageClass);
    } catch (final ClassNotFoundException e) {
        throw new IllegalStateException("Loading the java class failed.", e);
    }
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) ResourceFactory(org.kie.internal.io.ResourceFactory) ResourceType(org.kie.api.io.ResourceType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Message(org.drools.compiler.Message) KieServices(org.kie.api.KieServices) Assertions(org.assertj.core.api.Assertions) Assert.fail(org.junit.Assert.fail) KieBase(org.kie.api.KieBase) Channel(org.kie.api.runtime.Channel) KieSession(org.kie.api.runtime.KieSession) KieFileSystem(org.kie.api.builder.KieFileSystem) KieBuilder(org.kie.api.builder.KieBuilder) Predicate(java.util.function.Predicate) Assert.assertNotNull(org.junit.Assert.assertNotNull) CommonTestMethodBase(org.drools.compiler.CommonTestMethodBase) Results(org.kie.api.builder.Results) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Resource(org.kie.api.io.Resource) List(java.util.List) KieModule(org.kie.api.builder.KieModule) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) KieModuleModel(org.kie.api.builder.model.KieModuleModel) Assert.assertEquals(org.junit.Assert.assertEquals) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 67 with KieModule

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

the class KieBuilderTest method testValidXsdTargetNamespace.

@Test
public void testValidXsdTargetNamespace() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
    ks.newKieContainer(km.getReleaseId());
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 68 with KieModule

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

the class KieBuilderTest method testOldXsdTargetNamespace.

@Test
public void testOldXsdTargetNamespace() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
    ks.newKieContainer(km.getReleaseId());
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) Test(org.junit.Test)

Example 69 with KieModule

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

the class KieBuilderTest method testJavaSourceFileAndDrlDeploy.

@Test
public void testJavaSourceFileAndDrlDeploy() {
    final String java = "package org.drools.compiler;\n" + "public class JavaSourceMessage { }\n";
    final String drl = "package org.drools.compiler;\n" + "import org.drools.compiler.JavaSourceMessage;" + "rule R1 when\n" + "   $m : JavaSourceMessage()\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\">\n" + "    <ksession name=\"ksession1\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource javaResource = ResourceFactory.newByteArrayResource(java.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/JavaSourceMessage.java");
    final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, javaResource, drlResource);
    final KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
    try {
        final Class<?> messageClass = kieContainer.getClassLoader().loadClass("org.drools.compiler.JavaSourceMessage");
        assertNotNull(messageClass);
    } catch (final ClassNotFoundException e) {
        throw new IllegalStateException("Loading the java class failed.", e);
    }
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 70 with KieModule

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

the class KieBuilderTest method testResourceInclusion.

@Test
public void testResourceInclusion() {
    final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    final String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    final String drl4 = "package org.drools.compiler\n" + "rule R4 when\n" + "   $m : Message( message == \"Hello Earth\" )\n" + "then\n" + "end\n";
    final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + "         xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"kbase1\" default=\"true\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\" scope=\"javax.enterprise.context.ApplicationScoped\">\n" + "    <ksession name=\"ksession1\" type=\"stateful\" default=\"true\" clockType=\"realtime\" scope=\"javax.enterprise.context.ApplicationScoped\"/>\n" + "  </kbase>\n" + "</kmodule>";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
    final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
    final Resource r2 = ResourceFactory.newByteArrayResource(drl2.getBytes()).setResourceType(ResourceType.GDRL).setSourcePath("kbase1/drl2.gdrl");
    final Resource r3 = ResourceFactory.newByteArrayResource(drl3.getBytes()).setResourceType(ResourceType.RDRL).setSourcePath("kbase1/drl3.rdrl");
    final Resource r4 = ResourceFactory.newByteArrayResource(drl4.getBytes()).setResourceType(ResourceType.TDRL).setSourcePath("kbase1/drl4.tdrl");
    final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1, r2, r3, r4);
    final InternalKieModule ikm = (InternalKieModule) km;
    assertNotNull(ikm.getResource(r1.getSourcePath()));
    assertNotNull(ikm.getResource(r2.getSourcePath()));
    assertNotNull(ikm.getResource(r3.getSourcePath()));
    assertNotNull(ikm.getResource(r4.getSourcePath()));
    // Create a session and fire rules
    final KieContainer kc = ks.newKieContainer(km.getReleaseId());
    final KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(2, ksession.fireAllRules());
    ksession.dispose();
}
Also used : Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

KieModule (org.kie.api.builder.KieModule)124 KieServices (org.kie.api.KieServices)104 Test (org.junit.Test)94 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)84 ReleaseId (org.kie.api.builder.ReleaseId)84 KieContainer (org.kie.api.runtime.KieContainer)75 KieSession (org.kie.api.runtime.KieSession)64 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)53 Message (org.drools.compiler.Message)25 KieFileSystem (org.kie.api.builder.KieFileSystem)23 Resource (org.kie.api.io.Resource)20 ArrayList (java.util.ArrayList)15 KieModuleModel (org.kie.api.builder.model.KieModuleModel)15 KieBuilder (org.kie.api.builder.KieBuilder)13 KieRepository (org.kie.api.builder.KieRepository)10 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)7 KieBaseModel (org.kie.api.builder.model.KieBaseModel)7 KieBase (org.kie.api.KieBase)6 List (java.util.List)5 Results (org.kie.api.builder.Results)5