Search in sources :

Example 1 with IURIPortObject

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

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

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

the class ListZipLoopEndNodeModel method execute.

@Override
protected PortObject[] execute(PortObject[] inObjects, ExecutionContext exec) {
    if (!(getLoopStartNode() instanceof LoopStartNodeTerminator)) {
        throw new IllegalStateException("Loop End is not connected" + " to matching/corresponding Loop Start node. You" + " are trying to create an infinite loop!");
    }
    if (!m_loopStarted) {
        // first time we are getting to this: create container
        m_uris = new ArrayList<List<URIContent>>(PORT_COUNT);
        m_bufferedContainers = new BufferedDataContainer[PORT_COUNT];
        for (int i = 0; i < PORT_COUNT; ++i) {
            // create data container
            m_bufferedContainers[i] = exec.createDataContainer(createPseudoSpec());
            // create container to collect the incoming uris
            m_uris.add(new ArrayList<URIContent>());
        }
        m_loopStarted = true;
    }
    for (int i = 0; i < PORT_COUNT; i++) {
        if (inObjects[i] == null) {
            // skip unconnected ports
            continue;
        }
        IURIPortObject po = (IURIPortObject) inObjects[i];
        // some data we need
        int currentIteration = peekFlowVariableInt("currentIteration");
        if (po.getURIContents().size() > 1) {
            LOGGER.warn(String.format("More then one incoming object at port %d. The outgoing port will only hold the first one.", i));
        }
        // register file uri
        m_uris.get(i).add(po.getURIContents().get(0));
        // AbstractFileStoreURIPortObject
        if (po instanceof AbstractFileStoreURIPortObject) {
            PortObjectHandlerCell pfsc = new PortObjectHandlerCell((AbstractFileStoreURIPortObject) po);
            String rowKey = String.format("Row_%d_%d", i, currentIteration);
            m_bufferedContainers[i].addRowToTable(new DefaultRow(rowKey, pfsc));
        }
    }
    // check if this is the last iteration
    if (((LoopStartNodeTerminator) getLoopStartNode()).terminateLoop()) {
        URIPortObject[] portObjects = new URIPortObject[PORT_COUNT];
        for (int i = 0; i < PORT_COUNT; i++) {
            // assign collected uris to new portobject
            portObjects[i] = new URIPortObject(m_uris.get(i));
            // close the container
            m_bufferedContainers[i].close();
        }
        m_loopStarted = false;
        return portObjects;
    } else {
        continueLoop();
        return new PortObject[PORT_COUNT];
    }
}
Also used : AbstractFileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.AbstractFileStoreURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) PortObjectHandlerCell(com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell) AbstractFileStoreURIPortObject(com.genericworkflownodes.knime.base.data.port.AbstractFileStoreURIPortObject) IURIPortObject(org.knime.core.data.uri.IURIPortObject) LoopStartNodeTerminator(org.knime.core.node.workflow.LoopStartNodeTerminator) ArrayList(java.util.ArrayList) List(java.util.List) 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) URIContent(org.knime.core.data.uri.URIContent)

Example 4 with IURIPortObject

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

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

the class FileMergerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    List<URIContent> outPutURIs = new ArrayList<URIContent>();
    // collect all files from the input ports
    for (PortObject inPort : inData) {
        outPutURIs.addAll(((IURIPortObject) inPort).getURIContents());
    }
    URIPortObject outPort = new URIPortObject(outPutURIs);
    return new PortObject[] { outPort };
}
Also used : IURIPortObject(org.knime.core.data.uri.IURIPortObject) URIPortObject(org.knime.core.data.uri.URIPortObject) ArrayList(java.util.ArrayList) 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)

Aggregations

IURIPortObject (org.knime.core.data.uri.IURIPortObject)14 URIContent (org.knime.core.data.uri.URIContent)11 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 File (java.io.File)7 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 PortObject (org.knime.core.node.port.PortObject)6 URIPortObject (org.knime.core.data.uri.URIPortObject)5 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)4 PortObjectHandlerCell (com.genericworkflownodes.knime.base.data.port.PortObjectHandlerCell)3 URI (java.net.URI)3 List (java.util.List)3 DefaultRow (org.knime.core.data.def.DefaultRow)3 FileStore (org.knime.core.data.filestore.FileStore)3 BufferedDataTable (org.knime.core.node.BufferedDataTable)3 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)3 AbstractFileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.AbstractFileStoreURIPortObject)2 FileStoreURIPortObject (com.genericworkflownodes.knime.base.data.port.FileStoreURIPortObject)2 IPrefixURIPortObject (com.genericworkflownodes.knime.base.data.port.IPrefixURIPortObject)2 NoBinaryAvailableException (com.genericworkflownodes.knime.custom.config.NoBinaryAvailableException)2