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();
}
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();
}
Aggregations