Search in sources :

Example 6 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class JoinedTable method create.

/**
 * Creates new join table, does the sanity checks. Called from the
 * {@link org.knime.core.node.ExecutionContext#createJoinedTable(
 * BufferedDataTable, BufferedDataTable, ExecutionMonitor)} method.
 * @param left The left table.
 * @param right The right table.
 * @param prog For progress/cancel.
 * @return A joined table.
 * @throws CanceledExecutionException When canceled.
 * @throws IllegalArgumentException If row keys don't match or there are
 * duplicate columns.
 */
public static JoinedTable create(final BufferedDataTable left, final BufferedDataTable right, final ExecutionMonitor prog) throws CanceledExecutionException {
    long cnt = left.size();
    if (cnt != right.size()) {
        throw new IllegalArgumentException("Tables can't be joined, non " + "matching row counts: " + cnt + " vs. " + right.size());
    }
    // throws exception when duplicates encountered.
    DataTableSpec joinSpec = new DataTableSpec(left.getDataTableSpec(), right.getDataTableSpec());
    // check if rows come in same order
    RowIterator leftIt = left.iterator();
    RowIterator rightIt = right.iterator();
    int rowIndex = 0;
    while (leftIt.hasNext()) {
        RowKey leftKey = leftIt.next().getKey();
        RowKey rightKey = rightIt.next().getKey();
        if (!leftKey.equals(rightKey)) {
            throw new IllegalArgumentException("Tables contain non-matching rows or are sorted " + "differently, keys in row " + rowIndex + " do not match: \"" + leftKey + "\" vs. \"" + rightKey + "\"");
        }
        prog.checkCanceled();
        prog.setProgress(rowIndex / (double) cnt, "\"" + leftKey + "\" (" + rowIndex + "/" + cnt + ")");
        rowIndex++;
    }
    return new JoinedTable(left, right, joinSpec);
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) RowIterator(org.knime.core.data.RowIterator)

Example 7 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class RearrangeColumnsTable method calcNewColsSynchronously.

/**
 * Processes input sequentially in the caller thread.
 */
private static void calcNewColsSynchronously(final BufferedDataTable table, final ExecutionMonitor subProgress, final NewColumnsProducerMapping newColsProducerMapping, final DataContainer container) throws CanceledExecutionException {
    long finalRowCount = table.size();
    Set<CellFactory> newColsFactories = newColsProducerMapping.getUniqueCellFactoryMap().keySet();
    final int factoryCount = newColsFactories.size();
    int r = 0;
    CellFactory facForProgress = factoryCount > 0 ? newColsFactories.iterator().next() : null;
    for (RowIterator it = table.iterator(); it.hasNext(); r++) {
        DataRow row = it.next();
        DataRow append = calcNewCellsForRow(row, newColsProducerMapping);
        container.addRowToTable(append);
        if (facForProgress == null) {
            // no factory added means at least one columns gets type converted.
            assert !newColsProducerMapping.getConverterToIndexMap().isEmpty();
        } else {
            facForProgress.setProgress(r + 1, finalRowCount, row.getKey(), subProgress);
        }
        subProgress.checkCanceled();
    }
}
Also used : RowIterator(org.knime.core.data.RowIterator) DataRow(org.knime.core.data.DataRow)

Example 8 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class JoinerTest method compareTables.

private void compareTables(final BufferedDataTable reference, final BufferedDataTable test) {
    // Check if it has the same results as defaultResult
    assertThat("Unequal number of rows in result table", test.getRowCount(), is(reference.getRowCount()));
    RowIterator referenceIter = reference.iterator();
    RowIterator testIter = test.iterator();
    while (referenceIter.hasNext()) {
        DataRow refRow = referenceIter.next();
        DataRow testRow = testIter.next();
        assertThat("Unexpected row key", testRow.getKey(), is(refRow.getKey()));
        Iterator<DataCell> refCell = refRow.iterator();
        Iterator<DataCell> testCell = testRow.iterator();
        while (refCell.hasNext()) {
            assertThat("Unexpected cell in row " + refRow.getKey(), testCell.next(), is(refCell.next()));
        }
    }
}
Also used : RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 9 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class FilterColumnTableTest method tableTest.

/*
     * Invoked on each testXXX() method to test all rows and cells on equality
     * by iterating through the entire table, that is, the filter as well as the
     * original data table. @param The filter table to test equality on.
     */
private void tableTest(final FilterColumnTable f) {
    final int[] columns = f.getColumnIndices();
    RowIterator fIt = f.iterator();
    RowIterator tIt = m_table.iterator();
    for (; fIt.hasNext() && tIt.hasNext(); ) {
        DataRow rf = fIt.next();
        DataRow rt = tIt.next();
        // check also if the same rows are compared
        assertTrue(rf.getKey().equals(rt.getKey()));
        for (int i = 0; i < columns.length; i++) {
            // check cell from original with the mapped one
            assertTrue(rf.getCell(i).equals(rt.getCell(columns[i])));
        }
    }
}
Also used : RowIterator(org.knime.core.data.RowIterator) DataRow(org.knime.core.data.DataRow)

Example 10 with RowIterator

use of org.knime.core.data.RowIterator in project knime-core by knime.

the class ARFFTableTest method testARFFTableMissVals.

/**
 * tests stuff like '?' the ARFF missing value, comment lines and empty
 * lines in the file.
 *
 * @throws IOException some time.
 * @throws InvalidSettingsException sometimes.
 */
public void testARFFTableMissVals() throws IOException, InvalidSettingsException {
    // "% Comment\n"
    // + "% comment line 2\n"
    // + "@attribute col1 string\n"
    // + "@attribute col2 string\n"
    // + "\n\n"
    // + "@data\n\n"
    // + "foo, poo\n"
    // + "foo, ?\n"
    // + "?, foo\n"
    // + "%\n"
    // + "%\n"
    // + "\n";
    File tempFile = File.createTempFile("ARFFReaderUnitTest", "table");
    tempFile.deleteOnExit();
    Writer out = new BufferedWriter(new FileWriter(tempFile));
    out.write(ARFF_FIES);
    out.close();
    try {
        ARFFTable table = new ARFFTable(tempFile.toURI().toURL(), ARFFTable.createDataTableSpecFromARFFfile(tempFile.toURI().toURL(), null), "Row");
        assertEquals(table.getDataTableSpec().getNumColumns(), 2);
        assertEquals(table.getDataTableSpec().getColumnSpec(0).getType(), StringCell.TYPE);
        assertEquals(table.getDataTableSpec().getColumnSpec(1).getType(), StringCell.TYPE);
        assertNull(table.getDataTableSpec().getColumnSpec(0).getDomain().getValues());
        assertNull(table.getDataTableSpec().getColumnSpec(1).getDomain().getValues());
        DataRow row;
        RowIterator rIter = table.iterator();
        assertTrue(rIter.hasNext());
        row = rIter.next();
        assertEquals(row.getKey().toString(), "Row0");
        assertEquals(row.getCell(0).toString(), "foo");
        assertEquals(row.getCell(1).toString(), "poo");
        assertTrue(rIter.hasNext());
        row = rIter.next();
        assertEquals(row.getKey().toString(), "Row1");
        assertEquals(row.getCell(0).toString(), "foo");
        assertTrue(row.getCell(1).isMissing());
        assertTrue(rIter.hasNext());
        row = rIter.next();
        assertEquals(row.getKey().toString(), "Row2");
        assertTrue(row.getCell(0).isMissing());
        assertEquals(row.getCell(1).toString(), "foo");
        assertFalse(rIter.hasNext());
    } catch (CanceledExecutionException cee) {
    // no exec monitor, no cancel
    }
}
Also used : CanceledExecutionException(org.knime.core.node.CanceledExecutionException) FileWriter(java.io.FileWriter) RowIterator(org.knime.core.data.RowIterator) File(java.io.File) DataRow(org.knime.core.data.DataRow) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) Writer(java.io.Writer) BufferedWriter(java.io.BufferedWriter)

Aggregations

RowIterator (org.knime.core.data.RowIterator)77 DataRow (org.knime.core.data.DataRow)62 DataCell (org.knime.core.data.DataCell)28 DataTableSpec (org.knime.core.data.DataTableSpec)20 RowKey (org.knime.core.data.RowKey)16 DoubleValue (org.knime.core.data.DoubleValue)14 BufferedDataTable (org.knime.core.node.BufferedDataTable)13 DataColumnSpec (org.knime.core.data.DataColumnSpec)11 ArrayList (java.util.ArrayList)9 DefaultRow (org.knime.core.data.def.DefaultRow)8 PreparedStatement (java.sql.PreparedStatement)7 DataType (org.knime.core.data.DataType)6 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)6 HashSet (java.util.HashSet)5 Random (java.util.Random)5 TimeZone (java.util.TimeZone)5 DataTable (org.knime.core.data.DataTable)5 DoubleCell (org.knime.core.data.def.DoubleCell)5 StringCell (org.knime.core.data.def.StringCell)5 CanceledExecutionException (org.knime.core.node.CanceledExecutionException)5