Search in sources :

Example 1 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class TimeDifferenceNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    // get the selected granularity level
    final Granularity g = Granularity.valueOf(m_granularity.getStringValue());
    // create rearranger
    ColumnRearranger rearranger = new ColumnRearranger(inData[0].getDataTableSpec());
    String typeofref = m_typeofreference.getStringValue();
    if (typeofref.equals(TimeDifferenceNodeDialog.CFG_COLUMN)) {
        // append the new column with single cell factory
        rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.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(m_col1Idx);
                DataCell cell2 = row.getCell(m_col2Idx);
                if ((cell1.isMissing()) || (cell2.isMissing())) {
                    return DataType.getMissingCell();
                }
                long first = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
                long last = ((DateAndTimeValue) cell2).getUTCTimeInMillis();
                return getRoundedTimeDifference(first, last, g);
            }
        });
    } else if (typeofref.equals(TimeDifferenceNodeDialog.CFG_ROW_DIFF)) {
        // option for producing the time difference between current and the
        // previous row.
        // append the new column with single cell factory
        rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.getStringValue())) {

            /**
             * saves the previous time value (contained in the last row)
             */
            private DateAndTimeValue m_previous = null;

            /**
             * Value for the new column is based on the values of the
             * current row and the value of the previous row.
             * Therefore both rows must contain a DateAndTimeValue,
             * 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(m_col1Idx);
                // value
                if ((cell1.isMissing()) || !cell1.getType().isCompatible(DateAndTimeValue.class)) {
                    m_previous = null;
                    return DataType.getMissingCell();
                }
                // (e.g. we are in the first row)
                if (m_previous == null) {
                    m_previous = (DateAndTimeValue) cell1;
                    return DataType.getMissingCell();
                }
                long first = m_previous.getUTCTimeInMillis();
                long last = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
                m_previous = (DateAndTimeValue) cell1;
                return getRoundedTimeDifference(first, last, g);
            }
        });
    } else {
        final long time;
        if (typeofref.equals(TimeDifferenceNodeDialog.CFG_FIXDATE)) {
            time = m_timemodel.getCalendar().getTimeInMillis();
        } else {
            time = System.currentTimeMillis() + TimeZone.getDefault().getOffset(System.currentTimeMillis());
        }
        // append the new column with single cell factory
        rearranger.append(new SingleCellFactory(createOutputColumnSpec(inData[0].getDataTableSpec(), m_newColName.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(m_col1Idx);
                if ((cell1.isMissing())) {
                    return DataType.getMissingCell();
                }
                long first = ((DateAndTimeValue) cell1).getUTCTimeInMillis();
                return getRoundedTimeDifference(first, time, g);
            }
        });
    }
    BufferedDataTable out = exec.createColumnRearrangeTable(inData[0], rearranger, exec);
    return new BufferedDataTable[] { out };
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Example 2 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class DateFieldExtractorNodeModel method createColumnRearranger.

private SingleCellFactoryCompound createColumnRearranger(final DataTableSpec inSpec) {
    final int colIdx = inSpec.findColumnIndex(m_selectedColumn.getStringValue());
    ColumnRearranger rearranger = new ColumnRearranger(inSpec);
    List<AbstractTimeExtractorCellFactory> cellFactories = new ArrayList<AbstractTimeExtractorCellFactory>();
    // year
    if (m_useYear.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_yearColName.getStringValue());
        AbstractTimeExtractorCellFactory yearFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getYear();
            }
        };
        rearranger.append(yearFactory);
        cellFactories.add(yearFactory);
    }
    // quarter
    if (m_useQuarter.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_quarterColName.getStringValue());
        AbstractTimeExtractorCellFactory quarterFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                int month = value.getMonth();
                // hence we add 1
                return month / 3 + 1;
            }
        };
        rearranger.append(quarterFactory);
        cellFactories.add(quarterFactory);
    }
    // month
    AbstractTimeExtractorCellFactory monthFactory = null;
    if (m_useMonth.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_monthColName.getStringValue());
        if (m_monthRepresentation.getStringValue().equals(AbstractFieldExtractorNodeDialog.AS_INT)) {
            monthFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

                @Override
                protected int extractTimeField(final DateAndTimeValue value) {
                    // to java.util.Calendar#MONTH
                    return value.getMonth() + 1;
                }
            };
        } else {
            // extract the display name of the month
            monthFactory = new AbstractTimeExtractorStringCellFactory(colName, colIdx, false) {

                @Override
                protected String extractTimeField(final DateAndTimeValue value) {
                    return value.getUTCCalendarClone().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.getDefault());
                }
            };
        }
        rearranger.append(monthFactory);
        cellFactories.add(monthFactory);
    }
    // day of month
    if (m_useDay.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_dayColName.getStringValue());
        AbstractTimeExtractorCellFactory dayFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getDayOfMonth();
            }
        };
        rearranger.append(dayFactory);
        cellFactories.add(dayFactory);
    }
    // day of week
    AbstractTimeExtractorCellFactory dayOfWeekFactory = null;
    if (m_useDayOfWeek.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_dayOfWeekColName.getStringValue());
        if (m_dayOfWeekRepresentationModel.getStringValue().equals(AbstractFieldExtractorNodeDialog.AS_INT)) {
            dayOfWeekFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

                @Override
                protected int extractTimeField(final DateAndTimeValue value) {
                    return value.getUTCCalendarClone().get(Calendar.DAY_OF_WEEK);
                }
            };
        } else {
            dayOfWeekFactory = new AbstractTimeExtractorStringCellFactory(colName, colIdx, false) {

                @Override
                protected String extractTimeField(final DateAndTimeValue value) {
                    return value.getUTCCalendarClone().getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.getDefault());
                }
            };
        // extract the display name of the day of week
        }
        rearranger.append(dayOfWeekFactory);
        cellFactories.add(dayOfWeekFactory);
    }
    // day of year
    if (m_useDayOfYear.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_dayOfYearColName.getStringValue());
        AbstractTimeExtractorCellFactory dayofYearFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getUTCCalendarClone().get(Calendar.DAY_OF_YEAR);
            }
        };
        rearranger.append(dayofYearFactory);
        cellFactories.add(dayofYearFactory);
    }
    // week of year
    if (m_useWeekOfYear.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_weekOfYearColName.getStringValue());
        AbstractTimeExtractorCellFactory weekofYearFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, false) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getUTCCalendarClone().get(Calendar.WEEK_OF_YEAR);
            }
        };
        rearranger.append(weekofYearFactory);
        cellFactories.add(weekofYearFactory);
    }
    return new SingleCellFactoryCompound(rearranger, cellFactories);
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) SingleCellFactoryCompound(org.knime.timeseries.node.extract.SingleCellFactoryCompound) AbstractTimeExtractorIntCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorIntCellFactory) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) ArrayList(java.util.ArrayList) AbstractTimeExtractorStringCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorStringCellFactory) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) AbstractTimeExtractorCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory)

Example 3 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class ExtractTimeWindowNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    boolean useTime = m_fromDate.useTime() || m_toDate.useTime();
    BufferedDataTable in = inData[0];
    DataTableSpec outs = in.getDataTableSpec();
    final int colIndex = outs.findColumnIndex(m_columnName.getStringValue());
    BufferedDataContainer t = exec.createDataContainer(outs);
    final long totalRowCount = in.size();
    int currentIteration = 0;
    try {
        for (DataRow r : in) {
            // increment before printing to achieve a 1-based index
            currentIteration++;
            exec.checkCanceled();
            exec.setProgress(currentIteration / (double) totalRowCount, "Processing row " + currentIteration);
            DataCell cell = r.getCell(colIndex);
            if (cell.isMissing()) {
                // do not include missing values -> skip it
                continue;
            }
            Calendar time = ((DateAndTimeValue) cell).getUTCCalendarClone();
            // which is implemented as a real < or >
            if (!useTime) {
                DateAndTimeCell.resetTimeFields(time);
            }
            if (time.compareTo(m_fromDate.getCalendar()) >= 0 && time.compareTo(m_toDate.getCalendar()) <= 0) {
                t.addRowToTable(r);
            }
        }
    } finally {
        t.close();
    }
    return new BufferedDataTable[] { t.getTable() };
}
Also used : DataTableSpec(org.knime.core.data.DataTableSpec) BufferedDataContainer(org.knime.core.node.BufferedDataContainer) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) SettingsModelCalendar(org.knime.timeseries.util.SettingsModelCalendar) BufferedDataTable(org.knime.core.node.BufferedDataTable) DataCell(org.knime.core.data.DataCell) DataRow(org.knime.core.data.DataRow)

Example 4 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class MaskTimeNodeModel method createCellFactory.

private SingleCellFactory createCellFactory(final DataColumnSpec spec, final int colIdx, final String maskMode) {
    return new SingleCellFactory(spec) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell dc = row.getCell(colIdx);
            if (dc.isMissing()) {
                return DataType.getMissingCell();
            }
            if (dc.getType().isCompatible(DateAndTimeValue.class)) {
                DateAndTimeValue v = (DateAndTimeValue) dc;
                Calendar time = v.getUTCCalendarClone();
                if (maskMode.equals(MASK_DATE)) {
                    DateAndTimeCell.resetDateFields(time);
                    if (!v.hasTime()) {
                        // date is masked and no time -> missing value
                        m_nrInvalids++;
                        return DataType.getMissingCell();
                    }
                    m_onlyInvalids = false;
                    return new DateAndTimeCell(time.getTimeInMillis(), false, v.hasTime(), v.hasMillis());
                } else if (maskMode.equals(MASK_TIME)) {
                    DateAndTimeCell.resetTimeFields(time);
                    if (!v.hasDate()) {
                        // time is masked and no date -> missing cell
                        m_nrInvalids++;
                        return DataType.getMissingCell();
                    }
                    m_onlyInvalids = false;
                    return new DateAndTimeCell(time.getTimeInMillis(), v.hasDate(), false, false);
                } else if (maskMode.equals(MASK_MILLIS)) {
                    resetMilliSeconds(time);
                    m_onlyInvalids = false;
                    return new DateAndTimeCell(time.getTimeInMillis(), v.hasDate(), v.hasTime(), false);
                }
            }
            LOGGER.error("Unsupported data type: " + dc.getType() + "!");
            return DataType.getMissingCell();
        }
    };
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) Calendar(java.util.Calendar) 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 5 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue in project knime-core by knime.

the class Time2StringNodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 * @since 2.6
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec inSpec) throws InvalidSettingsException {
    // check if input has dateandtime column
    if (!inSpec.containsCompatibleType(DateAndTimeValue.class)) {
        throw new InvalidSettingsException("Input table must contain at least timestamp column!");
    }
    // currently selected column still there?
    String selectedColName = m_selectedCol.getStringValue();
    if (selectedColName != null && !selectedColName.isEmpty()) {
        if (!inSpec.containsName(selectedColName)) {
            throw new InvalidSettingsException("Column " + selectedColName + " not found in input spec!");
        }
    } else {
        // no value set: auto-configure -> choose first timeseries
        for (DataColumnSpec colSpec : inSpec) {
            if (colSpec.getType().isCompatible(DateAndTimeValue.class)) {
                String colName = colSpec.getName();
                m_selectedCol.setStringValue(colName);
                m_newColName.setStringValue(colName + "_" + COL_NAME_SUFFIX);
                setWarningMessage("Auto-selected column: '" + colName + "'");
                break;
            }
        }
    }
    ColumnRearranger rearranger = new ColumnRearranger(inSpec);
    // if replace -> use original column name
    final boolean replace = m_replaceCol.getBooleanValue();
    String colName = DataTableSpec.getUniqueColumnName(inSpec, m_newColName.getStringValue());
    if (replace) {
        colName = m_selectedCol.getStringValue();
    }
    DataColumnSpecCreator specCreator = new DataColumnSpecCreator(colName, StringCell.TYPE);
    final SimpleDateFormat dateFormat = new SimpleDateFormat(m_pattern.getStringValue());
    dateFormat.setTimeZone(DateAndTimeCell.UTC_TIMEZONE);
    final int colIdx = inSpec.findColumnIndex(m_selectedCol.getStringValue());
    SingleCellFactory factory = new SingleCellFactory(specCreator.createSpec()) {

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell dc = row.getCell(colIdx);
            if (dc.isMissing()) {
                return DataType.getMissingCell();
            }
            if (dc.getType().isCompatible(DateAndTimeValue.class)) {
                DateAndTimeValue v = (DateAndTimeValue) dc;
                String result = dateFormat.format(v.getUTCCalendarClone().getTime());
                return new StringCell(result);
            }
            LOGGER.error("Encountered unsupported data type: " + dc.getType() + " in row: " + row.getKey());
            return DataType.getMissingCell();
        }
    };
    if (!replace) {
        rearranger.append(factory);
    } else {
        rearranger.replace(factory, m_selectedCol.getStringValue());
    }
    return rearranger;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) StringCell(org.knime.core.data.def.StringCell) DataCell(org.knime.core.data.DataCell) SimpleDateFormat(java.text.SimpleDateFormat) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Aggregations

DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)24 DataCell (org.knime.core.data.DataCell)16 DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)11 DataRow (org.knime.core.data.DataRow)8 DoubleValue (org.knime.core.data.DoubleValue)8 IntValue (org.knime.core.data.IntValue)8 LongValue (org.knime.core.data.LongValue)8 IntCell (org.knime.core.data.def.IntCell)8 DoubleCell (org.knime.core.data.def.DoubleCell)7 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)7 Calendar (java.util.Calendar)6 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)6 LongCell (org.knime.core.data.def.LongCell)6 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)5 SettingsModelCalendar (org.knime.timeseries.util.SettingsModelCalendar)5 DataColumnSpec (org.knime.core.data.DataColumnSpec)3 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)3 BufferedDataTable (org.knime.core.node.BufferedDataTable)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2