Search in sources :

Example 1 with Resource

use of io.automatiko.engine.api.io.Resource in project automatiko-engine by automatiko-io.

the class DecisionCodegen method ofPath.

public static DecisionCodegen ofPath(List<String> dependencies, Path... paths) throws IOException {
    List<DMNResource> resources = new ArrayList<>();
    for (String dependency : dependencies) {
        List<Resource> dmnresources = new ArrayList<>();
        File file = new File(dependency);
        if (file.isDirectory()) {
            Path srcPath = file.toPath();
            if (Files.exists(srcPath)) {
                try (Stream<Path> filesStream = Files.walk(srcPath)) {
                    List<File> files = filesStream.filter(p -> p.toString().endsWith(".dmn")).map(Path::toFile).collect(Collectors.toList());
                    resources.addAll(parseFiles(srcPath, files));
                }
            }
        } else {
            try (ZipFile zipFile = new ZipFile(dependency)) {
                Enumeration<? extends ZipEntry> entries = zipFile.entries();
                while (entries.hasMoreElements()) {
                    ZipEntry entry = entries.nextElement();
                    if (entry.getName().endsWith(".dmn")) {
                        InternalResource resource = new ByteArrayResource(readBytesFromInputStream(zipFile.getInputStream(entry)));
                        resource.setSourcePath(entry.getName());
                        dmnresources.add(resource);
                    }
                }
            } catch (IOException e) {
            }
            resources.addAll(parseDecisions(Paths.get(dependency), dmnresources));
        }
    }
    for (Path path : paths) {
        Path srcPath = Paths.get(path.toString());
        if (Files.exists(srcPath)) {
            try (Stream<Path> filesStream = Files.walk(srcPath)) {
                List<File> files = filesStream.filter(p -> p.toString().endsWith(".dmn")).map(Path::toFile).collect(Collectors.toList());
                resources.addAll(parseFiles(srcPath, files));
            }
        }
    }
    return ofDecisions(resources);
}
Also used : Path(java.nio.file.Path) InternalResource(io.automatiko.engine.services.io.InternalResource) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) Resource(io.automatiko.engine.api.io.Resource) FileSystemResource(io.automatiko.engine.services.io.FileSystemResource) ByteArrayResource(io.automatiko.engine.services.io.ByteArrayResource) InternalResource(io.automatiko.engine.services.io.InternalResource) ByteArrayResource(io.automatiko.engine.services.io.ByteArrayResource) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile) ZipFile(java.util.zip.ZipFile) GeneratedFile(io.automatiko.engine.codegen.GeneratedFile) File(java.io.File)

Example 2 with Resource

use of io.automatiko.engine.api.io.Resource in project automatiko-engine by automatiko-io.

the class DecisionCodegen method ofJar.

public static DecisionCodegen ofJar(List<String> dependencies, Path... jarPaths) throws IOException {
    List<DMNResource> dmnResources = new ArrayList<>();
    for (Path jarPath : jarPaths) {
        List<Resource> resources = new ArrayList<>();
        try (ZipFile zipFile = new ZipFile(jarPath.toFile())) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.getName().endsWith(".dmn")) {
                    InternalResource resource = new ByteArrayResource(readBytesFromInputStream(zipFile.getInputStream(entry)));
                    resource.setSourcePath(entry.getName());
                    resources.add(resource);
                }
            }
        }
        dmnResources.addAll(parseDecisions(jarPath, resources));
    }
    for (String dependency : dependencies) {
        List<Resource> resources = new ArrayList<>();
        try (ZipFile zipFile = new ZipFile(dependency)) {
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.getName().endsWith(".dmn")) {
                    InternalResource resource = new ByteArrayResource(readBytesFromInputStream(zipFile.getInputStream(entry)));
                    resource.setSourcePath(entry.getName());
                    resources.add(resource);
                }
            }
        } catch (IOException e) {
        }
        dmnResources.addAll(parseDecisions(Paths.get(dependency), resources));
    }
    return ofDecisions(dmnResources);
}
Also used : Path(java.nio.file.Path) InternalResource(io.automatiko.engine.services.io.InternalResource) ZipEntry(java.util.zip.ZipEntry) ArrayList(java.util.ArrayList) Resource(io.automatiko.engine.api.io.Resource) FileSystemResource(io.automatiko.engine.services.io.FileSystemResource) ByteArrayResource(io.automatiko.engine.services.io.ByteArrayResource) InternalResource(io.automatiko.engine.services.io.InternalResource) ByteArrayResource(io.automatiko.engine.services.io.ByteArrayResource) IOException(java.io.IOException) ZipFile(java.util.zip.ZipFile)

Example 3 with Resource

use of io.automatiko.engine.api.io.Resource in project automatiko-engine by automatiko-io.

the class DmnRuntimeProvider method fromResources.

private static Either<Exception, DMNRuntime> fromResources(Collection<org.kie.api.io.Resource> resources) {
    ExtendedDMNProfile profile = new ExtendedDMNProfile();
    DMNCompilerConfigurationImpl cc = new DMNCompilerConfigurationImpl();
    cc.addExtensions(profile.getExtensionRegisters());
    cc.addDRGElementCompilers(profile.getDRGElementCompilers());
    cc.addFEELProfile(profile);
    DMNCompiler dmnCompiler = new DMNCompilerImpl(cc);
    List<DMNResource> dmnResources = new ArrayList<>();
    for (org.kie.api.io.Resource r : resources) {
        Definitions definitions;
        try {
            definitions = getMarshaller(cc).unmarshal(r.getReader());
        } catch (IOException e) {
            return Either.ofLeft(e);
        }
        DMNResource dmnResource = new DMNResource(definitions, new ResourceWithConfiguration() {

            @Override
            public ResourceConfiguration getResourceConfiguration() {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public org.kie.api.io.Resource getResource() {
                return (org.kie.api.io.Resource) r;
            }

            @Override
            public Consumer<Object> getBeforeAdd() {
                return null;
            }

            @Override
            public Consumer<Object> getAfterAdd() {
                return null;
            }
        });
        dmnResources.add(dmnResource);
    }
    DMNAssemblerService.enrichDMNResourcesWithImportsDependencies(dmnResources, Collections.emptyList());
    List<DMNResource> sortedDmnResources = DMNResourceDependenciesSorter.sort(dmnResources);
    List<DMNModel> dmnModels = new ArrayList<>();
    for (DMNResource dmnRes : sortedDmnResources) {
        DMNModel dmnModel = dmnCompiler.compile(dmnRes.getDefinitions(), dmnRes.getResAndConfig().getResource(), dmnModels);
        if (dmnModel != null) {
            dmnModels.add(dmnModel);
        } else {
            return Either.ofLeft(new IllegalStateException("Unable to compile DMN model for the resource " + dmnRes.getResAndConfig().getResource()));
        }
    }
    return Either.ofRight(new DMNRuntimeImpl(new DMNRuntimeKBStatic(dmnModels, Arrays.asList(profile))));
}
Also used : ExtendedDMNProfile(org.kie.dmn.core.compiler.profiles.ExtendedDMNProfile) DMNCompilerImpl(org.kie.dmn.core.compiler.DMNCompilerImpl) DMNResource(org.kie.dmn.core.assembler.DMNResource) DMNRuntimeImpl(org.kie.dmn.core.impl.DMNRuntimeImpl) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) FileSystemResource(org.drools.core.io.impl.FileSystemResource) Resource(io.automatiko.engine.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) DMNResource(org.kie.dmn.core.assembler.DMNResource) DMNCompiler(org.kie.dmn.api.core.DMNCompiler) IOException(java.io.IOException) ResourceWithConfiguration(org.kie.api.io.ResourceWithConfiguration) ResourceConfiguration(org.kie.api.io.ResourceConfiguration) Consumer(java.util.function.Consumer) DMNModel(org.kie.dmn.api.core.DMNModel) DMNCompilerConfigurationImpl(org.kie.dmn.core.compiler.DMNCompilerConfigurationImpl)

Example 4 with Resource

use of io.automatiko.engine.api.io.Resource in project automatiko-engine by automatiko-io.

the class ServerlessProcess method from.

public static List<ServerlessProcess> from(ProcessConfig config, Resource... resources) {
    List<ServerlessProcess> compiled = new ArrayList<>();
    ServerlessWorkflowParser parser = new ServerlessWorkflowParser();
    for (Resource resource : resources) {
        try {
            compiled.add(new ServerlessProcess(parser.parse(resource.getReader()), config));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return compiled;
}
Also used : ServerlessWorkflowParser(io.automatiko.engine.workflow.serverless.parser.ServerlessWorkflowParser) ArrayList(java.util.ArrayList) Resource(io.automatiko.engine.api.io.Resource) IOException(java.io.IOException)

Example 5 with Resource

use of io.automatiko.engine.api.io.Resource in project automatiko-engine by automatiko-io.

the class ClassPathResource method listResources.

public Collection<Resource> listResources() {
    try {
        URL url = getURL();
        if ("file".equals(url.getProtocol())) {
            File dir = new File(StringUtils.toURI(url.toString()).getSchemeSpecificPart());
            List<Resource> resources = new ArrayList<Resource>();
            for (File file : dir.listFiles()) {
                resources.add(new FileSystemResource(file));
            }
            return resources;
        }
    } catch (Exception e) {
    // swollow as we'll throw an exception anyway
    }
    throw new RuntimeException("This Resource cannot be listed, or is not a directory");
}
Also used : Resource(io.automatiko.engine.api.io.Resource) ArrayList(java.util.ArrayList) File(java.io.File) URL(java.net.URL) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

Resource (io.automatiko.engine.api.io.Resource)9 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)5 File (java.io.File)4 Process (io.automatiko.engine.api.definition.process.Process)3 ByteArrayResource (io.automatiko.engine.services.io.ByteArrayResource)3 FileSystemResource (io.automatiko.engine.services.io.FileSystemResource)3 InternalResource (io.automatiko.engine.services.io.InternalResource)3 XmlProcessReader (io.automatiko.engine.workflow.compiler.xml.XmlProcessReader)3 GeneratedFile (io.automatiko.engine.codegen.GeneratedFile)2 BPMNDISemanticModule (io.automatiko.engine.workflow.bpmn2.xml.BPMNDISemanticModule)2 BPMNExtensionsSemanticModule (io.automatiko.engine.workflow.bpmn2.xml.BPMNExtensionsSemanticModule)2 BPMNSemanticModule (io.automatiko.engine.workflow.bpmn2.xml.BPMNSemanticModule)2 SemanticModules (io.automatiko.engine.workflow.compiler.xml.SemanticModules)2 WorkflowProcess (io.automatiko.engine.workflow.process.core.WorkflowProcess)2 Path (java.nio.file.Path)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2