Search in sources :

Example 16 with CloseableRowIterator

use of org.knime.core.data.container.CloseableRowIterator in project knime-core by knime.

the class TestSubnode_StreamingPortObject method checkTableView.

private void checkTableView() throws Exception {
    NativeNodeContainer tableViewNNC = (NativeNodeContainer) findNodeContainer(m_tableViewInSubNode_8_8);
    checkState(tableViewNNC, EXECUTED);
    BufferedDataTable table = (BufferedDataTable) tableViewNNC.getNode().getInternalHeldPortObjects()[0];
    assertNotNull(table);
    try (CloseableRowIterator it = table.iterator()) {
        assertTrue(it.hasNext());
        assertNotNull(it.next());
    }
}
Also used : BufferedDataTable(org.knime.core.node.BufferedDataTable) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator)

Example 17 with CloseableRowIterator

use of org.knime.core.data.container.CloseableRowIterator in project knime-core by knime.

the class ReferenceColumnResorterNodeModel method readOrderFromTable.

/**
 * @param orderTable
 * @return
 */
private String[] readOrderFromTable(final BufferedDataTable orderTable) {
    int orderCol = orderTable.getSpec().findColumnIndex(m_orderCol.getStringValue());
    // get order from input[1]
    CloseableRowIterator rows = orderTable.iterator();
    List<String> orderList = new ArrayList<String>();
    while (rows.hasNext()) {
        orderList.add(rows.next().getCell(orderCol).toString());
    }
    switch(m_strategy.getStringValue()) {
        case "Last":
            orderList.add(UNKNOWN_COL_DUMMY.getName());
            break;
        case "First":
            orderList.add(0, UNKNOWN_COL_DUMMY.getName());
            break;
        case "Drop":
            break;
        default:
            throw new IllegalStateException("Node should have failed during configuration");
    }
    String[] order = orderList.toArray(new String[orderList.size()]);
    return order;
}
Also used : ArrayList(java.util.ArrayList) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString)

Example 18 with CloseableRowIterator

use of org.knime.core.data.container.CloseableRowIterator in project knime-core by knime.

the class TargetShufflingNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    final int colIndex = inData[0].getDataTableSpec().findColumnIndex(m_settings.columnName());
    final String colName = inData[0].getDataTableSpec().getColumnSpec(colIndex).getName();
    // create a new column rearranger from the input table
    ColumnRearranger colRe = new ColumnRearranger(inData[0].getDataTableSpec());
    for (DataColumnSpec c : inData[0].getDataTableSpec()) {
        if (!c.getName().equals(colName)) {
            // remove all columns except the selected one
            colRe.remove(c.getName());
        }
    }
    // append a new column with a random number for each cell
    String uniqueColumnName = DataTableSpec.getUniqueColumnName(inData[0].getDataTableSpec(), "random_col");
    colRe.append(new SingleCellFactory(new DataColumnSpecCreator(uniqueColumnName, LongCell.TYPE).createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            return new LongCell(m_random.nextLong());
        }
    });
    BufferedDataTable toSort = exec.createColumnRearrangeTable(exec.createBufferedDataTable(inData[0], exec), colRe, exec.createSilentSubProgress(.2));
    // sort the random numbers ---> shuffles the sorted column
    List<String> include = new ArrayList<String>();
    include.add(toSort.getDataTableSpec().getColumnSpec(1).getName());
    SortedTable sort = new SortedTable(toSort, include, new boolean[] { true }, exec.createSubExecutionContext(.6));
    final BufferedDataTable sorted = sort.getBufferedDataTable();
    // replace the selected column with the shuffled one
    final DataColumnSpec colSpec = inData[0].getDataTableSpec().getColumnSpec(colIndex);
    ColumnRearranger crea = new ColumnRearranger(inData[0].getDataTableSpec());
    crea.replace(new SingleCellFactory(colSpec) {

        private final CloseableRowIterator m_iterator = sorted.iterator();

        @Override
        public DataCell getCell(final DataRow row) {
            return m_iterator.next().getCell(0);
        }
    }, colName);
    return new BufferedDataTable[] { exec.createColumnRearrangeTable(inData[0], crea, exec.createSubProgress(0.2)) };
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ArrayList(java.util.ArrayList) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) LongCell(org.knime.core.data.def.LongCell) SortedTable(org.knime.base.data.sort.SortedTable) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 19 with CloseableRowIterator

use of org.knime.core.data.container.CloseableRowIterator in project knime-core by knime.

the class FixedPieNodeModel method createModel.

/**
 * {@inheritDoc}
 * @throws CanceledExecutionException
 * @throws TooManySectionsException
 */
@Override
protected void createModel(final ExecutionContext exec, final DataColumnSpec pieColSpec, final DataColumnSpec aggrColSpec, final BufferedDataTable dataTable, final int noOfRows, final boolean containsColorHandler) throws CanceledExecutionException, TooManySectionsException {
    m_model = new FixedPieDataModel(pieColSpec, aggrColSpec, containsColorHandler);
    final DataTableSpec spec = dataTable.getSpec();
    final int pieColIdx = spec.findColumnIndex(pieColSpec.getName());
    final int aggrColIdx;
    if (aggrColSpec == null) {
        aggrColIdx = -1;
    } else {
        aggrColIdx = spec.findColumnIndex(aggrColSpec.getName());
    }
    final double progressPerRow = 1.0 / noOfRows;
    double progress = 0.0;
    final CloseableRowIterator rowIterator = dataTable.iterator();
    try {
        for (int rowCounter = 0; rowCounter < noOfRows && rowIterator.hasNext(); rowCounter++) {
            final DataRow row = rowIterator.next();
            final Color rowColor = spec.getRowColor(row).getColor(false, false);
            final DataCell pieCell = row.getCell(pieColIdx);
            final DataCell aggrCell;
            if (aggrColIdx >= 0) {
                aggrCell = row.getCell(aggrColIdx);
            } else {
                aggrCell = null;
            }
            m_model.addDataRow(row, rowColor, pieCell, aggrCell);
            progress += progressPerRow;
            exec.setProgress(progress, "Adding data rows to pie chart...");
            exec.checkCanceled();
        }
    } finally {
        if (rowIterator != null) {
            rowIterator.close();
        }
    }
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) FixedPieDataModel(org.knime.base.node.viz.pie.datamodel.fixed.FixedPieDataModel) Color(java.awt.Color) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 20 with CloseableRowIterator

use of org.knime.core.data.container.CloseableRowIterator in project knime-core by knime.

the class FixedColumnHistogramNodeModel method createHistogramModel.

/**
 * {@inheritDoc}
 */
@Override
protected void createHistogramModel(final ExecutionContext exec, final int noOfRows, final BufferedDataTable table) throws CanceledExecutionException {
    LOGGER.debug("Entering createHistogramModel(exec, table) " + "of class FixedColumnHistogramNodeModel.");
    final Collection<ColorColumn> aggrColumns = getAggrColumns();
    final int noOfBins = BinningUtil.calculateIntegerMaxNoOfBins(m_noOfBins.getIntValue(), getXColSpec());
    m_model = new FixedHistogramDataModel(getXColSpec(), AggregationMethod.getDefaultMethod(), aggrColumns, noOfBins);
    if (noOfRows < 1) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("No rows available");
        }
        return;
    }
    exec.setMessage("Adding data rows to histogram...");
    final double progressPerRow = 1.0 / noOfRows;
    double progress = 0.0;
    int aggrColSize = 0;
    if (aggrColumns != null) {
        aggrColSize = aggrColumns.size();
    }
    final DataTableSpec tableSpec = getTableSpec();
    final int xColIdx = getXColIdx();
    // save the aggregation column indices
    final int[] aggrColIdxs = new int[aggrColSize];
    if (aggrColumns != null) {
        int idx = 0;
        for (final ColorColumn aggrCol : aggrColumns) {
            aggrColIdxs[idx++] = tableSpec.findColumnIndex(aggrCol.getColumnName());
        }
    }
    final CloseableRowIterator rowIterator = table.iterator();
    try {
        for (int rowCounter = 0; rowCounter < noOfRows && rowIterator.hasNext(); rowCounter++) {
            final DataRow row = rowIterator.next();
            final Color color = tableSpec.getRowColor(row).getColor(false, false);
            if (aggrColSize < 1) {
                m_model.addDataRow(row.getKey(), color, row.getCell(xColIdx), DataType.getMissingCell());
            } else {
                final DataCell[] aggrCells = new DataCell[aggrColSize];
                for (int i = 0, length = aggrColIdxs.length; i < length; i++) {
                    aggrCells[i] = row.getCell(aggrColIdxs[i]);
                }
                m_model.addDataRow(row.getKey(), color, row.getCell(xColIdx), aggrCells);
            }
            progress += progressPerRow;
            exec.setProgress(progress, "Adding data rows to histogram...");
            exec.checkCanceled();
        }
    } finally {
        if (rowIterator != null) {
            rowIterator.close();
        }
    }
    exec.setMessage("Sorting rows...");
    exec.setProgress(1.0, "Histogram finished.");
    LOGGER.debug("Exiting createHistogramModel(exec, table) " + "of class FixedColumnHistogramNodeModel.");
}
Also used : FixedHistogramDataModel(org.knime.base.node.viz.histogram.datamodel.FixedHistogramDataModel) DataTableSpec(org.knime.core.data.DataTableSpec) ColorColumn(org.knime.base.node.viz.histogram.util.ColorColumn) Color(java.awt.Color) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Aggregations

CloseableRowIterator (org.knime.core.data.container.CloseableRowIterator)21 DataRow (org.knime.core.data.DataRow)13 BufferedDataTable (org.knime.core.node.BufferedDataTable)12 DataCell (org.knime.core.data.DataCell)8 DataTableSpec (org.knime.core.data.DataTableSpec)6 ArrayList (java.util.ArrayList)5 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)4 Color (java.awt.Color)3 HashMap (java.util.HashMap)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 DataColumnSpec (org.knime.core.data.DataColumnSpec)2 DefaultRow (org.knime.core.data.def.DefaultRow)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 Interpreter (bsh.Interpreter)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 IOException (java.io.IOException)1 BitSet (java.util.BitSet)1 Collection (java.util.Collection)1