Search in sources :

Example 1 with InactiveBranchPortObject

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

the class EndifNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] rawInData, final ExecutionContext exec) throws Exception {
    if (m_enableHiliting) {
        // create empty hilite translation map (so we correctly
        // handle the internals even if we return with a IBPO:
        Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
        m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
    }
    if (rawInData[0] instanceof InactiveBranchPortObject) {
        return new PortObject[] { rawInData[1] };
    }
    if (rawInData[1] instanceof InactiveBranchPortObject) {
        return new PortObject[] { rawInData[0] };
    }
    // no inactive branch - check compatibility of specs - which in
    // this case must be BFT Specs!
    DataTableSpec spec0 = (DataTableSpec) (rawInData[0].getSpec());
    DataTableSpec spec1 = (DataTableSpec) (rawInData[1].getSpec());
    if (spec0.equalStructure(spec1)) {
        // concatenate tables and return result
        BufferedDataTable[] inData = new BufferedDataTable[2];
        inData[0] = (BufferedDataTable) rawInData[0];
        inData[1] = (BufferedDataTable) rawInData[1];
        int totalRowCount = 0;
        for (BufferedDataTable t : inData) {
            totalRowCount += t.getRowCount();
        }
        AppendedRowsTable out = new AppendedRowsTable((m_isAppendSuffix ? m_suffix : null), inData);
        // note, this iterator throws runtime exceptions when canceled.
        AppendedRowsIterator it = out.iterator(exec, totalRowCount);
        BufferedDataContainer c = exec.createDataContainer(out.getDataTableSpec());
        try {
            while (it.hasNext()) {
                // may throw exception, also sets progress
                c.addRowToTable(it.next());
            }
        } catch (AppendedRowsIterator.RuntimeCanceledExecutionException rcee) {
            throw rcee.getCause();
        } finally {
            c.close();
        }
        if (it.getNrRowsSkipped() > 0) {
            setWarningMessage("Filtered out " + it.getNrRowsSkipped() + " duplicate row id(s).");
        }
        if (m_enableHiliting) {
            // create hilite translation map
            Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
            // map of all RowKeys and duplicate RowKeys in the resulting table
            Map<RowKey, RowKey> dupMap = it.getDuplicateNameMap();
            for (Map.Entry<RowKey, RowKey> e : dupMap.entrySet()) {
                // if a duplicate key
                if (!e.getKey().equals(e.getValue())) {
                    Set<RowKey> set = Collections.singleton(e.getValue());
                    // put duplicate key and original key into map
                    map.put(e.getKey(), set);
                } else {
                    // skip duplicate keys
                    if (!dupMap.containsKey(new RowKey(e.getKey().getString() + m_suffix))) {
                        Set<RowKey> set = Collections.singleton(e.getValue());
                        map.put(e.getKey(), set);
                    }
                }
            }
            m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
        }
        return new BufferedDataTable[] { c.getTable() };
    }
    throw new Exception("Both input ports have data but the tables " + "have incompatible specs");
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) Set(java.util.Set) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) HashMap(java.util.HashMap) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) AppendedRowsIterator(org.knime.core.data.append.AppendedRowsIterator) AppendedRowsTable(org.knime.core.data.append.AppendedRowsTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) PortObject(org.knime.core.node.port.PortObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with InactiveBranchPortObject

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

the class EndcaseNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    Vector<BufferedDataTable> tables = new Vector<BufferedDataTable>();
    for (int i = 0; i < getNrInPorts(); i++) {
        if (inData[i] != null) {
            // if connected...
            if (!(inData[i] instanceof InactiveBranchPortObject)) {
                // ...and active, add it:
                tables.add((BufferedDataTable) inData[i]);
            }
        }
    }
    if (tables.size() == 0) {
        // be connected!)
        assert inData[0] instanceof InactiveBranchPortObject;
        if (m_enableHiliting) {
            // create empty hilite translation map (so we correctly
            // handle the internals).
            Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
            m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
        }
        return new PortObject[] { inData[0] };
    }
    assert tables.size() > 0;
    // check compatibility of specs against first spec in list
    for (int i = 1; i < tables.size(); i++) {
        if (!(tables.get(0).getSpec().equalStructure(tables.get(i).getSpec()))) {
            // incompatible - refuse to execute
            throw new Exception("The data table structures of the active " + "ports are not compatible.");
        }
    }
    int totalRowCount = 0;
    DataTable[] dtables = new DataTable[tables.size()];
    int i = 0;
    for (BufferedDataTable t : tables) {
        totalRowCount += t.getRowCount();
        dtables[i] = t;
        i++;
    }
    AppendedRowsTable out = new AppendedRowsTable((m_isAppendSuffix ? m_suffix : null), dtables);
    // note, this iterator throws runtime exceptions when canceled.
    AppendedRowsIterator it = out.iterator(exec, totalRowCount);
    BufferedDataContainer c = exec.createDataContainer(out.getDataTableSpec());
    try {
        while (it.hasNext()) {
            // may throw exception, also sets progress
            c.addRowToTable(it.next());
        }
    } catch (RuntimeCanceledExecutionException rcee) {
        throw rcee.getCause();
    } finally {
        c.close();
    }
    if (it.getNrRowsSkipped() > 0) {
        setWarningMessage("Filtered out " + it.getNrRowsSkipped() + " duplicate row id(s).");
    }
    if (m_enableHiliting) {
        // create hilite translation map
        Map<RowKey, Set<RowKey>> map = new HashMap<RowKey, Set<RowKey>>();
        // map of all RowKeys and duplicate RowKeys in the resulting table
        Map<RowKey, RowKey> dupMap = it.getDuplicateNameMap();
        for (Map.Entry<RowKey, RowKey> e : dupMap.entrySet()) {
            // if a duplicate key
            if (!e.getKey().equals(e.getValue())) {
                Set<RowKey> set = Collections.singleton(e.getValue());
                // put duplicate key and original key into map
                map.put(e.getKey(), set);
            } else {
                // skip duplicate keys
                if (!dupMap.containsKey(new RowKey(e.getKey().getString() + m_suffix))) {
                    Set<RowKey> set = Collections.singleton(e.getValue());
                    map.put(e.getKey(), set);
                }
            }
        }
        m_hiliteTranslator.setMapper(new DefaultHiLiteMapper(map));
    }
    return new BufferedDataTable[] { c.getTable() };
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) Set(java.util.Set) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) HashMap(java.util.HashMap) RuntimeCanceledExecutionException(org.knime.base.data.append.row.AppendedRowsIterator.RuntimeCanceledExecutionException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) IOException(java.io.IOException) AppendedRowsIterator(org.knime.core.data.append.AppendedRowsIterator) AppendedRowsTable(org.knime.core.data.append.AppendedRowsTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) RuntimeCanceledExecutionException(org.knime.base.data.append.row.AppendedRowsIterator.RuntimeCanceledExecutionException) Vector(java.util.Vector) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) PortObject(org.knime.core.node.port.PortObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with InactiveBranchPortObject

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

the class GenericCatchNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
    if (!(inData[0] instanceof InactiveBranchPortObject)) {
        // main branch is active - no failure so far...
        return new PortObject[] { inData[0], FlowVariablePortObject.INSTANCE };
    }
    // main branch inactive, grab spec from alternative (default) input
    // and push error reasons on stack (they come from the ScopeObject
    // which will we removed after this node, closing the scope).
    FlowTryCatchContext ftcc = getFlowContext();
    if ((ftcc != null) && (ftcc.hasErrorCaught())) {
        pushFlowVariableString("FailingNode", ftcc.getNode());
        pushFlowVariableString("FailingNodeMessage", ftcc.getReason());
        pushFlowVariableString("FailingNodeStackTrace", ftcc.getStacktrace());
    } else if (m_alwaysPopulate.getBooleanValue()) {
        pushFlowVariableString("FailingNode", m_defaultVariable.getStringValue());
        pushFlowVariableString("FailingNodeMessage", m_defaultText.getStringValue());
        pushFlowVariableString("FailingNodeStackTrace", m_defaultStackTrace.getStringValue());
    }
    return new PortObject[] { inData[1], FlowVariablePortObject.INSTANCE };
}
Also used : InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) FlowTryCatchContext(org.knime.core.node.workflow.FlowTryCatchContext) FlowVariablePortObject(org.knime.core.node.port.flowvariable.FlowVariablePortObject) PortObject(org.knime.core.node.port.PortObject) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject)

Example 4 with InactiveBranchPortObject

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

the class Node method setOutPortObjects.

/**
 * Called after execute in order to put the computed result into the
 * outports. It will do a sequence of sanity checks whether the argument
 * is valid (non-null, correct type, etc.)
 * @param newOutData The computed output data
 * @param tolerateNullOutports used e.g. when loop is continued (outports may not yet be available)
 * @param tolerateDifferentSpecs used e.g. when re-executing a node (table may be different from configure)
 * @return Whether that is successful (false in case of incompatible port objects)
 */
private boolean setOutPortObjects(final PortObject[] newOutData, final boolean tolerateNullOutports, final boolean tolerateDifferentSpecs) {
    CheckUtils.checkArgumentNotNull(newOutData, "Port object array is null");
    if (newOutData.length != getNrOutPorts()) {
        throw new IndexOutOfBoundsException("Array is expected to be of " + "length " + getNrOutPorts() + ": " + newOutData.length);
    }
    // check for compatible output PortObjects
    for (int i = 0; i < newOutData.length; i++) {
        PortType thisType = m_outputs[i].type;
        if (newOutData[i] == null && !tolerateNullOutports) {
            createErrorMessageAndNotify("Output at port " + i + " is null");
            return false;
        }
        if (newOutData[i] != null) {
            if (newOutData[i] instanceof InactiveBranchPortObject) {
            // allow PO coming from inactive branch
            // TODO ensure model was skipped during configure?
            } else if (!thisType.getPortObjectClass().isInstance(newOutData[i])) {
                createErrorMessageAndNotify("Invalid output port object at port " + i);
                LOGGER.error("  (Wanted: " + thisType.getPortObjectClass().getName() + ", " + "actual: " + newOutData[i].getClass().getName() + ")");
                return false;
            }
            PortObjectSpec spec;
            try {
                spec = newOutData[i].getSpec();
            } catch (Throwable t) {
                createErrorMessageAndNotify("PortObject \"" + newOutData[i].getClass().getName() + "\" threw " + t.getClass().getSimpleName() + " on #getSpec() ", t);
                return false;
            }
            if (spec == null) {
                createErrorMessageAndNotify("Implementation Error: PortObject \"" + newOutData[i].getClass().getName() + "\" must not" + " have null spec (output port " + i + ").");
                return false;
            }
        }
    }
    for (int p = 0; p < getNrOutPorts(); p++) {
        if (newOutData[p] instanceof BufferedDataTable) {
            BufferedDataTable thisTable = (BufferedDataTable) newOutData[p];
            DataTableSpec portSpec = (DataTableSpec) (m_outputs[p].spec);
            DataTableSpec newPortSpec = thisTable.getDataTableSpec();
            if ((portSpec != null) && !tolerateDifferentSpecs) {
                if (!portSpec.equalStructure(newPortSpec)) {
                    // this used to be an ERROR but is now more frequently encountered due to file reader nodes
                    // caching their output spec to avoid in I/O in #configure
                    final String warningMsg = "DataSpec generated by configure does not match spec after execution.";
                    LOGGER.coding(warningMsg);
                    createWarningMessageAndNotify(warningMsg);
                }
            }
            BufferedDataTable t = thisTable;
            t.setOwnerRecursively(this);
            m_outputs[p].object = t;
            m_outputs[p].summary = t.getSummary();
            m_outputs[p].spec = newPortSpec;
        } else {
            m_outputs[p].object = newOutData[p];
            if (newOutData[p] != null) {
                m_outputs[p].spec = newOutData[p].getSpec();
                m_outputs[p].summary = newOutData[p].getSummary();
            } else {
                m_outputs[p].summary = null;
            }
        }
    }
    return true;
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) InactiveBranchPortObject(org.knime.core.node.port.inactive.InactiveBranchPortObject) InactiveBranchPortObjectSpec(org.knime.core.node.port.inactive.InactiveBranchPortObjectSpec) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) FlowVariablePortObjectSpec(org.knime.core.node.port.flowvariable.FlowVariablePortObjectSpec) PortType(org.knime.core.node.port.PortType)

Example 5 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)

Aggregations

InactiveBranchPortObject (org.knime.core.node.port.inactive.InactiveBranchPortObject)7 PortObject (org.knime.core.node.port.PortObject)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 DataTableSpec (org.knime.core.data.DataTableSpec)2 RowKey (org.knime.core.data.RowKey)2 AppendedRowsIterator (org.knime.core.data.append.AppendedRowsIterator)2 AppendedRowsTable (org.knime.core.data.append.AppendedRowsTable)2 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)2 FlowVariablePortObject (org.knime.core.node.port.flowvariable.FlowVariablePortObject)2 DefaultHiLiteMapper (org.knime.core.node.property.hilite.DefaultHiLiteMapper)2 FlowTryCatchContext (org.knime.core.node.workflow.FlowTryCatchContext)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Vector (java.util.Vector)1