Search in sources :

Example 6 with InactiveBranchPortObject

use of org.knime.core.node.port.inactive.InactiveBranchPortObject 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)

Example 7 with InactiveBranchPortObject

use of org.knime.core.node.port.inactive.InactiveBranchPortObject in project knime-core by knime.

the class CaseEndNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    PortObject result = InactiveBranchPortObject.INSTANCE;
    MultipleActiveHandling h = MultipleActiveHandling.valueOf(m_multipleActiveHandlingSettingsModel.getStringValue());
    for (int i = 0, l = inData.length; i < l; i++) {
        if (inData[i] != null && !(inData[i] instanceof InactiveBranchPortObject)) {
            if (result instanceof InactiveBranchPortObject) {
                // only assign the first, never any subsequent
                result = inData[i];
            } else if (h.equals(MultipleActiveHandling.Fail)) {
                throw new InvalidSettingsException("Multiple inputs are active - causing node to fail. " + "You can change this behavior in the node configuration dialog.");
            }
        }
    }
    return new PortObject[] { result };
}
Also used : InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) PortObject(org.knime.core.node.port.PortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject)

Example 8 with InactiveBranchPortObject

use of org.knime.core.node.port.inactive.InactiveBranchPortObject in project knime-core by knime.

the class VirtualSubNodeInputNodeModel method executeModel.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] executeModel(final PortObject[] rawData, final ExecutionEnvironment exEnv, final ExecutionContext exec) throws Exception {
    CheckUtils.checkNotNull(m_subNodeContainer, "No Wrapped Metanode container set");
    PortObject[] dataFromParent = m_subNodeContainer.fetchInputDataFromParent();
    if (dataFromParent == null) {
        setWarningMessage("Not all inputs available");
        Thread.currentThread().interrupt();
        return null;
    }
    // a node is marked as inactive if any of its inputs is inactive, including the flow variable port to which
    // the plain "execute" method has no access.
    boolean containsInactive = false;
    for (PortObject o : dataFromParent) {
        if (o instanceof InactiveBranchPortObject) {
            containsInactive = true;
            break;
        }
    }
    if (containsInactive) {
        PortObject[] clone = ArrayUtils.clone(dataFromParent);
        Arrays.fill(clone, InactiveBranchPortObject.INSTANCE);
        return clone;
    } else {
        return super.executeModel(rawData, exEnv, exec);
    }
}
Also used : InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) PortObject(org.knime.core.node.port.PortObject)

Aggregations

InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)8 PortObject (org.knime.core.node.port.PortObject)7 IOException (java.io.IOException)3 Map (java.util.Map)3 DataTableSpec (org.knime.core.data.DataTableSpec)3 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)3 FlowTryCatchContext (org.knime.core.node.workflow.FlowTryCatchContext)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 RowKey (org.knime.core.data.RowKey)2 AppendedRowsIterator (org.knime.core.data.append.AppendedRowsIterator)2 AppendedRowsTable (org.knime.core.data.append.AppendedRowsTable)2 FileStorePortObject (org.knime.core.data.filestore.FileStorePortObject)2 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 PortObjectSpec (org.knime.core.node.port.PortObjectSpec)2 PortType (org.knime.core.node.port.PortType)2 InactiveBranchPortObjectSpec (org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec)2