Search in sources :

Example 86 with Resource

use of org.kie.api.io.Resource 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)

Example 87 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieCompilationCacheTest method testCompilationCache.

@Test
public void testCompilationCache() throws Exception {
    String drl = "package org.drools.compiler\n" + "declare type X\n" + "    foo : String\n" + "end\n" + "rule R1 when\n" + "   $m : X( foo == \"Hello World\" )\n" + "then\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    ks.newKieBuilder(kfs).buildAll();
    ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
    InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
    byte[] jar = kieModule.getBytes();
    MemoryFileSystem mfs = MemoryFileSystem.readFromJar(jar);
    File file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase1"));
    assertNotNull(file);
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    KieContainer kc = ks.newKieContainer(km.getReleaseId());
    KieBase kbase = kc.getKieBase();
    FactType type = kbase.getFactType("org.drools.compiler", "X");
    FactField foo = type.getField("foo");
    Object x = type.newInstance();
    foo.set(x, "Hello World");
    KieSession ksession = kc.newKieSession();
    ksession.insert(x);
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : FactField(org.kie.api.definition.type.FactField) KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) FactType(org.kie.api.definition.type.FactType) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) File(org.drools.compiler.compiler.io.File) 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)

Example 88 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieContainerTest method testClassLoaderGetResources.

@Test
public void testClassLoaderGetResources() throws IOException {
    KieServices kieServices = KieServices.Factory.get();
    String drl1 = "package org.drools.testdrl;\n" + "rule R1 when\n" + "   $m : Object()\n" + "then\n" + "end\n";
    Resource resource1 = kieServices.getResources().newReaderResource(new StringReader(drl1), "UTF-8");
    resource1.setTargetPath("org/drools/testdrl/rules1.drl");
    String drl2 = "package org.drools.testdrl;\n" + "rule R2 when\n" + "   $m : Object()\n" + "then\n" + "end\n";
    Resource resource2 = kieServices.getResources().newReaderResource(new StringReader(drl2), "UTF-8");
    resource2.setTargetPath("org/drools/testdrl/rules2.drl");
    String java3 = "package org.drools.testjava;\n" + "public class Message {}";
    Resource resource3 = kieServices.getResources().newReaderResource(new StringReader(java3), "UTF-8");
    resource3.setTargetPath("org/drools/testjava/Message.java");
    resource3.setResourceType(ResourceType.JAVA);
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"testKbase\" packages=\"org.drools.testdrl\">\n" + "    <ksession name=\"testKsession\"/>\n" + "  </kbase>\n" + "</kmodule>";
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource1, resource2, resource3);
    KieContainer kieContainer = kieServices.newKieContainer(releaseId);
    ClassLoader classLoader = kieContainer.getClassLoader();
    // no trailing "/"
    assertEnumerationSize(1, classLoader.getResources("org/drools/testjava"));
    // trailing "/" to test both variants
    assertEnumerationSize(1, classLoader.getResources("org/drools/testdrl/"));
    // make sure the package resource correctly lists all its child resources (files in this case)
    URL url = classLoader.getResources("org/drools/testdrl").nextElement();
    List<String> lines = IOUtils.readLines(url.openStream());
    Assertions.assertThat(lines).contains("rules1.drl", "rules1.drl.properties", "rules2.drl", "rules2.drl.properties");
    assertUrlEnumerationContainsMatch("^mfs\\:/$", classLoader.getResources(""));
}
Also used : Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) URL(java.net.URL) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 89 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieContainerTest method testGetDefaultKieSessionModel.

@Test
public void testGetDefaultKieSessionModel() {
    KieServices kieServices = KieServices.Factory.get();
    String drl = "package org.drools.test\n" + "rule R1 when\n" + "   $m : Object()\n" + "then\n" + "end\n";
    Resource resource = kieServices.getResources().newReaderResource(new StringReader(drl), "UTF-8");
    resource.setTargetPath("org/drools/test/rules.drl");
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"testKbase\" packages=\"org.drools.test\">\n" + "    <ksession name=\"testKsession\" default=\"true\"/>\n" + "  </kbase>\n" + "</kmodule>";
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-testGetDefaultKieSessionModel", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource);
    KieContainer kieContainer = kieServices.newKieContainer(releaseId);
    KieSessionModel sessionModel = kieContainer.getKieSessionModel(null);
    assertNotNull(sessionModel);
    assertEquals("testKsession", sessionModel.getName());
}
Also used : Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) KieSessionModel(org.kie.api.builder.model.KieSessionModel) Test(org.junit.Test)

Example 90 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieContainerTest method testMemoryFileSystemFolderUniqueness.

@Test
public void testMemoryFileSystemFolderUniqueness() {
    KieServices kieServices = KieServices.Factory.get();
    String drl = "package org.drools.test\n" + "rule R1 when\n" + "   $m : Object()\n" + "then\n" + "end\n";
    Resource resource = kieServices.getResources().newReaderResource(new StringReader(drl), "UTF-8");
    resource.setTargetPath("org/drools/test/rules.drl");
    String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"testKbase\" packages=\"org.drools.test\">\n" + "    <ksession name=\"testKsession\"/>\n" + "  </kbase>\n" + "</kmodule>";
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource);
    KieContainer kieContainer = kieServices.newKieContainer(releaseId);
    KieModule kieModule = ((InternalKieContainer) kieContainer).getMainKieModule();
    MemoryFileSystem memoryFileSystem = ((MemoryKieModule) kieModule).getMemoryFileSystem();
    Folder rootFolder = memoryFileSystem.getFolder("");
    Object[] members = rootFolder.getMembers().toArray();
    assertEquals(2, members.length);
    Folder firstFolder = (Folder) members[0];
    Folder secondFolder = (Folder) members[1];
    assertEquals(firstFolder.getParent(), secondFolder.getParent());
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) Folder(org.drools.compiler.compiler.io.Folder) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) StringReader(java.io.StringReader) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) InternalKieContainer(org.drools.core.impl.InternalKieContainer) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Aggregations

Resource (org.kie.api.io.Resource)162 Test (org.junit.Test)100 KieSession (org.kie.api.runtime.KieSession)50 KieServices (org.kie.api.KieServices)43 KieBase (org.kie.api.KieBase)42 ArrayList (java.util.ArrayList)30 KieContainer (org.kie.api.runtime.KieContainer)27 StringReader (java.io.StringReader)23 ReleaseId (org.kie.api.builder.ReleaseId)23 KieBuilder (org.kie.api.builder.KieBuilder)20 KieModule (org.kie.api.builder.KieModule)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 ClassPathResource (org.drools.core.io.impl.ClassPathResource)18 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)17 FactType (org.kie.api.definition.type.FactType)17 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)14 IOException (java.io.IOException)13 PMMLResource (org.drools.compiler.compiler.PMMLResource)11