Search in sources :

Example 1 with URIPortObject

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

the class ListZipLoopStartNodeModel method execute.

@Override
protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) throws InvalidSettingsException {
    // check the loop conditions
    if (m_iteration == 0) {
        assert getLoopEndNode() == null : "1st iteration but end node set";
        // check the content of the different Ports
        if (!m_reuse.getBooleanValue()) {
            int numberOfURIs = ((IURIPortObject) inObjects[0]).getURIContents().size();
            for (int i = 1; i < m_numAssignedIncomingPorts; ++i) {
                if (((IURIPortObject) inObjects[i]).getURIContents().size() != numberOfURIs) {
                    throw new InvalidSettingsException("Invalid settings. The number of URIs at the incoming ports differ.");
                }
            }
        }
    } else {
        assert getLoopEndNode() != null : "No end node set";
    }
    IURIPortObject[] uriOutputObjects = new URIPortObject[PORT_COUNT];
    m_rowCount = ((IURIPortObject) inObjects[0]).getURIContents().size();
    // 1st port is handled separately
    URIContent uri = ((IURIPortObject) inObjects[0]).getURIContents().get(m_iteration);
    List<URIContent> uriContents = new ArrayList<URIContent>();
    uriContents.add(uri);
    uriOutputObjects[0] = new URIPortObject(uriContents);
    for (int i = 1; i < PORT_COUNT; i++) {
        IURIPortObject in = (IURIPortObject) inObjects[i];
        if (i < m_numAssignedIncomingPorts) {
            if (m_reuse.getBooleanValue()) {
                uriOutputObjects[i] = new URIPortObject(in.getURIContents());
            } else {
                List<URIContent> localUriContents = new ArrayList<URIContent>();
                URIContent localUri = in.getURIContents().get(m_iteration);
                localUriContents.add(localUri);
                uriOutputObjects[i] = new URIPortObject(localUriContents);
            }
        } else {
            uriOutputObjects[i] = new URIPortObject(new ArrayList<URIContent>());
        }
    }
    // TODO: check if this is necessary
    pushFlowVariableInt("currentIteration", m_iteration);
    pushFlowVariableInt("maxIterations", m_rowCount);
    // proceed in the number of iterations
    m_iteration++;
    return uriOutputObjects;
}
Also used : IURIPortObject(org.knime.core.data.uri.IURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) IURIPortObject(org.knime.core.data.uri.IURIPortObject) ArrayList(java.util.ArrayList) URIContent(org.knime.core.data.uri.URIContent)

Example 2 with URIPortObject

use of org.knime.core.data.uri.URIPortObject 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];
    URIPortObject[] outputs = new URIPortObject[getOutgoing().length];
    for (int i = 0; i < outputs.length - 1 && input.getURIContents().size() > i; i++) {
        outputs[i] = new URIPortObject(Arrays.asList(input.getURIContents().get(i)));
    }
    if (input.getURIContents().size() > getOutgoing().length - 1) {
        outputs[outputs.length - 1] = new URIPortObject(input.getURIContents().subList(outputs.length - 1, input.getURIContents().size()));
    } else {
        for (int i = input.getURIContents().size(); i < outputs.length; i++) {
            outputs[i] = new URIPortObject(new ArrayList<URIContent>());
        }
    }
    return outputs;
}
Also used : IURIPortObject(org.knime.core.data.uri.IURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) ArrayList(java.util.ArrayList)

Example 3 with URIPortObject

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

the class SplitTableToPortNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable input = (BufferedDataTable) inData[0];
    List<URIContent> contents = new ArrayList<>();
    String mimetype = null;
    int index = input.getDataTableSpec().findColumnIndex(m_fileCol.getColumnName());
    for (DataRow row : input) {
        PortObjectHandlerCell cell = (PortObjectHandlerCell) row.getCell(index);
        URIContent uc = cell.getURIContent();
        String mt = MIMETypeHelper.getMIMEtypeByExtension(uc.getExtension());
        if (mimetype == null) {
            mimetype = mt;
        } else if (!mt.equals(mimetype)) {
            throw new InvalidSettingsException("File ports do not support mixed mimetypes.");
        }
        contents.add(uc);
    }
    URIPortObject output = new URIPortObject(contents);
    return new PortObject[] { output };
}
Also used : URIPortObject(org.knime.core.data.uri.URIPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) FileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject) PortObjectHandlerCell(com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) BufferedDataTable(org.knime.core.node.BufferedDataTable) ArrayList(java.util.ArrayList) DataRow(org.knime.core.data.DataRow) 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 4 with URIPortObject

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

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

the class ListMimeFileImporterNodeModel method execute.

@Override
protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) throws Exception {
    String[] filenames = m_filenames.getStringArrayValue();
    List<URIContent> uris = new ArrayList<URIContent>();
    for (String filename : filenames) {
        File in = new File(convertToURL(filename).toURI());
        if (!in.canRead()) {
            throw new Exception("Cannot read from input file: " + in.getAbsolutePath());
        }
        uris.add(new URIContent(in.toURI(), (m_file_extension.isActive() ? m_file_extension.getStringValue() : MIMETypeHelper.getMIMEtypeExtension(filename))));
    }
    return new PortObject[] { new URIPortObject(uris) };
}
Also used : IURIPortObject(org.knime.core.data.uri.IURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) ArrayList(java.util.ArrayList) SettingsModelOptionalString(org.knime.core.node.defaultnodesettings.SettingsModelOptionalString) File(java.io.File) IURIPortObject(org.knime.core.data.uri.IURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) PortObject(org.knime.core.node.port.PortObject) URIContent(org.knime.core.data.uri.URIContent) MalformedURLException(java.net.MalformedURLException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException)

Aggregations

IURIPortObject (org.knime.core.data.uri.IURIPortObject)7 URIPortObject (org.knime.core.data.uri.URIPortObject)7 ArrayList (java.util.ArrayList)6 URIContent (org.knime.core.data.uri.URIContent)6 PortObject (org.knime.core.node.port.PortObject)5 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)4 File (java.io.File)3 FileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)2 PortObjectHandlerCell (com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 AbstractFileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.AbstractFileStoreURIPortObject)1 URI (java.net.URI)1 ZipFile (java.util.zip.ZipFile)1 ConnectionInformationPortObject (org.knime.base.filehandling.remote.connectioninformation.port.ConnectionInformationPortObject)1 ConnectionMonitor (org.knime.base.filehandling.remote.files.ConnectionMonitor)1 RemoteFile (org.knime.base.filehandling.remote.files.RemoteFile)1 DataRow (org.knime.core.data.DataRow)1