Search in sources :

Example 16 with CellFactory

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);
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleCell(org.knime.core.data.def.DoubleCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 17 with CellFactory

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;
}
Also used : SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ArrayList(java.util.ArrayList) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) AbstractCellFactory(org.knime.core.data.container.AbstractCellFactory) CellFactory(org.knime.core.data.container.CellFactory) IntCellFactory(org.knime.core.data.def.IntCell.IntCellFactory) LongCellFactory(org.knime.core.data.def.LongCell.LongCellFactory)

Example 18 with CellFactory

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;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DataColumnSpec(org.knime.core.data.DataColumnSpec) StringCell(org.knime.core.data.def.StringCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) CellFactory(org.knime.core.data.container.CellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 19 with CellFactory

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]));
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataCell(org.knime.core.data.DataCell) DefaultRow(org.knime.core.data.def.DefaultRow) CellFactory(org.knime.core.data.container.CellFactory)

Example 20 with CellFactory

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;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) OutCol(org.knime.base.node.jsnippet.util.field.OutCol) OutColList(org.knime.base.node.jsnippet.util.JavaFieldList.OutColList) CellFactory(org.knime.core.data.container.CellFactory)

Aggregations

CellFactory (org.knime.core.data.container.CellFactory)26 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)23 DataColumnSpec (org.knime.core.data.DataColumnSpec)20 DataRow (org.knime.core.data.DataRow)20 DataCell (org.knime.core.data.DataCell)13 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)13 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)12 RowKey (org.knime.core.data.RowKey)8 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)8 DataTableSpec (org.knime.core.data.DataTableSpec)7 BufferedDataTable (org.knime.core.node.BufferedDataTable)7 ExecutionMonitor (org.knime.core.node.ExecutionMonitor)7 StringCell (org.knime.core.data.def.StringCell)6 PortObject (org.knime.core.node.port.PortObject)6 Matrix (Jama.Matrix)3 ArrayList (java.util.ArrayList)3 LinkedHashMap (java.util.LinkedHashMap)3 Map (java.util.Map)3 DataType (org.knime.core.data.DataType)3 DoubleValue (org.knime.core.data.DoubleValue)3