Search in sources :

Example 21 with MemoryFileSystem

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

the class KieRepositoryImpl method getKieModule.

public KieModule getKieModule(Resource resource) {
    InternalResource res = (InternalResource) resource;
    try {
        KieModule kModule;
        // find kmodule.xml
        if (res.hasURL()) {
            String urlPath = res.getURL().toExternalForm();
            if (res.isDirectory()) {
                if (!urlPath.endsWith("/")) {
                    urlPath = urlPath + "/";
                }
                urlPath = urlPath + KieModuleModelImpl.KMODULE_JAR_PATH;
            } else {
                urlPath = "jar:" + urlPath + "!/" + KieModuleModelImpl.KMODULE_JAR_PATH;
            }
            kModule = ClasspathKieProject.fetchKModule(new URL(urlPath));
            log.debug("Fetched KieModule from resource: " + resource);
        } else {
            // might be a byte[] resource
            MemoryFileSystem mfs = MemoryFileSystem.readFromJar(res.getInputStream());
            byte[] bytes = mfs.getBytes(KieModuleModelImpl.KMODULE_JAR_PATH);
            KieModuleModel kieProject = KieModuleModelImpl.fromXML(new ByteArrayInputStream(bytes));
            setDefaultsforEmptyKieModule(kieProject);
            String pomProperties = mfs.findPomProperties();
            ReleaseId releaseId = ReleaseIdImpl.fromPropertiesString(pomProperties);
            kModule = new MemoryKieModule(releaseId, kieProject, mfs);
        }
        return kModule;
    } catch (Exception e) {
        throw new RuntimeException("Unable to fetch module from resource: " + res, e);
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) ByteArrayInputStream(java.io.ByteArrayInputStream) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId) KieModule(org.kie.api.builder.KieModule) KieBuilderImpl.setDefaultsforEmptyKieModule(org.drools.compiler.kie.builder.impl.KieBuilderImpl.setDefaultsforEmptyKieModule) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 22 with MemoryFileSystem

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

the class KieBuilderTest method testInvalidPomXmlGAV.

@Test
public void testInvalidPomXmlGAV() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    KieModuleModel kProj = createKieProject(namespace);
    ReleaseId releaseId = new ReleaseIdImpl("", "", "");
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML(kfs, releaseId);
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    KieBuilder kieBuilder = createKieBuilder(kfs);
    kieBuilder.buildAll();
    assertTrue(kieBuilder.getResults().hasMessages(Level.ERROR));
}
Also used : ReleaseIdImpl(org.drools.compiler.kproject.ReleaseIdImpl) MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 23 with MemoryFileSystem

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

the class KieBuilderTest method testEmptyProjectXml.

@Test
public void testEmptyProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    KieServices ks = KieServices.Factory.get();
    KieModuleModel kProj = ks.newKieModuleModel();
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0-SNAPSHOT");
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generateKProjectXML(kfs, namespace, kProj);
    generatePomXML(kfs, releaseId);
    generateMessageClass(kfs, namespace);
    generateRule(kfs, namespace);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) Test(org.junit.Test)

Example 24 with MemoryFileSystem

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

the class KieBuilderTest method testOnDisc.

@Test
public void testOnDisc() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";
    KieModuleModel kProj = createKieProject(namespace);
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0-SNAPSHOT");
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs, namespace, releaseId, kProj);
    MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
    File file = fileManager.getRootDirectory();
    mfs.writeAsFs(file);
    createAndTestKieContainer(releaseId, createKieBuilder(kfs), namespace);
}
Also used : MemoryFileSystem(org.drools.compiler.compiler.io.memory.MemoryFileSystem) KieFileSystem(org.kie.api.builder.KieFileSystem) KieFileSystemImpl(org.drools.compiler.kie.builder.impl.KieFileSystemImpl) KieModuleModel(org.kie.api.builder.model.KieModuleModel) ReleaseId(org.kie.api.builder.ReleaseId) File(java.io.File) Test(org.junit.Test)

Example 25 with MemoryFileSystem

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

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