Search in sources :

Example 6 with MemoryKieModule

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();
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ResourceProvider(org.drools.wiring.api.ResourceProvider) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) MemoryFolder(org.drools.compiler.compiler.io.memory.MemoryFolder) Test(org.junit.Test)

Example 7 with MemoryKieModule

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");
}
Also used : MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ResourceProvider(org.drools.wiring.api.ResourceProvider) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) Test(org.junit.Test)

Example 8 with MemoryKieModule

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;
}
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 9 with MemoryKieModule

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());
}
Also used : Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) Folder(org.drools.compiler.compiler.io.Folder) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) StringReader(java.io.StringReader) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) InternalKieContainer(org.drools.core.impl.InternalKieContainer) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieModule(org.kie.api.builder.KieModule) InternalKieContainer(org.drools.core.impl.InternalKieContainer) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 10 with MemoryKieModule

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();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Resource(org.kie.api.io.Resource) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) KieBaseModel(org.kie.api.builder.model.KieBaseModel) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) MemoryKieModule(org.drools.compiler.kie.builder.impl.MemoryKieModule) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Person(org.drools.modelcompiler.domain.Person)

Aggregations

MemoryKieModule (org.drools.compiler.kie.builder.impl.MemoryKieModule)12 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)11 Test (org.junit.Test)8 ResourceProvider (org.drools.wiring.api.ResourceProvider)6 KieServices (org.kie.api.KieServices)5 MemoryFile (org.drools.compiler.compiler.io.memory.MemoryFile)4 MemoryFolder (org.drools.compiler.compiler.io.memory.MemoryFolder)4 ReleaseId (org.kie.api.builder.ReleaseId)4 StringReader (java.io.StringReader)3 Folder (org.drools.compiler.compiler.io.Folder)3 Resource (org.kie.api.io.Resource)3 InternalKieContainer (org.drools.core.impl.InternalKieContainer)2 KieBuilder (org.kie.api.builder.KieBuilder)2 KieModule (org.kie.api.builder.KieModule)2 KieBaseModel (org.kie.api.builder.model.KieBaseModel)2 KieModuleModel (org.kie.api.builder.model.KieModuleModel)2 KieContainer (org.kie.api.runtime.KieContainer)2 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1