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);
}
}
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]);
}
}
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());
}
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);
}
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);
}
Aggregations