use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class StringReplacerNodeModel method createColumnRearranger.
/**
* Creates the column rearranger that computes the new cells.
*
* @param spec the spec of the input table
* @return a column rearranger
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
final Pattern pattern = createPattern(m_settings);
DataColumnSpec colSpec;
if (m_settings.createNewColumn()) {
colSpec = new DataColumnSpecCreator(m_settings.newColumnName(), StringCell.TYPE).createSpec();
} else {
colSpec = new DataColumnSpecCreator(m_settings.columnName(), StringCell.TYPE).createSpec();
}
final String replacement;
if (m_settings.patternIsRegex()) {
replacement = m_settings.replacement();
} else {
replacement = m_settings.replacement().replaceAll("(\\$\\d+)", "\\\\$1");
}
final int index = spec.findColumnIndex(m_settings.columnName());
SingleCellFactory cf = new SingleCellFactory(colSpec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(index);
if (cell.isMissing()) {
return cell;
}
final String stringValue = ((StringValue) cell).getStringValue();
Matcher m = pattern.matcher(stringValue);
if (m_settings.replaceAllOccurrences()) {
return new StringCell(m.replaceAll(replacement));
} else if (m.matches()) {
if (".*".equals(pattern.pattern())) {
// therefore the replacement value is doubled
return new StringCell(replacement);
} else {
return new StringCell(m.replaceAll(replacement));
}
} else {
return new StringCell(stringValue);
}
}
};
ColumnRearranger crea = new ColumnRearranger(spec);
if (m_settings.createNewColumn()) {
if (spec.containsName(m_settings.newColumnName())) {
throw new InvalidSettingsException("Duplicate column name: " + m_settings.newColumnName());
}
crea.append(cf);
} else {
crea.replace(cf, m_settings.columnName());
}
return crea;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class OneMissingValueReplacementFunction method getDataTableSpec.
/**
* {@inheritDoc}
*/
@Override
public DataTableSpec getDataTableSpec() {
DataTableSpec spec = getFactory().getModelSpec();
final int idx = spec.getNumColumns() - 5;
final DataColumnSpec cspec = spec.getColumnSpec(idx);
DataColumnSpecCreator cr = new DataColumnSpecCreator(cspec);
TreeSet<DataCell> domValues = new TreeSet<DataCell>(cspec.getType().getComparator());
domValues.addAll(m_bfs.keySet());
if (cspec.getDomain().hasValues()) {
domValues.addAll(cspec.getDomain().getValues());
}
cr.setDomain(new DataColumnDomainCreator(domValues).createDomain());
ColumnRearranger colre = new ColumnRearranger(spec);
colre.replace(new SingleCellFactory(cr.createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
return row.getCell(idx);
}
}, idx);
return colre.createSpec();
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class CreateByteVectorNodeModel method createRearranger.
private ColumnRearranger createRearranger(final DataTableSpec inSpec) {
final ColumnRearranger ret = new ColumnRearranger(inSpec);
final String[] includes = m_inputColumns.applyTo(inSpec).getIncludes();
if (m_removeInput.getBooleanValue()) {
ret.remove(includes);
}
final DataColumnSpecCreator newCol = new DataColumnSpecCreator(DataTableSpec.getUniqueColumnName(inSpec, m_outputColumn.getStringValue()), DenseByteVectorCell.TYPE);
newCol.setElementNames(includes);
final int[] sourceColumnIndices = SourceColumnsAsProperties.indices(m_inputColumns.applyTo(inSpec), inSpec);
for (int i = sourceColumnIndices.length; i-- > 0; ) {
if (sourceColumnIndices[i] < 0) {
throw new IllegalStateException("Unknown column: " + includes[i]);
}
}
ret.append(new SingleCellFactory(newCol.createSpec()) {
@Override
public DataCell getCell(final DataRow row) {
final DenseByteVectorCellFactory fac = new DenseByteVectorCellFactory(sourceColumnIndices.length);
for (int i = sourceColumnIndices.length; i-- > 0; ) {
DataCell cell = row.getCell(sourceColumnIndices[i]);
if (cell.isMissing()) {
if (m_failOnMissing.getBooleanValue()) {
throw new IllegalArgumentException("Missing value in the row: " + row.getKey() + "\nin the column: " + includes[i]);
} else {
// return DataType.getMissingCell();
fac.setValue(i, 0);
}
} else if (cell instanceof IntValue) {
int intValue = ((IntValue) cell).getIntValue();
if (intValue < 0 || intValue > 255) {
if (m_failOnOutOfInterval.getBooleanValue()) {
throw new IllegalArgumentException("Invalid value: " + intValue + "\nin row: " + row.getKey() + "\nin the column: " + includes[i]);
} else {
fac.setValue(i, 0);
}
} else {
fac.setValue(i, intValue);
}
} else {
throw new IllegalStateException("Not an int value: " + cell + " (" + cell.getType() + ")");
}
}
return fac.createDataCell();
}
});
return ret;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class ColumnAutoTypeCasterNodeModel method createNumberConverter.
private SingleCellFactory createNumberConverter(final int colIdx, final DataType type, final DataColumnSpec colSpec) {
return new SingleCellFactory(colSpec) {
private final DataCellFactory m_fac = new DataCellFactory();
@Override
public DataCell getCell(final DataRow row) {
m_fac.setMissingValuePattern(m_missValPat);
DataCell cell = row.getCell(colIdx);
if (!cell.isMissing()) {
String str = cell.toString();
if (str == null) {
return DataType.getMissingCell();
}
// create String-, Int- or DoubleCell
DataCell c = m_fac.createDataCellOfType(type, str);
if (c == null) {
throw new NumberFormatException("Can't convert '" + str + "' to " + type.toString() + ". In " + row.getKey() + " Column" + colIdx + ". Disable " + "quickscan and try again.");
}
return c;
} else {
// create MissingCell
return DataType.getMissingCell();
}
}
};
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class ColumnAutoTypeCasterNodeModel method createDateAndTimeConverter.
private SingleCellFactory createDateAndTimeConverter(final int colIdx, final DataColumnSpec colSpec) {
return new SingleCellFactory(colSpec) {
private final Calendar m_cal = Calendar.getInstance(TimeZone.getDefault());
private final SimpleDateFormat m_format = new SimpleDateFormat(m_dateFormat);
private final boolean m_hasDate;
private final boolean m_hasTime;
private final boolean m_hasMillis;
{
TimeZone timeZone = TimeZone.getTimeZone("UTC");
m_format.setTimeZone(timeZone);
m_cal.setTimeZone(timeZone);
m_hasDate = m_dateFormat.contains("d");
m_hasTime = m_dateFormat.contains("H");
m_hasMillis = m_dateFormat.contains("S");
}
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIdx);
if (!cell.isMissing()) {
String str = ((StringValue) cell).getStringValue();
if (!str.equals(m_missValPat)) {
try {
m_cal.setTime(m_format.parse(str));
return new DateAndTimeCell(m_cal.getTimeInMillis(), m_hasDate, m_hasTime, m_hasMillis);
} catch (ParseException e) {
throw new IllegalArgumentException("Can't convert '" + str + "' to " + DateAndTimeCell.TYPE.toString() + ". In " + row.getKey() + " Column" + colIdx + ". Disable quickscan and try again.", e);
}
} else {
return DataType.getMissingCell();
}
} else {
// create MissingCell
return DataType.getMissingCell();
}
}
};
}
Aggregations