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);
}
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());
}
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);
}
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());
}
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);
}
Aggregations