Search in sources :

Example 6 with FileStorePortObject

use of org.knime.core.data.filestore.FileStorePortObject in project knime-core by knime.

the class NodeModel method executeModel.

/**
 * Invokes the abstract <code>#execute()</code> method of this model. In
 * addition, this method notifies all assigned views of the model about the
 * changes.
 *
 * @param rawData An array of <code>PortObject</code> objects holding the data
 *            from the inputs (includes flow variable port).
 * @param exEnv The execution environment used for execution of this model.
 * @param exec The execution monitor which is passed to the execute method
 *            of this model.
 * @return The result of the execution in form of an array with
 *         <code>DataTable</code> elements, as many as the node has
 *         outputs.
 * @throws Exception any exception or error that is fired in the derived
 *             model will be just forwarded. It may throw an
 *             CanceledExecutionException if the user pressed cancel during
 *             execution. Even if the derived model doesn't check, the
 *             result will be discarded and the exception thrown.
 * @throws IllegalStateException If the number of <code>PortObject</code>
 *             objects returned by the derived <code>NodeModel</code>
 *             does not match the number of outputs. Or if any of them is
 *             null.
 * @see #execute(PortObject[],ExecutionContext)
 * @since 2.8
 * @noreference This method is not intended to be referenced by clients
 *              (use Node class instead)
 */
PortObject[] executeModel(final PortObject[] rawData, final ExecutionEnvironment exEnv, final ExecutionContext exec) throws Exception {
    final PortObject[] data = ArrayUtils.remove(rawData, 0);
    assert (data != null && data.length == getNrInPorts());
    assert (exec != null);
    setWarningMessage(null);
    // check for compatible input PortObjects
    for (int i = 0; i < data.length; i++) {
        PortType thisType = getInPortType(i);
        if (thisType.isOptional() && data[i] == null) {
        // ignore non-populated optional input
        } else if (data[i] instanceof InactiveBranchPortObject) {
            assert this instanceof InactiveBranchConsumer;
        // allow Inactive POs at InactiveBranchConsumer
        } else if (!(thisType.getPortObjectClass().isInstance(data[i]))) {
            m_logger.error("  (Wanted: " + thisType.getPortObjectClass().getName() + ", " + "actual: " + data[i].getClass().getName() + ")");
            throw new IllegalStateException("Connection Error: Mismatch" + " of input port types (port " + (i) + ").");
        }
    }
    // temporary storage for result of derived model.
    // EXECUTE DERIVED MODEL
    PortObject[] outData;
    if (!exEnv.reExecute()) {
        outData = execute(data, exec);
    } else {
        // FIXME: implement reexecution with loading view content and execute
        if (this instanceof InteractiveNode) {
            InteractiveNode iThis = (InteractiveNode) this;
            ViewContent viewContent = exEnv.getPreExecuteViewContent();
            iThis.loadViewValue(viewContent, exEnv.getUseAsDefault());
            outData = execute(data, exec);
        } else if (this instanceof LoopStartNode) {
            outData = execute(data, exec);
        } else {
            m_logger.coding("Cannot re-execute non interactive node. Using normal execute instead.");
            outData = execute(data, exec);
        }
    }
    // if execution was canceled without exception flying return false
    if (exec.isCanceled()) {
        throw new CanceledExecutionException("Result discarded due to user cancel");
    }
    if (outData == null) {
        outData = new PortObject[getNrOutPorts()];
    }
    /* Cleanup operation for nodes that just pass on their input
         * data table. We need to wrap those here so that the framework
         * explicitly references them (instead of copying) */
    for (int i = 0; i < outData.length; i++) {
        if (outData[i] instanceof BufferedDataTable) {
            for (int j = 0; j < data.length; j++) {
                if (outData[i] == data[j]) {
                    outData[i] = exec.createWrappedTable((BufferedDataTable) data[j]);
                }
            }
        } else if (outData[i] instanceof FileStorePortObject) {
            // file stores can be 'external', e.g. when a model reader node reads an external model file
            FileStorePortObject fsPO = (FileStorePortObject) outData[i];
            FileStoreHandlerRepository expectedRep = exec.getFileStoreHandler().getFileStoreHandlerRepository();
            FileStoreHandlerRepository actualRep = FileStoreUtil.getFileStores(fsPO).stream().map(FileStoreUtil::getFileStoreHandler).map(h -> h.getFileStoreHandlerRepository()).findFirst().orElse(expectedRep);
            if (actualRep != expectedRep) {
                outData[i] = Node.copyPortObject(fsPO, exec);
            }
        }
    }
    // if number of out tables does not match: fail
    if (outData.length != getNrOutPorts()) {
        throw new IllegalStateException("Invalid result. Execution failed. " + "Reason: Incorrect implementation; the execute" + " method in " + this.getClass().getSimpleName() + " returned null or an incorrect number of output" + " tables.");
    }
    // check the result, data tables must not be null
    for (int i = 0; i < outData.length; i++) {
        // of a loop and another loop iteration is requested
        if ((getLoopContext() == null) && (outData[i] == null)) {
            m_logger.error("Execution failed: Incorrect implementation;" + " the execute method in " + this.getClass().getSimpleName() + "returned a null data table at port: " + i);
            throw new IllegalStateException("Invalid result. " + "Execution failed, reason: data at output " + i + " is null.");
        }
    }
    // - only if the execute didn't issue a warning already
    if ((m_warningMessage == null) || (m_warningMessage.length() == 0)) {
        boolean hasData = false;
        // number of BDT ports
        int bdtPortCount = 0;
        for (int i = 0; i < outData.length; i++) {
            if (outData[i] instanceof BufferedDataTable) {
                // do some sanity checks on PortObjects holding data tables
                bdtPortCount += 1;
                BufferedDataTable outDataTable = (BufferedDataTable) outData[i];
                if (outDataTable.size() > 0) {
                    hasData = true;
                } else {
                    m_logger.info("The result table at port " + i + " contains no rows");
                }
            }
        }
        if (!hasData && bdtPortCount > 0) {
            if (bdtPortCount == 1) {
                setWarningMessage("Node created an empty data table.");
            } else {
                setWarningMessage("Node created empty data tables on all out-ports.");
            }
        }
    }
    setHasContent(true);
    PortObject[] rawOutData = new PortObject[getNrOutPorts() + 1];
    rawOutData[0] = FlowVariablePortObject.INSTANCE;
    System.arraycopy(outData, 0, rawOutData, 1, outData.length);
    return rawOutData;
}
Also used : Arrays(java.util.Arrays) FileStoreUtil(org.knime.core.data.filestore.FileStoreUtil) InteractiveNode(org.knime.core.node.interactive.InteractiveNode) ExecutionEnvironment(org.knime.core.node.workflow.ExecutionEnvironment) Map(java.util.Map) FlowTryCatchContext(org.knime.core.node.workflow.FlowTryCatchContext) InactiveBranchPortObjectSpec(org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec) FileStoreHandlerRepository(org.knime.core.data.filestore.internal.FileStoreHandlerRepository) PortType(org.knime.core.node.port.PortType) PortInput(org.knime.core.node.streamable.PortInput) PartitionInfo(org.knime.core.node.streamable.PartitionInfo) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) PortObjectOutput(org.knime.core.node.streamable.PortObjectOutput) InputPortRole(org.knime.core.node.streamable.InputPortRole) HiLiteHandler(org.knime.core.node.property.hilite.HiLiteHandler) PortObject(org.knime.core.node.port.PortObject) RowOutput(org.knime.core.node.streamable.RowOutput) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) DataTableSpec(org.knime.core.data.DataTableSpec) LoopEndNode(org.knime.core.node.workflow.LoopEndNode) FlowVariable(org.knime.core.node.workflow.FlowVariable) PortObjectInput(org.knime.core.node.streamable.PortObjectInput) ArrayUtils(org.apache.commons.lang3.ArrayUtils) LinkedHashMap(java.util.LinkedHashMap) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) CredentialsProvider(org.knime.core.node.workflow.CredentialsProvider) NoSuchElementException(java.util.NoSuchElementException) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) FlowObjectStack(org.knime.core.node.workflow.FlowObjectStack) ScopeStartNode(org.knime.core.node.workflow.ScopeStartNode) RowInput(org.knime.core.node.streamable.RowInput) InteractiveView(org.knime.core.node.interactive.InteractiveView) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) FlowScopeContext(org.knime.core.node.workflow.FlowScopeContext) IOException(java.io.IOException) InactiveBranchConsumer(org.knime.core.node.port.inactive.InactiveBranchConsumer) OutputPortRole(org.knime.core.node.streamable.OutputPortRole) NodeContext(org.knime.core.node.workflow.NodeContext) File(java.io.File) ViewContent(org.knime.core.node.interactive.ViewContent) PortOutput(org.knime.core.node.streamable.PortOutput) ICredentials(org.knime.core.node.workflow.ICredentials) LoopStartNode(org.knime.core.node.workflow.LoopStartNode) FlowLoopContext(org.knime.core.node.workflow.FlowLoopContext) Collections(java.util.Collections) MergeOperator(org.knime.core.node.streamable.MergeOperator) ViewableModel(org.knime.core.node.AbstractNodeView.ViewableModel) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FileStoreHandlerRepository(org.knime.core.data.filestore.internal.FileStoreHandlerRepository) InactiveBranchConsumer(org.knime.core.node.port.inactive.InactiveBranchConsumer) FileStoreUtil(org.knime.core.data.filestore.FileStoreUtil) InteractiveNode(org.knime.core.node.interactive.InteractiveNode) ViewContent(org.knime.core.node.interactive.ViewContent) LoopStartNode(org.knime.core.node.workflow.LoopStartNode) PortObject(org.knime.core.node.port.PortObject) FileStorePortObject(org.knime.core.data.filestore.FileStorePortObject) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) PortType(org.knime.core.node.port.PortType)

Aggregations

File (java.io.File)6 FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)6 IOException (java.io.IOException)4 FileStoreKey (org.knime.core.data.filestore.FileStoreKey)4 BufferedInputStream (java.io.BufferedInputStream)3 ReferencedFile (org.knime.core.internal.ReferencedFile)3 PortObject (org.knime.core.node.port.PortObject)3 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)3 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)3 InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)3 InactiveBranchPortObjectSpec (org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)3 BufferedOutputStream (java.io.BufferedOutputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileInputStream (java.io.FileInputStream)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 ZipEntry (java.util.zip.ZipEntry)2 NotInWorkflowWriteFileStoreHandler (org.knime.core.data.filestore.internal.NotInWorkflowWriteFileStoreHandler)2 PortObjectSpecZipInputStream (org.knime.core.node.port.PortObjectSpecZipInputStream)2 PortObjectSpecZipOutputStream (org.knime.core.node.port.PortObjectSpecZipOutputStream)2