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