use of org.drools.compiler.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class MemoryFolderTest method testNestedRelativePathReverseArguments.
@Test
public void testNestedRelativePathReverseArguments() {
FileSystem fs = new MemoryFileSystem();
Folder f1 = fs.getFolder("src/main/java/org");
Folder f2 = fs.getFolder("src/main/java/");
f1.create();
f2.create();
assertEquals("..", f2.getPath().toRelativePortableString(f1.getPath()));
fs = new MemoryFileSystem();
f1 = fs.getFolder("src/main/java/org/drools/reteoo");
f2 = fs.getFolder("src/main/java");
f1.create();
f2.create();
assertEquals("../../..", f2.getPath().toRelativePortableString(f1.getPath()));
}
use of org.drools.compiler.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class KieBuilderImpl method buildAll.
public KieBuilder buildAll(BiFunction<InternalKieModule, ClassLoader, KieModuleKieProject> kprojectSupplier, Predicate<String> classFilter) {
PomModel pomModel = init();
// kModuleModel will be null if a provided pom.xml or kmodule.xml is invalid
if (!isBuilt() && kModuleModel != null) {
trgMfs = new MemoryFileSystem();
writePomAndKModule();
addKBasesFilesToTrg();
markSource();
MemoryKieModule memoryKieModule = new MemoryKieModule(adapt(releaseId), kModuleModel, trgMfs);
if (kieDependencies != null && !kieDependencies.isEmpty()) {
for (KieModule kieModule : kieDependencies) {
memoryKieModule.addKieDependency((InternalKieModule) kieModule);
}
}
if (pomModel != null) {
memoryKieModule.setPomModel(pomModel);
}
KieModuleKieProject kProject = kprojectSupplier.apply(memoryKieModule, classLoader);
for (ReleaseId unresolvedDep : memoryKieModule.getUnresolvedDependencies()) {
results.addMessage(Level.ERROR, "pom.xml", "Unresolved dependency " + unresolvedDep);
}
List<String> pmmlFileNames = getPmmlFileNames(srcMfs);
if (pmmlFileNames != null && !pmmlFileNames.isEmpty()) {
buildPMMLPojos(pmmlFileNames, kProject);
}
compileJavaClasses(kProject.getClassLoader(), classFilter);
buildKieProject(results, kProject, trgMfs);
kModule = kProject.getInternalKieModule();
}
return this;
}
use of org.drools.compiler.compiler.io.memory.MemoryFileSystem 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 + ".KBase1").setDefault(true);
kieBaseModel1.newKieSessionModel(namespace + ".KSession1").setType(KieSessionType.STATELESS).setClockType(ClockTypeOption.get("realtime")).setDefault(true);
kieBaseModel1.newKieSessionModel(namespace + ".KSession2").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo"));
kieBaseModel1.newKieSessionModel(namespace + ".KSessionDefault").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("pseudo")).setDefault(true);
KieBaseModel kieBaseModel2 = kproj.newKieBaseModel(namespace + ".KBase2").setEqualsBehavior(EqualityBehaviorOption.IDENTITY).addPackage(namespace + ".KBase2").setEventProcessingMode(EventProcessingOption.CLOUD);
kieBaseModel2.newKieSessionModel(namespace + ".KSession3").setType(KieSessionType.STATEFUL).setClockType(ClockTypeOption.get("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.get("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/" + kieBaseModel1.getName().replace('.', '/');
String fldKB2 = "src/main/resources/" + kieBaseModel2.getName().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.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class KieCompilationCacheTest method testCacheWigAccumulate.
@Test
public void testCacheWigAccumulate() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "rule R11 when\n" + " Number() from accumulate(String(), \n" + " init(int x = 0;)," + " action(x++;)," + " reverse(x--;)," + " result(x))\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("org.kie", "cache-accumulate", "1.0-SNAPSHOT");
KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
ks.newKieBuilder(kfs).buildAll();
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);
KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
ksession.insert(new String("Hello World"));
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.compiler.compiler.io.memory.MemoryFileSystem in project drools by kiegroup.
the class KieCompilationCacheTest method testHelloWorldWithPackagesAnd2KieBases.
@Test
public void testHelloWorldWithPackagesAnd2KieBases() throws Exception {
String drl1 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R11 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R12 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
String drl2 = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getCanonicalName() + "\n" + "rule R21 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n" + "rule R22 when\n" + " $m : Message( message == \"Aloha Earth\" )\n" + "then\n" + "end\n";
KieServices ks = KieServices.Factory.get();
ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");
KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1).write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2).writeKModuleXML(createKieProjectWithPackagesAnd2KieBases(ks).toXML());
ks.newKieBuilder(kfs).buildAll();
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);
file = mfs.getFile(KieBuilderImpl.getCompilationCachePath(releaseId, "KBase2"));
assertNotNull(file);
Resource jarRes = ks.getResources().newByteArrayResource(jar);
KieModule km = ks.getRepository().addKieModule(jarRes);
KieSession ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
ksession.insert(new Message("Hello World"));
assertEquals(1, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
ksession.insert(new Message("Hi Universe"));
assertEquals(1, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession1");
ksession.insert(new Message("Aloha Earth"));
assertEquals(0, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
ksession.insert(new Message("Hello World"));
assertEquals(1, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
ksession.insert(new Message("Hi Universe"));
assertEquals(0, ksession.fireAllRules());
ksession = ks.newKieContainer(km.getReleaseId()).newKieSession("KSession2");
ksession.insert(new Message("Aloha Earth"));
assertEquals(1, ksession.fireAllRules());
}
Aggregations