Search in sources :

Example 36 with SingleCellFactory

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

Example 37 with SingleCellFactory

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;
}
Also used : DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 38 with SingleCellFactory

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);
}
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 39 with SingleCellFactory

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;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DoubleValue(org.knime.core.data.DoubleValue) DataCell(org.knime.core.data.DataCell) SettingsModelFilterString(org.knime.core.node.defaultnodesettings.SettingsModelFilterString) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 40 with SingleCellFactory

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;
}
Also used : LongValue(org.knime.core.data.LongValue) Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) ZonedDateTime(java.time.ZonedDateTime) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) LocalDateTime(java.time.LocalDateTime) DurationValue(org.knime.core.data.time.duration.DurationValue) LocalTimeValue(org.knime.core.data.time.localtime.LocalTimeValue) LocalDateValue(org.knime.core.data.time.localdate.LocalDateValue) LocalDateTimeCellFactory(org.knime.core.data.time.localdatetime.LocalDateTimeCellFactory) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataColumnSpec(org.knime.core.data.DataColumnSpec) ZonedDateTimeCellFactory(org.knime.core.data.time.zoneddatetime.ZonedDateTimeCellFactory) SettingsModelInteger(org.knime.core.node.defaultnodesettings.SettingsModelInteger) SimpleStreamableFunctionNodeModel(org.knime.core.node.streamable.simple.SimpleStreamableFunctionNodeModel) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Duration(java.time.Duration) LocalTime(java.time.LocalTime) Granularity(org.knime.time.util.Granularity) DataCell(org.knime.core.data.DataCell) ZonedDateTimeValue(org.knime.core.data.time.zoneddatetime.ZonedDateTimeValue) Period(java.time.Period) IntValue(org.knime.core.data.IntValue) DurationPeriodFormatUtils(org.knime.time.util.DurationPeriodFormatUtils) LocalDateCellFactory(org.knime.core.data.time.localdate.LocalDateCellFactory) LocalTimeCellFactory(org.knime.core.data.time.localtime.LocalTimeCellFactory) PeriodValue(org.knime.core.data.time.period.PeriodValue) SettingsModelColumnFilter2(org.knime.core.node.defaultnodesettings.SettingsModelColumnFilter2) DataRow(org.knime.core.data.DataRow) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) DateTimeParseException(java.time.format.DateTimeParseException) LocalDateTimeValue(org.knime.core.data.time.localdatetime.LocalDateTimeValue) MissingCell(org.knime.core.data.MissingCell) LocalDate(java.time.LocalDate) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) DateTimeParseException(java.time.format.DateTimeParseException) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Aggregations

SingleCellFactory (org.knime.core.data.container.SingleCellFactory)48 DataRow (org.knime.core.data.DataRow)47 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)41 DataCell (org.knime.core.data.DataCell)40 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)35 DataColumnSpec (org.knime.core.data.DataColumnSpec)34 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)19 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)19 DataType (org.knime.core.data.DataType)12 CellFactory (org.knime.core.data.container.CellFactory)12 StringCell (org.knime.core.data.def.StringCell)10 Calendar (java.util.Calendar)8 DataTableSpec (org.knime.core.data.DataTableSpec)8 StringValue (org.knime.core.data.StringValue)8 DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)8 ArrayList (java.util.ArrayList)7 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)6 SettingsModelCalendar (org.knime.timeseries.util.SettingsModelCalendar)5 ParseException (java.text.ParseException)4 ZonedDateTime (java.time.ZonedDateTime)4