Search in sources :

Example 31 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator 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 32 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.

the class OldToNewTimeNodeModel method createStreamableOperator.

/**
 * {@inheritDoc}
 */
@Override
public StreamableOperator createStreamableOperator(final PartitionInfo partitionInfo, final PortObjectSpec[] inSpecs) throws InvalidSettingsException {
    return new StreamableOperator() {

        SimpleStreamableOperatorInternals m_internals = new SimpleStreamableOperatorInternals();

        /**
         * {@inheritDoc}
         */
        @Override
        public void runIntermediate(final PortInput[] inputs, final ExecutionContext exec) throws Exception {
            if (partitionInfo.getPartitionIndex() == 0) {
                final RowInput rowInput = (RowInput) inputs[0];
                final DataRow row = rowInput.poll();
                if (row != null) {
                    if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
                        final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells()];
                        final DataTableSpec inSpec = rowInput.getDataTableSpec();
                        final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
                        final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
                        for (int i = 0; i < inSpec.getNumColumns(); i++) {
                            final int searchIdx = Arrays.binarySearch(includeIndexes, i);
                            if (searchIdx < 0) {
                                colSpecs[i] = inSpec.getColumnSpec(i);
                            } else {
                                colSpecs[i] = newColumnSpecs[searchIdx];
                            }
                        }
                        final Config config = m_internals.getConfig();
                        config.addBoolean("hasIterated", false);
                        for (int i = 0; i < inSpec.getNumColumns(); i++) {
                            config.addDataType("type" + i, colSpecs[i].getType());
                            config.addString("colname" + i, colSpecs[i].getName());
                        }
                        config.addInt("sizeRow", colSpecs.length);
                    } else {
                        final DataTableSpec inSpec = rowInput.getDataTableSpec();
                        final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
                        final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
                        final DataColumnSpec[] colSpecs = new DataColumnSpec[row.getNumCells() + includeIndexes.length];
                        for (int i = 0; i < inSpec.getNumColumns(); i++) {
                            colSpecs[i] = inSpec.getColumnSpec(i);
                        }
                        for (int i = 0; i < newColumnSpecs.length; i++) {
                            colSpecs[i + inSpec.getNumColumns()] = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
                        }
                        final Config config = m_internals.getConfig();
                        config.addBoolean("hasIterated", false);
                        for (int i = 0; i < colSpecs.length; i++) {
                            config.addDataType("type" + i, colSpecs[i].getType());
                            config.addString("colname" + i, colSpecs[i].getName());
                        }
                        config.addInt("sizeRow", colSpecs.length);
                    }
                } else {
                    m_internals.getConfig().addInt("sizeRow", 0);
                }
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public StreamableOperatorInternals saveInternals() {
            return m_internals;
        }

        @Override
        public void runFinal(final PortInput[] inputs, final PortOutput[] outputs, final ExecutionContext exec) throws Exception {
            final RowInput in = (RowInput) inputs[0];
            final RowOutput out = (RowOutput) outputs[0];
            final DataTableSpec inSpec = in.getDataTableSpec();
            final int[] includeIndexes = Arrays.stream(m_colSelect.applyTo(inSpec).getIncludes()).mapToInt(s -> inSpec.findColumnIndex(s)).toArray();
            DataRow row;
            while ((row = in.poll()) != null) {
                exec.checkCanceled();
                final DataColumnSpec[] newColumnSpecs = getNewIncludedColumnSpecs(inSpec, row);
                DataCell[] datacells = new DataCell[includeIndexes.length];
                for (int i = 0; i < includeIndexes.length; i++) {
                    if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
                        ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(newColumnSpecs[i], i, includeIndexes[i]);
                        datacells[i] = cellFac.getCells(row)[0];
                    } else {
                        final DataColumnSpec dataColSpec = new UniqueNameGenerator(inSpec).newColumn(newColumnSpecs[i].getName() + m_suffix.getStringValue(), newColumnSpecs[i].getType());
                        ConvertTimeCellFactory cellFac = new ConvertTimeCellFactory(dataColSpec, i, includeIndexes[i]);
                        datacells[i] = cellFac.getCells(row)[0];
                    }
                }
                if (m_isReplaceOrAppend.getStringValue().equals(OPTION_REPLACE)) {
                    out.push(new ReplacedColumnsDataRow(row, datacells, includeIndexes));
                } else {
                    out.push(new AppendedColumnRow(row, datacells));
                }
            }
            in.close();
            out.close();
        }
    };
}
Also used : Arrays(java.util.Arrays) NodeSettingsRO(org.knime.core.node.NodeSettingsRO) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) ZonedDateTime(java.time.ZonedDateTime) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) LocalDateTimeCellFactory(org.knime.core.data.time.localdatetime.LocalDateTimeCellFactory) DataColumnSpec(org.knime.core.data.DataColumnSpec) LocalTime(java.time.LocalTime) PortInput(org.knime.core.node.streamable.PortInput) ExecutionMonitor(org.knime.core.node.ExecutionMonitor) PartitionInfo(org.knime.core.node.streamable.PartitionInfo) SettingsModelColumnFilter2(org.knime.core.node.defaultnodesettings.SettingsModelColumnFilter2) NodeModel(org.knime.core.node.NodeModel) LocalDateCell(org.knime.core.data.time.localdate.LocalDateCell) ZoneId(java.time.ZoneId) BufferedDataTable(org.knime.core.node.BufferedDataTable) InputPortRole(org.knime.core.node.streamable.InputPortRole) LocalDate(java.time.LocalDate) Config(org.knime.core.node.config.Config) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) LocalDateTimeCell(org.knime.core.data.time.localdatetime.LocalDateTimeCell) RowOutput(org.knime.core.node.streamable.RowOutput) DataTableSpec(org.knime.core.data.DataTableSpec) ZonedDateTimeCell(org.knime.core.data.time.zoneddatetime.ZonedDateTimeCell) LocalDateTime(java.time.LocalDateTime) AppendedColumnRow(org.knime.core.data.append.AppendedColumnRow) ExecutionContext(org.knime.core.node.ExecutionContext) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) StreamableOperatorInternals(org.knime.core.node.streamable.StreamableOperatorInternals) ZonedDateTimeCellFactory(org.knime.core.data.time.zoneddatetime.ZonedDateTimeCellFactory) DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DateTimeType(org.knime.time.util.DateTimeType) DataCell(org.knime.core.data.DataCell) LocalDateCellFactory(org.knime.core.data.time.localdate.LocalDateCellFactory) LocalTimeCellFactory(org.knime.core.data.time.localtime.LocalTimeCellFactory) RowInput(org.knime.core.node.streamable.RowInput) LocalTimeCell(org.knime.core.data.time.localtime.LocalTimeCell) SettingsModelBoolean(org.knime.core.node.defaultnodesettings.SettingsModelBoolean) PortObjectSpec(org.knime.core.node.port.PortObjectSpec) IOException(java.io.IOException) OutputPortRole(org.knime.core.node.streamable.OutputPortRole) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) File(java.io.File) DataRow(org.knime.core.data.DataRow) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) PortOutput(org.knime.core.node.streamable.PortOutput) NodeSettingsWO(org.knime.core.node.NodeSettingsWO) TimeUnit(java.util.concurrent.TimeUnit) MissingCell(org.knime.core.data.MissingCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataType(org.knime.core.data.DataType) MergeOperator(org.knime.core.node.streamable.MergeOperator) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) DataTableSpec(org.knime.core.data.DataTableSpec) SimpleStreamableOperatorInternals(org.knime.core.node.streamable.simple.SimpleStreamableOperatorInternals) Config(org.knime.core.node.config.Config) StreamableOperator(org.knime.core.node.streamable.StreamableOperator) RowInput(org.knime.core.node.streamable.RowInput) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) DataRow(org.knime.core.data.DataRow) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) RowOutput(org.knime.core.node.streamable.RowOutput) ExecutionContext(org.knime.core.node.ExecutionContext) DataColumnSpec(org.knime.core.data.DataColumnSpec) DataCell(org.knime.core.data.DataCell) ReplacedColumnsDataRow(org.knime.base.data.replace.ReplacedColumnsDataRow) AppendedColumnRow(org.knime.core.data.append.AppendedColumnRow)

Example 33 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator in project knime-core by knime.

the class ExtractDateTimeFieldsNodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec) throws InvalidSettingsException {
    final String selectedCol = m_colSelectModel.getStringValue();
    if (selectedCol == null || selectedCol.isEmpty()) {
        throw new InvalidSettingsException("Node must be configured.");
    }
    final int selectedColIdx = spec.findColumnIndex(selectedCol);
    if (selectedColIdx < 0) {
        throw new InvalidSettingsException("Column " + selectedCol + " not found in the input table.");
    }
    final DataType selectedColType = spec.getColumnSpec(selectedCol).getType();
    final boolean isDate = isDateType(selectedColType);
    final boolean isTime = isTimeType(selectedColType);
    if (!isDate && !isTime) {
        throw new InvalidSettingsException("Column " + selectedCol + " does not contain a Date&Time type.");
    }
    final boolean isLocalDate = selectedColType.isCompatible(LocalDateValue.class);
    final boolean isLocalTime = selectedColType.isCompatible(LocalTimeValue.class);
    final boolean isLocalDateTime = selectedColType.isCompatible(LocalDateTimeValue.class);
    final boolean isZonedDateTime = selectedColType.isCompatible(ZonedDateTimeValue.class);
    final Locale locale = LocaleUtils.toLocale(m_localeModel.getStringValue());
    final UniqueNameGenerator nameGenerator = new UniqueNameGenerator(spec);
    final DataColumnDomainCreator domainCreator = new DataColumnDomainCreator();
    final ColumnRearranger rearranger = new ColumnRearranger(spec);
    if (isDate) {
        if (m_yearModel.getBooleanValue()) {
            final DataColumnSpec colSpec = nameGenerator.newColumn(YEAR, IntCell.TYPE);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return IntCellFactory.create(value.getLocalDate().getYear());
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().getYear());
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().getYear());
                    }
                });
            }
        }
        if (m_quarterModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, QUARTER, 1, 4);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return IntCellFactory.create((value.getLocalDate().getMonthValue() + 2) / 3);
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create((value.getLocalDateTime().getMonthValue() + 2) / 3);
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create((value.getZonedDateTime().getMonthValue() + 2) / 3);
                    }
                });
            }
        }
        if (m_monthNumberModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, MONTH_NUMBER, 1, 12);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return IntCellFactory.create(value.getLocalDate().getMonthValue());
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().getMonthValue());
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().getMonthValue());
                    }
                });
            }
        }
        if (m_monthNameModel.getBooleanValue()) {
            final DataColumnSpec colSpec = nameGenerator.newColumn(MONTH_NAME, StringCell.TYPE);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return StringCellFactory.create(value.getLocalDate().getMonth().getDisplayName(TextStyle.FULL, locale));
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return StringCellFactory.create(value.getLocalDateTime().getMonth().getDisplayName(TextStyle.FULL, locale));
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return StringCellFactory.create(value.getZonedDateTime().getMonth().getDisplayName(TextStyle.FULL, locale));
                    }
                });
            }
        }
        if (m_weekModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, WEEK, 1, 52);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return IntCellFactory.create(value.getLocalDate().get(WeekFields.of(locale).weekOfWeekBasedYear()));
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().get(WeekFields.of(locale).weekOfWeekBasedYear()));
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().get(WeekFields.of(locale).weekOfWeekBasedYear()));
                    }
                });
            }
        }
        if (m_dayYearModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, DAY_OF_YEAR, 1, 366);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return IntCellFactory.create(value.getLocalDate().getDayOfYear());
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().getDayOfYear());
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().getDayOfYear());
                    }
                });
            }
        }
        if (m_dayMonthModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, DAY_OF_MONTH, 1, 31);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return IntCellFactory.create(value.getLocalDate().getDayOfMonth());
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().getDayOfMonth());
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().getDayOfMonth());
                    }
                });
            }
        }
        if (m_dayWeekNumberModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, DAY_OF_WEEK_NUMBER, 1, 7);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return IntCellFactory.create(value.getLocalDate().get(WeekFields.of(locale).dayOfWeek()));
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().get(WeekFields.of(locale).dayOfWeek()));
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().get(WeekFields.of(locale).dayOfWeek()));
                    }
                });
            }
        }
        if (m_dayWeekNameModel.getBooleanValue()) {
            final DataColumnSpec colSpec = nameGenerator.newColumn(DAY_OF_WEEK_NAME, StringCell.TYPE);
            if (isLocalDate) {
                rearranger.append(new AbstractLocalDateFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateValue value) {
                        return StringCellFactory.create(value.getLocalDate().getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE, locale));
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return StringCellFactory.create(value.getLocalDateTime().getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE, locale));
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return StringCellFactory.create(value.getZonedDateTime().getDayOfWeek().getDisplayName(TextStyle.FULL_STANDALONE, locale));
                    }
                });
            }
        }
    }
    if (isTime) {
        if (m_hourModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, HOUR, 0, 23);
            if (isLocalTime) {
                rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalTimeValue value) {
                        return IntCellFactory.create(value.getLocalTime().getHour());
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().getHour());
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().getHour());
                    }
                });
            }
        }
        if (m_minuteModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, MINUTE, 0, 59);
            if (isLocalTime) {
                rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalTimeValue value) {
                        return IntCellFactory.create(value.getLocalTime().getMinute());
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().getMinute());
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().getMinute());
                    }
                });
            }
        }
        if (m_secondModel.getBooleanValue()) {
            final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, SECOND, 0, 59);
            if (isLocalTime) {
                rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalTimeValue value) {
                        return IntCellFactory.create(value.getLocalTime().getSecond());
                    }
                });
            } else if (isLocalDateTime) {
                rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final LocalDateTimeValue value) {
                        return IntCellFactory.create(value.getLocalDateTime().getSecond());
                    }
                });
            } else if (isZonedDateTime) {
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return IntCellFactory.create(value.getZonedDateTime().getSecond());
                    }
                });
            }
        }
        if (m_subsecondModel.getBooleanValue()) {
            final String subsecondUnit = m_subsecondUnitsModel.getStringValue();
            if (subsecondUnit.equals(MILLISECOND)) {
                // in milliseconds
                final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, SUBSECOND_COL + " (in " + MILLISECOND + ")", 0, 999);
                if (isLocalTime) {
                    rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final LocalTimeValue value) {
                            return IntCellFactory.create(value.getLocalTime().get(ChronoField.MILLI_OF_SECOND));
                        }
                    });
                } else if (isLocalDateTime) {
                    rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final LocalDateTimeValue value) {
                            return IntCellFactory.create(value.getLocalDateTime().get(ChronoField.MILLI_OF_SECOND));
                        }
                    });
                } else if (isZonedDateTime) {
                    rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final ZonedDateTimeValue value) {
                            return IntCellFactory.create(value.getZonedDateTime().get(ChronoField.MILLI_OF_SECOND));
                        }
                    });
                }
            } else if (subsecondUnit.equals(MICROSECOND)) {
                // in microseconds
                final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, SUBSECOND_COL + " (in " + MICROSECOND + ")", 0, 999_999);
                if (isLocalTime) {
                    rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final LocalTimeValue value) {
                            return IntCellFactory.create(value.getLocalTime().get(ChronoField.MICRO_OF_SECOND));
                        }
                    });
                } else if (isLocalDateTime) {
                    rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final LocalDateTimeValue value) {
                            return IntCellFactory.create(value.getLocalDateTime().get(ChronoField.MICRO_OF_SECOND));
                        }
                    });
                } else if (isZonedDateTime) {
                    rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final ZonedDateTimeValue value) {
                            return IntCellFactory.create(value.getZonedDateTime().get(ChronoField.MICRO_OF_SECOND));
                        }
                    });
                }
            } else if (subsecondUnit.equals(NANOSECOND)) {
                // in nanoseconds
                final DataColumnSpec colSpec = createBoundedIntColumn(domainCreator, nameGenerator, SUBSECOND_COL + " (in " + NANOSECOND + ")", 0, 999_999_999);
                if (isLocalTime) {
                    rearranger.append(new AbstractLocalTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final LocalTimeValue value) {
                            return IntCellFactory.create(value.getLocalTime().getNano());
                        }
                    });
                } else if (isLocalDateTime) {
                    rearranger.append(new AbstractLocalDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final LocalDateTimeValue value) {
                            return IntCellFactory.create(value.getLocalDateTime().getNano());
                        }
                    });
                } else if (isZonedDateTime) {
                    rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                        @Override
                        protected DataCell getCell(final ZonedDateTimeValue value) {
                            return IntCellFactory.create(value.getZonedDateTime().getNano());
                        }
                    });
                }
            }
        }
        if (isZonedDateTime) {
            if (m_timeZoneNameModel.getBooleanValue()) {
                final DataColumnSpec colSpec = nameGenerator.newColumn(TIME_ZONE_NAME, StringCell.TYPE);
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return StringCellFactory.create(value.getZonedDateTime().getZone().getId().toString());
                    }
                });
            }
            if (m_timeZoneOffsetModel.getBooleanValue()) {
                final DataColumnSpec colSpec = nameGenerator.newColumn(TIME_ZONE_OFFSET, StringCell.TYPE);
                rearranger.append(new AbstractZonedDateTimeFieldCellFactory(selectedColIdx, colSpec) {

                    @Override
                    protected DataCell getCell(final ZonedDateTimeValue value) {
                        return StringCellFactory.create(value.getZonedDateTime().getOffset().getDisplayName(TextStyle.FULL_STANDALONE, locale));
                    }
                });
            }
        }
    }
    if (rearranger.getColumnCount() == spec.getNumColumns()) {
        getLogger().info("No fields will be extracted. Output table will equal input table.");
    }
    return rearranger;
}
Also used : Locale(java.util.Locale) LocalTimeValue(org.knime.core.data.time.localtime.LocalTimeValue) DataColumnDomainCreator(org.knime.core.data.DataColumnDomainCreator) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) LocalDateValue(org.knime.core.data.time.localdate.LocalDateValue) UniqueNameGenerator(org.knime.core.util.UniqueNameGenerator) LocalDateTimeValue(org.knime.core.data.time.localdatetime.LocalDateTimeValue) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataColumnSpec(org.knime.core.data.DataColumnSpec) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataType(org.knime.core.data.DataType) DataCell(org.knime.core.data.DataCell) ZonedDateTimeValue(org.knime.core.data.time.zoneddatetime.ZonedDateTimeValue)

Example 34 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator 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)

Example 35 with UniqueNameGenerator

use of org.knime.core.util.UniqueNameGenerator 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)

Aggregations

UniqueNameGenerator (org.knime.core.util.UniqueNameGenerator)36 DataColumnSpec (org.knime.core.data.DataColumnSpec)33 DataTableSpec (org.knime.core.data.DataTableSpec)31 DataCell (org.knime.core.data.DataCell)24 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)22 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)19 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)18 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)18 DataRow (org.knime.core.data.DataRow)17 Arrays (java.util.Arrays)16 DataType (org.knime.core.data.DataType)16 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)16 NodeSettingsRO (org.knime.core.node.NodeSettingsRO)16 NodeSettingsWO (org.knime.core.node.NodeSettingsWO)16 SettingsModelColumnFilter2 (org.knime.core.node.defaultnodesettings.SettingsModelColumnFilter2)13 File (java.io.File)10 IOException (java.io.IOException)10 ZonedDateTime (java.time.ZonedDateTime)10 ArrayList (java.util.ArrayList)10 AppendedColumnRow (org.knime.core.data.append.AppendedColumnRow)10