Search in sources :

Example 6 with DefaultTable

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

the class SorterNodeModelTest method testExecuteBufferedDataTableArrayExecutionContext.

/**
 * Test method for {@link org.knime.base.node.preproc.sorter.SorterNodeModel#execute(org.knime.core.node.BufferedDataTable[], org.knime.core.node.ExecutionContext)}.
 * @throws Exception
 * @throws CanceledExecutionException
 */
@Test
public final void testExecuteBufferedDataTableArrayExecutionContext() throws CanceledExecutionException, Exception {
    // try to sort a table with 1 entry
    String[] columnNames = { "col1", "col2", "col3", "col4" };
    DataType[] columnTypes = { DoubleCell.TYPE, StringCell.TYPE, IntCell.TYPE, DoubleCell.TYPE };
    DataRow[] rows = new DataRow[1];
    DataCell[] myRow = new DataCell[4];
    myRow[0] = new DoubleCell(2.4325);
    myRow[1] = new StringCell("Test");
    myRow[2] = new IntCell(7);
    myRow[3] = new DoubleCell(32432.324);
    rows[0] = new DefaultRow(Integer.toString(1), myRow);
    DataTable[] inputTable = { new DefaultTable(rows, columnNames, columnTypes) };
    DataTable[] resultTable = { new DefaultTable(rows, columnNames, columnTypes) };
    // set settings
    String[] includeCols = { "col1", "col2", "col3", "col4" };
    m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
    boolean[] sortorder = { true, true, true, true };
    m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
    m_snm.loadValidatedSettingsFrom(m_settings);
    resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable, EXEC_CONTEXT), EXEC_CONTEXT);
    // test output
    RowIterator rowIt = resultTable[0].iterator();
    Assert.assertTrue(rowIt.hasNext());
    Assert.assertEquals(rows[0], rowIt.next());
    Assert.assertFalse(rowIt.hasNext());
    m_snm.reset();
    // *********************************************//
    // try to sort a large array of DataRows
    // In this case we generate a unit matrix
    // *********************************************//
    // start with a little one
    int dimension = 50;
    // *********************************************//
    // set settings
    includeCols = new String[dimension];
    for (int i = 0; i < dimension; i++) {
        includeCols[i] = "col" + i;
    }
    m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
    sortorder = new boolean[dimension];
    for (int i = 0; i < dimension; i++) {
        sortorder[i] = true;
    }
    m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
    DataTable[] inputTable2 = { generateUnitMatrixTable(dimension) };
    m_snm.loadValidatedSettingsFrom(m_settings);
    resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable2, EXEC_CONTEXT), EXEC_CONTEXT);
    // test output (should have sorted all rows in reverse order)
    rowIt = resultTable[0].iterator();
    Assert.assertTrue(rowIt.hasNext());
    int k = dimension - 1;
    while (rowIt.hasNext()) {
        RowKey rk = rowIt.next().getKey();
        int ic = Integer.parseInt(rk.getString());
        Assert.assertEquals(k, ic);
        k--;
    }
    Assert.assertFalse(rowIt.hasNext());
    m_snm.reset();
    // *********************************************//
    // try to sort a very large array of DataRows
    // In this case we generate a unit matrix
    // *********************************************//
    // dimension 300 => 15,8 secs.
    // dimension 500 => 49,7 secs.
    dimension = 100;
    // *********************************************//
    // set settings
    includeCols = new String[dimension];
    for (int i = 0; i < dimension; i++) {
        includeCols[i] = "col" + i;
    }
    m_settings.addStringArray(SorterNodeModel.INCLUDELIST_KEY, includeCols);
    sortorder = new boolean[dimension];
    for (int i = 0; i < dimension; i++) {
        sortorder[i] = true;
    }
    m_settings.addBooleanArray(SorterNodeModel.SORTORDER_KEY, sortorder);
    DataTable[] inputTable3 = { generateUnitMatrixTable(dimension) };
    m_snm.loadValidatedSettingsFrom(m_settings);
    resultTable = m_snm.execute(EXEC_CONTEXT.createBufferedDataTables(inputTable3, EXEC_CONTEXT), EXEC_CONTEXT);
    // test output (should have sorted all rows in reverse order)
    rowIt = resultTable[0].iterator();
    Assert.assertTrue(rowIt.hasNext());
    k = dimension - 1;
    while (rowIt.hasNext()) {
        RowKey rk = rowIt.next().getKey();
        int ic = Integer.parseInt(rk.getString());
        Assert.assertEquals(k, ic);
        k--;
    }
    Assert.assertFalse(rowIt.hasNext());
    m_snm.reset();
}
Also used : DataTable(org.knime.core.data.DataTable) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DefaultTable(org.knime.core.data.def.DefaultTable) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) RowIterator(org.knime.core.data.RowIterator) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) Test(org.junit.Test)

Example 7 with DefaultTable

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

the class TableContentModelTest method testShowOnlyHiLited.

// testCachingStrategy()
/**
 * Tests the correctness of the model when only hilited are shown.
 */
public final void testShowOnlyHiLited() {
    final String[] colnames = new String[] { "C1" };
    final DataType[] colclasses = new DataType[] { DoubleCell.TYPE };
    final DataRow[] data = new DefaultRow[500];
    for (int i = 0; i < data.length; i++) {
        data[i] = new DefaultRow(new RowKey("Row_" + i), new double[] { i });
    }
    // override DataTable to set own iterator
    final DataTable table = new DefaultTable(data, colnames, colclasses);
    TableContentModel model = new TableContentModel(table);
    // change default chunk and cache size
    final int chunkSize = 25;
    final int cacheSize = 2 * chunkSize;
    model.setChunkSize(chunkSize);
    assertEquals(model.getChunkSize(), chunkSize);
    model.setCacheSize(cacheSize);
    assertEquals(model.getCacheSize(), cacheSize);
    model.setTableContentFilter(TableContentFilter.HiliteOnly);
    assertEquals(model.getRowCount(), 0);
    assertTrue(model.isRowCountFinal());
    final HiLiteHandler hiliter = new HiLiteHandler();
    model.setHiLiteHandler(hiliter);
    final Random rand = new Random();
    int nrHiLitKeys = 0;
    // reflected in the model
    for (int i = 0; i < 500; i++) {
        if (i % 100 == 0) {
            // clear all, also that should work
            hiliter.fireClearHiLiteEvent();
            flushEDTQueue();
            nrHiLitKeys = 0;
        } else {
            // let at most 20% change
            int count = rand.nextInt(data.length / 5);
            // change randomly drawn keys
            for (int c = 0; c < count; c++) {
                int index = rand.nextInt(data.length);
                RowKey keyForIndex = data[index].getKey();
                boolean isHilit = hiliter.isHiLit(keyForIndex);
                if (isHilit) {
                    hiliter.fireUnHiLiteEvent(keyForIndex);
                    flushEDTQueue();
                    nrHiLitKeys--;
                } else {
                    hiliter.fireHiLiteEvent(keyForIndex);
                    flushEDTQueue();
                    nrHiLitKeys++;
                }
            }
        }
        flushEDTQueue();
        // now the sanity checks
        for (int row = 0; row < model.getRowCount(); row++) {
            RowKey key = model.getRow(row).getKey();
            assertTrue(hiliter.isHiLit(key));
            assertTrue(model.isHiLit(row));
        }
        assertEquals(model.getRowCount(), nrHiLitKeys);
        assertTrue(model.isRowCountFinal());
    }
}
Also used : DataTable(org.knime.core.data.DataTable) HiLiteHandler(org.knime.core.node.property.hilite.HiLiteHandler) RowKey(org.knime.core.data.RowKey) DefaultTable(org.knime.core.data.def.DefaultTable) DataRow(org.knime.core.data.DataRow) Random(java.util.Random) DataType(org.knime.core.data.DataType) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 8 with DefaultTable

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

the class TableContentModelTest method testCachingStrategy.

/**
 * Tests ring buffer chaching strategy of the
 * <code>TableContentModel</code>. This method uses a modified
 * <code>Iterator</code> that throws an <code>Exception</code> when called
 * at an inappropriate time.
 */
public final void testCachingStrategy() {
    final String[] colnames = new String[] { "C1" };
    final DataType[] colclasses = new DataType[] { DoubleCell.TYPE };
    final DataRow[] data = new DefaultRow[500];
    for (int i = 0; i < data.length; i++) {
        data[i] = new DefaultRow(new RowKey("Row_" + i), new double[] { i });
    }
    // these flags keep track when the iterator of the table may be
    // accessed (will be changed in this method, first flag) and when the
    // iterator is indeed being accessed (set in the iterator below to true,
    // reset here, second flag)
    final boolean[] flags = new boolean[] { true, false };
    // override DataTable to set own iterator
    final DataTable table = new DefaultTable(data, colnames, colclasses) {

        @Override
        public RowIterator iterator() {
            return new RestrictedAccessIterator(getRowsInList(), flags);
        }
    };
    TableContentModel model = new TableContentModel(table);
    // change default chunk and cache size
    final int chunkSize = 25;
    final int cacheSize = 2 * chunkSize;
    model.setChunkSize(chunkSize);
    assertEquals(model.getChunkSize(), chunkSize);
    model.setCacheSize(cacheSize);
    assertEquals(model.getCacheSize(), cacheSize);
    // init of table uses iterator
    assertTrue(flags[1]);
    flags[1] = false;
    // allow table access
    flags[0] = true;
    // get first row, iterator is used
    model.getRow(0);
    // is true when iterator has indeed been used
    assertTrue(flags[1]);
    // simulate scrolling down - for first "chunksize" rows
    // iterator access
    flags[0] = false;
    flags[1] = false;
    for (int i = 0; i < chunkSize; i++) {
        // access rows 0 - 24
        // will throw exception when iterator is accessed
        model.getRow(i);
    }
    assertFalse(flags[0]);
    assertFalse(flags[1]);
    // row "chunksize": update cache!
    flags[0] = true;
    // now in cache: Row_1 - Row_50 (release Row_0)
    model.getRow(chunkSize);
    // iterator has been used
    assertTrue(flags[1]);
    flags[0] = false;
    flags[1] = false;
    // cache is full, containing Row_1 - Row_50
    for (int i = 1; i < cacheSize; i++) {
        // must not access Row_50 (will use iterator)
        model.getRow(i);
    }
    assertFalse(flags[0]);
    assertFalse(flags[1]);
    // ok, let's simulate arbitrary jumping in the table and check the
    // cache, let's say 20 different positions, the rows in the cache are:
    // [row+chunksize-cachesize+1 : row+chunksize-1]
    final Random rand = new Random();
    for (int i = 0; i < 200; i++) {
        // draw some row to access
        int row = rand.nextInt(500);
        flags[0] = true;
        flags[1] = false;
        model.getRow(row);
        if (!flags[1]) {
            // cache not changed, we may continue
            continue;
        }
        // disallow access
        flags[0] = false;
        flags[1] = false;
        int firstRow = Math.max(0, row + chunkSize - cacheSize + 1);
        int lastRow = Math.min(row + chunkSize, 500);
        for (int r = firstRow; r < lastRow; r++) {
            model.getRow(r);
        }
        assertFalse(flags[0]);
        assertFalse(flags[1]);
    }
}
Also used : DataTable(org.knime.core.data.DataTable) RowKey(org.knime.core.data.RowKey) DefaultTable(org.knime.core.data.def.DefaultTable) DataRow(org.knime.core.data.DataRow) Random(java.util.Random) DataType(org.knime.core.data.DataType) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 9 with DefaultTable

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

the class TableContentModelTest method testGetValueAt.

/**
 * Method being tested: Object getValueAt(int, int).
 */
public final void testGetValueAt() {
    final TableContentModel m = new TableContentModel();
    try {
        m.getValueAt(1, 0);
        fail("Expected " + IndexOutOfBoundsException.class + " not thrown");
    } catch (IndexOutOfBoundsException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass().getName(), e);
    }
    // create big data table (so that it has to cache) and
    // try to get the wrong value at the right position (hopefully, we fail)
    final double[][] ddata = new double[20000][50];
    final long seed = System.currentTimeMillis();
    // use random access, so the cache is updated frequently
    Random rand = new Random(seed);
    for (int row = 0; row < ddata.length; row++) {
        double[] curRow = ddata[row];
        for (int col = 0; col < curRow.length; col++) {
            curRow[col] = rand.nextDouble();
        }
    }
    DataTable data = new DefaultTable(ddata, null, null);
    m.setDataTable(data);
    // do random search on table
    for (int i = 0; i < 20000; i++) {
        int row = rand.nextInt(20000);
        int col = rand.nextInt(50);
        Object value = m.getValueAt(row, col);
        assertNotNull(value);
        assertTrue(value instanceof DoubleValue);
        double val = ((DoubleValue) value).getDoubleValue();
        String errorMessage = "getValueAt(" + row + ", " + col + ") not equal to what it was set once. Used Random seed " + seed + "; You may want to use that for debugging.";
        assertEquals(errorMessage, val, ddata[row][col], 0.0);
    }
    try {
        m.getValueAt(-4, 0);
        fail("Expected " + IndexOutOfBoundsException.class + " not thrown");
    } catch (IndexOutOfBoundsException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass().getName(), e);
    }
    try {
        m.getValueAt(0, -2);
        fail("Expected " + IndexOutOfBoundsException.class + " not thrown");
    } catch (IndexOutOfBoundsException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass().getName(), e);
    }
    try {
        m.getValueAt(20000, 0);
        fail("Expected " + IndexOutOfBoundsException.class + " not thrown");
    } catch (IndexOutOfBoundsException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass().getName(), e);
    }
    try {
        m.getValueAt(0, 500);
        fail("Expected " + IndexOutOfBoundsException.class + " not thrown");
    } catch (IndexOutOfBoundsException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass().getName(), e);
    }
}
Also used : DataTable(org.knime.core.data.DataTable) DefaultTable(org.knime.core.data.def.DefaultTable) Random(java.util.Random) DoubleValue(org.knime.core.data.DoubleValue)

Example 10 with DefaultTable

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

the class AppendedRowsTableTest method testGetRowIterator.

/**
 * Test method for getRowIterator().
 */
public void testGetRowIterator() {
    DataTable firstTable = new DefaultTable(DATA, DATA_H, DATA_TYPES);
    DataTable firstTableShuffle = new DefaultTable(DATA_SHUFFLE, DATA_SHUFFLE_H, DATA_SHUFFLE_TYPES);
    DataTable ap = new AppendedRowsTable(new DataTable[] { firstTable, firstTableShuffle });
    RowIterator apIt = ap.iterator();
    for (RowIterator fiIt = firstTable.iterator(); fiIt.hasNext(); ) {
        assertTrue(apIt.hasNext());
        DataRow apRow = apIt.next();
        DataRow fiRow = fiIt.next();
        assertEquals(apRow.getKey(), fiRow.getKey());
        assertEquals(apRow.getCell(0), fiRow.getCell(0));
        assertEquals(apRow.getCell(1), fiRow.getCell(1));
        assertEquals(apRow.getCell(2), fiRow.getCell(2));
    }
    for (RowIterator seIt = firstTableShuffle.iterator(); seIt.hasNext(); ) {
        assertTrue(apIt.hasNext());
        DataRow apRow = apIt.next();
        DataRow seRow = seIt.next();
        assertEquals(apRow.getKey(), seRow.getKey());
        // first and second are swapped!
        assertEquals(apRow.getCell(0), seRow.getCell(1));
        assertEquals(apRow.getCell(1), seRow.getCell(0));
        assertEquals(apRow.getCell(2), seRow.getCell(2));
    }
    assertFalse(apIt.hasNext());
    DataTable duplicateTable = new AppendedRowsTable(new DataTable[] { firstTable, firstTable });
    RowIterator dupIt = duplicateTable.iterator();
    for (RowIterator fiIt = firstTable.iterator(); fiIt.hasNext(); ) {
        dupIt.next();
        fiIt.next();
    }
    // it should not return duplicate keys.
    assertFalse(dupIt.hasNext());
}
Also used : DataTable(org.knime.core.data.DataTable) RowIterator(org.knime.core.data.RowIterator) DefaultTable(org.knime.core.data.def.DefaultTable) DataRow(org.knime.core.data.DataRow)

Aggregations

DefaultTable (org.knime.core.data.def.DefaultTable)12 DataTable (org.knime.core.data.DataTable)10 DataRow (org.knime.core.data.DataRow)7 DataType (org.knime.core.data.DataType)4 RowKey (org.knime.core.data.RowKey)4 DefaultRow (org.knime.core.data.def.DefaultRow)4 Random (java.util.Random)3 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 DataTableSpec (org.knime.core.data.DataTableSpec)3 DataCell (org.knime.core.data.DataCell)2 RowIterator (org.knime.core.data.RowIterator)2 IntCell (org.knime.core.data.def.IntCell)2 Hashtable (java.util.Hashtable)1 Test (org.junit.Test)1 DoubleValue (org.knime.core.data.DoubleValue)1 DoubleCell (org.knime.core.data.def.DoubleCell)1 StringCell (org.knime.core.data.def.StringCell)1 HiLiteHandler (org.knime.core.node.property.hilite.HiLiteHandler)1