Search in sources :

Example 16 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue 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 17 with DateAndTimeValue

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

the class AbstractTimeExtractorIntCellFactory method getCell.

/**
 * {@inheritDoc}
 */
@Override
public DataCell getCell(final DataRow row) {
    DataCell cell = row.getCell(getColumnIndex());
    if (cell.isMissing()) {
        return DataType.getMissingCell();
    }
    DateAndTimeValue value = (DateAndTimeValue) cell;
    if (checkTime() && value.hasTime()) {
        producedValidValue();
        return new IntCell(extractTimeField(value));
    }
    if (checkDate() && value.hasDate()) {
        producedValidValue();
        return new IntCell(extractTimeField(value));
    }
    // no date set
    increaseMissingValueCount();
    return DataType.getMissingCell();
}
Also used : DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DataCell(org.knime.core.data.DataCell) IntCell(org.knime.core.data.def.IntCell)

Example 18 with DateAndTimeValue

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

the class TimeFieldExtractorNodeModel 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>();
    // ************************* TIME fields factories *******************/
    // hour
    AbstractTimeExtractorCellFactory hourFactory = null;
    if (m_useHour.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_hourColName.getStringValue());
        hourFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, true) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getHourOfDay();
            }
        };
        rearranger.append(hourFactory);
        cellFactories.add(hourFactory);
    }
    // minute
    AbstractTimeExtractorCellFactory minuteFactory = null;
    if (m_useMinute.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_minuteColName.getStringValue());
        minuteFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, true) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getMinute();
            }
        };
        rearranger.append(minuteFactory);
        cellFactories.add(minuteFactory);
    }
    // second
    AbstractTimeExtractorCellFactory secondFactory = null;
    if (m_useSecond.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_secondColName.getStringValue());
        secondFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, true) {

            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getSecond();
            }
        };
        rearranger.append(secondFactory);
        cellFactories.add(secondFactory);
    }
    // millisecond
    AbstractTimeExtractorCellFactory milliFactory = null;
    if (m_useMillis.getBooleanValue()) {
        String colName = DataTableSpec.getUniqueColumnName(inSpec, m_milliColName.getStringValue());
        milliFactory = new AbstractTimeExtractorIntCellFactory(colName, colIdx, true) {

            // here we also have to check if the value has millis
            @Override
            protected int extractTimeField(final DateAndTimeValue value) {
                return value.getMillis();
            }

            @Override
            public DataCell getCell(final DataRow row) {
                DataCell cell = row.getCell(colIdx);
                if (cell.isMissing()) {
                    return DataType.getMissingCell();
                }
                DateAndTimeValue value = (DateAndTimeValue) cell;
                if (value.hasMillis()) {
                    producedValidValue();
                    return new IntCell(extractTimeField(value));
                }
                // no date set
                increaseMissingValueCount();
                return DataType.getMissingCell();
            }
        };
        rearranger.append(milliFactory);
        cellFactories.add(milliFactory);
    }
    return new SingleCellFactoryCompound(rearranger, cellFactories);
}
Also used : 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) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) AbstractTimeExtractorCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory) IntCell(org.knime.core.data.def.IntCell) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) DataCell(org.knime.core.data.DataCell)

Example 19 with DateAndTimeValue

use of org.knime.core.data.date.DateAndTimeValue 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 20 with DateAndTimeValue

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

the class AbstractTimeExtractorStringCellFactory method getCell.

/**
 * {@inheritDoc}
 */
@Override
public DataCell getCell(final DataRow row) {
    DataCell cell = row.getCell(getColumnIndex());
    if (cell.isMissing()) {
        return DataType.getMissingCell();
    }
    DateAndTimeValue value = (DateAndTimeValue) cell;
    if (checkTime() && value.hasTime()) {
        producedValidValue();
        return new StringCell(extractTimeField(value));
    }
    if (checkDate() && value.hasDate()) {
        producedValidValue();
        return new StringCell(extractTimeField(value));
    }
    // no date set
    increaseMissingValueCount();
    return DataType.getMissingCell();
}
Also used : StringCell(org.knime.core.data.def.StringCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) DataCell(org.knime.core.data.DataCell)

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