Search in sources :

Example 1 with DateAndTimeCell

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

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

the class String2DateNodeModel method createColumnRearranger.

/**
 * {@inheritDoc}
 * @throws InvalidSettingsException
 * @since 2.6
 */
@Override
protected ColumnRearranger createColumnRearranger(final DataTableSpec spec, final SimpleStreamableOperatorInternals emptyInternals) throws InvalidSettingsException {
    if (m_formatModel.getStringValue() == null) {
        throw new InvalidSettingsException("No format selected.");
    }
    try {
        m_dateFormat = new SimpleDateFormat(m_formatModel.getStringValue());
    } catch (IllegalArgumentException ex) {
        throw new InvalidSettingsException("Invalid format: " + m_formatModel.getStringValue(), ex);
    }
    m_dateFormat.setTimeZone(DateAndTimeCell.UTC_TIMEZONE);
    String selectedCol = m_selectedColModel.getStringValue();
    if (selectedCol == null || selectedCol.isEmpty()) {
        // try to find first String compatible one and auto-guess it
        for (DataColumnSpec cs : spec) {
            if (cs.getType().isCompatible(StringValue.class)) {
                m_selectedColModel.setStringValue(cs.getName());
                setWarningMessage("Auto-guessing first String compatible column: " + cs.getName());
                break;
            }
        }
    }
    // if still null -> no String compatible column at all
    if (selectedCol == null || selectedCol.isEmpty()) {
        throw new InvalidSettingsException("No String compatible column found!");
    }
    final int colIndex = spec.findColumnIndex(selectedCol);
    if (colIndex < 0) {
        throw new InvalidSettingsException("No such column: " + selectedCol);
    }
    DataColumnSpec colSpec = spec.getColumnSpec(colIndex);
    if (!colSpec.getType().isCompatible(StringValue.class)) {
        throw new InvalidSettingsException("Column \"" + selectedCol + "\" does not contain string values: " + colSpec.getType().toString());
    }
    ColumnRearranger result = new ColumnRearranger(spec);
    String uniqueColName = selectedCol;
    if (!m_replace.getBooleanValue()) {
        // if we do not have a default new column name yet
        // create one as done in
        // check whether the new column name is unique...
        uniqueColName = DataTableSpec.getUniqueColumnName(spec, m_newColNameModel.getStringValue());
        m_newColNameModel.setStringValue(uniqueColName);
    }
    DataColumnSpec newColSpec = new DataColumnSpecCreator(uniqueColName, DateAndTimeCell.TYPE).createSpec();
    m_dateFormat = new SimpleDateFormat(m_formatModel.getStringValue());
    m_dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
    SingleCellFactory c = new SingleCellFactory(newColSpec) {

        private int m_failCounter = 0;

        @Override
        public DataCell getCell(final DataRow row) {
            DataCell cell = row.getCell(colIndex);
            if (cell.isMissing() || !(cell instanceof StringValue)) {
                return DataType.getMissingCell();
            }
            try {
                String source = ((StringValue) cell).getStringValue();
                Date date = m_dateFormat.parse(source);
                Calendar calendar = DateAndTimeCell.getUTCCalendar();
                calendar.setTimeInMillis(date.getTime());
                // dependent on the type create the referring cell
                return new DateAndTimeCell(calendar.getTimeInMillis(), m_useDate, m_useTime, m_useMillis);
            } catch (ParseException pe) {
                m_failCounter++;
                if (m_cancelOnFail.getBooleanValue() && m_failCounter >= m_failNumberModel.getIntValue()) {
                    throw new IllegalArgumentException("Maximum number of fails reached: " + m_failNumberModel.getIntValue());
                }
                return DataType.getMissingCell();
            }
        }

        @Override
        public void afterProcessing() {
            setFailMessage(m_failCounter);
            emptyInternals.getConfig().addLong(INTERNALS_KEY_FAIL_COUNT, m_failCounter);
        }
    };
    if (m_replace.getBooleanValue()) {
        result.replace(c, colIndex);
    } else {
        result.append(c);
    }
    return result;
}
Also used : DataColumnSpecCreator(org.knime.core.data.DataColumnSpecCreator) Calendar(java.util.Calendar) SettingsModelString(org.knime.core.node.defaultnodesettings.SettingsModelString) DataRow(org.knime.core.data.DataRow) Date(java.util.Date) DataColumnSpec(org.knime.core.data.DataColumnSpec) ColumnRearranger(org.knime.core.data.container.ColumnRearranger) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) DataCell(org.knime.core.data.DataCell) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) ParseException(java.text.ParseException) StringValue(org.knime.core.data.StringValue) SimpleDateFormat(java.text.SimpleDateFormat) SingleCellFactory(org.knime.core.data.container.SingleCellFactory)

Example 3 with DateAndTimeCell

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

the class TSAverageHandler method getMean.

private DataCell getMean() {
    if (m_previous instanceof IntValue) {
        // get an int, create an int
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new IntCell((int) Math.round(mean));
    }
    if (m_previous instanceof LongValue) {
        // get an int, create an int
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new LongCell(Math.round(mean));
    }
    if (m_previous instanceof DoubleValue) {
        // get an double, create an double
        double mean = (((DoubleValue) m_previous).getDoubleValue() + ((DoubleValue) m_next).getDoubleValue()) * 0.5;
        return new DoubleCell(mean);
    }
    if (m_previous instanceof DateAndTimeValue) {
        // get an int, create an int
        DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
        DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
        boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
        boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
        boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
        double d = dataCell1.getUTCTimeInMillis() + dataCell2.getUTCTimeInMillis();
        d *= 0.5;
        return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
    }
    return DataType.getMissingCell();
}
Also used : LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) LongValue(org.knime.core.data.LongValue) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue) IntCell(org.knime.core.data.def.IntCell)

Example 4 with DateAndTimeCell

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

the class TSLinearHandler method getLinearInterpolation.

private DataCell getLinearInterpolation(final double stepnumber, final double stepcount) {
    if (m_previous instanceof DoubleValue || m_previous instanceof IntValue || m_previous instanceof LongValue) {
        double prev = ((DoubleValue) m_previous).getDoubleValue();
        double next = ((DoubleValue) m_next).getDoubleValue();
        double lin = prev + 1.0 * stepnumber / (1.0 * stepcount) * (next - prev);
        if (m_previous instanceof IntValue) {
            // get an int, create an int
            return new IntCell((int) Math.round(lin));
        }
        if (m_previous instanceof LongValue) {
            // get an long, create an long
            return new LongCell(Math.round(lin));
        }
        return new DoubleCell(lin);
    }
    if (m_previous instanceof DateAndTimeValue) {
        // get an int, create an int
        DateAndTimeValue dataCell1 = (DateAndTimeValue) m_previous;
        DateAndTimeValue dataCell2 = ((DateAndTimeValue) m_next);
        boolean hasDate = dataCell1.hasDate() | dataCell2.hasDate();
        boolean hasTime = dataCell1.hasTime() | dataCell2.hasTime();
        boolean hasMilis = dataCell1.hasMillis() | dataCell2.hasMillis();
        double prev = dataCell1.getUTCTimeInMillis();
        double next = dataCell2.getUTCTimeInMillis();
        double d = prev + stepnumber / stepcount * (next - prev);
        return new DateAndTimeCell((long) d, hasDate, hasTime, hasMilis);
    }
    return DataType.getMissingCell();
}
Also used : LongCell(org.knime.core.data.def.LongCell) DoubleValue(org.knime.core.data.DoubleValue) DoubleCell(org.knime.core.data.def.DoubleCell) DateAndTimeValue(org.knime.core.data.date.DateAndTimeValue) LongValue(org.knime.core.data.LongValue) DateAndTimeCell(org.knime.core.data.date.DateAndTimeCell) IntValue(org.knime.core.data.IntValue) IntCell(org.knime.core.data.def.IntCell)

Example 5 with DateAndTimeCell

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

the class DateShiftConfigure method getTimeBasedValueCellFactory.

/**
 * @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 time the configured time as Calendar
 * @return the cell factory
 */
public static SingleCellFactory getTimeBasedValueCellFactory(final DataTableSpec spec, final int col1Idx, final int g, final DateShiftConfigure conf, final Calendar time) {
    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);
            if ((cell1.isMissing())) {
                return DataType.getMissingCell();
            }
            Calendar c = (Calendar) time.clone();
            c.add(g, ((IntValue) cell1).getIntValue());
            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) SingleCellFactory(org.knime.core.data.container.SingleCellFactory) DataRow(org.knime.core.data.DataRow)

Aggregations

DateAndTimeCell (org.knime.core.data.date.DateAndTimeCell)16 DataCell (org.knime.core.data.DataCell)12 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)11 Calendar (java.util.Calendar)9 DataRow (org.knime.core.data.DataRow)8 SingleCellFactory (org.knime.core.data.container.SingleCellFactory)8 DoubleValue (org.knime.core.data.DoubleValue)6 IntValue (org.knime.core.data.IntValue)6 LongValue (org.knime.core.data.LongValue)6 DoubleCell (org.knime.core.data.def.DoubleCell)6 IntCell (org.knime.core.data.def.IntCell)6 LongCell (org.knime.core.data.def.LongCell)6 SettingsModelCalendar (org.knime.timeseries.util.SettingsModelCalendar)6 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)4 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 DataColumnSpecCreator (org.knime.core.data.DataColumnSpecCreator)2 StringValue (org.knime.core.data.StringValue)2 ColumnRearranger (org.knime.core.data.container.ColumnRearranger)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2