Search in sources :

Example 71 with KieModule

use of org.kie.api.builder.KieModule 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 72 with KieModule

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

the class KieContainerTest method testMainKieModule.

@Test
public void testMainKieModule() {
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId = ks.newReleaseId("org.kie", "test-delete", "1.0.0");
    createAndDeployJar(ks, releaseId, createDRL("ruleA"));
    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieModule kmodule = ((InternalKieContainer) kieContainer).getMainKieModule();
    assertEquals(releaseId, kmodule.getReleaseId());
}
Also used : KieServices(org.kie.api.KieServices) InternalKieContainer(org.drools.core.impl.InternalKieContainer) ReleaseId(org.kie.api.builder.ReleaseId) 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)

Example 73 with KieModule

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

the class KieContainerTest method testSharedTypeDeclarationsUsingClassLoader.

@Test
public void testSharedTypeDeclarationsUsingClassLoader() throws Exception {
    String type = "package org.drools.test\n" + "declare Message\n" + "   message : String\n" + "end\n";
    String drl1 = "package org.drools.test\n" + "rule R1 when\n" + "   $o : Object()\n" + "then\n" + "   if ($o.getClass().getName().equals(\"org.drools.test.Message\") && $o.getClass() != new Message(\"Test\").getClass()) {\n" + "       throw new RuntimeException();\n" + "   }\n" + "end\n";
    String drl2 = "package org.drools.test\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "   if ($m.getClass() != new Message(\"Test\").getClass()) {\n" + "       throw new RuntimeException();\n" + "   }\n" + "end\n";
    KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-delete", "1.0.0");
    KieModule km = createAndDeployJar(ks, releaseId1, type, drl1, drl2);
    KieContainer kieContainer = ks.newKieContainer(releaseId1);
    KieContainer kieContainer2 = ks.newKieContainer(releaseId1);
    KieSession ksession = kieContainer.newKieSession();
    KieSession ksession2 = kieContainer2.newKieSession();
    Class cls1 = kieContainer.getClassLoader().loadClass("org.drools.test.Message");
    Constructor constructor = cls1.getConstructor(String.class);
    ksession.insert(constructor.newInstance("Hello World"));
    assertEquals(2, ksession.fireAllRules());
    Class cls2 = kieContainer2.getClassLoader().loadClass("org.drools.test.Message");
    Constructor constructor2 = cls2.getConstructor(String.class);
    ksession2.insert(constructor2.newInstance("Hello World"));
    assertEquals(2, ksession2.fireAllRules());
    assertNotSame(cls1, cls2);
}
Also used : Constructor(java.lang.reflect.Constructor) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) 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)

Example 74 with KieModule

use of org.kie.api.builder.KieModule 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)

Example 75 with KieModule

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

the class KieHelloWorldTest method testHelloWorldWithSpace.

@Test
public void testHelloWorldWithSpace() throws Exception {
    // DROOLS-2338
    final KieServices kieServices = KieServices.get();
    final Path dir = Paths.get("/tmp/t tt");
    Files.createDirectories(dir);
    final String text = "rule \"Hello world rule\"\n" + "when\n" + "then\n" + "    System.out.println(\"Hello world\");" + "end\n";
    final Path filePath = dir.resolve("one.drl");
    Files.write(filePath, text.getBytes());
    final KieFileSystem fs = kieServices.newKieFileSystem();
    fs.write(ResourceFactory.newUrlResource("file:/tmp/t%20tt/one.drl"));
    KieBuilder kieBuilder = kieServices.newKieBuilder(fs);
    kieBuilder.buildAll();
    KieModule kieModule = kieBuilder.getKieModule();
    KieSession ksession = kieServices.newKieContainer(kieModule.getReleaseId()).newKieSession();
    ksession.insert(new Object());
    int count = ksession.fireAllRules();
    assertEquals(1, count);
}
Also used : Path(java.nio.file.Path) KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) KieModule(org.kie.api.builder.KieModule) 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