Search in sources :

Example 1 with FileStoreURIPortObject

use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject in project GenericKnimeNodes by genericworkflownodes.

the class FileSplitterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    IURIPortObject input = (IURIPortObject) inData[0];
    if (input.getURIContents().size() != 1) {
        throw new InvalidSettingsException("This node can only split a single file");
    }
    // The factory for creating the splitter
    String factoryID = m_factoryID.getStringValue();
    m_splitter = SplitterFactoryManager.getInstance().getFactory(factoryID).createSplitter();
    File f = Paths.get(input.getURIContents().get(0).getURI()).toFile();
    // File Store in which we store the files
    FileStore fs = exec.createFileStore("FileSplitter");
    File[] outputs = new File[m_numParts.getIntValue()];
    for (int i = 0; i < m_numParts.getIntValue(); i++) {
        int idx = f.getPath().lastIndexOf('.');
        String ext;
        String name;
        if (idx == -1) {
            ext = "";
            name = f.getName();
        } else {
            ext = f.getPath().substring(idx);
            name = f.getName().substring(0, f.getName().lastIndexOf('.'));
        }
        outputs[i] = Paths.get(fs.getFile().toString()).resolve(name + i + ext).toFile();
        outputs[i].getParentFile().mkdirs();
    }
    m_splitter.split(f, outputs);
    DataContainer dc = exec.createDataContainer(createSpec());
    for (int i = 0; i < m_numParts.getIntValue(); i++) {
        FileStoreURIPortObject po = new FileStoreURIPortObject(fs);
        String relPath = Paths.get(fs.getFile().toString()).relativize(Paths.get(outputs[i].getAbsolutePath())).toString();
        po.registerFile(relPath);
        PortObjectHandlerCell cell = new PortObjectHandlerCell(po);
        dc.addRowToTable(new DefaultRow(new RowKey("Row" + i), cell));
    }
    dc.close();
    return new PortObject[] { (BufferedDataTable) dc.getTable() };
}
Also used : FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) RowKey(org.knime.core.data.RowKey) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) FileStore(org.knime.core.data.filestore.FileStore) DataContainer(org.knime.core.data.container.DataContainer) PortObjectHandlerCell(com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) IURIPortObject(org.knime.core.data.uri.IURIPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) DefaultRow(org.knime.core.data.def.DefaultRow) File(java.io.File) IURIPortObject(org.knime.core.data.uri.IURIPortObject) PortObject(org.knime.core.node.port.PortObject) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)

Example 2 with FileStoreURIPortObject

use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject in project GenericKnimeNodes by genericworkflownodes.

the class DynamicGenericNodeModel 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);
        String name = port.getName();
        String ext = "";
        boolean isPrefix = port.isPrefix();
        if (!isPrefix) {
            ext = getOutputType(i);
        }
        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.");
            }
            // if MimeType is "Inactive" just create Empty FileStore(Prefix)URIPortObjects
            PortObject fsupo;
            if (!ext.equals("Inactive")) {
                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());
                }
            } else {
                fsupo = InactiveBranchPortObject.INSTANCE;
            }
            // 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()) {
            // if MimeType is "Inactive" just create Empty FileStore(Prefix)URIPortObjects
            PortObject po;
            if (!ext.equals("Inactive")) {
                // 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);
                }
            } else {
                po = InactiveBranchPortObject.INSTANCE;
            }
            // 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) ExecutionFailedException(com.genericworkflownodes.knime.generic_node.ExecutionFailedException) 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 3 with FileStoreURIPortObject

use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject 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 4 with FileStoreURIPortObject

use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject in project GenericKnimeNodes by genericworkflownodes.

the class PortToFileStoreNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    IURIPortObject input = (IURIPortObject) inData[0];
    DataContainer dc = exec.createDataContainer(createSpec());
    /**
     * Files that are not yet managed by KNIME (e.g. when they come from an InputFiles node) come as URIPortObject
     * and must be copied into a FileStore to be handled properly.
     * Other files already have a file store and can be put into a <code>PortObjectHandlerCell</code>.
     */
    if (input instanceof AbstractFileStoreURIPortObject) {
        PortObjectHandlerCell cell = new PortObjectHandlerCell((AbstractFileStoreURIPortObject) input);
        dc.addRowToTable(new DefaultRow(new RowKey("files"), cell));
    } else {
        FileStore fs = exec.createFileStore("files");
        fs.getFile().mkdirs();
        for (URIContent uc : input.getURIContents()) {
            String filename = Paths.get(uc.getURI()).getFileName().toString();
            if (!filename.endsWith(uc.getExtension())) {
                filename = filename.concat(".").concat(uc.getExtension());
            }
            // TODO: Report progress
            Files.copy(Paths.get(uc.getURI()), Paths.get(fs.getFile().toURI()).resolve(filename));
            AbstractFileStoreURIPortObject portObject = new FileStoreURIPortObject(fs);
            portObject.registerFile(filename);
            PortObjectHandlerCell cell = new PortObjectHandlerCell(portObject);
            dc.addRowToTable(new DefaultRow(new RowKey(filename), cell));
        }
    }
    dc.close();
    return new PortObject[] { (BufferedDataTable) dc.getTable() };
}
Also used : FileStore(org.knime.core.data.filestore.FileStore) DataContainer(org.knime.core.data.container.DataContainer) PortObjectHandlerCell(com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell) AbstractFileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.AbstractFileStoreURIPortObject) AbstractFileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.AbstractFileStoreURIPortObject) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) RowKey(org.knime.core.data.RowKey) IURIPortObject(org.knime.core.data.uri.IURIPortObject) BufferedDataTable(org.knime.core.node.BufferedDataTable) DefaultRow(org.knime.core.data.def.DefaultRow) AbstractFileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.AbstractFileStoreURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) PortObject(org.knime.core.node.port.PortObject) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) URIContent(org.knime.core.data.uri.URIContent)

Example 5 with FileStoreURIPortObject

use of com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject in project GenericKnimeNodes by genericworkflownodes.

the class Image2FilePortNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    ImagePortObject imageObj = (ImagePortObject) inObjects[0];
    DataCell imageCellDC = imageObj.toDataCell();
    if (!(imageCellDC instanceof ImageValue)) {
        throw new InvalidSettingsException("Image object does not produce" + " valid image object but " + imageCellDC.getClass().getName());
    }
    ImageValue v = (ImageValue) imageCellDC;
    ImageContent content = v.getImageContent();
    // check if the extension matches our PNG assumption
    checkExtension(v);
    // write the file to stash
    FileStoreURIPortObject outPort = writeImageFile(content, exec);
    return new PortObject[] { outPort };
}
Also used : FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ImageContent(org.knime.core.data.image.ImageContent) DataCell(org.knime.core.data.DataCell) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) ImageValue(org.knime.core.data.image.ImageValue) IURIPortObject(org.knime.core.data.uri.IURIPortObject) PortObject(org.knime.core.node.port.PortObject) ImagePortObject(org.knime.core.node.port.image.ImagePortObject) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)

Aggregations

FileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)7 File (java.io.File)5 IURIPortObject (org.knime.core.data.uri.IURIPortObject)5 PortObject (org.knime.core.node.port.PortObject)5 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 ArrayList (java.util.ArrayList)3 BufferedDataTable (org.knime.core.node.BufferedDataTable)3 FileStorePrefixURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStorePrefixURIPortObject)2 IPrefixURIPortObject (com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject)2 PortObjectHandlerCell (com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell)2 NoBinaryAvailableException (com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException)2 UnknownCommandGeneratorException (com.genericworkflownodes.knime.execution.UnknownCommandGeneratorException)2 UnknownToolExecutorException (com.genericworkflownodes.knime.execution.UnknownToolExecutorException)2 FileListParameter (com.genericworkflownodes.knime.parameter.FileListParameter)2 FileParameter (com.genericworkflownodes.knime.parameter.FileParameter)2 IFileParameter (com.genericworkflownodes.knime.parameter.IFileParameter)2 InvalidParameterValueException (com.genericworkflownodes.knime.parameter.InvalidParameterValueException)2 Port (com.genericworkflownodes.knime.port.Port)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2