Search in sources :

Example 31 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class BatchExecutorTestcase method testCancel.

/**
 * Test if canceling using the .cancel-file works.
 *
 * @throws Exception if an error occurs
 */
@Test
public void testCancel() throws Exception {
    final MutableInteger ret = new MutableInteger(-1);
    final AtomicReference<Exception> exception = new AtomicReference<Exception>();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                ret.setValue(BatchExecutor.mainRun(new String[] { "-workflowFile=" + standardTestWorkflowZip.getAbsolutePath(), "-nosave", "-workflow.variable=skipLongrunner,0,int" }));
            } catch (Exception ex) {
                exception.set(ex);
            }
        }
    };
    t.start();
    Thread.sleep(2000);
    File ws = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile();
    File cancelFile = new File(ws, ".cancel");
    cancelFile.createNewFile();
    t.join(5000);
    assertFalse("Workflow did not finish 5000ms after cancel request", t.isAlive());
    assertEquals("Wrong return value for canceled execution", BatchExecutor.EXIT_ERR_EXECUTION, ret.intValue());
    if (exception.get() != null) {
        throw exception.get();
    }
    t.interrupt();
}
Also used : MutableInteger(org.knime.core.util.MutableInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) ZipFile(java.util.zip.ZipFile) File(java.io.File) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 32 with MutableInteger

use of org.knime.core.util.MutableInteger in project knime-core by knime.

the class RowKeyUtil method changeRowKey.

/**
 * <p>Replaces the row key by the values of the column with the given name
 * and appends a new column with the old key values if the
 * <code>newColName</code> variable is a non empty <code>String</code>.</p>
 * <p>
 * Call the {@link RowKeyUtil#getDuplicatesCounter()} and
 * {@link RowKeyUtil#getMissingValueCounter()}
 * methods to get information about the replaced duplicates and missing
 * values after this method is completed.
 * </p>
 * @param inData The {@link BufferedDataTable} with the input data
 * @param exec the {@link ExecutionContext} to check for cancel and to
 * provide status messages
 * @param selRowKeyColName the name of the column which should replace
 * the row key or <code>null</code> if a new one should be created
 * @param appendColumn <code>true</code> if a new column should be created
 * @param newColSpec the {@link DataColumnSpec} of the new column or
 * <code>null</code>  if no column should be created at all
 * @param ensureUniqueness if set to <code>true</code> the method ensures
 * the uniqueness of the row key even if the values of the selected row
 * aren't unique
 * @param replaceMissingVals if set to <code>true</code> the method
 * replaces missing values with ?
 * @param removeRowKeyCol removes the selected row key column if set
 * to <code>true</code>
 * @param hiliteMap <code>true</code> if a map should be maintained that
 * maps the new row id to the old row id
 * @return the {@link BufferedDataTable} with the replaced row key and
 * the optional appended new column with the old row keys.
 * @throws Exception if the cancel button was pressed or the input data
 * isn't valid.
 */
public BufferedDataTable changeRowKey(final BufferedDataTable inData, final ExecutionContext exec, final String selRowKeyColName, final boolean appendColumn, final DataColumnSpec newColSpec, final boolean ensureUniqueness, final boolean replaceMissingVals, final boolean removeRowKeyCol, final boolean hiliteMap) throws Exception {
    LOGGER.debug("Entering changeRowKey(inData, exec, selRowKeyColName, " + "newColName) of class RowKeyUtil.");
    final DataTableSpec inSpec = inData.getDataTableSpec();
    DataTableSpec outSpec = inSpec;
    if (removeRowKeyCol) {
        outSpec = createTableSpec(outSpec, selRowKeyColName);
    }
    if (appendColumn) {
        if (newColSpec == null) {
            throw new NullPointerException("NewColumnSpec must not be null");
        }
        outSpec = AppendedColumnTable.getTableSpec(outSpec, newColSpec);
    }
    final BufferedDataContainer newContainer = exec.createDataContainer(outSpec, false);
    final int noOfCols = outSpec.getNumColumns();
    final int newRowKeyColIdx;
    if (selRowKeyColName != null) {
        newRowKeyColIdx = inSpec.findColumnIndex(selRowKeyColName);
        if (newRowKeyColIdx < 0) {
            throw new InvalidSettingsException("Column name not found.");
        }
    } else {
        newRowKeyColIdx = -1;
    }
    final int totalNoOfRows = inData.getRowCount();
    if (hiliteMap) {
        m_hiliteMapping = new HashMap<RowKey, Set<RowKey>>(totalNoOfRows);
    }
    final Map<String, MutableInteger> vals = new HashMap<String, MutableInteger>(totalNoOfRows);
    final double progressPerRow = 1.0 / totalNoOfRows;
    // update the progress monitor every percent
    final int checkPoint = Math.max((totalNoOfRows / 1000), 1);
    int rowCounter = 0;
    exec.setProgress(0.0, "Processing data...");
    m_missingValueCounter = 0;
    m_duplicatesCounter = 0;
    for (final DataRow row : inData) {
        rowCounter++;
        final DataCell[] cells = new DataCell[noOfCols];
        int newCellCounter = 0;
        for (int i = 0, length = inSpec.getNumColumns(); i < length; i++) {
            if (removeRowKeyCol && i == newRowKeyColIdx) {
                continue;
            }
            cells[newCellCounter++] = row.getCell(i);
        }
        if (appendColumn) {
            cells[noOfCols - 1] = new StringCell(row.getKey().getString());
        }
        final RowKey newKeyVal;
        if (newRowKeyColIdx >= 0) {
            final DataCell keyCell = row.getCell(newRowKeyColIdx);
            String key = null;
            if (keyCell.isMissing()) {
                if (replaceMissingVals) {
                    key = MISSING_VALUE_REPLACEMENT;
                    m_missingValueCounter++;
                } else {
                    throw new InvalidSettingsException("Missing value found in row " + rowCounter);
                }
            } else {
                key = keyCell.toString();
            }
            if (ensureUniqueness) {
                if (vals.containsKey(key)) {
                    if (!keyCell.isMissing()) {
                        m_duplicatesCounter++;
                    }
                    StringBuilder uniqueKey = new StringBuilder(key);
                    final MutableInteger index = vals.get(uniqueKey.toString());
                    while (vals.containsKey(uniqueKey.toString())) {
                        index.inc();
                        uniqueKey = new StringBuilder(key);
                        uniqueKey.append("(");
                        uniqueKey.append(index.toString());
                        uniqueKey.append(")");
                    }
                    key = uniqueKey.toString();
                }
                // put the current key which is new into the values map
                final MutableInteger index = new MutableInteger(0);
                vals.put(key, index);
            }
            newKeyVal = new RowKey(key);
        } else {
            newKeyVal = RowKey.createRowKey(rowCounter);
        }
        final DefaultRow newRow = new DefaultRow(newKeyVal, cells);
        newContainer.addRowToTable(newRow);
        if (hiliteMap) {
            final Set<RowKey> oldKeys = new HashSet<RowKey>(1);
            oldKeys.add(row.getKey());
            m_hiliteMapping.put(newKeyVal, oldKeys);
        }
        exec.checkCanceled();
        if (rowCounter % checkPoint == 0) {
            exec.setProgress(progressPerRow * rowCounter, rowCounter + " rows of " + totalNoOfRows + " rows processed.");
        }
    }
    exec.setProgress(1.0, "Finished");
    newContainer.close();
    return newContainer.getTable();
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) HashSet(java.util.HashSet) Set(java.util.Set) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) RowKey(org.knime.core.data.RowKey) HashMap(java.util.HashMap) MutableInteger(org.knime.core.util.MutableInteger) DataRow(org.knime.core.data.DataRow) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) HashSet(java.util.HashSet)

Aggregations

MutableInteger (org.knime.core.util.MutableInteger)32 DataCell (org.knime.core.data.DataCell)12 HashMap (java.util.HashMap)11 DataRow (org.knime.core.data.DataRow)8 RowKey (org.knime.core.data.RowKey)7 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)6 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)6 HashSet (java.util.HashSet)5 DataTableSpec (org.knime.core.data.DataTableSpec)5 DefaultRow (org.knime.core.data.def.DefaultRow)5 BufferedDataTable (org.knime.core.node.BufferedDataTable)5 Set (java.util.Set)4 DataColumnSpec (org.knime.core.data.DataColumnSpec)4 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 DoubleCell (org.knime.core.data.def.DoubleCell)3 StringCell (org.knime.core.data.def.StringCell)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2