Search in sources :

Example 6 with DataTable

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

the class AbstractTableSorter method sortInMemory.

private DataTable sortInMemory(final ExecutionMonitor exec) throws CanceledExecutionException {
    final DataTable dataTable = m_inputTable;
    List<DataRow> rowList = new ArrayList<DataRow>();
    int progress = 0;
    final long rowCount = m_rowsInInputTable;
    exec.setMessage("Reading data");
    ExecutionMonitor readExec = exec.createSubProgress(0.5);
    for (final DataRow r : dataTable) {
        readExec.checkCanceled();
        if (rowCount > 0) {
            readExec.setProgress(progress / (double) rowCount, r.getKey().getString());
        } else {
            readExec.setMessage(r.getKey() + " (row " + progress + ")");
        }
        rowList.add(r);
        progress++;
    }
    // "rowCount" as it might not be set)
    if (rowList.size() <= 1) {
        return m_inputTable;
    }
    exec.setMessage("Sorting");
    Collections.sort(rowList, m_rowComparator);
    exec.setMessage("Creating sorted table");
    final DataContainer dc = createDataContainer(dataTable.getDataTableSpec(), false);
    ExecutionMonitor writeExec = exec.createSubProgress(0.5);
    progress = 0;
    for (DataRow r : rowList) {
        exec.checkCanceled();
        if (rowCount > 0) {
            writeExec.setProgress(progress / (double) rowCount, r.getKey().getString());
        } else {
            writeExec.setMessage(r.getKey() + " (row " + progress + ")");
        }
        dc.addRowToTable(r);
        progress++;
    }
    dc.close();
    return dc.getTable();
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataContainer(org.knime.core.data.container.DataContainer) ArrayList(java.util.ArrayList) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) DataRow(org.knime.core.data.DataRow)

Example 7 with DataTable

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

the class AbstractTableSorter method sortOnDisk.

/**
 * Sorts the given data table using a disk-based k-way merge sort.
 *
 * @param dataTable the data table that sgetRowCounthould be sorted
 * @param exec an execution context for reporting progress and creating BufferedDataContainers
 * @throws CanceledExecutionException if the user has canceled execution
 */
private DataTable sortOnDisk(final ExecutionMonitor exec) throws CanceledExecutionException {
    final DataTable dataTable = m_inputTable;
    m_progress = 0.0;
    m_incProgress = m_rowsInInputTable <= 0 ? -1.0 : 1.0 / (2.0 * m_rowsInInputTable);
    long counter = createInitialChunks(exec, dataTable);
    // (can't rely on global rowCount - might not be set)
    if (counter <= 1) {
        return m_inputTable;
    }
    exec.setMessage("Merging temporary tables");
    // The final output container
    // merge chunks until there are only so much left, as m_maxopencontainers
    Iterator<DataRow> result = mergeChunks(exec, false);
    // add results to the final container
    // The final output container, leave it to the
    // system to do the caching (bug 1809)
    DataContainer resultContainer = createDataContainer(dataTable.getDataTableSpec(), false);
    while (result.hasNext()) {
        resultContainer.addRowToTable(result.next());
    }
    resultContainer.close();
    return resultContainer.getTable();
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataContainer(org.knime.core.data.container.DataContainer) DataRow(org.knime.core.data.DataRow)

Example 8 with DataTable

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

the class TableContentView method addColumn.

/**
 * Overridden to set proper header content and apply renderer. The
 * header of the column will be set to the <code>DataTable</code>'s
 * <code>DataColumnSpec</code> and for the renderer the
 * type's <code>getNewRenderer()</code> is used
 *
 * @param aColumn column to be added
 * @see javax.swing.JTable#addColumn(javax.swing.table.TableColumn)
 * @see org.knime.core.data.DataType#getRenderer(DataColumnSpec)
 * @see DataColumnSpec
 */
@Override
public void addColumn(final TableColumn aColumn) {
    assert (hasData());
    int i = aColumn.getModelIndex();
    aColumn.sizeWidthToFit();
    DataTable data = getContentModel().getDataTable();
    DataColumnSpec headerValue = data.getDataTableSpec().getColumnSpec(i);
    aColumn.setHeaderValue(headerValue);
    DataValueRendererFamily renderer = getRendererFamily(headerValue);
    String[] descs = renderer.getRendererDescriptions();
    // setting a certain column property will set a preferred renderer
    String preferredRenderer = headerValue.getProperties().getProperty(DataValueRenderer.PROPERTY_PREFERRED_RENDERER);
    if (Arrays.asList(descs).contains(preferredRenderer) && renderer.accepts(preferredRenderer, headerValue)) {
        renderer.setActiveRenderer(preferredRenderer);
    } else {
        for (String s : descs) {
            if (renderer.accepts(s, headerValue)) {
                renderer.setActiveRenderer(s);
                break;
            }
        }
    }
    aColumn.setCellRenderer(renderer);
    super.addColumn(aColumn);
}
Also used : DataTable(org.knime.core.data.DataTable) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataValueRendererFamily(org.knime.core.data.renderer.DataValueRendererFamily)

Example 9 with DataTable

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

the class TableContentView method setModel.

// TableContentView(DataTable)
/**
 * Checks that the given argument is of type {@link TableContentModel}
 * and throws exception if it is not. Otherwise it sets the new model and
 * updates the table.
 *
 * @param tableModel the new model, necessarily of type
 *        {@link TableContentModel}
 * @see javax.swing.JTable#setModel(javax.swing.table.TableModel)
 * @throws ClassCastException if dataModel not of type
 *         {@link TableContentModel}
 * @throws IllegalArgumentException if <code>dataModel</code> is
 *         <code>null</code> as done by
 *         <code>JTable.setModel(TableModel)</code>
 */
@Override
public void setModel(final TableModel tableModel) {
    TableContentModel tblModel = (TableContentModel) tableModel;
    if (m_dataListener == null) {
        // may be null when in <init>
        m_dataListener = new PropertyChangeListener() {

            @Override
            public void propertyChange(final PropertyChangeEvent evt) {
                String id = evt.getPropertyName();
                if (!id.equals(TableContentModel.PROPERTY_DATA)) {
                    return;
                }
                TableColumnModel tcM = getColumnModel();
                if (hasData()) {
                    DataTable data = getContentModel().getDataTable();
                    DataTableSpec spec = data.getDataTableSpec();
                    for (int i = 0; i < tcM.getColumnCount(); i++) {
                        int colInModel = convertColumnIndexToModel(i);
                        DataColumnSpec headerValue = spec.getColumnSpec(colInModel);
                        tcM.getColumn(i).setHeaderValue(headerValue);
                    }
                }
                // repaint sort icon
                getTableHeader().revalidate();
                // property data update to row header view
                // (only sort icon - data update done via TableModelEvent)
                firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
            }
        };
    } else {
        getContentModel().removePropertyChangeListener(m_dataListener);
    }
    tblModel.addPropertyChangeListener(m_dataListener);
    super.setModel(tblModel);
}
Also used : DataTable(org.knime.core.data.DataTable) PropertyChangeEvent(java.beans.PropertyChangeEvent) DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpec(org.knime.core.data.DataColumnSpec) PropertyChangeListener(java.beans.PropertyChangeListener) TableColumnModel(javax.swing.table.TableColumnModel)

Example 10 with DataTable

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

the class TableContentModel method setDataTableIntern.

/**
 * Sets new data for this table. The table argument may be
 * <code>null</code> to indicate invalid data (nothing displayed).
 */
private void setDataTableIntern(final DataTable originalData, final DataTable data, final TableSortOrder sortOrder) {
    assert SwingUtilities.isEventDispatchThread();
    if (m_data == data) {
        // do not start event storm
        return;
    }
    boolean clearOldTable = m_tableSortOrder != null;
    if (m_tableSorterWorker != null) {
        m_tableSorterWorker.cancel(true);
        m_tableSorterWorker = null;
    }
    m_tableSortOrder = sortOrder;
    cancelRowCountingInBackground();
    int oldColCount = getColumnCount();
    int newColCount = data != null ? data.getDataTableSpec().getNumColumns() : 0;
    int oldRowCount = getRowCount();
    DataTable oldData = m_data;
    m_originalUnsortedTable = originalData;
    m_data = data;
    m_cachedRows = null;
    m_hilitSet = null;
    if (m_iterator instanceof CloseableRowIterator) {
        ((CloseableRowIterator) m_iterator).close();
    }
    m_iterator = null;
    m_rowCountOfInterestInIterator = 0;
    m_rowCountOfInterest = 0;
    m_maxRowCount = 0;
    cancelRowCountingInBackground();
    m_isMaxRowCountFinal = true;
    m_isRowCountOfInterestFinal = true;
    boolean structureChanged = oldColCount != newColCount;
    if (oldColCount == newColCount) {
        if (oldRowCount > 0) {
            fireTableRowsDeleted(0, oldRowCount - 1);
        }
        if (newColCount > 0) {
            structureChanged = !data.getDataTableSpec().equalStructure(oldData.getDataTableSpec());
        }
    }
    if (data != null) {
        // new data available, release old stuff
        // assume that there are rows, may change in cacheNextRow() below
        m_isMaxRowCountFinal = false;
        m_isRowCountOfInterestFinal = false;
        final long rowCountFromTable;
        if (data instanceof BufferedDataTable) {
            rowCountFromTable = ((BufferedDataTable) data).size();
        } else if (data instanceof ContainerTable) {
            rowCountFromTable = ((ContainerTable) data).size();
        } else {
            // unknown
            rowCountFromTable = -1;
        }
        if (rowCountFromTable >= 0) {
            m_isMaxRowCountFinal = true;
            if (rowCountFromTable > Integer.MAX_VALUE) {
                NodeLogger.getLogger(getClass()).warn("Table view will show only the first " + Integer.MAX_VALUE + " rows of " + rowCountFromTable + ".");
                m_maxRowCount = Integer.MAX_VALUE;
            } else {
                m_maxRowCount = (int) rowCountFromTable;
            }
            if (!m_tableFilter.performsFiltering()) {
                m_rowCountOfInterest = m_maxRowCount;
                m_isRowCountOfInterestFinal = true;
            }
        }
        int cacheSize = getCacheSize();
        m_cachedRows = new DataRow[cacheSize];
        m_hilitSet = new BitSet(cacheSize);
        // will instantiate a new iterator.
        clearCache();
        // will also set m_isRowCountOfInterestFinal etc. accordingly
        cacheNextRow();
    }
    if (structureChanged) {
        // notify listeners
        fireTableStructureChanged();
    } else {
        int newRowCount = getRowCount();
        if (newRowCount > 0) {
            fireTableRowsInserted(0, newRowCount);
        }
    }
    m_propertySupport.firePropertyChange(PROPERTY_DATA, oldData, m_data);
    if (clearOldTable && oldData instanceof ContainerTable) {
        ((ContainerTable) oldData).clear();
    }
}
Also used : DataTable(org.knime.core.data.DataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) BitSet(java.util.BitSet) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) ContainerTable(org.knime.core.data.container.ContainerTable)

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