use of org.knime.core.data.container.CellFactory 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.CellFactory in project knime-core by knime.
the class ExtractDurationPeriodFieldsNodeModel method createColumnRearranger.
/**
* {@inheritDoc}
*/
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
final ColumnRearranger rearranger = new ColumnRearranger(spec);
if (m_colSelectModel.getStringValue() == null) {
throw new InvalidSettingsException("Node must be configured!");
}
if (spec.findColumnIndex(m_colSelectModel.getStringValue()) < 0) {
throw new InvalidSettingsException("Column '" + m_colSelectModel.getStringValue() + "' not found in the input table.");
}
final boolean isDurationColumn = spec.getColumnSpec(m_colSelectModel.getStringValue()).getType().isCompatible(DurationValue.class);
final List<DataColumnSpec> colSpecs = new ArrayList<>();
if (isDurationColumn) {
for (final SettingsModelBoolean model : m_durModels) {
if (model.getBooleanValue()) {
if (model.equals(m_hourModel)) {
colSpecs.add(new UniqueNameGenerator(spec).newColumn(model.getConfigName(), LongCell.TYPE));
} else {
colSpecs.add(new UniqueNameGenerator(spec).newColumn(model.getConfigName(), IntCell.TYPE));
}
}
}
if (m_subSecondModel.getBooleanValue()) {
colSpecs.add(new UniqueNameGenerator(spec).newColumn(m_subSecondUnitsModel.getStringValue(), IntCell.TYPE));
}
} else {
for (final SettingsModelBoolean model : m_perModels) {
if (model.getBooleanValue()) {
colSpecs.add(new UniqueNameGenerator(spec).newColumn(model.getConfigName(), LongCell.TYPE));
}
}
}
final DataColumnSpec[] colSpecsArray = new DataColumnSpec[colSpecs.size()];
colSpecs.toArray(colSpecsArray);
final CellFactory cellFac;
if (isDurationColumn) {
cellFac = new ExtractDurationFieldsCellFactory(spec.findColumnIndex(m_colSelectModel.getStringValue()), colSpecsArray);
} else {
cellFac = new ExtractPeriodFieldsCellFactory(spec.findColumnIndex(m_colSelectModel.getStringValue()), colSpecsArray);
}
rearranger.append(cellFac);
return rearranger;
}
use of org.knime.core.data.container.CellFactory in project knime-core by knime.
the class RowKeyUtil method createColumnRearranger.
/**
* Creates the {@link ColumnRearranger} that appends a new column with the
* values of the row id to a data table.
*
* @param inSpec the <code>DataTableSpec</code> of table were the column
* should be appended
* @param newColName the name of the added column
* @param type the <code>DataType</code> of the new column
* @return the {@link ColumnRearranger} to use
*/
public static ColumnRearranger createColumnRearranger(final DataTableSpec inSpec, final String newColName, final DataType type) {
final ColumnRearranger c = new ColumnRearranger(inSpec);
// column specification of the appended column
final DataColumnSpecCreator colSpecCreater = new DataColumnSpecCreator(newColName, type);
final DataColumnSpec newColSpec = colSpecCreater.createSpec();
// utility object that performs the calculation
final CellFactory factory = new SingleCellFactory(newColSpec) {
@Override
public DataCell getCell(final DataRow row) {
return new StringCell(row.getKey().getString());
}
};
c.append(factory);
return c;
}
use of org.knime.core.data.container.CellFactory in project knime-core by knime.
the class JavaSnippet method execute.
/**
* The execution method when no input table is present. I.e. used by the java edit variable node.
*
* @param flowVariableRepository flow variables at the input
* @param exec the execution context to report progress, may be null when this method is called from configure
*/
public void execute(final FlowVariableRepository flowVariableRepository, final ExecutionContext exec) {
DataTableSpec spec = new DataTableSpec();
CellFactory factory = new JavaSnippetCellFactory(this, spec, flowVariableRepository, 1, exec);
factory.getCells(new DefaultRow(RowKey.createRowKey(0L), new DataCell[0]));
}
use of org.knime.core.data.container.CellFactory in project knime-core by knime.
the class JavaSnippet method createRearranger.
/**
* The rearranger is the working horse for creating the output table.
*/
ColumnRearranger createRearranger(final DataTableSpec spec, final FlowVariableRepository flowVariableRepository, final int rowCount, final ExecutionContext context) throws InvalidSettingsException {
int offset = spec.getNumColumns();
CellFactory factory = new JavaSnippetCellFactory(this, spec, flowVariableRepository, rowCount, context);
ColumnRearranger c = new ColumnRearranger(spec);
// add factory to the column rearranger
c.append(factory);
// define which new columns do replace others
OutColList outFields = m_fields.getOutColFields();
for (int i = outFields.size() - 1; i >= 0; i--) {
OutCol field = outFields.get(i);
int index = spec.findColumnIndex(field.getKnimeName());
if (index >= 0) {
if (field.getReplaceExisting()) {
c.remove(index);
c.move(offset + i - 1, index);
} else {
throw new InvalidSettingsException("Field \"" + field.getJavaName() + "\" is configured to " + "replace no existing columns.");
}
}
}
return c;
}
Aggregations