Search in sources :

Example 11 with MemoryFileSystem

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()));
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) FileSystem(org.drools.compiler.compiler.io.FileSystem) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) Folder(org.drools.compiler.compiler.io.Folder) MemoryFolder(org.drools.compiler.compiler.io.memory.MemoryFolder) Test(org.junit.Test)

Example 12 with MemoryFileSystem

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;
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) PomModel(org.appformer.maven.support.PomModel) AFReleaseId(org.appformer.maven.support.AFReleaseId) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule)

Example 13 with MemoryFileSystem

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;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) KieModuleModelImpl(org.drools.compiler.kproject.models.KieModuleModelImpl)

Example 14 with MemoryFileSystem

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());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) 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) Test(org.junit.Test)

Example 15 with MemoryFileSystem

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());
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) 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) Test(org.junit.Test)

Aggregations

MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)35 Test (org.junit.Test)30 ReleaseId (org.kie.api.builder.ReleaseId)14 Folder (org.drools.compiler.compiler.io.Folder)12 FileSystem (org.drools.compiler.compiler.io.FileSystem)11 MemoryFolder (org.drools.compiler.compiler.io.memory.MemoryFolder)11 File (org.drools.compiler.compiler.io.File)10 KieFileSystem (org.kie.api.builder.KieFileSystem)10 KieFileSystemImpl (org.drools.compiler.kie.builder.impl.KieFileSystemImpl)7 KieServices (org.kie.api.KieServices)7 KieModule (org.kie.api.builder.KieModule)7 ResourceProvider (org.drools.core.common.ResourceProvider)6 KieModuleModel (org.kie.api.builder.model.KieModuleModel)6 ByteArrayInputStream (java.io.ByteArrayInputStream)4 MemoryFile (org.drools.compiler.compiler.io.memory.MemoryFile)4 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)4 Resource (org.kie.api.io.Resource)4 KieSession (org.kie.api.runtime.KieSession)4 KieContainer (org.kie.api.runtime.KieContainer)3 IOException (java.io.IOException)2