Search in sources :

Example 36 with Temporal

use of java.time.temporal.Temporal in project knime-core by knime.

the class LoopStartWindowNodeModel method skipTemporalWindow.

/**
 * Skips the window for temporal data types until we obtain a window containing at least one row.
 *
 * @param row that is currently considered.
 * @param column index of the time column.
 * @param startInterval starting interval of the windows.
 * @param windowDuration duration of the window.
 */
private void skipTemporalWindow(DataRow row, final int column, final TemporalAmount startInterval, final TemporalAmount windowDuration) {
    while (row != null) {
        /* Check if current row lies beyond next starting temporal. */
        while (compareTemporal(getTemporal(row.getCell(column)), m_nextStartTemporal) < 0 && m_rowIterator.hasNext()) {
            DataRow temp = m_rowIterator.next();
            if (temp.getCell(column).isMissing()) {
                printMissingWarning();
                continue;
            } else if (compareTemporal(m_prevTemporal, getTemporal(temp.getCell(column))) > 0) {
                throw new IllegalStateException(m_orderException);
            }
            m_prevTemporal = getTemporal(row.getCell(column));
            row = temp;
        }
        /* Check for overflow of the window. Necessary for the three different window definitions.*/
        if (compareTemporal(m_nextStartTemporal.plus(windowDuration), m_nextStartTemporal) < 0) {
            switch(m_windowConfig.getWindowDefinition()) {
                case FORWARD:
                    m_bufferedRows.addFirst(row);
                    break;
                case BACKWARD:
                    break;
                case CENTRAL:
                    /* Check if currently considered central time point lies before the overflow. */
                    if (compareTemporal(m_nextStartTemporal.plus(windowDuration), getMin(m_nextStartTemporal).plus(((Duration) windowDuration).dividedBy(2))) < 0) {
                        /* Check if the current row lies after the current starting point. */
                        if (compareTemporal(getTemporal(row.getCell(column)), m_nextStartTemporal) >= 0) {
                            m_bufferedRows.addFirst(row);
                        }
                    }
                    break;
                default:
            }
            break;
        }
        /* Checks if current row lies within next temporal window */
        if (compareTemporal(getTemporal(row.getCell(column)), m_nextStartTemporal) < 0 && !m_rowIterator.hasNext()) {
            /* There are no more rows that could lie within an upcoming window. */
            break;
        } else if (compareTemporal(getTemporal(row.getCell(column)), m_windowEndTemporal.plus(startInterval)) <= 0) {
            m_bufferedRows.addFirst(row);
            break;
        }
        /* If next row lies beyond the defined next window move it until the rows lies within an upcoming window or the window passed said row. */
        Temporal tempNextEnd = m_windowEndTemporal.plus(startInterval).plus(startInterval);
        Temporal nextTemporalStart = tempNextEnd.minus(windowDuration);
        /* Checks for overflow. */
        if (compareTemporal(tempNextEnd, m_windowEndTemporal) <= 0 && compareTemporal(nextTemporalStart, tempNextEnd) <= 0) {
            m_rowIterator.close();
            break;
        } else if (compareTemporal(nextTemporalStart, tempNextEnd) >= 0) {
            m_nextStartTemporal = getMin(m_nextStartTemporal);
        } else {
            m_nextStartTemporal = nextTemporalStart;
        }
        m_windowEndTemporal = m_windowEndTemporal.plus(startInterval);
    // Temporal nextTemporalStart = m_nextStartTemporal.plus(startInterval);
    // 
    // /* Check for overflow of the next starting interval. */
    // if (compareTemporal(nextTemporalStart, m_nextStartTemporal) <= 0) {
    // m_rowIterator.close();
    // break;
    // } else {
    // m_nextStartTemporal = nextTemporalStart;
    // }
    }
}
Also used : Temporal(java.time.temporal.Temporal) DataRow(org.knime.core.data.DataRow)

Example 37 with Temporal

use of java.time.temporal.Temporal in project drools by kiegroup.

the class EvalHelper method getDefinedValue.

public static PropertyValueResult getDefinedValue(final Object current, final String property) {
    Object result;
    if (current == null) {
        return PropertyValueResult.notDefined();
    } else if (current instanceof Map) {
        result = ((Map) current).get(property);
        if (result == null) {
            // most cases "result" will be defined, so checking here only in case null was to signify missing key altogether.
            if (!((Map) current).containsKey(property)) {
                return PropertyValueResult.notDefined();
            }
        }
    } else if (current instanceof Period) {
        switch(property) {
            case "years":
                result = ((Period) current).getYears();
                break;
            case "months":
                result = ((Period) current).getMonths() % 12;
                break;
            case "days":
                result = ((Period) current).getDays() % 30;
                break;
            default:
                return PropertyValueResult.notDefined();
        }
    } else if (current instanceof Duration) {
        switch(property) {
            case "days":
                result = ((Duration) current).toDays();
                break;
            case "hours":
                result = ((Duration) current).toHours() % 24;
                break;
            case "minutes":
                result = ((Duration) current).toMinutes() % 60;
                break;
            case "seconds":
                result = ((Duration) current).getSeconds() % 60;
                break;
            default:
                return PropertyValueResult.notDefined();
        }
    } else if (current instanceof Temporal) {
        switch(property) {
            case "year":
                result = ((Temporal) current).get(ChronoField.YEAR);
                break;
            case "month":
                result = ((Temporal) current).get(ChronoField.MONTH_OF_YEAR);
                break;
            case "day":
                result = ((Temporal) current).get(ChronoField.DAY_OF_MONTH);
                break;
            case "hour":
                result = ((Temporal) current).get(ChronoField.HOUR_OF_DAY);
                break;
            case "minute":
                result = ((Temporal) current).get(ChronoField.MINUTE_OF_HOUR);
                break;
            case "second":
                result = ((Temporal) current).get(ChronoField.SECOND_OF_MINUTE);
                break;
            case "time offset":
            case "timezone":
                result = Duration.ofSeconds(((Temporal) current).get(ChronoField.OFFSET_SECONDS));
                break;
            case "weekday":
                result = ((Temporal) current).get(ChronoField.DAY_OF_WEEK);
                break;
            default:
                return PropertyValueResult.notDefined();
        }
    } else {
        Method getter = getGenericAccessor(current.getClass(), property);
        if (getter != null) {
            try {
                result = getter.invoke(current);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                e.printStackTrace();
                return PropertyValueResult.of(Either.ofLeft(e));
            }
        } else {
            // WORST-CASE: if code reached here, means that "property" is not defined on the "current" object at all.
            return PropertyValueResult.notDefined();
        }
    }
    // before returning, coerce "result" into number.
    result = coerceNumber(result);
    return PropertyValueResult.ofValue(result);
}
Also used : Temporal(java.time.temporal.Temporal) Period(java.time.Period) Duration(java.time.Duration) Method(java.lang.reflect.Method) Map(java.util.Map) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Temporal (java.time.temporal.Temporal)37 Test (org.testng.annotations.Test)24 TemporalAmount (java.time.temporal.TemporalAmount)12 Duration (java.time.Duration)7 DateTimeParseException (java.time.format.DateTimeParseException)7 TemporalUnit (java.time.temporal.TemporalUnit)7 DateTimeException (java.time.DateTimeException)6 LocalDate (java.time.LocalDate)6 LocalDateTime (java.time.LocalDateTime)6 TemporalAdjuster (java.time.temporal.TemporalAdjuster)6 LocalTime (java.time.LocalTime)5 ZonedDateTime (java.time.ZonedDateTime)5 ArrayList (java.util.ArrayList)5 Period (java.time.Period)4 DataRow (org.knime.core.data.DataRow)4 BufferedDataContainer (org.knime.core.node.BufferedDataContainer)4 BufferedDataTable (org.knime.core.node.BufferedDataTable)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 DataCell (org.knime.core.data.DataCell)3 RowKey (org.knime.core.data.RowKey)3