Search in sources :

Example 41 with SingleCellFactory

use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.

the class ModifyTimeZoneNodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) throws InvalidSettingsException {
    CheckUtils.checkSetting(m_hasValidatedConfiguration, "Node must be configured!");
    final String modification = CheckUtils.checkSettingNotNull(m_modifyAction.getStringValue(), "must not be null");
    CheckUtils.checkSetting(modification.equals(MODIFY_OPTION_SET) || modification.equals(MODIFY_OPTION_SHIFT) || modification.equals(MODIFY_OPTION_REMOVE), "Unknow modification operation '%s'. Most likely the parameter '%s' was controlled by " + "an invalid flow variable.", CFG_KEY_MODIFY_SELECT, m_modifyAction.getStringValue());
    final ColumnRearranger rearranger = new ColumnRearranger(inSpec);
    final String[] includeList = m_colSelect.applyTo(inSpec).getIncludes();
    final int[] includeIndeces = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
    int i = 0;
    final ZoneId zone = m_timeZone.getZone();
    DataType dataType;
    if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_REMOVE)) {
        dataType = LocalDateTimeCellFactory.TYPE;
    } else {
        dataType = ZonedDateTimeCellFactory.TYPE;
    }
    for (String includedCol : includeList) {
        if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
            final DataColumnSpecCreator dataColumnSpecCreator = new DataColumnSpecCreator(includedCol, dataType);
            final SingleCellFactory cellFac;
            if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SET)) {
                cellFac = new SetTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++], zone);
            } else if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SHIFT)) {
                cellFac = new ShiftTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++], zone);
            } else {
                cellFac = new RemoveTimeZoneCellFactory(dataColumnSpecCreator.createSpec(), includeIndeces[i++]);
            }
            rearranger.replace(cellFac, includedCol);
        } else {
            DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(includedCol + m_suffix.getStringValue(), dataType);
            final SingleCellFactory cellFac;
            if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SET)) {
                cellFac = new SetTimeZoneCellFactory(dataColSpec, includeIndeces[i++], zone);
            } else if (m_modifyAction.getStringValue().equals(MODIFY_OPTION_SHIFT)) {
                cellFac = new ShiftTimeZoneCellFactory(dataColSpec, includeIndeces[i++], zone);
            } else {
                cellFac = new RemoveTimeZoneCellFactory(dataColSpec, includeIndeces[i++]);
            }
            rearranger.append(cellFac);
        }
    }
    return rearranger;
}
Also used : Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) DataTableSpec(org.knime.core.data.DataTableSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataTypeColumnFilter(org.knime.core.node.util.filter.column.DataTypeColumnFilter) ZonedDateTime(java.time.ZonedDateTime) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) 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) SimpleStreamableFunctionNodeModel(org.knime.core.node.streamable.simple.SimpleStreamableFunctionNodeModel) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ChangeListener(javax.swing.event.ChangeListener) DataCell(org.knime.core.data.DataCell) ZonedDateTimeValue(org.knime.core.data.time.zoneddatetime.ZonedDateTimeValue) ChangeEvent(javax.swing.event.ChangeEvent) ZoneId(java.time.ZoneId) DataRow(org.knime.core.data.DataRow) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) DataColumnSpecFilterConfiguration(org.knime.core.node.util.filter.column.DataColumnSpecFilterConfiguration) LocalDateTimeValue(org.knime.core.data.time.localdatetime.LocalDateTimeValue) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) SettingsModelDateTime(org.knime.time.util.SettingsModelDateTime) DataType(org.knime.core.data.DataType) CheckUtils(org.knime.core.node.util.CheckUtils) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) ZoneId(java.time.ZoneId) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataType(org.knime.core.data.DataType) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 42 with SingleCellFactory

use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.

the class DateShiftConfigure method getTimeBasedCellFactory.

/**
 * @param spec the  output column spec
 * @param col1Idx the column index of the numerical column to add
 * @param g the time field to modify (as defined by calendar constants)
 * @param conf the configuration object
 * @param time the configured time as Calendar
 * @return the cell factory
 */
public static SingleCellFactory getTimeBasedCellFactory(final DataColumnSpec spec, final int col1Idx, final int g, final DateShiftConfigure conf, final Calendar time) {
    return new SingleCellFactory(spec) {

        /**
         * Value for the new column is based on the values of two column of the row (first and second date column),
         * the selected granularity, and the fraction digits for rounding.
         *
         * @param row the current row
         * @return the difference between the two date values with the given granularity and rounding
         */
        @Override
        public DataCell getCell(final DataRow row) {
            final int value;
            String typeofshift = conf.gettypeofshift().getStringValue();
            if (typeofshift.equals(DateShiftNodeDialog.CFG_COLUMN_SHIFT)) {
                DataCell cell1 = row.getCell(col1Idx);
                if ((cell1.isMissing())) {
                    return DataType.getMissingCell();
                }
                value = ((IntValue) cell1).getIntValue();
            } else {
                value = conf.getvalueofshift().getIntValue();
            }
            Calendar c = (Calendar) time.clone();
            c.add(g, value);
            return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
        }
    };
}
Also used : Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 43 with SingleCellFactory

use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.

the class DateShiftConfigure method getColumnValuebasedCellFactory.

/**
 * @param spec the previous data table spec
 * @param col1Idx the column index of the numerical column to add
 * @param g the time field to modify (as defined by calendar constants)
 * @param conf the configuration object
 * @param col2Idx the time column
 * @return the cell factory
 */
public static SingleCellFactory getColumnValuebasedCellFactory(final DataTableSpec spec, final int col1Idx, final int col2Idx, final int g, final DateShiftConfigure conf) {
    return new SingleCellFactory(createOutputColumnSpec(spec, conf.getNewColumnName().getStringValue())) {

        /**
         * Value for the new column is based on the values of two column of the row (first and second date column),
         * the selected granularity, and the fraction digits for rounding.
         *
         * @param row the current row
         * @return the difference between the two date values with the given granularity and rounding
         */
        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell1 = row.getCell(col1Idx);
            DataCell cell2 = row.getCell(col2Idx);
            if ((cell1.isMissing()) || (cell2.isMissing())) {
                return DataType.getMissingCell();
            }
            Calendar c = ((DateAndTimeValue) cell2).getUTCCalendarClone();
            c.add(g, ((IntValue) cell1).getIntValue());
            return new DateAndTimeCell(c.getTimeInMillis(), conf.getHasDate().getBooleanValue(), conf.getHasTime().getBooleanValue(), conf.getHasMiliSeconds().getBooleanValue());
        }
    };
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 44 with SingleCellFactory

use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.

the class TimePresetNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    BufferedDataTable input = inData[0];
    DataTableSpec spec = input.getDataTableSpec();
    String selectedColName = m_selectedCol.getStringValue();
    final int colIdx = spec.findColumnIndex(selectedColName);
    if (colIdx < 0) {
        throw new IllegalArgumentException("Column " + selectedColName + " not found in input table!");
    }
    DataColumnSpecCreator colSpecCreator = new DataColumnSpecCreator(spec.getColumnSpec(selectedColName));
    /*
         * Reset the domain here. Had problems when time was there and a date is
         * added, then the time without the date will stay the lower bound of 
         * the domain.
         */
    colSpecCreator.setDomain(null);
    ColumnRearranger rearranger = new ColumnRearranger(spec);
    final Calendar preset = m_presetCalendar.getCalendar();
    rearranger.replace(new SingleCellFactory(colSpecCreator.createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell dc = row.getCell(colIdx);
            if (dc.isMissing()) {
                if (m_replaceMissingValues.getBooleanValue()) {
                    return new DateAndTimeCell(preset.getTimeInMillis(), m_presetCalendar.useDate(), m_presetCalendar.useTime(), m_presetCalendar.useMilliseconds());
                }
                return DataType.getMissingCell();
            }
            if (dc.getType().isCompatible(DateAndTimeValue.class)) {
                DateAndTimeValue v = (DateAndTimeValue) dc;
                Calendar existing = v.getUTCCalendarClone();
                // look at the date
                if (m_presetCalendar.useDate() && !v.hasDate()) {
                    // set it
                    existing.set(Calendar.YEAR, preset.get(Calendar.YEAR));
                    existing.set(Calendar.MONTH, preset.get(Calendar.MONTH));
                    existing.set(Calendar.DAY_OF_MONTH, preset.get(Calendar.DAY_OF_MONTH));
                }
                if (m_presetCalendar.useTime() && !v.hasTime()) {
                    // set it
                    existing.set(Calendar.HOUR_OF_DAY, preset.get(Calendar.HOUR_OF_DAY));
                    existing.set(Calendar.MINUTE, preset.get(Calendar.MINUTE));
                    existing.set(Calendar.SECOND, preset.get(Calendar.SECOND));
                    if (m_presetCalendar.useMilliseconds()) {
                        existing.set(Calendar.MILLISECOND, preset.get(Calendar.MILLISECOND));
                    }
                }
                return new DateAndTimeCell(existing.getTimeInMillis(), m_presetCalendar.useDate() || v.hasDate(), m_presetCalendar.useTime() || v.hasTime(), m_presetCalendar.useMilliseconds() || v.hasMillis());
            }
            LOGGER.error("Unsupported type " + dc.getType() + " found in row " + row.getKey() + "!");
            return DataType.getMissingCell();
        }
    }, colIdx);
    BufferedDataTable out = exec.createColumnRearrangeTable(input, rearranger, exec);
    return new BufferedDataTable[] { out };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 45 with SingleCellFactory

use of org.knime.core.data.container.SingleCellFactory in project knime-core by knime.

the class MaskTimeNodeModel method createRearranger.

private ColumnRearranger createRearranger(final DataTableSpec inSpec) {
    // get the selected column index
    final int colIdx = inSpec.findColumnIndex(m_selectedColumn.getStringValue());
    if (colIdx < 0) {
        throw new IllegalArgumentException("Column " + m_selectedColumn.getStringValue() + " not found in input table!");
    }
    ColumnRearranger rearranger = new ColumnRearranger(inSpec);
    final String maskMode = m_maskSelection.getStringValue();
    m_nrInvalids = 0;
    m_onlyInvalids = true;
    DataColumnSpec existing = inSpec.getColumnSpec(colIdx);
    SingleCellFactory fac = createCellFactory(createNewColumnSpec(existing), colIdx, maskMode);
    if (m_replaceColumn.getBooleanValue()) {
        rearranger.replace(fac, colIdx);
    } else {
        rearranger.append(fac);
    }
    return rearranger;
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) 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