Search in sources :

Example 1 with Unit

use of org.knime.time.node.window.LoopStartWindowConfiguration.Unit in project knime-core by knime.

the class LoopStartWindowNodeDialog method saveSettingsTo.

/**
 * {@inheritDoc}
 */
@Override
protected void saveSettingsTo(final NodeSettingsWO settings) throws InvalidSettingsException {
    LoopStartWindowConfiguration config = new LoopStartWindowConfiguration();
    config.setEventWindowSize((Integer) m_windowSizeSpinner.getValue());
    config.setEventStepSize((Integer) m_stepSizeSpinner.getValue());
    config.setLimitWindow(m_limitWindowCheckBox.isSelected());
    if (m_forwardRButton.isSelected()) {
        config.setWindowDefinition(WindowDefinition.FORWARD);
    } else if (m_backwardRButton.isSelected()) {
        config.setWindowDefinition(WindowDefinition.BACKWARD);
    } else {
        config.setWindowDefinition(WindowDefinition.CENTRAL);
    }
    if (m_rowTrigRButton.isSelected()) {
        config.setTrigger(Trigger.ROW);
    } else {
        config.setTrigger(Trigger.TIME);
        if (m_columnSelector == null || m_columnSelector.getSelectedAsSpec() == null) {
            throw new InvalidSettingsException("No valid time column has been chosen");
        }
        /* Check if step size is smaller than 0. */
        try {
            if (m_stepSizeTime.getText() != null && Double.parseDouble(m_stepSizeTime.getText()) < 0) {
                throw new InvalidSettingsException("Step size '" + m_stepSizeTime.getText() + "' invalid. Step size must be greater than 0.");
            }
        } catch (NumberFormatException e) {
        }
        try {
            if (m_windowSizeTime.getText() != null && Double.parseDouble(m_windowSizeTime.getText()) < 0) {
                throw new InvalidSettingsException("Window size '" + m_windowSizeTime.getText() + "' invalid. Window size must be greater than 0.");
            }
        } catch (NumberFormatException e) {
        }
        /* Check that either no unit is selected or that the given input does not contain any letters. */
        if (m_startTimeUnit.getSelectedItem() != Unit.NO_UNIT && !m_stepSizeTime.getText().matches("^[0-9]+$")) {
            throw new InvalidSettingsException("Step size: input '" + m_stepSizeTime.getText() + "' invalid. Only integers are allowed when unit '" + m_startTimeUnit.getSelectedItem() + "' is chosen");
        }
        if (m_timeWindowUnit.getSelectedItem() != Unit.NO_UNIT && !m_windowSizeTime.getText().matches("^[0-9]+$")) {
            throw new InvalidSettingsException("Window size: input '" + m_windowSizeTime.getText() + "' invalid. Only integers are allowed when unit '" + m_timeWindowUnit.getSelectedItem() + "' is chosen");
        }
        try {
            Duration startDur = null;
            /* If unit is milliseconds we have it to change to seconds for parsing. */
            if (((Unit) m_startTimeUnit.getSelectedItem()) == Unit.MILLISECONDS) {
                double tempStart = Double.parseDouble(m_stepSizeTime.getText());
                tempStart /= 1000;
                startDur = DurationPeriodFormatUtils.parseDuration(tempStart + Unit.SECONDS.getUnitLetter());
            } else {
                startDur = DurationPeriodFormatUtils.parseDuration(m_stepSizeTime.getText() + ((Unit) m_startTimeUnit.getSelectedItem()).getUnitLetter());
            }
            /* Limit step size to 24h */
            if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalTimeCell.class))) {
                Duration temp = Duration.ofHours(24);
                if (startDur.compareTo(temp) > 0) {
                    throw new InvalidSettingsException("Step size must not be greater than 24h when LocalTime is selected");
                } else if (startDur.compareTo(Duration.ZERO) == 0 || startDur.isNegative()) {
                    throw new InvalidSettingsException("Step size '" + m_stepSizeTime.getText() + ((Unit) m_startTimeUnit.getSelectedItem()).getUnitLetter() + "' invalid. Step size must be greater than 0.");
                }
            }
            if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalDateCell.class))) {
                throw new InvalidSettingsException("Step size: Duration based step size '" + m_stepSizeTime.getText() + "' is not allowed for type LocalDate. Note that 'm' is reserved for minutes, use 'M' for months.");
            }
            config.setTimeStepSize(m_stepSizeTime.getText());
        } catch (DateTimeParseException e) {
            try {
                Period startPer = DurationPeriodFormatUtils.parsePeriod(m_stepSizeTime.getText() + ((Unit) m_startTimeUnit.getSelectedItem()).getUnitLetter());
                /* Period is not allowed. */
                if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalTimeCell.class))) {
                    throw new InvalidSettingsException("Step size: Date based step size '" + m_stepSizeTime.getText() + "' is not allowed for type LocalTime. Note that 'M' is reserved for months, use 'm' for minutes.");
                } else if (m_centralRButton.isSelected()) {
                    throw new InvalidSettingsException("Step size: Date based step size '" + m_stepSizeTime.getText() + "' is not allowed for central windowing. Note that 'M' is reserved for months, use 'm' for minutes.");
                }
                if (startPer.isZero() || startPer.isNegative()) {
                    throw new InvalidSettingsException("Step size '" + m_stepSizeTime.getText() + ((Unit) m_startTimeUnit.getSelectedItem()).getUnitLetter() + "' invalid. Step Size must be greater than 0");
                }
                config.setTimeStepSize(m_stepSizeTime.getText());
            } catch (DateTimeParseException e2) {
                throw new InvalidSettingsException("Step size: '" + m_stepSizeTime.getText() + "' is not a valid duration. Note that 'M' is reserved for months, use 'm' for minutes.");
            }
        }
        try {
            Duration windowDur = null;
            /* If unit is milliseconds we have it to change to seconds for parsing. */
            if (((Unit) m_timeWindowUnit.getSelectedItem()) == Unit.MILLISECONDS) {
                double tempWindow = Double.parseDouble(m_windowSizeTime.getText());
                tempWindow /= 1000;
                windowDur = DurationPeriodFormatUtils.parseDuration(tempWindow + Unit.SECONDS.getUnitLetter());
            } else {
                windowDur = DurationPeriodFormatUtils.parseDuration(m_windowSizeTime.getText() + ((Unit) m_timeWindowUnit.getSelectedItem()).getUnitLetter());
            }
            /* Limit window to 24h */
            if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalTimeCell.class))) {
                Duration temp = Duration.ofHours(24);
                if (windowDur.compareTo(temp) > 0) {
                    throw new InvalidSettingsException("Window size must not be greater than 24h when LocalTime is selected");
                } else if (windowDur.isZero() || windowDur.isNegative()) {
                    throw new InvalidSettingsException("Window size '" + m_windowSizeTime.getText() + ((Unit) m_timeWindowUnit.getSelectedItem()).getUnitLetter() + "' invalid. Window size must be greater than 0");
                }
            }
            if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalDateCell.class))) {
                throw new InvalidSettingsException("Window size: Time based window size '" + m_windowSizeTime.getText() + "' is not allowed for type LocalDate. Note that 'm' is reserved for minutes, use 'M' for months.");
            }
            config.setTimeWindowSize(m_windowSizeTime.getText());
        } catch (DateTimeParseException e) {
            try {
                Period windowPer = DurationPeriodFormatUtils.parsePeriod(m_windowSizeTime.getText() + ((Unit) m_timeWindowUnit.getSelectedItem()).getUnitLetter());
                /* Period is not allowed. */
                if (m_columnSelector.getSelectedAsSpec().getType().equals(DataType.getType(LocalTimeCell.class))) {
                    throw new InvalidSettingsException("Window size: Date based window size '" + m_windowSizeTime.getText() + "' is not allowed for type LocalTime. Note that 'M' is reserved for months, use 'm' for minutes.");
                } else if (m_centralRButton.isSelected()) {
                    throw new InvalidSettingsException("Window size: Date based window size '" + m_windowSizeTime.getText() + "' is not allowed for central windowing. Note that 'M' is reserved for months, use 'm' for minutes.");
                }
                if (windowPer.isZero() || windowPer.isNegative()) {
                    throw new InvalidSettingsException("Window size '" + m_windowSizeTime.getText() + ((Unit) m_timeWindowUnit.getSelectedItem()).getUnitLetter() + "' invalid. Window size must be greater than 0");
                }
                config.setTimeWindowSize(m_windowSizeTime.getText());
            } catch (DateTimeParseException e2) {
                throw new InvalidSettingsException("Window size: '" + m_windowSizeTime.getText() + "' is not a valid duration. Note that 'M' is reserved for months, use 'm' for minutes.");
            }
        }
        config.setTimeWindowUnit((Unit) m_timeWindowUnit.getSelectedItem());
        config.setTimeStepUnit((Unit) m_startTimeUnit.getSelectedItem());
    }
    config.setUseSpecifiedStartTime(m_useSpecifiedStartTimeCheckBox.isSelected());
    config.saveSettingsTo(settings);
    m_specifiedStartTime.saveSettingsTo(settings);
    m_columnSelector.saveSettingsTo(settings);
}
Also used : DateTimeParseException(java.time.format.DateTimeParseException) InvalidSettingsException(org.knime.core.node.InvalidSettingsException) LocalDateCell(org.knime.core.data.time.localdate.LocalDateCell) Period(java.time.Period) Duration(java.time.Duration) LocalTimeCell(org.knime.core.data.time.localtime.LocalTimeCell) Unit(org.knime.time.node.window.LoopStartWindowConfiguration.Unit)

Aggregations

Duration (java.time.Duration)1 Period (java.time.Period)1 DateTimeParseException (java.time.format.DateTimeParseException)1 LocalDateCell (org.knime.core.data.time.localdate.LocalDateCell)1 LocalTimeCell (org.knime.core.data.time.localtime.LocalTimeCell)1 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)1 Unit (org.knime.time.node.window.LoopStartWindowConfiguration.Unit)1