use of org.knime.core.data.StringValue in project knime-core by knime.
the class TableColumnToVariableNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected PortObject[] execute(final PortObject[] inData, final ExecutionContext exec) throws Exception {
if (inData[0] instanceof BufferedDataTable) {
final BufferedDataTable table = (BufferedDataTable) inData[0];
final int colIndex = table.getSpec().findColumnIndex(m_column.getStringValue());
assert colIndex >= 0 : colIndex;
for (DataRow dataRow : table) {
DataCell cell = dataRow.getCell(colIndex);
if (cell.isMissing()) {
if (m_ignoreMissing.getBooleanValue()) {
continue;
}
throw new Exception("Missing value in column (" + m_column.getColumnName() + ") in row: " + dataRow.getKey());
}
if (cell instanceof IntValue) {
final IntValue iv = (IntValue) cell;
pushFlowVariableInt(dataRow.getKey().getString(), iv.getIntValue());
} else if (cell instanceof DoubleValue) {
final DoubleValue dv = (DoubleValue) cell;
pushFlowVariableDouble(dataRow.getKey().getString(), dv.getDoubleValue());
} else if (cell instanceof StringValue) {
final StringValue sv = (StringValue) cell;
pushFlowVariableString(dataRow.getKey().getString(), sv.getStringValue());
}
}
}
return new FlowVariablePortObject[] { FlowVariablePortObject.INSTANCE };
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class BitVectorGeneratorNodeModel method scanMaxPos.
private int scanMaxPos(final BufferedDataTable data, final ExecutionMonitor exec) {
int maxPos = Integer.MIN_VALUE;
int cellIdx = data.getDataTableSpec().findColumnIndex(m_stringColumn.getStringValue());
long nrRows = data.size();
long currRow = 0;
for (DataRow row : data) {
currRow++;
exec.setProgress((double) currRow / (double) nrRows, "scanning row " + currRow);
DataCell cell = row.getCell(cellIdx);
if (cell.isMissing()) {
continue;
}
if (!cell.getType().isCompatible(StringValue.class)) {
throw new RuntimeException("Found incompatible type in row " + row.getKey().getString());
}
String toParse = ((StringValue) cell).getStringValue();
String[] numbers = toParse.split("\\s");
for (int i = 0; i < numbers.length; i++) {
int pos = -1;
try {
pos = Integer.parseInt(numbers[i].trim());
maxPos = Math.max(maxPos, pos);
} catch (NumberFormatException nfe) {
// nothing to do here
// same exception will be logged from cell factory
}
}
}
return maxPos + 1;
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class IdString2BitVectorCellFactory method getCell.
/**
* {@inheritDoc}
*/
@Override
public DataCell getCell(final DataRow row) {
incrementNrOfRows();
if (!row.getCell(getColumnIndex()).getType().isCompatible(StringValue.class)) {
printError(LOGGER, row, "Cell in column " + getColumnIndex() + " is not a string value!");
return DataType.getMissingCell();
}
if (row.getCell(getColumnIndex()).isMissing()) {
return DataType.getMissingCell();
}
String toParse = ((StringValue) row.getCell(getColumnIndex())).getStringValue().trim();
toParse = toParse.trim();
try {
int newlySetBits = 0;
final BitVectorType type = getVectorType();
final BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(m_maxPos);
if (!toParse.isEmpty()) {
final String[] numbers = toParse.split("\\s");
for (int i = 0; i < numbers.length; i++) {
int pos = Integer.parseInt(numbers[i].trim());
if (pos < 0) {
printError(LOGGER, row, "Invalid negative index in index string: " + toParse);
return DataType.getMissingCell();
}
if (!factory.get(pos)) {
factory.set(pos);
newlySetBits++;
}
}
}
m_nrOfSetBits += newlySetBits;
return factory.createDataCell();
} catch (NumberFormatException nfe) {
String nfeMsg = nfe.getMessage();
if (nfeMsg == null) {
nfeMsg = "<sorry, no further details>";
}
printError(LOGGER, row, "Unable to convert \"" + toParse + "\" to " + "bit vector: " + nfeMsg);
return DataType.getMissingCell();
}
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class BitString2BitVectorCellFactory method getCell.
/**
* {@inheritDoc}
*/
@Override
public DataCell getCell(final DataRow row) {
incrementNrOfRows();
String bitString;
DataCell cell = row.getCell(getColumnIndex());
if (cell.isMissing()) {
return DataType.getMissingCell();
}
if (!cell.getType().isCompatible(StringValue.class)) {
printError(LOGGER, row, "Cell in column " + getColumnIndex() + " is not of type string.");
return DataType.getMissingCell();
}
bitString = ((StringValue) cell).getStringValue().trim();
final BitVectorType type = getVectorType();
final BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(bitString.length());
int pos = 0;
int numberOf0s = 0;
int numberOf1s = 0;
for (int i = bitString.length() - 1; i >= 0; i--) {
char c = bitString.charAt(i);
if (c == '0') {
pos++;
numberOf0s++;
} else if (c == '1') {
factory.set(pos++);
numberOf1s++;
} else {
printError(LOGGER, row, "Invalid character ('" + c + "') in bitvector string");
return DataType.getMissingCell();
}
}
m_nrOfNotSetBits += numberOf0s;
m_nrOfSetBits += numberOf1s;
return factory.createDataCell();
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class Hex2BitVectorCellFactory method getCell.
/**
* {@inheritDoc}
*/
@Override
public DataCell getCell(final DataRow row) {
incrementNrOfRows();
final DataCell old = row.getCell(getColumnIndex());
if (old.isMissing()) {
return DataType.getMissingCell();
}
if (old instanceof StringValue) {
String val = ((StringValue) old).getStringValue();
DataCell newCell;
try {
String hexString = val.trim();
BitVectorType type = getVectorType();
BitVectorCellFactory<? extends DataCell> factory = type.getCellFactory(hexString);
// hopefully int does it
int card = (int) factory.cardinality();
m_nrOfSetBits += card;
m_nrOfNotSetBits += factory.length() - card;
newCell = factory.createDataCell();
} catch (IllegalArgumentException nfe) {
String nfeMsg = nfe.getMessage();
if (nfeMsg == null) {
nfeMsg = "<sorry, no further details>";
}
printError(LOGGER, row, "Unable to convert \"" + val + "\" to bit vector: " + nfeMsg);
newCell = DataType.getMissingCell();
}
return newCell;
} else {
printError(LOGGER, row, "Unable to convert \"" + old + "\" to bit vector, not a string value cell.");
return DataType.getMissingCell();
}
}
Aggregations