use of org.drools.compiler.kie.builder.impl.MemoryKieModule in project drools by kiegroup.
the class MemoryKieModuleResourceProviderTest method testGetResourceForEmptyFolder.
@Test
public void testGetResourceForEmptyFolder() throws Exception {
MemoryFileSystem mfs = new MemoryFileSystem();
mfs.createFolder(new MemoryFolder(mfs, KiePath.of("src/main/java")));
MemoryKieModule mkm = new MemoryKieModule(releaseId, kieModuleModel, mfs);
ResourceProvider resourceProvider = mkm.createResourceProvider();
String folderContents = IOUtils.toString(resourceProvider.getResource("src/main/java").openStream());
Assertions.assertThat(folderContents).isEmpty();
}
use of org.drools.compiler.kie.builder.impl.MemoryKieModule in project drools by kiegroup.
the class MemoryKieModuleResourceProviderTest method testGetResourceTrailingSlashIgnored.
@Test
public void testGetResourceTrailingSlashIgnored() throws Exception {
MemoryFileSystem mfs = new MemoryFileSystem();
mfs.setFileContents(new MemoryFile(mfs, "my-file1", mfs.getFolder("src/main/resources")), new byte[] { 65, 66 });
MemoryKieModule mkm = new MemoryKieModule(releaseId, kieModuleModel, mfs);
ResourceProvider resourceProvider = mkm.createResourceProvider();
String noTrailingSlashContents = IOUtils.toString(resourceProvider.getResourceAsStream("src/main/resources/my-file1"));
// "AB" == new byte[] {65, 66}
Assertions.assertThat(noTrailingSlashContents).hasLineCount(1).contains("AB");
String withTrailingSlashContents = IOUtils.toString(resourceProvider.getResourceAsStream("src/main/resources/my-file1/"));
// "AB" == new byte[] {65, 66}
Assertions.assertThat(withTrailingSlashContents).hasLineCount(1).contains("AB");
}
use of org.drools.compiler.kie.builder.impl.MemoryKieModule in project drools by kiegroup.
the class AbstractKnowledgeTest method createKieModule.
public KieModuleModel createKieModule(String namespace, boolean createJar, String version) throws IOException, ClassNotFoundException, InterruptedException {
KieModuleModel kproj = new KieModuleModelImpl();
KieBaseModel kieBaseModel1 = kproj.newKieBaseModel(namespace + ".KBase1").setEqualsBehavior(EqualityBehaviorOption.EQUALITY).setEventProcessingMode(EventProcessingOption.STREAM).addPackage(namespace + ".test1").setDefault(true);
kieBaseModel1.newKieSessionModel(namespace + ".KSession1").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.REALTIME).setDefault(true);
kieBaseModel1.newKieSessionModel(namespace + ".KSession2").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.PSEUDO);
kieBaseModel1.newKieSessionModel(namespace + ".KSessionDefault").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.PSEUDO).setDefault(true);
KieBaseModel kieBaseModel2 = kproj.newKieBaseModel(namespace + ".KBase2").setEqualsBehavior(EqualityBehaviorOption.IDENTITY).addPackage(namespace + ".test2").setEventProcessingMode(EventProcessingOption.CLOUD);
kieBaseModel2.newKieSessionModel(namespace + ".KSession3").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.PSEUDO);
KieBaseModel kieBaseModel3 = kproj.newKieBaseModel(namespace + ".KBase3").addInclude(kieBaseModel1.getName()).addInclude(kieBaseModel2.getName()).setEqualsBehavior(EqualityBehaviorOption.IDENTITY).setEventProcessingMode(EventProcessingOption.CLOUD);
kieBaseModel3.newKieSessionModel(namespace + ".KSession4").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.PSEUDO);
KieServices ks = KieServices.Factory.get();
KieFileSystemImpl kfs = (KieFileSystemImpl) ks.newKieFileSystem();
kfs.write("src/main/resources/META-INF/beans.xml", generateBeansXML());
kfs.writeKModuleXML(((KieModuleModelImpl) kproj).toXML());
ReleaseId releaseId = ks.newReleaseId(namespace, "art1", version);
kfs.generateAndWritePomXML(releaseId);
String kBase1R1 = getRule(namespace + ".test1", "rule1", version);
String kBase1R2 = getRule(namespace + ".test1", "rule2", version);
String kbase2R1 = getRule(namespace + ".test2", "rule1", version);
String kbase2R2 = getRule(namespace + ".test2", "rule2", version);
String fldKB1 = "src/main/resources/" + (namespace + ".test1").replace('.', '/');
String fldKB2 = "src/main/resources/" + (namespace + ".test2").replace('.', '/');
kfs.write(fldKB1 + "/rule1.drl", kBase1R1.getBytes());
kfs.write(fldKB1 + "/rule2.drl", kBase1R2.getBytes());
kfs.write(fldKB2 + "/rule1.drl", kbase2R1.getBytes());
kfs.write(fldKB2 + "/rule2.drl", kbase2R2.getBytes());
kfs.write("src/main/java/org/drools/compiler/cdi/test/KProjectTestClass" + namespace + ".java", generateKProjectTestClass(kproj, namespace));
KieBuilder kBuilder = ks.newKieBuilder(kfs);
kBuilder.buildAll();
if (kBuilder.getResults().hasMessages(Level.ERROR)) {
fail("should not have errors" + kBuilder.getResults());
}
MemoryKieModule kieModule = (MemoryKieModule) kBuilder.getKieModule();
MemoryFileSystem trgMfs = kieModule.getMemoryFileSystem();
if (createJar) {
trgMfs.writeAsJar(fileManager.getRootDirectory(), namespace + "-" + version);
} else {
java.io.File file = fileManager.newFile(namespace + "-" + version);
trgMfs.writeAsFs(file);
}
return kproj;
}
use of org.drools.compiler.kie.builder.impl.MemoryKieModule 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.drools.compiler.kie.builder.impl.MemoryKieModule in project drools by kiegroup.
the class GeneratedClassNamesTest method testModuleWithDep.
private void testModuleWithDep(ProjectClassLoader projectClassLoader) throws Exception {
KieServices ks = KieServices.get();
ReleaseId releaseIdDep = ks.newReleaseId("org.example", "depRule", "1.0.0");
ReleaseId releaseIdMain = ks.newReleaseId("org.example", "mainRule", "1.0.0");
String depStr = "package org.example.dep\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule R_dep when\n" + " $p : Person(name == \"Mario\")\n" + "then\n" + " System.out.println(\"hello dep rule\");\n" + "end";
String mainStr = "package org.example.main\n" + "import " + Person.class.getCanonicalName() + ";\n" + "rule R_main when\n" + " $p : Person(name == \"Luca\")\n" + "then\n" + " System.out.println(\"hello main rule\");\n" + "end";
String mainPom = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + " xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd\">\n" + " <modelVersion>4.0.0</modelVersion>\n" + "\n" + " <groupId>" + releaseIdMain.getGroupId() + "</groupId>\n" + " <artifactId>" + releaseIdMain.getArtifactId() + "</artifactId>\n" + " <version>" + releaseIdMain.getVersion() + "</version>\n" + " <dependencies>\n" + " <dependency>\n" + " <groupId>" + releaseIdDep.getGroupId() + "</groupId>\n" + " <artifactId>" + releaseIdDep.getArtifactId() + "</artifactId>\n" + " <version>" + releaseIdDep.getVersion() + "</version>\n" + " </dependency>\n" + " </dependencies>" + "</project>";
// create dep
KieFileSystem depKfs = ks.newKieFileSystem();
KieModuleModel depModuleModel = ks.newKieModuleModel();
KieBaseModel depKieBaseModel = depModuleModel.newKieBaseModel("dep").setDefault(false);
depKieBaseModel.addPackage("*");
depKfs.writeKModuleXML(depModuleModel.toXML());
depKfs.generateAndWritePomXML(releaseIdDep);
Resource depResource = ResourceFactory.newByteArrayResource(depStr.getBytes());
depResource.setSourcePath("src/main/resources/org/example/dep/r1.drl");
depKfs.write(depResource);
KieBuilder depKieBuilder = ks.newKieBuilder(depKfs);
depKieBuilder.buildAll(ExecutableModelProject.class);
CanonicalKieModule depKieModule = (CanonicalKieModule) depKieBuilder.getKieModule();
MemoryKieModule depMemoryKieModule = (MemoryKieModule) depKieModule.getInternalKieModule();
MemoryFileSystem depMfs = depMemoryKieModule.getMemoryFileSystem();
byte[] depBinary = depMfs.writeAsBytes();
Resource depJarResource = ks.getResources().newByteArrayResource(depBinary);
// create main
KieFileSystem mainKfs = ks.newKieFileSystem();
KieModuleModel mainModuleModel = ks.newKieModuleModel();
KieBaseModel mainKieBaseModel = mainModuleModel.newKieBaseModel("main").setDefault(true);
mainKieBaseModel.addPackage("*");
mainKieBaseModel.addInclude("dep");
Resource mainResource = ResourceFactory.newByteArrayResource(mainStr.getBytes());
mainResource.setSourcePath("src/main/resources/org/example/main/r2.drl");
mainKfs.write(mainResource);
mainKfs.writeKModuleXML(mainModuleModel.toXML());
mainKfs.write("pom.xml", mainPom);
KieBuilder mainKieBuilder = ks.newKieBuilder(mainKfs);
mainKieBuilder.setDependencies(depJarResource);
mainKieBuilder.buildAll(ExecutableModelProject.class);
CanonicalKieModule mainKieModule = (CanonicalKieModule) mainKieBuilder.getKieModule();
MemoryKieModule mainMemoryKieModule = (MemoryKieModule) mainKieModule.getInternalKieModule();
MemoryFileSystem mainMfs = mainMemoryKieModule.getMemoryFileSystem();
byte[] mainBinary = mainMfs.writeAsBytes();
Resource mainJarResource = ks.getResources().newByteArrayResource(mainBinary);
// cleanup
ks.getRepository().removeKieModule(releaseIdMain);
ks.getRepository().removeKieModule(releaseIdDep);
ks.getRepository().addKieModule(mainJarResource, depJarResource);
KieContainerImpl kieContainer;
if (projectClassLoader == null) {
kieContainer = (KieContainerImpl) ks.newKieContainer(releaseIdMain);
} else {
kieContainer = (KieContainerImpl) ks.newKieContainer(releaseIdMain, projectClassLoader);
}
CanonicalKieModule mainLoadedModule = (CanonicalKieModule) kieContainer.getKieModuleForKBase("main");
KieBase kbase = kieContainer.getKieBase();
Set<String> mainGeneratedClassNames = mainLoadedModule.getGeneratedClassNames();
assertGeneratedClassNamesWithDep(mainGeneratedClassNames);
KieSession ksession = kbase.newKieSession();
ksession.insert(new Person("Mario"));
ksession.insert(new Person("Luca"));
int fired = ksession.fireAllRules();
assertEquals(2, fired);
ksession.dispose();
}
Aggregations