Search in sources :

Example 26 with MemoryFileSystem

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

the class JavaParserCompiler method compileAll.

public static Map<String, Class<?>> compileAll(KnowledgeBuilderImpl kbuilder, ClassLoader classLoader, List<GeneratedClassWithPackage> classes) {
    if (classes == null || classes.isEmpty()) {
        return Collections.emptyMap();
    }
    MemoryFileSystem srcMfs = new MemoryFileSystem();
    MemoryFileSystem trgMfs = new MemoryFileSystem();
    String[] resources = writeModel(classes, srcMfs);
    CompilationResult resultCompilation = getCompiler().compile(resources, srcMfs, trgMfs, classLoader);
    CompilationProblem[] errors = resultCompilation.getErrors();
    if (errors.length != 0) {
        classes.forEach(c -> logger.error(c.toString()));
        for (CompilationProblem error : errors) {
            kbuilder.addBuilderResult(new CompilationProblemErrorResult(error));
        }
        return Collections.emptyMap();
    }
    InternalClassLoader internalClassLoader = new InternalClassLoader(classLoader, trgMfs);
    Map<String, Class<?>> result = new HashMap<>();
    for (GeneratedClassWithPackage cls : classes) {
        final String fullClassName = cls.getPackageName() + "." + cls.getGeneratedClass().getNameAsString();
        try {
            result.put(fullClassName, Class.forName(fullClassName, true, internalClassLoader));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    return result;
}
Also used : CompilationProblemErrorResult(org.drools.modelcompiler.builder.errors.CompilationProblemErrorResult) HashMap(java.util.HashMap) CompilationProblem(org.drools.compiler.commons.jci.problems.CompilationProblem) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) CompilationResult(org.drools.compiler.commons.jci.compilers.CompilationResult)

Example 27 with MemoryFileSystem

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

the class MemoryFileTest method testFilePath.

@Test
public void testFilePath() {
    FileSystem fs = new MemoryFileSystem();
    Folder mres = fs.getFolder("src/main/java/org/domain");
    File f1 = mres.getFile("MyClass.java");
    assertEquals("src/main/java/org/domain/MyClass.java", f1.getPath().toPortableString());
}
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) File(org.drools.compiler.compiler.io.File) Test(org.junit.Test)

Example 28 with MemoryFileSystem

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

the class MemoryFileTest method testRelativeToParentFilePath.

@Test
public void testRelativeToParentFilePath() {
    FileSystem fs = new MemoryFileSystem();
    Folder mres = fs.getFolder("src/main/java/org/domain");
    Folder f2 = fs.getFolder("src/main/java/org/domain/f1/f2/");
    File f1 = mres.getFile("MyClass.java");
    assertEquals("../../MyClass.java", f1.getPath().toRelativePortableString(f2.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) File(org.drools.compiler.compiler.io.File) Test(org.junit.Test)

Example 29 with MemoryFileSystem

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

the class MemoryFileTest method testFileCreation.

@Test
public void testFileCreation() throws IOException {
    FileSystem fs = new MemoryFileSystem();
    Folder mres = fs.getFolder("src/main/java/org/domain");
    File f1 = mres.getFile("MyClass.java");
    f1.create(new ByteArrayInputStream("ABC".getBytes()));
    mres.create();
    f1 = mres.getFile("MyClass.java");
    assertTrue(f1.exists());
    f1.create(new ByteArrayInputStream("ABC".getBytes()));
    f1 = mres.getFile("MyClass.java");
    assertTrue(f1.exists());
    assertEquals("ABC", StringUtils.toString(f1.getContents()));
    f1.create(new ByteArrayInputStream("ABC".getBytes()));
    f1.setContents(new ByteArrayInputStream("DEF".getBytes()));
    assertEquals("DEF", StringUtils.toString(f1.getContents()));
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) FileSystem(org.drools.compiler.compiler.io.FileSystem) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) Folder(org.drools.compiler.compiler.io.Folder) File(org.drools.compiler.compiler.io.File) Test(org.junit.Test)

Example 30 with MemoryFileSystem

use of org.drools.compiler.compiler.io.memory.MemoryFileSystem 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

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