use of org.knime.core.data.container.SingleCellFactory 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.container.SingleCellFactory in project knime-core by knime.
the class ColumnMergerNodeModel method createColumnRearranger.
/**
* Creates column rearranger doing all the work.
* @param spec The input spec.
* @return The rearranger creating the output table/spec.
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
ColumnMergerConfiguration cfg = m_configuration;
if (cfg == null) {
throw new InvalidSettingsException("No settings available");
}
final int primColIndex = spec.findColumnIndex(cfg.getPrimaryColumn());
final int secColIndex = spec.findColumnIndex(cfg.getSecondaryColumn());
if (primColIndex < 0) {
throw new InvalidSettingsException("No such primary column: " + cfg.getPrimaryColumn());
}
if (secColIndex < 0) {
throw new InvalidSettingsException("No such secondary column: " + cfg.getSecondaryColumn());
}
DataColumnSpec c1 = spec.getColumnSpec(primColIndex);
DataColumnSpec c2 = spec.getColumnSpec(secColIndex);
DataType commonType = DataType.getCommonSuperType(c1.getType(), c2.getType());
String name;
switch(cfg.getOutputPlacement()) {
case ReplacePrimary:
case ReplaceBoth:
name = c1.getName();
break;
case ReplaceSecondary:
name = c2.getName();
break;
case AppendAsNewColumn:
name = DataTableSpec.getUniqueColumnName(spec, cfg.getOutputName());
break;
default:
throw new InvalidSettingsException("Coding problem: unhandled case");
}
DataColumnSpec outColSpec = new DataColumnSpecCreator(name, commonType).createSpec();
SingleCellFactory fac = new SingleCellFactory(outColSpec) {
/**
* {@inheritDoc}
*/
@Override
public DataCell getCell(final DataRow row) {
DataCell cell1 = row.getCell(primColIndex);
DataCell cell2 = row.getCell(secColIndex);
return !cell1.isMissing() ? cell1 : cell2;
}
};
ColumnRearranger result = new ColumnRearranger(spec);
switch(cfg.getOutputPlacement()) {
case ReplacePrimary:
result.replace(fac, primColIndex);
break;
case ReplaceBoth:
result.replace(fac, primColIndex);
result.remove(secColIndex);
break;
case ReplaceSecondary:
result.replace(fac, secColIndex);
break;
case AppendAsNewColumn:
result.append(fac);
break;
default:
throw new InvalidSettingsException("Coding problem: unhandled case");
}
return result;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class SortedCorrelationComputer method replace.
/**
* Replaces the values of the column colIndex in the Table bdt, with the values from mylist.
* @param bdt original data table
* @param colIndex column to replace
* @param myList new values
* @return the original data table where the defined column is replaced by the values from mylist
* @throws CanceledExecutionException if canceled by user.
*/
private BufferedDataTable replace(final BufferedDataTable bdt, final int colIndex, final LinkedList<SortablePair> myList, final ExecutionContext exec) throws CanceledExecutionException {
// Create ColumnRearranger
ColumnRearranger c = new ColumnRearranger(bdt.getDataTableSpec());
// Spec of the new Counter Column
DataColumnSpec newColSpec = new DataColumnSpecCreator(bdt.getDataTableSpec().getColumnSpec(colIndex).getName(), DoubleCell.TYPE).createSpec();
final Iterator<SortablePair> it = myList.iterator();
// Fill the cells of the new column
CellFactory factory = new SingleCellFactory(newColSpec) {
@Override
public DataCell getCell(final DataRow row) {
if (it.hasNext()) {
return new DoubleCell(it.next().getRank());
}
return DataType.getMissingCell();
}
};
// Append Column
c.replace(factory, colIndex);
return exec.createColumnRearrangeTable(bdt, c, exec);
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class MovingAverageNodeModel method createColRearranger.
private ColumnRearranger createColRearranger(final DataTableSpec spec) {
ColumnRearranger result = new ColumnRearranger(spec);
for (String thisCol : m_columnNames.getIncludeList()) {
final int colIndex = spec.findColumnIndex(thisCol);
DataColumnSpec newColSpec = new DataColumnSpecCreator(DataTableSpec.getUniqueColumnName(spec, "MA(" + thisCol + ")"), DoubleCell.TYPE).createSpec();
SingleCellFactory c = new SingleCellFactory(newColSpec) {
@Override
public DataCell getCell(final DataRow row) {
DataCell cell = row.getCell(colIndex);
if (cell.isMissing() || !(cell instanceof DoubleValue)) {
return DataType.getMissingCell();
}
return m_mas[colIndex].getMeanandUpdate(((DoubleValue) cell).getDoubleValue());
}
};
if (m_replace.getBooleanValue()) {
result.replace(c, colIndex);
} else {
result.append(c);
}
}
return result;
}
use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.
the class DateTimeShiftNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
ColumnRearranger rearranger = new ColumnRearranger(spec);
String[] includeList = m_colSelect.applyTo(spec).getIncludes();
int[] includeIndices = Arrays.stream(m_colSelect.applyTo(spec).getIncludes()).mapToInt(s -> spec.findColumnIndex(s)).toArray();
int i = 0;
int periodColIndex = spec.findColumnIndex(m_periodColSelect.getStringValue());
int numericalColIndex = spec.findColumnIndex(m_numericalColSelect.getStringValue());
boolean isPeriod;
if (m_periodSelection.isEnabled()) {
if (m_periodColSelect.isEnabled()) {
if (spec.getColumnSpec(periodColIndex).getType().isCompatible(PeriodValue.class)) {
isPeriod = true;
} else {
isPeriod = false;
}
} else {
periodColIndex = -1;
try {
DurationPeriodFormatUtils.parsePeriod(m_periodValue.getStringValue());
isPeriod = true;
} catch (DateTimeParseException e) {
isPeriod = false;
}
}
} else {
if (!m_numericalColSelect.isEnabled()) {
numericalColIndex = -1;
}
if (!Granularity.fromString(m_numericalGranularity.getStringValue()).isPartOfDate()) {
isPeriod = false;
} else {
isPeriod = true;
}
}
for (String includedCol : includeList) {
if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
final SingleCellFactory cellFac;
final DataColumnSpec dataColSpec = new DataColumnSpecCreator(includedCol, spec.getColumnSpec(includedCol).getType()).createSpec();
if (isPeriod) {
cellFac = new DateTimeShiftPeriodCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
} else {
cellFac = new DateTimeShiftDurationCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
}
rearranger.replace(cellFac, includedCol);
} else {
final DataColumnSpec dataColSpec = new UniqueNameGenerator(spec).newColumn(includedCol + m_suffix.getStringValue(), spec.getColumnSpec(includedCol).getType());
final SingleCellFactory cellFac;
if (isPeriod) {
cellFac = new DateTimeShiftPeriodCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
} else {
cellFac = new DateTimeShiftDurationCellFactory(dataColSpec, includeIndices[i++], periodColIndex, numericalColIndex);
}
rearranger.append(cellFac);
}
}
return rearranger;
}
Aggregations