Search in sources :

Example 1 with MemoryFile

use of org.drools.compiler.compiler.io.memory.MemoryFile in project drools by kiegroup.

the class MemoryKieModuleResourceProviderTest method testGetResourceAsStreamForFile.

@Test
public void testGetResourceAsStreamForFile() 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 folderContents = IOUtils.toString(resourceProvider.getResourceAsStream("src/main/resources/my-file1"));
    // "AB" == new byte[] {65, 66}
    Assertions.assertThat(folderContents).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.core.common.ResourceProvider) Test(org.junit.Test)

Example 2 with MemoryFile

use of org.drools.compiler.compiler.io.memory.MemoryFile 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.core.common.ResourceProvider) Test(org.junit.Test)

Example 3 with MemoryFile

use of org.drools.compiler.compiler.io.memory.MemoryFile in project drools by kiegroup.

the class AbstractKnowledgeTest method writeFs.

public void writeFs(MemoryFileSystem mfs, Folder f, java.io.File file1) {
    for (Resource rs : f.getMembers()) {
        if (rs instanceof Folder) {
            java.io.File file2 = new java.io.File(file1, ((Folder) rs).getName());
            file2.mkdir();
            writeFs(mfs, (Folder) rs, file2);
        } else {
            byte[] bytes = mfs.getFileContents((MemoryFile) rs);
            try {
                IoUtils.write(new java.io.File(file1, ((File) rs).getName()), bytes);
            } catch (IOException e) {
                fail("Unable to write project to file system\n" + e.getMessage());
            }
        }
    }
}
Also used : Resource(org.drools.compiler.compiler.io.Resource) IOException(java.io.IOException) Folder(org.drools.compiler.compiler.io.Folder) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) File(org.drools.compiler.compiler.io.File)

Example 4 with MemoryFile

use of org.drools.compiler.compiler.io.memory.MemoryFile in project drools by kiegroup.

the class CanonicalModelKieProject method writeProjectOutput.

@Override
public void writeProjectOutput(MemoryFileSystem trgMfs, ResultsImpl messages) {
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    ModelWriter modelWriter = new ModelWriter();
    List<String> modelFiles = new ArrayList<>();
    for (ModelBuilderImpl modelBuilder : modelBuilders) {
        final ModelWriter.Result result = modelWriter.writeModel(srcMfs, modelBuilder.getPackageModels());
        modelFiles.addAll(result.getModelFiles());
        final String[] sources = result.getSources();
        if (sources.length != 0) {
            CompilationResult res = getCompiler().compile(sources, srcMfs, trgMfs, getClassLoader());
            Stream.of(res.getErrors()).collect(groupingBy(CompilationProblem::getFileName)).forEach((name, errors) -> {
                errors.forEach(messages::addMessage);
                File srcFile = srcMfs.getFile(name);
                if (srcFile instanceof MemoryFile) {
                    String src = new String(srcMfs.getFileContents((MemoryFile) srcFile));
                    messages.addMessage(Message.Level.ERROR, name, "Java source of " + name + " in error:\n" + src);
                }
            });
            for (CompilationProblem problem : res.getWarnings()) {
                messages.addMessage(problem);
            }
        }
    }
    modelWriter.writeModelFile(modelFiles, trgMfs);
}
Also used : MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) ArrayList(java.util.ArrayList) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult) File(org.drools.compiler.compiler.io.File) MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile)

Example 5 with MemoryFile

use of org.drools.compiler.compiler.io.memory.MemoryFile in project drools by kiegroup.

the class MemoryKieModuleResourceProviderTest method testGetResourceForFolderWithFilesAndSubFolders.

@Test
public void testGetResourceForFolderWithFilesAndSubFolders() throws Exception {
    MemoryFileSystem mfs = new MemoryFileSystem();
    mfs.createFolder(new MemoryFolder(mfs, "src/main/java"));
    mfs.createFolder(new MemoryFolder(mfs, "src/main/java/org"));
    mfs.createFolder(new MemoryFolder(mfs, "src/main/java/com"));
    mfs.setFileContents(new MemoryFile(mfs, "my-file1", mfs.getFolder("src/main/java")), new byte[10]);
    mfs.setFileContents(new MemoryFile(mfs, "my-file2", mfs.getFolder("src/main/java")), new byte[10]);
    MemoryKieModule mkm = new MemoryKieModule(releaseId, kieModuleModel, mfs);
    ResourceProvider resourceProvider = mkm.createResourceProvider();
    String folderContents = IOUtils.toString(resourceProvider.getResource("src/main/java").openStream());
    Assertions.assertThat(folderContents).hasLineCount(4).contains("com", "org", "my-file1", "my-file2");
}
Also used : MemoryFile(org.drools.compiler.compiler.io.memory.MemoryFile) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ResourceProvider(org.drools.core.common.ResourceProvider) MemoryFolder(org.drools.compiler.compiler.io.memory.MemoryFolder) Test(org.junit.Test)

Aggregations

MemoryFile (org.drools.compiler.compiler.io.memory.MemoryFile)5 MemoryFileSystem (org.drools.compiler.compiler.io.memory.MemoryFileSystem)4 ResourceProvider (org.drools.core.common.ResourceProvider)3 Test (org.junit.Test)3 File (org.drools.compiler.compiler.io.File)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CompilationResult (org.drools.compiler.commons.jci.compilers.CompilationResult)1 CompilationProblem (org.drools.compiler.commons.jci.problems.CompilationProblem)1 Folder (org.drools.compiler.compiler.io.Folder)1 Resource (org.drools.compiler.compiler.io.Resource)1 MemoryFolder (org.drools.compiler.compiler.io.memory.MemoryFolder)1