Search in sources :

Example 56 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class DataContainerTest method generateRows.

private static RowIterator generateRows(final int count) {
    return new RowIterator() {

        private int m_index = 0;

        @Override
        public DataRow next() {
            DefaultRow r = new DefaultRow(RowKey.createRowKey(m_index), new StringCell("String " + m_index), new IntCell(m_index), new DoubleCell(m_index));
            m_index++;
            return r;
        }

        @Override
        public boolean hasNext() {
            return m_index < count;
        }
    };
}
Also used : StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) RowIterator(org.knime.core.data.RowIterator) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Example 57 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class DataContainerTest method testDuplicateKey.

/**
 * method being tested: addRowToTable().
 */
public final void testDuplicateKey() {
    String[] colNames = new String[] { "Column 1", "Column 2" };
    DataType[] colTypes = new DataType[] { StringCell.TYPE, IntCell.TYPE };
    DataTableSpec spec1 = new DataTableSpec(colNames, colTypes);
    DataContainer c = new DataContainer(spec1);
    RowKey r1Key = new RowKey("row 1");
    DataCell r1Cell1 = new StringCell("Row 1, Cell 1");
    DataCell r1Cell2 = new IntCell(12);
    DataRow r1 = new DefaultRow(r1Key, new DataCell[] { r1Cell1, r1Cell2 });
    RowKey r2Key = new RowKey("row 2");
    DataCell r2Cell1 = new StringCell("Row 2, Cell 1");
    DataCell r2Cell2 = new IntCell(22);
    DataRow r2 = new DefaultRow(r2Key, new DataCell[] { r2Cell1, r2Cell2 });
    c.addRowToTable(r1);
    c.addRowToTable(r2);
    // add row 1 twice
    try {
        c.addRowToTable(r1);
        c.close();
        // ... eh eh, you don't do this
        fail("Expected " + DuplicateKeyException.class + " not thrown");
    } catch (DuplicateKeyException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass(), e);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) DuplicateKeyException(org.knime.core.util.DuplicateKeyException) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 58 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class DataContainerTest method testIncompatibleTypes.

/**
 * method being tested: addRowToTable().
 */
public final void testIncompatibleTypes() {
    String[] colNames = new String[] { "Column 1", "Column 2" };
    DataType[] colTypes = new DataType[] { StringCell.TYPE, IntCell.TYPE };
    DataTableSpec spec1 = new DataTableSpec(colNames, colTypes);
    DataContainer c = new DataContainer(spec1);
    RowKey r1Key = new RowKey("row 1");
    DataCell r1Cell1 = new StringCell("Row 1, Cell 1");
    DataCell r1Cell2 = new IntCell(12);
    DataRow r1 = new DefaultRow(r1Key, new DataCell[] { r1Cell1, r1Cell2 });
    RowKey r2Key = new RowKey("row 2");
    DataCell r2Cell1 = new StringCell("Row 2, Cell 1");
    DataCell r2Cell2 = new IntCell(22);
    DataRow r2 = new DefaultRow(r2Key, new DataCell[] { r2Cell1, r2Cell2 });
    RowKey r3Key = new RowKey("row 3");
    DataCell r3Cell1 = new StringCell("Row 3, Cell 1");
    DataCell r3Cell2 = new IntCell(32);
    DataRow r3 = new DefaultRow(r3Key, new DataCell[] { r3Cell1, r3Cell2 });
    c.addRowToTable(r1);
    c.addRowToTable(r2);
    c.addRowToTable(r3);
    // add incompatible types
    RowKey r4Key = new RowKey("row 4");
    DataCell r4Cell1 = new StringCell("Row 4, Cell 1");
    // not allowed
    DataCell r4Cell2 = new DoubleCell(42.0);
    DataRow r4 = new DefaultRow(r4Key, new DataCell[] { r4Cell1, r4Cell2 });
    try {
        c.addRowToTable(r4);
        c.close();
        fail("Expected " + DataContainerException.class + " not thrown");
    } catch (DataContainerException e) {
        if (!(e.getCause() instanceof IllegalArgumentException)) {
            throw e;
        } else {
            NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getCause().getClass(), e.getCause());
        }
    } catch (IllegalArgumentException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass(), e);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 59 with DefaultRow

use of org.knime.core.data.def.DefaultRow 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 60 with DefaultRow

use of org.knime.core.data.def.DefaultRow in project knime-core by knime.

the class AdapterNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected PortObject[] execute(final PortObject[] inObjects, final ExecutionContext exec) throws Exception {
    if (getNrInPorts() == 0 && getNrOutPorts() == 1) {
        // assume simple source node with one table output
        BufferedDataContainer cnt = exec.createDataContainer(createDefaultOutputSpec());
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(0), new DataCell[] { new StringCell("Cell-1.1"), new IntCell(12), new DoubleCell(1.3) }));
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(1), new DataCell[] { new StringCell("Cell-2.1"), new IntCell(22), new DoubleCell(2.3) }));
        cnt.addRowToTable(new DefaultRow(RowKey.createRowKey(2), new DataCell[] { new StringCell("Cell-3.1"), new IntCell(32), new DoubleCell(3.3) }));
        cnt.close();
        return new BufferedDataTable[] { cnt.getTable() };
    }
    return inObjects;
}
Also used : BufferedDataContainer(org.knime.core.node.BufferedDataContainer) StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Aggregations

DefaultRow (org.knime.core.data.def.DefaultRow)207 DataCell (org.knime.core.data.DataCell)165 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)94 DataTableSpec (org.knime.core.data.DataTableSpec)92 DataRow (org.knime.core.data.DataRow)88 RowKey (org.knime.core.data.RowKey)80 DoubleCell (org.knime.core.data.def.DoubleCell)66 StringCell (org.knime.core.data.def.StringCell)65 BufferedDataTable (org.knime.core.node.BufferedDataTable)56 IntCell (org.knime.core.data.def.IntCell)46 ArrayList (java.util.ArrayList)26 DataType (org.knime.core.data.DataType)26 DataColumnSpec (org.knime.core.data.DataColumnSpec)22 DataContainer (org.knime.core.data.container.DataContainer)21 HashSet (java.util.HashSet)18 LinkedHashMap (java.util.LinkedHashMap)17 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)16 LinkedHashSet (java.util.LinkedHashSet)14 DoubleValue (org.knime.core.data.DoubleValue)14 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)14