Search in sources :

Example 11 with CloseableRowIterator

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

the class Joiner method compareDuplicates.

/**
 * @param leftTable The left input table
 * @param rightTable The right input table
 * @param duplicates The list of columns to test for identity
 */
private void compareDuplicates(final BufferedDataTable leftTable, final BufferedDataTable rightTable, final List<String> duplicates) {
    int[] leftIndex = getIndicesOf(leftTable, duplicates);
    int[] rightIndex = getIndicesOf(rightTable, duplicates);
    String[] messages = new String[duplicates.size()];
    CloseableRowIterator leftIter = leftTable.iterator();
    CloseableRowIterator rightIter = rightTable.iterator();
    while (leftIter.hasNext()) {
        if (!rightIter.hasNext()) {
            // right table has less rows
            m_runtimeWarnings.add("Possible problem in configuration " + "found. The \"Duplicate Column Handling\" is " + "configured to  filter duplicates, but the " + "duplicate columns are not equal since the " + "top table has more elements than the bottom " + "table.");
            break;
        }
        DataRow left = leftIter.next();
        DataRow right = rightIter.next();
        for (int i = 0; i < duplicates.size(); i++) {
            if (null == messages[i] && !left.getCell(leftIndex[i]).equals(right.getCell(rightIndex[i]))) {
                // Two cells do not match
                messages[i] = "The column \"" + duplicates.get(i) + "\" can be found in " + "both input tables but the content is not " + "equal. " + "Only the one in the top input table will show " + "up in the output table. Please change the " + "Duplicate Column Handling if both columns " + "should show up in the output table.";
            }
        }
    }
    if (rightIter.hasNext()) {
        // right table has more rows
        m_runtimeWarnings.add("Possible problem in configuration found. " + "The \"Duplicate Column Handling\" is configured to " + "filter duplicates, but the duplicate columns are not " + "equal since the bottom table has more elements than the " + "top table.");
    }
    for (int i = 0; i < duplicates.size(); i++) {
        if (null != messages[i]) {
            m_runtimeWarnings.add(messages[i]);
        }
    }
}
Also used : CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataRow(org.knime.core.data.DataRow)

Example 12 with CloseableRowIterator

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

the class CrossJoinerNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    boolean showLeft = m_showLeft.getBooleanValue();
    boolean showRight = m_showRight.getBooleanValue();
    int chunksize = m_cacheSize.getIntValue();
    String sep = m_rkseparator.getStringValue();
    DataContainer dc = exec.createDataContainer(createSpec(inData[0].getDataTableSpec(), inData[1].getDataTableSpec(), showLeft, showRight));
    long numOutRows = inData[0].size() * inData[1].size();
    long rowcounter = 0;
    CloseableRowIterator leftit = inData[0].iterator();
    // iterate over all possible chunks of left table
    for (long chunkcount = 0; chunkcount < Math.ceil(inData[0].size() * 1.0 / chunksize); chunkcount++) {
        // read one chunk of left table
        List<DataRow> rowsleft = new LinkedList<DataRow>();
        for (int i = 0; i < chunksize && leftit.hasNext(); i++) {
            rowsleft.add(leftit.next());
            exec.checkCanceled();
        }
        // iterate over all possible chunks of right table
        CloseableRowIterator rightit = inData[1].iterator();
        for (long chunkcount2 = 0; chunkcount2 < Math.ceil(inData[1].size() * 1.0 / chunksize); chunkcount2++) {
            // read  one chunk of right table
            List<DataRow> rowsright = new LinkedList<DataRow>();
            for (int i = 0; i < chunksize && rightit.hasNext(); i++) {
                rowsright.add(rightit.next());
            }
            for (DataRow left : rowsleft) {
                for (DataRow right : rowsright) {
                    DataRow newRow = joinRow(left, right, showLeft, showRight, sep);
                    dc.addRowToTable(newRow);
                    exec.checkCanceled();
                    exec.setProgress(rowcounter++ / (double) numOutRows, "Generating Row " + newRow.getKey().toString());
                }
            }
        }
        rightit.close();
    }
    leftit.close();
    dc.close();
    return new BufferedDataTable[] { (BufferedDataTable) dc.getTable() };
}
Also used : DataContainer(org.knime.core.data.container.DataContainer) BufferedDataTable(org.knime.core.node.BufferedDataTable) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) LinkedList(java.util.LinkedList)

Example 13 with CloseableRowIterator

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

the class EditNominalDomainDicNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    final BufferedDataTable additionalValues = inData[1];
    final DataTableSpec orgSpec = inData[0].getDataTableSpec();
    final DataTableSpec valueSpec = additionalValues.getDataTableSpec();
    Map<Integer, Set<DataCell>> doCreateSpec = createNewSpec(orgSpec, valueSpec, new NewDomainValuesAdder<CanceledExecutionException>() {

        private long m_currentRowIndex = 0;

        private final long m_size = additionalValues.size();

        @Override
        void addNewDomainValues(final Map<Integer, String> orgIndexToColNameMap, final Map<Integer, Set<DataCell>> orgIndexToNewDomainValuesMap) throws CanceledExecutionException {
            CloseableRowIterator addValuesIterator = additionalValues.iterator();
            try {
                Collection<String> values = orgIndexToColNameMap.values();
                while (addValuesIterator.hasNext()) {
                    DataRow currRow = addValuesIterator.next();
                    // 
                    exec.setProgress(// 
                    m_currentRowIndex++ / (double) m_size, "adding values to domain of row: " + currRow.getKey().getString());
                    for (String addValRowId : values) {
                        DataCell cell = currRow.getCell(valueSpec.findColumnIndex(addValRowId));
                        if (!cell.isMissing()) {
                            orgIndexToNewDomainValuesMap.get(orgSpec.findColumnIndex(addValRowId)).add(cell);
                        }
                    }
                    exec.checkCanceled();
                }
            } finally {
                addValuesIterator.close();
            }
        }
    });
    return new BufferedDataTable[] { exec.createSpecReplacerTable(inData[0], mergeTableSpecs(orgSpec, doCreateSpec).createSpec()) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataRow(org.knime.core.data.DataRow) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) BufferedDataTable(org.knime.core.node.BufferedDataTable) Collection(java.util.Collection) DataCell(org.knime.core.data.DataCell)

Example 14 with CloseableRowIterator

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

the class EditNumericDomainNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // 
    final DataTableSpec originalTableSpec = inData[0].getDataTableSpec();
    DataTableSpec editedTableSpec = processDomainSettings(inData[0].getDataTableSpec());
    FilterResult filterResult = m_configuration.getColumnspecFilterConfig().applyTo(originalTableSpec);
    List<String> includedColumnNames = Arrays.asList(filterResult.getIncludes());
    long currentRowIndex = 0;
    long size = inData[0].size();
    String[] tableNames = originalTableSpec.getColumnNames();
    Map<Integer, DataColumnSpec> map = new HashMap<Integer, DataColumnSpec>();
    for (int i = 0; i < originalTableSpec.getNumColumns(); i++) {
        if (includedColumnNames.contains(tableNames[i])) {
            map.put(i, editedTableSpec.getColumnSpec(i));
        } else {
            map.put(i, originalTableSpec.getColumnSpec(i));
        }
    }
    CloseableRowIterator rowIterator = inData[0].iterator();
    long rowIndex = 0;
    try {
        while (rowIterator.hasNext()) {
            DataRow currentRow = rowIterator.next();
            // 
            exec.setProgress(// 
            currentRowIndex++ / (double) size, "checking domains of row: " + currentRow.getKey().getString());
            for (String colName : includedColumnNames) {
                int currIndex = originalTableSpec.findColumnIndex(colName);
                DataCell currCell = currentRow.getCell(currIndex);
                if (!currCell.isMissing() && outOfDomain(currCell, map.get(currIndex))) {
                    switch(m_configuration.getDomainOverflowPolicy()) {
                        case CALCULATE_BOUNDS:
                            map.put(currIndex, calculateAndCreateBoundedColumnSpec(currCell, map.get(currIndex)));
                            break;
                        case USE_EXISTING_BOUNDS:
                            map.put(currIndex, originalTableSpec.getColumnSpec(currIndex));
                            break;
                        default:
                            throw new EditNumericDomainOverflowException(tableNames[currIndex], ((DoubleValue) currCell).getDoubleValue(), m_configuration.getLowerBound(), m_configuration.getUpperBound(), rowIndex, currentRow.getKey());
                    }
                }
            }
            exec.checkCanceled();
            rowIndex++;
        }
    } finally {
        rowIterator.close();
    }
    DataTableSpecCreator newTableSpecCreator = new DataTableSpecCreator().setName(originalTableSpec.getName()).putProperties(originalTableSpec.getProperties());
    for (int i = 0; i < originalTableSpec.getNumColumns(); i++) {
        newTableSpecCreator.addColumns(map.get(i));
    }
    return new BufferedDataTable[] { exec.createSpecReplacerTable(inData[0], newTableSpecCreator.createSpec()) };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) HashMap(java.util.HashMap) DataTableSpecCreator(org.knime.core.data.DataTableSpecCreator) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) FilterResult(org.knime.core.node.util.filter.NameFilterConfiguration.FilterResult)

Example 15 with CloseableRowIterator

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

the class TestSubnodeView method validateReexecutionResult.

private void validateReexecutionResult() throws Exception {
    // validate results
    Map<String, String> newValueMap = buildValueMap();
    String stringInputValue = newValueMap.get(m_stringInputID.toString());
    assertNotNull("Value for string input node should exist", stringInputValue);
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonValue = mapper.readTree(stringInputValue);
    assertNotNull("String input value should contain new string value", jsonValue.get("string"));
    assertEquals("String input value should be '" + CHANGED_URL + "'", CHANGED_URL, jsonValue.get("string").asText());
    // check one row was selected and filtered
    NodeContainer container = getManager().getNodeContainer(m_subnodeID);
    BufferedDataTable table = (BufferedDataTable) container.getOutPort(1).getPortObject();
    assertEquals("Table should contain one selected row", 1, table.size());
    try (CloseableRowIterator it = table.iterator()) {
        DataRow row = it.next();
        assertEquals("Filtered row should be the previously selected one", "Row1", row.getKey().toString());
    }
    // check flow variable created from string input
    NodeContainer cacheContainer = getManager().getNodeContainer(m_blockID);
    FlowVariable var = cacheContainer.getFlowObjectStack().getAvailableFlowVariables().get("string-input");
    assertNotNull("Flow variable 'string input' should exist", var);
    assertEquals("Flow variable should contain new string value", CHANGED_URL, var.getStringValue());
}
Also used : BufferedDataTable(org.knime.core.node.BufferedDataTable) CloseableRowIterator(org.knime.core.data.container.CloseableRowIterator) JsonNode(com.fasterxml.jackson.databind.JsonNode) DataRow(org.knime.core.data.DataRow) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

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