Search in sources :

Example 6 with URIContent

use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.

the class DynamicGenericNodeModel 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) {
            ((FileParameter) 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) 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) 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 7 with URIContent

use of org.knime.core.data.uri.URIContent 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 8 with URIContent

use of org.knime.core.data.uri.URIContent 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 9 with URIContent

use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.

the class AbstractFileStoreURIPortObject method load.

/**
 * Reconstruct the {@link AbstractFileStoreURIPortObject} from the given
 * {@link ModelContentRO}.
 *
 * @param model
 *            The {@link ModelContentRO} from where the object should be
 *            reconstructed.
 * @param spec
 *            The expected {@link PortObjectSpec}.
 * @param exec
 *            The current {@link ExecutionContext}.
 * @throws InvalidSettingsException
 *             Thrown if the content is invalid.
 */
void load(final ModelContentRO model, PortObjectSpec spec, ExecutionMonitor exec) throws InvalidSettingsException {
    List<URIContent> list = new ArrayList<URIContent>();
    List<String> relPathList = new ArrayList<String>();
    for (String key : model.keySet()) {
        if (key.startsWith("file-")) {
            ModelContentRO child = model.getModelContent(key);
            list.add(URIContent.load(child));
            relPathList.add(child.getString(SETTINGS_KEY_REL_PATH));
        }
    }
    m_uriContents = list;
    m_relPaths = relPathList;
    m_uriPortObjectSpec = (URIPortObjectSpec) spec;
}
Also used : ModelContentRO(org.knime.core.node.ModelContentRO) ArrayList(java.util.ArrayList) URIContent(org.knime.core.data.uri.URIContent)

Example 10 with URIContent

use of org.knime.core.data.uri.URIContent in project GenericKnimeNodes by genericworkflownodes.

the class AbstractFileStoreURIPortObject method registerFile.

/**
 * Adds the given file to the {@link FileStoreURIPortObject}.
 *
 * @param filename
 *            The relative path that should be stored inside the file-store,
 *            e.g., outfile.txt or subfolder/outfile.txt.
 * @return A {@link File} object pointing to the registered file.
 */
public File registerFile(String filename) {
    // register the URIContent
    File child = new File(getFileStoreRootDirectory(), filename);
    URIContent uric = new URIContent(child.toURI(), MIMETypeHelper.getMIMEtypeExtension(filename));
    // update content and spec accordingly
    m_uriContents.add(uric);
    m_uriPortObjectSpec = URIPortObjectSpec.create(m_uriContents);
    m_relPaths.add(filename);
    // give the file object to the client so he can work with it
    return child;
}
Also used : File(java.io.File) URIContent(org.knime.core.data.uri.URIContent)

Aggregations

URIContent (org.knime.core.data.uri.URIContent)21 File (java.io.File)13 ArrayList (java.util.ArrayList)13 IURIPortObject (org.knime.core.data.uri.IURIPortObject)13 IOException (java.io.IOException)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)10 URIPortObject (org.knime.core.data.uri.URIPortObject)6 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)6 PortObject (org.knime.core.node.port.PortObject)6 URI (java.net.URI)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 FileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)3 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 FileParameter (com.genericworkflownodes.knime.parameter.FileParameter)2 IFileParameter (com.genericworkflownodes.knime.parameter.IFileParameter)2 InvalidParameterValueException (com.genericworkflownodes.knime.parameter.InvalidParameterValueException)2