Search in sources :

Example 41 with StringCell

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

the class DataTableDomainCreatorTest method testPossibleValues.

/**
 * Checks whether possible values are computed correctly.
 */
@Test
public void testPossibleValues() {
    DataColumnSpecCreator colSpecCrea = new DataColumnSpecCreator("String col", StringCell.TYPE);
    DataTableSpec tableSpec = new DataTableSpec(colSpecCrea.createSpec());
    RowKey rowKey = new RowKey("Row0");
    DataTableDomainCreator domainCreator = new DataTableDomainCreator(tableSpec, false);
    domainCreator.setMaxPossibleValues(2);
    // initially no values
    Set<DataCell> expectedValues = new LinkedHashSet<>();
    DataColumnDomain colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(expectedValues));
    // add two values
    expectedValues.add(new StringCell("v1"));
    domainCreator.updateDomain(new DefaultRow(rowKey, "v1"));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(expectedValues));
    expectedValues.add(new StringCell("v2"));
    domainCreator.updateDomain(new DefaultRow(rowKey, "v2"));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(expectedValues));
    // add more than the maximum number removes all values
    domainCreator.updateDomain(new DefaultRow(rowKey, "v3"));
    colDomain = domainCreator.createSpec().getColumnSpec(0).getDomain();
    assertThat("Unexpected possible values", colDomain.getValues(), is(nullValue()));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) StringCell(org.knime.core.data.def.StringCell) DefaultRow(org.knime.core.data.def.DefaultRow) Test(org.junit.Test)

Example 42 with StringCell

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

the class DataContainerTest method generateRows.

private static RowIterator generateRows(final int count) {
    return new RowIterator() {

        private int m_index = 0;

        @Override
        public DataRow next() {
            DefaultRow r = new DefaultRow(RowKey.createRowKey(m_index), new StringCell("String " + m_index), new IntCell(m_index), new DoubleCell(m_index));
            m_index++;
            return r;
        }

        @Override
        public boolean hasNext() {
            return m_index < count;
        }
    };
}
Also used : StringCell(org.knime.core.data.def.StringCell) DoubleCell(org.knime.core.data.def.DoubleCell) RowIterator(org.knime.core.data.RowIterator) DefaultRow(org.knime.core.data.def.DefaultRow) IntCell(org.knime.core.data.def.IntCell)

Example 43 with StringCell

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

the class DataContainerTest method testDuplicateKey.

/**
 * method being tested: addRowToTable().
 */
public final void testDuplicateKey() {
    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 });
    c.addRowToTable(r1);
    c.addRowToTable(r2);
    // add row 1 twice
    try {
        c.addRowToTable(r1);
        c.close();
        // ... eh eh, you don't do this
        fail("Expected " + DuplicateKeyException.class + " not thrown");
    } catch (DuplicateKeyException e) {
        NodeLogger.getLogger(getClass()).debug("Got expected exception: " + e.getClass(), e);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DataRow(org.knime.core.data.DataRow) DuplicateKeyException(org.knime.core.util.DuplicateKeyException) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 44 with StringCell

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

the class DataContainerTest method testIncompatibleTypes.

/**
 * method being tested: addRowToTable().
 */
public final void testIncompatibleTypes() {
    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 incompatible types
    RowKey r4Key = new RowKey("row 4");
    DataCell r4Cell1 = new StringCell("Row 4, Cell 1");
    // not allowed
    DataCell r4Cell2 = new DoubleCell(42.0);
    DataRow r4 = new DefaultRow(r4Key, new DataCell[] { r4Cell1, r4Cell2 });
    try {
        c.addRowToTable(r4);
        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);
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) RowKey(org.knime.core.data.RowKey) DoubleCell(org.knime.core.data.def.DoubleCell) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell) StringCell(org.knime.core.data.def.StringCell) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow)

Example 45 with StringCell

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

the class JavaToDataCellConversionTest method testToStringCell.

/**
 * Test String -> StringCell and Object -> StringCell conversions.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testToStringCell() throws Exception {
    final StringCell cell = testSimpleConversion(String.class, StringCell.TYPE, StringCell.class, new String("KNIME"));
    assertEquals("KNIME", cell.getStringValue());
}
Also used : StringCell(org.knime.core.data.def.StringCell) Test(org.junit.Test)

Aggregations

StringCell (org.knime.core.data.def.StringCell)176 DataCell (org.knime.core.data.DataCell)130 DoubleCell (org.knime.core.data.def.DoubleCell)67 DefaultRow (org.knime.core.data.def.DefaultRow)65 IntCell (org.knime.core.data.def.IntCell)55 DataRow (org.knime.core.data.DataRow)52 DataTableSpec (org.knime.core.data.DataTableSpec)49 ArrayList (java.util.ArrayList)41 DataColumnSpec (org.knime.core.data.DataColumnSpec)37 RowKey (org.knime.core.data.RowKey)36 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)26 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)26 DataType (org.knime.core.data.DataType)22 LinkedHashSet (java.util.LinkedHashSet)21 BufferedDataTable (org.knime.core.node.BufferedDataTable)20 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)19 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)16 LinkedHashMap (java.util.LinkedHashMap)15 Test (org.junit.Test)15 HashMap (java.util.HashMap)11