use of org.knime.core.data.def.IntCell in project knime-core by knime.
the class NodeSettingsTest method testDataCell.
/**
* Test write/read of DataCells.
*
* @throws Exception Should not happen.
*/
@Test
public void testDataCell() throws Exception {
StringCell s = new StringCell("stringi");
m_settings.addDataCell("string", s);
assertTrue(m_settings.containsKey("string"));
assertTrue(m_settings.getDataCell("string").equals(s));
DoubleCell d = new DoubleCell(45.42);
m_settings.addDataCell("double", d);
assertTrue(m_settings.containsKey("double"));
assertTrue(m_settings.getDataCell("double").equals(d));
IntCell i = new IntCell(11);
m_settings.addDataCell("int", i);
assertTrue(m_settings.containsKey("int"));
assertTrue(m_settings.getDataCell("int").equals(i));
DataCell m = DataType.getMissingCell();
m_settings.addDataCell("missing", m);
assertTrue(m_settings.containsKey("missing"));
assertTrue(m_settings.getDataCell("missing").equals(m));
ComplexNumberCell c = new ComplexNumberCell(5.4, 4.5);
m_settings.addDataCell("complex", c);
assertTrue(m_settings.containsKey("complex"));
assertTrue(m_settings.getDataCell("complex").equals(c));
FuzzyNumberCell n = new FuzzyNumberCell(1, 2, 4);
m_settings.addDataCell("fnumber", n);
assertTrue(m_settings.containsKey("fnumber"));
assertTrue(m_settings.getDataCell("fnumber").equals(n));
FuzzyIntervalCell f = new FuzzyIntervalCell(1, 2, 3, 4);
m_settings.addDataCell("finterval", f);
assertTrue(m_settings.containsKey("finterval"));
assertTrue(m_settings.getDataCell("finterval").equals(f));
DataCell unknownCell = new UnknownCell();
m_settings.addDataCell("unknownCell", unknownCell);
assertTrue(m_settings.containsKey("unknownCell"));
assertTrue(m_settings.getDataCell("unknownCell").equals(unknownCell));
}
use of org.knime.core.data.def.IntCell in project knime-core by knime.
the class DataTableDomainCreatorTest method testBoundsInt.
/**
* Check whether upper and lower bounds are computed correctly for int column.
*/
@Test
public void testBoundsInt() {
DataColumnSpecCreator colSpecCrea = new DataColumnSpecCreator("Int col", IntCell.TYPE);
DataTableSpec tableSpec = new DataTableSpec(colSpecCrea.createSpec());
RowKey rowKey = new RowKey("Row0");
DataTableDomainCreator domainCreator = new DataTableDomainCreator(tableSpec, false);
// initially bounds are null
DataColumnDomain colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
assertThat("Unexpected lower bound", colDomain.getLowerBound(), is(nullValue()));
assertThat("Unexpected upper bound", colDomain.getUpperBound(), is(nullValue()));
// missing cells are ignored
domainCreator.updateDomain(new DefaultRow(rowKey, DataType.getMissingCell()));
colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
assertThat("Unexpected lower bound", colDomain.getLowerBound(), is(nullValue()));
assertThat("Unexpected upper bound", colDomain.getUpperBound(), is(nullValue()));
// change lower and upper bound
domainCreator.updateDomain(new DefaultRow(rowKey, new IntCell(0)));
colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(0)));
assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(0)));
// change upper bound
domainCreator.updateDomain(new DefaultRow(rowKey, new IntCell(1)));
colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(0)));
assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(1)));
// change lower bound
domainCreator.updateDomain(new DefaultRow(rowKey, new IntCell(-1)));
colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(-1)));
assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(1)));
// ignore missing values (again)
domainCreator.updateDomain(new DefaultRow(rowKey, DataType.getMissingCell()));
colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(-1)));
assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(1)));
// change lower bound to MIN_VALUE
domainCreator.updateDomain(new DefaultRow(rowKey, new IntCell(Integer.MIN_VALUE)));
colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(Integer.MIN_VALUE)));
assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(1)));
// change upper bound to MAX_VALUE
domainCreator.updateDomain(new DefaultRow(rowKey, new IntCell(Integer.MAX_VALUE)));
colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
assertThat("Unexpected lower bound", colDomain.getLowerBound(), is((DataCell) new IntCell(Integer.MIN_VALUE)));
assertThat("Unexpected upper bound", colDomain.getUpperBound(), is((DataCell) new IntCell(Integer.MAX_VALUE)));
}
use of org.knime.core.data.def.IntCell in project knime-core by knime.
the class DataContainerTest method testWrongCellCountRow.
/**
* method being tested: addRowToTable().
*/
public final void testWrongCellCountRow() {
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 wrong sized row
RowKey r5Key = new RowKey("row 5");
DataCell r5Cell1 = new StringCell("Row 5, Cell 1");
DataCell r5Cell2 = new IntCell(52);
DataCell r5Cell3 = new DoubleCell(53.0);
DataRow r5 = new DefaultRow(r5Key, new DataCell[] { r5Cell1, r5Cell2, r5Cell3 });
try {
c.addRowToTable(r5);
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);
}
}
use of org.knime.core.data.def.IntCell 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.def.IntCell in project knime-core by knime.
the class DataCellToJavaConversionTest method testNestedCollectionTypes.
/**
* Test ListCell(ListCell(IntCell)) -> Integer[][] conversion.
*
* @throws Exception When something went wrong
*/
@Test
public void testNestedCollectionTypes() throws Exception {
ArrayList<DataCell> coll = new ArrayList<>();
for (int i = 0; i < 5; ++i) {
coll.add(new IntCell(i * i));
}
// collection cells can always contain missing cells.
coll.add(new MissingCell("42"));
final ListCell listCell = CollectionCellFactory.createListCell(Arrays.asList(CollectionCellFactory.createListCell(coll)));
final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer[][]>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(listCell.getType(), Integer[][].class).stream().findFirst();
assertTrue(factory.isPresent());
final DataCellToJavaConverter<DataCell, Integer[][]> converter = (DataCellToJavaConverter<DataCell, Integer[][]>) factory.get().create();
assertNotNull(converter);
final Integer[][] array = converter.convert(listCell);
for (int i = 0; i < 5; ++i) {
assertEquals(new Integer(i * i), array[0][i]);
}
assertNull(array[0][5]);
}
Aggregations