Search in sources :

Example 56 with DataTable

use of org.knime.core.data.DataTable 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 57 with DataTable

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

the class DataContainerTest method testBigFile.

// testAddRowToTable()
/**
 * Try a big file :-).
 */
public void testBigFile() {
    // with these setting (50, 1000) it will write an 250MB cache file
    // (the latest data this value was checked: 31. August 2006...)
    final int colCount = 50;
    final int rowCount = 100;
    String[] names = new String[colCount];
    DataType[] types = new DataType[colCount];
    for (int c = 0; c < colCount; c++) {
        names[c] = "Column " + c;
        switch(c % 3) {
            case 0:
                types[c] = DoubleCell.TYPE;
                break;
            case 1:
                types[c] = StringCell.TYPE;
                break;
            case 2:
                types[c] = IntCell.TYPE;
                break;
            default:
                throw new InternalError();
        }
    }
    DataTableSpec spec = new DataTableSpec(names, types);
    names = null;
    types = null;
    DataContainer container = new DataContainer(spec);
    final ObjectToDataCellConverter conv = new ObjectToDataCellConverter();
    final long seed = System.currentTimeMillis();
    Random rand = new Random(seed);
    for (int i = 0; i < rowCount; i++) {
        DataRow row = createRandomRow(i, colCount, rand, conv);
        container.addRowToTable(row);
    }
    container.close();
    final Throwable[] throwables = new Throwable[1];
    final DataTable table = container.getTable();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                int i = 0;
                Random rand1 = new Random(seed);
                for (RowIterator it = table.iterator(); it.hasNext(); i++) {
                    DataRow row1 = createRandomRow(i, colCount, rand1, conv);
                    DataRow row2 = it.next();
                    assertEquals(row1, row2);
                }
                assertEquals(i, rowCount);
            } catch (Throwable t) {
                throwables[0] = t;
            }
        }
    };
    // Runnable
    // make two threads read the buffer (file) concurrently.
    Thread t1 = new Thread(runnable);
    Thread t2 = new Thread(runnable);
    t1.start();
    t2.start();
    try {
        // seems that the event dispatch thread must not release the
        // reference to the table, otherwise it is (I guess!!) garbage
        // collected: You comment these lines and see the error message.
        t1.join();
        t2.join();
    } catch (InterruptedException ie) {
        ie.printStackTrace();
        fail();
    }
    if (throwables[0] != null) {
        throw new RuntimeException(throwables[0]);
    }
}
Also used : DataTable(org.knime.core.data.DataTable) DataTableSpec(org.knime.core.data.DataTableSpec) ObjectToDataCellConverter(org.knime.core.data.util.ObjectToDataCellConverter) DataRow(org.knime.core.data.DataRow) Random(java.util.Random) RowIterator(org.knime.core.data.RowIterator) DataType(org.knime.core.data.DataType)

Example 58 with DataTable

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

the class DataContainerTest method testRowOrder.

/**
 * method being tested: addRowToTable().
 */
public final void testRowOrder() {
    // addRow should preserve the order, we try here randomly generated
    // IntCells as key (the container puts it in a linked has map)
    DataCell[] values = new DataCell[0];
    Vector<RowKey> order = new Vector<RowKey>(500);
    for (int i = 0; i < 500; i++) {
        // fill it - this should be easy to preserve (as the int value
        // is also the hash code)
        order.add(new RowKey(Integer.toString(i)));
    }
    // shuffle it - that should screw it up
    Collections.shuffle(order);
    DataContainer c = new DataContainer(EMPTY_SPEC);
    for (RowKey key : order) {
        c.addRowToTable(new DefaultRow(key, values));
    }
    c.close();
    DataTable table = c.getTable();
    int pos = 0;
    for (RowIterator it = table.iterator(); it.hasNext(); pos++) {
        DataRow cur = it.next();
        assertEquals(cur.getKey().getString(), order.get(pos).getString());
    }
    assertEquals(pos, order.size());
}
Also used : DataTable(org.knime.core.data.DataTable) RowKey(org.knime.core.data.RowKey) RowIterator(org.knime.core.data.RowIterator) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) Vector(java.util.Vector) DataRow(org.knime.core.data.DataRow)

Example 59 with DataTable

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

the class DataContainerTest method testTableDomain.

// testBigFile()
/**
 * Test if the domain is retained.
 */
public void testTableDomain() {
    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 });
    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);
    // add in different order
    c.addRowToTable(r2);
    c.addRowToTable(r1);
    c.addRowToTable(r3);
    c.close();
    DataTable table = c.getTable();
    DataTableSpec tableSpec = table.getDataTableSpec();
    // check possible values
    Set<DataCell> possibleValues = tableSpec.getColumnSpec(0).getDomain().getValues();
    assertEquals(possibleValues.size(), 3);
    assertTrue(possibleValues.contains(r1Cell1));
    assertTrue(possibleValues.contains(r2Cell1));
    assertTrue(possibleValues.contains(r3Cell1));
    // no possible values for integer column
    possibleValues = tableSpec.getColumnSpec(1).getDomain().getValues();
    assertNull(possibleValues);
    // check min max
    DataCell min = tableSpec.getColumnSpec(0).getDomain().getLowerBound();
    DataCell max = tableSpec.getColumnSpec(0).getDomain().getLowerBound();
    assertNull(min);
    assertNull(max);
    min = tableSpec.getColumnSpec(1).getDomain().getLowerBound();
    max = tableSpec.getColumnSpec(1).getDomain().getUpperBound();
    Comparator<DataCell> comparator = tableSpec.getColumnSpec(1).getType().getComparator();
    assertTrue(comparator.compare(min, max) < 0);
    assertEquals(min, r1Cell2);
    assertEquals(max, r3Cell2);
}
Also used : DataTable(org.knime.core.data.DataTable) DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DataType(org.knime.core.data.DataType) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 60 with DataTable

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

the class DoubleVectorCellTest method testSerialization.

@Test
public void testSerialization() throws Exception {
    double[] d = IntStream.range(0, 10000).mapToDouble(i -> i).toArray();
    DataCell cell = DoubleVectorCellFactory.createCell(d);
    DataContainer c = new DataContainer(new DataTableSpec(new DataColumnSpecCreator("foo", DoubleVectorCellFactory.TYPE).createSpec()));
    c.addRowToTable(new DefaultRow("row", cell));
    c.close();
    DataTable table = c.getTable();
    byte[] bytes;
    try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
        DataContainer.writeToStream(table, output, new ExecutionMonitor());
        output.close();
        bytes = output.toByteArray();
    }
    ContainerTable containerTable;
    try (ByteArrayInputStream input = new ByteArrayInputStream(bytes)) {
        containerTable = DataContainer.readFromStream(input);
    }
    DataCell cell2 = containerTable.iterator().next().getCell(0);
    Assert.assertNotSame(c, cell2);
    Assert.assertEquals(cell, cell2);
}
Also used : IntStream(java.util.stream.IntStream) CoreMatchers(org.hamcrest.CoreMatchers) DefaultRow(org.knime.core.data.def.DefaultRow) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataTableSpec(org.knime.core.data.DataTableSpec) Test(org.junit.Test) DataTable(org.knime.core.data.DataTable) DataValueComparator(org.knime.core.data.DataValueComparator) DataContainer(org.knime.core.data.container.DataContainer) ByteArrayInputStream(java.io.ByteArrayInputStream) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) OrderingComparison(org.hamcrest.number.OrderingComparison) Assert(org.junit.Assert) DataCell(org.knime.core.data.DataCell) ContainerTable(org.knime.core.data.container.ContainerTable) DataTable(org.knime.core.data.DataTable) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ContainerTable(org.knime.core.data.container.ContainerTable) DataContainer(org.knime.core.data.container.DataContainer) ByteArrayInputStream(java.io.ByteArrayInputStream) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) Test(org.junit.Test)

Aggregations

DataTable (org.knime.core.data.DataTable)64 BufferedDataTable (org.knime.core.node.BufferedDataTable)33 DataRow (org.knime.core.data.DataRow)20 DataTableSpec (org.knime.core.data.DataTableSpec)19 RowKey (org.knime.core.data.RowKey)14 DataCell (org.knime.core.data.DataCell)12 DataColumnSpec (org.knime.core.data.DataColumnSpec)12 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)11 DefaultTable (org.knime.core.data.def.DefaultTable)10 DefaultRow (org.knime.core.data.def.DefaultRow)8 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)7 DefaultDataArray (org.knime.base.node.util.DefaultDataArray)6 DataType (org.knime.core.data.DataType)6 PortObject (org.knime.core.node.port.PortObject)6 RowIterator (org.knime.core.data.RowIterator)5 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)5 ContainerTable (org.knime.core.data.container.ContainerTable)5 DataContainer (org.knime.core.data.container.DataContainer)5 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)5 SettingsModelFilterString (org.knime.core.node.defaultnodesettings.SettingsModelFilterString)5