use of org.knime.core.data.StringValue in project knime-core by knime.
the class ExtensionPointTest method testDataCellToJava.
/**
* Test whether {@link StringCellToIntTestConverterFactory} was correctly registered at the
* "org.knime.core.DataCellToJavaConverter" extension point and can be used for conversion.
*
* @throws Exception
*/
@Test
public void testDataCellToJava() throws Exception {
final Optional<? extends DataCellToJavaConverterFactory<? extends DataValue, Integer>> factory = DataCellToJavaConverterRegistry.getInstance().getConverterFactories(StringCell.TYPE, Integer.class).stream().findFirst();
assertTrue(factory.isPresent());
final DataCellToJavaConverter<StringValue, Integer> converter = (DataCellToJavaConverter<StringValue, Integer>) factory.get().create();
assertNotNull(converter);
final Integer convert = converter.convert(new StringCell("Answer to Life, the Universe, and Everything"));
assertEquals(convert, new Integer(42));
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class CellSplitterByPosCellFactory method getCells.
/**
* {@inheritDoc}
*/
public DataCell[] getCells(final DataRow row) {
DataCell[] result = new DataCell[m_splitPoints.length + 1];
// preset the result to empty strings
StringCell e = new StringCell("");
for (int r = 0; r < result.length; r++) {
result[r] = e;
}
if (row.getCell(m_colIdx).isMissing()) {
// split string is missing - all result cells will be empty
return result;
}
String splitString = ((StringValue) row.getCell(m_colIdx)).getStringValue();
int lastSplit = 0;
int s = 0;
while (s < m_splitPoints.length) {
int endIdx = m_splitPoints[s];
if (endIdx > splitString.length()) {
// string is shorter than the rest of the splits - done.
break;
}
result[s] = new StringCell(splitString.substring(lastSplit, endIdx));
lastSplit = endIdx;
s++;
}
// put the rest of the string in the last result cell
if (lastSplit < splitString.length()) {
result[s] = new StringCell(splitString.substring(lastSplit));
}
return result;
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class ColCombine2NodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) {
ColumnRearranger result = new ColumnRearranger(spec);
DataColumnSpec append = new DataColumnSpecCreator(m_newColName, StringCell.TYPE).createSpec();
final int[] indices = new int[m_included.length];
int j = 0;
for (int k = 0; k < spec.getNumColumns() && j < m_included.length; k++) {
DataColumnSpec cs = spec.getColumnSpec(k);
if (m_included[j].equals(cs.getName())) {
indices[j++] = k;
}
}
// ", " -> ","
// " " -> " " (do not let the resulting string be empty)
// " bla bla " -> "bla bla"
final String delimTrim = trimDelimString(m_delimString);
result.append(new SingleCellFactory(append) {
@Override
public DataCell getCell(final DataRow row) {
String[] cellContents = new String[indices.length];
for (int i = 0; i < indices.length; i++) {
DataCell c = row.getCell(indices[i]);
String s = c instanceof StringValue ? ((StringValue) c).getStringValue() : c.toString();
cellContents[i] = s;
}
return new StringCell(handleContent(cellContents, delimTrim));
}
});
return result;
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class CategoryToNumberApplyCellFactory method getCells.
/**
* {@inheritDoc}
*/
@Override
public DataCell[] getCells(final DataRow row) {
DataCell cell = row.getCell(m_index);
if (cell.isMissing()) {
return m_mapMissingTo;
}
String inValue = ((StringValue) cell).getStringValue();
DataCell[] value = m_categories.get(inValue);
if (value == null) {
return m_defaultValue;
}
return value;
}
use of org.knime.core.data.StringValue in project knime-core by knime.
the class ColumnHeaderInsertNodeModel method execute.
/**
* {@inheritDoc}
*/
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
// init name map
LinkedHashMap<String, String> dictionaryMap = new LinkedHashMap<String, String>();
DataTableSpec dataSpec = inData[0].getDataTableSpec();
for (DataColumnSpec dataCol : dataSpec) {
dictionaryMap.put(dataCol.getName(), null);
}
// read dictionary
BufferedDataTable dictionaryTable = inData[1];
DataTableSpec dictionaryTableSpec = dictionaryTable.getDataTableSpec();
String lookupColumn = m_config.getLookupColumn();
int lookupColIdx = lookupColumn == null ? -1 : dictionaryTableSpec.findColumnIndex(lookupColumn);
String valueColumnIdx = m_config.getValueColumn();
int valueColIndex = dictionaryTableSpec.findColumnIndex(valueColumnIdx);
int rowIndex = 0;
final long rowCount = dictionaryTable.size();
for (DataRow row : dictionaryTable) {
RowKey key = row.getKey();
exec.setProgress(rowIndex / (double) rowCount, "Reading dictionary, " + "row \"" + key + "\" (" + rowIndex + "/" + rowCount + ")");
rowIndex += 1;
String lookup;
if (lookupColIdx < 0) {
lookup = row.getKey().getString();
} else {
DataCell c = row.getCell(lookupColIdx);
lookup = c.isMissing() ? null : ((StringValue) c).getStringValue();
}
if (!dictionaryMap.containsKey(lookup)) {
continue;
}
DataCell valueCell = row.getCell(valueColIndex);
// if missing, assign original column name
String value = valueCell.isMissing() ? lookup : ((StringValue) valueCell).getStringValue();
if (dictionaryMap.put(lookup, value) != null) {
throw new Exception("Multiple occurrences of lookup key \"" + lookup + "\" in dictionary table; consider to remove " + "duplicates using, e.g. the GroupBy node.");
}
}
// check consistency in new column name values
HashSet<String> uniqNames = new HashSet<String>();
for (Map.Entry<String, String> e : dictionaryMap.entrySet()) {
String value = e.getValue();
if (value == null) {
if (m_config.isFailIfNoMatch()) {
throw new Exception("No name assignment for column \"" + e.getKey() + "\" -- set the appropriate option " + "in the configuration dialog to keep the " + "original column name.");
} else {
// (try to) keep original name
value = e.getKey();
}
}
String newName = value;
int unifier = 1;
while (!uniqNames.add(newName)) {
newName = value + " (#" + (unifier++) + ")";
}
e.setValue(newName);
}
// assign new names
DataColumnSpec[] cols = new DataColumnSpec[dataSpec.getNumColumns()];
for (int i = 0; i < cols.length; i++) {
DataColumnSpec c = dataSpec.getColumnSpec(i);
DataColumnSpecCreator creator = new DataColumnSpecCreator(c);
creator.setName(dictionaryMap.get(c.getName()));
cols[i] = creator.createSpec();
}
DataTableSpec outSpec = new DataTableSpec(dataSpec.getName(), cols);
BufferedDataTable outTable = exec.createSpecReplacerTable(inData[0], outSpec);
return new BufferedDataTable[] { outTable };
}
Aggregations