Search in sources :

Example 76 with DataType

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

the class ColorExtractNodeModel method extractColorTable.

/**
 * @param nom
 * @return
 * @throws InvalidSettingsException
 */
private DataTable extractColorTable(final ColorModelNominal nom) throws InvalidSettingsException {
    DataType superType = null;
    for (DataCell c : nom) {
        if (superType == null) {
            superType = c.getType();
        } else {
            superType = DataType.getCommonSuperType(superType, c.getType());
        }
    }
    if (superType == null) {
        throw new InvalidSettingsException("No nominal values in model");
    }
    DataTableSpec spec = createSpec(superType);
    DataContainer cnt = new DataContainer(spec);
    int counter = 0;
    for (DataCell c : nom) {
        Color clr = nom.getColorAttr(c).getColor();
        DataRow row = new DefaultRow(RowKey.createRowKey(counter++), c, new IntCell(clr.getRed()), new IntCell(clr.getGreen()), new IntCell(clr.getBlue()), new IntCell(clr.getAlpha()), new IntCell(clr.getRGB()));
        cnt.addRowToTable(row);
    }
    cnt.close();
    return cnt.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataContainer(org.knime.core.data.container.DataContainer) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) Color(java.awt.Color) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) DataRow(org.knime.core.data.DataRow) IntCell(org.knime.core.data.def.IntCell)

Example 77 with DataType

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

the class DataContainerTest method testRestoreIntoMemory.

// testBigFile()
/**
 * Restoring into main memory.
 * @see ContainerTable#restoreIntoMemory()
 */
public void testRestoreIntoMemory() {
    // with these setting (50, 100) 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, true, 0);
    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);
        row = null;
    }
    container.close();
    assertTrue(container.getBufferedTable().getBuffer().usesOutFile());
    final Throwable[] throwables = new Throwable[1];
    final ContainerTable table = container.getBufferedTable();
    table.restoreIntoMemory();
    // different iterators restore the content, each of which one row
    RowIterator[] its = new RowIterator[10];
    for (int i = 0; i < its.length; i++) {
        its[i] = table.iterator();
        for (int count = 0; count < i + 1; count++) {
            its[i].next();
        }
    }
    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 : 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 78 with DataType

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

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

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

the class DefaultTableTest method testGetColumnType.

// testGetColumnName()
/**
 * Tests if column types as given in constructor are returned when
 * <code>getColumnType(int)</code> is called. If constructor parameter is
 * <code>null</code> it must return <code>DataCell.class</code> for
 * every column.
 *
 * @see DataColumnSpec#getType()
 */
public final void testGetColumnType() {
    DefaultTable auto = new DefaultTable(CASE_1_CONTENT, CASE_1_ROWHEADER, CASE_1_COLHEADER);
    for (int c = 0; c < CASE_1_COLHEADER.length; c++) {
        DataType t = auto.getDataTableSpec().getColumnSpec(c).getType();
        assertTrue(t.isCompatible(StringValue.class));
    }
}
Also used : DataType(org.knime.core.data.DataType) StringValue(org.knime.core.data.StringValue)

Aggregations

DataType (org.knime.core.data.DataType)330 DataColumnSpec (org.knime.core.data.DataColumnSpec)142 DataTableSpec (org.knime.core.data.DataTableSpec)101 DataCell (org.knime.core.data.DataCell)96 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)95 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)71 DoubleValue (org.knime.core.data.DoubleValue)67 DataRow (org.knime.core.data.DataRow)61 ArrayList (java.util.ArrayList)55 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)34 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)32 DefaultRow (org.knime.core.data.def.DefaultRow)24 HashSet (java.util.HashSet)23 HashMap (java.util.HashMap)20 StringCell (org.knime.core.data.def.StringCell)20 NominalValue (org.knime.core.data.NominalValue)18 DoubleCell (org.knime.core.data.def.DoubleCell)18 IntCell (org.knime.core.data.def.IntCell)18 BitVectorValue (org.knime.core.data.vector.bitvector.BitVectorValue)18 ByteVectorValue (org.knime.core.data.vector.bytevector.ByteVectorValue)18