Search in sources :

Example 1 with AbstractTimeExtractorCellFactory

use of org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory 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 2 with AbstractTimeExtractorCellFactory

use of org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory 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 3 with AbstractTimeExtractorCellFactory

use of org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory in project knime-core by knime.

the class TimeFieldExtractorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    if (!checkSelection()) {
        setWarningMessage(AbstractFieldExtractorNodeDialog.NOTHING_SELECTED_MESSAGE);
    }
    SingleCellFactoryCompound compound = createColumnRearranger(inData[0].getDataTableSpec());
    ColumnRearranger rearranger = compound.getColumnRearranger();
    BufferedDataTable out = exec.createColumnRearrangeTable(inData[0], rearranger, exec);
    int nrMissingValues = 0;
    for (AbstractTimeExtractorCellFactory cellFactory : compound.getUsedCellFactories()) {
        nrMissingValues += cellFactory.getNumberMissingValues();
    }
    if (nrMissingValues > 0) {
        setWarningMessage("Produced " + nrMissingValues + " missing values due to missing time" + " information in input date/time!");
    }
    return new BufferedDataTable[] { out };
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) SingleCellFactoryCompound(org.knime.timeseries.node.extract.SingleCellFactoryCompound) BufferedDataTable(org.knime.core.node.BufferedDataTable) AbstractTimeExtractorCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory)

Example 4 with AbstractTimeExtractorCellFactory

use of org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory in project knime-core by knime.

the class DateFieldExtractorNodeModel method execute.

/**
 * {@inheritDoc}
 */
@Override
protected BufferedDataTable[] execute(final BufferedDataTable[] inData, final ExecutionContext exec) throws Exception {
    if (!checkSelection()) {
        setWarningMessage(AbstractFieldExtractorNodeDialog.NOTHING_SELECTED_MESSAGE);
    }
    SingleCellFactoryCompound compound = createColumnRearranger(inData[0].getDataTableSpec());
    ColumnRearranger rearranger = compound.getColumnRearranger();
    BufferedDataTable out = exec.createColumnRearrangeTable(inData[0], rearranger, exec);
    int nrMissingValues = 0;
    for (AbstractTimeExtractorCellFactory cellFactory : compound.getUsedCellFactories()) {
        nrMissingValues += cellFactory.getNumberMissingValues();
    }
    if (nrMissingValues > 0) {
        setWarningMessage("Produced " + nrMissingValues + " missing values due to missing date" + " information in input date/time!");
    }
    return new BufferedDataTable[] { out };
}
Also used : ColumnRearranger(org.knime.core.data.container.ColumnRearranger) SingleCellFactoryCompound(org.knime.timeseries.node.extract.SingleCellFactoryCompound) BufferedDataTable(org.knime.core.node.BufferedDataTable) AbstractTimeExtractorCellFactory(org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory)

Aggregations

ColumnRearranger (org.knime.core.data.container.ColumnRearranger)4 AbstractTimeExtractorCellFactory (org.knime.timeseries.node.extract.AbstractTimeExtractorCellFactory)4 SingleCellFactoryCompound (org.knime.timeseries.node.extract.SingleCellFactoryCompound)4 ArrayList (java.util.ArrayList)2 DateAndTimeValue (org.knime.core.data.date.DateAndTimeValue)2 BufferedDataTable (org.knime.core.node.BufferedDataTable)2 SettingsModelString (org.knime.core.node.defaultnodesettings.SettingsModelString)2 AbstractTimeExtractorIntCellFactory (org.knime.timeseries.node.extract.AbstractTimeExtractorIntCellFactory)2 DataCell (org.knime.core.data.DataCell)1 DataRow (org.knime.core.data.DataRow)1 IntCell (org.knime.core.data.def.IntCell)1 AbstractTimeExtractorStringCellFactory (org.knime.timeseries.node.extract.AbstractTimeExtractorStringCellFactory)1