Search in sources :

Example 11 with FileParameter

use of com.genericworkflownodes.knime.parameter.FileParameter in project GenericKnimeNodes by genericworkflownodes.

the class GenericKnimeNodeModel method transferIncomingPorts2Config.

/**
 * Transfers the incoming ports into the config, that it can be written out
 * into a config file or can be transferred to the command line.
 *
 * @param inData
 *            The incoming port objects.
 * @throws Exception
 */
private void transferIncomingPorts2Config(PortObject[] inData) throws Exception {
    // Transfer settings from the input ports into the configuration object
    for (int i = 0; i < inData.length; i++) {
        // find the internal port for this PortObject
        Port port = m_nodeConfig.getInputPorts().get(i);
        IURIPortObject po = (IURIPortObject) inData[i];
        String name = port.getName();
        // find the associated parameter in the configuration
        Parameter<?> p = m_nodeConfig.getParameter(name);
        boolean isMultiFile = port.isMultiFile();
        boolean isPrefix = port.isPrefix();
        // skip optional and unconnected inport ports
        if (inData[i] == null) {
            p.setValue(null);
            continue;
        }
        // connected: check contents
        List<URIContent> uris = po.getURIContents();
        // check validity of subtypes with actual inputs
        if (uris.size() > 1 && (!isMultiFile && !isPrefix)) {
            throw new Exception("IURIPortObject with multiple URIs supplied at single URI port #" + i);
        }
        // port
        if (!(p instanceof IFileParameter)) {
            throw new Exception("Invalid reference from port to non-file parameter. URI port #" + i);
        }
        if (isPrefix) {
            // we pass only the prefix to the tool
            IPrefixURIPortObject puri = (IPrefixURIPortObject) inData[i];
            ((FileParameter) p).setValue(puri.getPrefix());
        } else if (isMultiFile) {
            // we need to collect all filenames and then set them as a batch
            // in the config
            List<String> filenames = new ArrayList<String>();
            for (URIContent uric : uris) {
                URI uri = uric.getURI();
                filenames.add(new File(uri).getAbsolutePath());
            }
            ((FileListParameter) p).setValue(filenames);
        } else {
            // just one filename
            URI uri = uris.get(0).getURI();
            String filename = new File(uri).getAbsolutePath();
            ((FileParameter) p).setValue(filename);
        }
    }
}
Also used : IPrefixURIPortObject(com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject) Port(com.genericworkflownodes.knime.port.Port) URI(java.net.URI) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) InvalidParameterValueException(com.genericworkflownodes.knime.parameter.InvalidParameterValueException) UnknownToolExecutorException(com.genericworkflownodes.knime.execution.UnknownToolExecutorException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NoBinaryAvailableException(com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException) UnknownCommandGeneratorException(com.genericworkflownodes.knime.execution.UnknownCommandGeneratorException) IFileParameter(com.genericworkflownodes.knime.parameter.IFileParameter) IURIPortObject(org.knime.core.data.uri.IURIPortObject) ArrayList(java.util.ArrayList) List(java.util.List) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter) IFileParameter(com.genericworkflownodes.knime.parameter.IFileParameter) File(java.io.File) URIContent(org.knime.core.data.uri.URIContent)

Example 12 with FileParameter

use of com.genericworkflownodes.knime.parameter.FileParameter in project GenericKnimeNodes by genericworkflownodes.

the class GenericKnimeNodeModel method transferOutgoingPorts2Config.

/**
 * Creates a list of lists of output files (as {@link URI}s) pointing to the
 * files that will be generated by the executed tool.
 *
 * @param jobdir
 *            The working directory of the executable.
 * @param inData
 *            The input data as {@link PortObject} array
 * @return A list of lists of output files
 * @throws Exception
 *             If the input has an invalid configuration.
 */
private List<PortObject> transferOutgoingPorts2Config(final File jobdir, PortObject[] inData, ExecutionContext exec) throws Exception {
    final int nOut = m_nodeConfig.getOutputPorts().size();
    List<PortObject> outPorts = new ArrayList<PortObject>(nOut);
    for (int i = 0; i < nOut; i++) {
        Port port = m_nodeConfig.getOutputPorts().get(i);
        // Before we know about its extension, a port is reset to active
        port.setActive(true);
        String name = port.getName();
        String ext = "";
        boolean isPrefix = port.isPrefix();
        if (!isPrefix) {
            ext = getOutputType(i);
        }
        if (ext.toLowerCase().equals("inactive")) {
            port.setActive(false);
            outPorts.add(InactiveBranchPortObject.INSTANCE);
            continue;
        }
        Parameter<?> p = m_nodeConfig.getParameter(name);
        // basenames and number of output files guessed from input
        List<String> basenames = getOutputBaseNames();
        if (p instanceof FileListParameter && port.isMultiFile()) {
            // we currently do not support lists of prefixes
            if (isPrefix) {
                throw new InvalidSettingsException("GKN currently does not support lists of prefixes as output.");
            }
            FileListParameter flp = (FileListParameter) p;
            List<String> fileNames = new ArrayList<String>();
            if (basenames.size() == 0) {
                throw new Exception("Cannot determine number of output files if no input file is given.");
            }
            PortObject fsupo = new FileStoreURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i));
            for (int f = 0; f < basenames.size(); ++f) {
                // create basename: <base_name>_<port_nr>_<outfile_nr>
                String file_basename = String.format("%s_%d", basenames.get(f), f);
                File file = ((FileStoreURIPortObject) fsupo).registerFile(file_basename + "." + ext);
                fileNames.add(file.getAbsolutePath());
            }
            // add filled portobject
            outPorts.add(fsupo);
            // overwrite existing settings with new values generated by the
            // stash
            flp.setValue(fileNames);
        } else if (p instanceof FileParameter && !port.isMultiFile()) {
            PortObject po;
            // if we have no basename to use (e.g., Node without input-file)
            // we use the nodename
            String basename;
            if (basenames.isEmpty()) {
                basename = m_nodeConfig.getName();
            } else {
                basename = basenames.get(0);
            }
            // create basename: <base_name>_<outfile_nr>
            String fileName = basename;
            if (!isPrefix) {
                fileName += '.' + ext;
            }
            if (isPrefix) {
                po = new FileStorePrefixURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i), fileName);
                ((FileParameter) p).setValue(((FileStorePrefixURIPortObject) po).getPrefix());
                LOGGER.debug("> setting param " + name + "->" + ((FileStorePrefixURIPortObject) po).getPrefix());
            } else {
                po = new FileStoreURIPortObject(exec.createFileStore(m_nodeConfig.getName() + "_" + i));
                // we do not append the file extension if we have a prefix
                File file = ((FileStoreURIPortObject) po).registerFile(fileName);
                ((FileParameter) p).setValue(file.getAbsolutePath());
                LOGGER.debug("> setting param " + name + "->" + file);
            }
            // remember output file
            outPorts.add(po);
        } else {
            throw new Exception("Invalid connection between ports and parameters.");
        }
    }
    return outPorts;
}
Also used : FileListParameter(com.genericworkflownodes.knime.parameter.FileListParameter) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) Port(com.genericworkflownodes.knime.port.Port) ArrayList(java.util.ArrayList) FileStorePrefixURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStorePrefixURIPortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) InvalidParameterValueException(com.genericworkflownodes.knime.parameter.InvalidParameterValueException) UnknownToolExecutorException(com.genericworkflownodes.knime.execution.UnknownToolExecutorException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NoBinaryAvailableException(com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException) UnknownCommandGeneratorException(com.genericworkflownodes.knime.execution.UnknownCommandGeneratorException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter) IFileParameter(com.genericworkflownodes.knime.parameter.IFileParameter) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) IPrefixURIPortObject(com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject) FileStorePrefixURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStorePrefixURIPortObject) PortObject(org.knime.core.node.port.PortObject) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) File(java.io.File)

Example 13 with FileParameter

use of com.genericworkflownodes.knime.parameter.FileParameter in project GenericKnimeNodes by genericworkflownodes.

the class CLICommandGenerator method handleListParameter.

private void handleListParameter(final List<List<? extends CommandLineElement>> extractedParameterValues, final ListParameter listParameter) {
    final int nValues = listParameter.getStrings().size();
    final String key = ((Parameter<?>) listParameter).getKey();
    final List<CommandLineElement> tmpList = new LinkedList<CommandLineElement>();
    // there's only one element in the list
    if (nValues > 1) {
        int sequence = 0;
        for (final String value : listParameter.getStrings()) {
            final CommandLineElement commandLineElement;
            if (listParameter instanceof IFileParameter) {
                commandLineElement = new CommandLineFile(new FileParameter(key, value));
            } else {
                commandLineElement = new CommandLineParameter(new StringParameter(key, value));
            }
            commandLineElement.setSequenceNumber(sequence++);
            tmpList.add(commandLineElement);
        }
    } else {
        // only one value in the list, no need to use sequence numbers
        final String value = listParameter.getStrings().get(0);
        if (listParameter instanceof IFileParameter) {
            tmpList.add(new CommandLineFile(new FileParameter(key, value)));
        } else {
            tmpList.add(new CommandLineParameter(new StringParameter(key, value)));
        }
    }
    extractedParameterValues.add(tmpList);
}
Also used : IFileParameter(com.genericworkflownodes.knime.parameter.IFileParameter) StringParameter(com.genericworkflownodes.knime.parameter.StringParameter) CommandLineParameter(com.genericworkflownodes.knime.commandline.impl.CommandLineParameter) CommandLineFile(com.genericworkflownodes.knime.commandline.impl.CommandLineFile) ListParameter(com.genericworkflownodes.knime.parameter.ListParameter) BoolParameter(com.genericworkflownodes.knime.parameter.BoolParameter) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter) CommandLineParameter(com.genericworkflownodes.knime.commandline.impl.CommandLineParameter) StringParameter(com.genericworkflownodes.knime.parameter.StringParameter) Parameter(com.genericworkflownodes.knime.parameter.Parameter) IFileParameter(com.genericworkflownodes.knime.parameter.IFileParameter) CommandLineFixedString(com.genericworkflownodes.knime.commandline.impl.CommandLineFixedString) CommandLineElement(com.genericworkflownodes.knime.commandline.CommandLineElement) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter) IFileParameter(com.genericworkflownodes.knime.parameter.IFileParameter) LinkedList(java.util.LinkedList)

Example 14 with FileParameter

use of com.genericworkflownodes.knime.parameter.FileParameter in project GenericKnimeNodes by genericworkflownodes.

the class DockerCommandGenerator method extractParamterValues.

private List<List<? extends CommandLineElement>> extractParamterValues(CLIElement cliElement, List<CommandLineElement> dockerCommands, Map<String, String> hostDockerMap) throws IOException {
    List<List<? extends CommandLineElement>> extractedParameterValues = new ArrayList<List<? extends CommandLineElement>>();
    for (CLIMapping cliMapping : cliElement.getMapping()) {
        if (nodeConfig.getParameterKeys().contains(cliMapping.getReferenceName())) {
            Parameter<?> p = nodeConfig.getParameter(cliMapping.getReferenceName());
            if (!p.isNull()) {
                if (p instanceof ListParameter) {
                    ListParameter lp = (ListParameter) p;
                    if (lp.getStrings().size() > 0) {
                        final List<CommandLineElement> tmp = new ArrayList<CommandLineElement>();
                        for (final String s : lp.getStrings()) {
                            tmp.add(new CommandLineFixedString(s));
                        }
                        extractedParameterValues.add(tmp);
                    }
                } else if (p instanceof FileParameter) {
                    extractedParameterValues.add(handleFileParameter(((FileParameter) p).getValue(), dockerCommands, hostDockerMap));
                } else if (p instanceof FileListParameter) {
                    List<String> fl = ((FileListParameter) p).getValue();
                    if (fl.size() > 0) {
                        for (String hostFile : fl) {
                            extractedParameterValues.add(handleFileParameter(hostFile, dockerCommands, hostDockerMap));
                        }
                    }
                } else {
                    List<CommandLineElement> l = new ArrayList<CommandLineElement>();
                    l.add(new CommandLineParameter(p));
                    extractedParameterValues.add(l);
                }
            }
        }
    }
    return extractedParameterValues;
}
Also used : FileListParameter(com.genericworkflownodes.knime.parameter.FileListParameter) CommandLineParameter(com.genericworkflownodes.knime.commandline.impl.CommandLineParameter) ArrayList(java.util.ArrayList) CLIMapping(com.genericworkflownodes.knime.cliwrapper.CLIMapping) CommandLineElement(com.genericworkflownodes.knime.commandline.CommandLineElement) CommandLineFixedString(com.genericworkflownodes.knime.commandline.impl.CommandLineFixedString) CommandLineFixedString(com.genericworkflownodes.knime.commandline.impl.CommandLineFixedString) ArrayList(java.util.ArrayList) List(java.util.List) FileParameter(com.genericworkflownodes.knime.parameter.FileParameter) FileListParameter(com.genericworkflownodes.knime.parameter.FileListParameter) ListParameter(com.genericworkflownodes.knime.parameter.ListParameter)

Aggregations

FileParameter (com.genericworkflownodes.knime.parameter.FileParameter)14 ArrayList (java.util.ArrayList)9 IFileParameter (com.genericworkflownodes.knime.parameter.IFileParameter)8 FileListParameter (com.genericworkflownodes.knime.parameter.FileListParameter)7 Port (com.genericworkflownodes.knime.port.Port)7 NoBinaryAvailableException (com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException)6 UnknownCommandGeneratorException (com.genericworkflownodes.knime.execution.UnknownCommandGeneratorException)6 UnknownToolExecutorException (com.genericworkflownodes.knime.execution.UnknownToolExecutorException)6 InvalidParameterValueException (com.genericworkflownodes.knime.parameter.InvalidParameterValueException)6 IOException (java.io.IOException)6 ExecutionException (java.util.concurrent.ExecutionException)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)6 IPrefixURIPortObject (com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject)4 INodeConfiguration (com.genericworkflownodes.knime.config.INodeConfiguration)4 TestDataSource (com.genericworkflownodes.knime.test.data.TestDataSource)4 File (java.io.File)4 Test (org.junit.Test)4 IURIPortObject (org.knime.core.data.uri.IURIPortObject)4 CommandLineElement (com.genericworkflownodes.knime.commandline.CommandLineElement)3 CommandLineParameter (com.genericworkflownodes.knime.commandline.impl.CommandLineParameter)3