Search in sources :

Example 6 with DefaultHiLiteMapper

use of org.knime.core.node.property.hilite.DefaultHiLiteMapper in project knime-core by knime.

the class UngroupOperation2 method compute.

/**
 * Performs the ungroup operation on the given row input and pushes the result to the row output.
 *
 * @param in the row input, will NOT be closed when finished
 * @param out the row input, will NOT be closed when finished
 * @param exec the execution context to check cancellation and (optional) progress logging
 * @param rowCount row count to track the progress or <code>-1</code> without progress tracking
 * @param trans the hilite translater, will be modified directly. Must be non-null if hiliting is enabled, can be
 *            <code>null</code> otherwise
 * @throws CanceledExecutionException if the execution has been canceled
 * @throws InterruptedException if the execution has been interrupted
 * @throws IllegalArgumentException if hiliting is enabled and no hilite translater is given
 */
public void compute(final RowInput in, final RowOutput out, final ExecutionContext exec, final long rowCount, final HiLiteTranslator trans) throws CanceledExecutionException, InterruptedException {
    if (m_enableHilite && trans == null) {
        throw new IllegalArgumentException("HiLiteTranslator must not be null when hiliting is enabled!");
    }
    final Map<RowKey, Set<RowKey>> hiliteMapping = new HashMap<RowKey, Set<RowKey>>();
    @SuppressWarnings("unchecked") Iterator<DataCell>[] iterators = new Iterator[m_colIndices.length];
    final DataCell[] missingCells = new DataCell[m_colIndices.length];
    Arrays.fill(missingCells, DataType.getMissingCell());
    long rowCounter = 0;
    DataRow row = null;
    while ((row = in.poll()) != null) {
        rowCounter++;
        exec.checkCanceled();
        if (rowCount > 0) {
            exec.setProgress(rowCounter / (double) rowCount, "Processing row " + rowCounter + " of " + rowCount);
        }
        boolean allMissing = true;
        for (int i = 0, length = m_colIndices.length; i < length; i++) {
            final DataCell cell = row.getCell(m_colIndices[i]);
            final CollectionDataValue listCell;
            final Iterator<DataCell> iterator;
            if (cell instanceof CollectionDataValue) {
                listCell = (CollectionDataValue) cell;
                iterator = listCell.iterator();
                allMissing = false;
            } else {
                iterator = null;
            }
            iterators[i] = iterator;
        }
        if (allMissing) {
            // with missing cells as well if the skip missing value option is disabled
            if (!m_skipMissingValues) {
                final DefaultRow newRow = createClone(row.getKey(), row, m_colIndices, m_removeCollectionCol, missingCells);
                if (m_enableHilite) {
                    // create the hilite entry
                    final Set<RowKey> keys = new HashSet<RowKey>(1);
                    keys.add(row.getKey());
                    hiliteMapping.put(row.getKey(), keys);
                }
                out.push(newRow);
            }
            continue;
        }
        long counter = 1;
        final Set<RowKey> keys;
        if (m_enableHilite) {
            keys = new HashSet<RowKey>();
        } else {
            keys = null;
        }
        boolean continueLoop = false;
        boolean allEmpty = true;
        do {
            // reset the loop flag
            allMissing = true;
            continueLoop = false;
            final DataCell[] newCells = new DataCell[iterators.length];
            for (int i = 0, length = iterators.length; i < length; i++) {
                Iterator<DataCell> iterator = iterators[i];
                DataCell newCell;
                if (iterator != null && iterator.hasNext()) {
                    allEmpty = false;
                    continueLoop = true;
                    newCell = iterator.next();
                } else {
                    if (iterator == null) {
                        allEmpty = false;
                    }
                    newCell = DataType.getMissingCell();
                }
                if (!newCell.isMissing()) {
                    allMissing = false;
                }
                newCells[i] = newCell;
            }
            if (!allEmpty && !continueLoop) {
                break;
            }
            if (!allEmpty && allMissing && m_skipMissingValues) {
                continue;
            }
            final RowKey oldKey = row.getKey();
            final RowKey newKey = new RowKey(oldKey.getString() + "_" + counter++);
            final DefaultRow newRow = createClone(newKey, row, m_colIndices, m_removeCollectionCol, newCells);
            out.push(newRow);
            if (keys != null) {
                keys.add(newKey);
            }
        } while (continueLoop);
        if (keys != null && !keys.isEmpty()) {
            hiliteMapping.put(row.getKey(), keys);
        }
    }
    if (m_enableHilite) {
        trans.setMapper(new DefaultHiLiteMapper(hiliteMapping));
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) RowKey(org.knime.core.data.RowKey) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataRow(org.knime.core.data.DataRow) Iterator(java.util.Iterator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) CollectionDataValue(org.knime.core.data.collection.CollectionDataValue) HashSet(java.util.HashSet)

Example 7 with DefaultHiLiteMapper

use of org.knime.core.node.property.hilite.DefaultHiLiteMapper in project knime-core by knime.

the class ValueCounterNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    if (m_settings.hiliting()) {
        NodeSettings s = new NodeSettings("Hiliting");
        ((DefaultHiLiteMapper) m_translator.getMapper()).save(s);
        File f = new File(nodeInternDir, "Hiliting.conf.gz");
        OutputStream out = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
        s.saveToXML(out);
        out.close();
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) GZIPOutputStream(java.util.zip.GZIPOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 8 with DefaultHiLiteMapper

use of org.knime.core.node.property.hilite.DefaultHiLiteMapper 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 9 with DefaultHiLiteMapper

use of org.knime.core.node.property.hilite.DefaultHiLiteMapper in project knime-core by knime.

the class EndifNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    if (m_enableHiliting) {
        final NodeSettings config = new NodeSettings("hilite_mapping");
        ((DefaultHiLiteMapper) m_hiliteTranslator.getMapper()).save(config);
        config.saveToXML(new GZIPOutputStream(new FileOutputStream(new File(nodeInternDir, "hilite_mapping.xml.gz"))));
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) File(java.io.File)

Example 10 with DefaultHiLiteMapper

use of org.knime.core.node.property.hilite.DefaultHiLiteMapper in project knime-core by knime.

the class AppendedRowsNodeModel method saveInternals.

/**
 * {@inheritDoc}
 */
@Override
protected void saveInternals(final File nodeInternDir, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    if (m_enableHiliting) {
        final NodeSettings config = new NodeSettings("hilite_mapping");
        ((DefaultHiLiteMapper) m_hiliteTranslator.getMapper()).save(config);
        config.saveToXML(new GZIPOutputStream(new FileOutputStream(new File(nodeInternDir, "hilite_mapping.xml.gz"))));
    }
}
Also used : NodeSettings(org.knime.core.node.NodeSettings) GZIPOutputStream(java.util.zip.GZIPOutputStream) FileOutputStream(java.io.FileOutputStream) DefaultHiLiteMapper(org.knime.core.node.property.hilite.DefaultHiLiteMapper) File(java.io.File)

Aggregations

DefaultHiLiteMapper (org.knime.core.node.property.hilite.DefaultHiLiteMapper)35 File (java.io.File)16 FileOutputStream (java.io.FileOutputStream)16 NodeSettings (org.knime.core.node.NodeSettings)16 Set (java.util.Set)12 RowKey (org.knime.core.data.RowKey)12 BufferedDataTable (org.knime.core.node.BufferedDataTable)12 GZIPOutputStream (java.util.zip.GZIPOutputStream)9 DataRow (org.knime.core.data.DataRow)8 DataTableSpec (org.knime.core.data.DataTableSpec)8 DataCell (org.knime.core.data.DataCell)7 DefaultRow (org.knime.core.data.def.DefaultRow)7 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)7 HashMap (java.util.HashMap)6 LinkedHashSet (java.util.LinkedHashSet)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)6 LinkedHashMap (java.util.LinkedHashMap)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4