Search in sources :

Example 51 with ChronoPeriod

use of java.time.chrono.ChronoPeriod 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 ChronoPeriod) {
        switch(property) {
            case "years":
                result = ((ChronoPeriod) current).get(ChronoUnit.YEARS);
                break;
            case "months":
                result = ((ChronoPeriod) current).get(ChronoUnit.MONTHS) % 12;
                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 TemporalAccessor) {
        switch(property) {
            case "year":
                result = ((TemporalAccessor) current).get(ChronoField.YEAR);
                break;
            case "month":
                result = ((TemporalAccessor) current).get(ChronoField.MONTH_OF_YEAR);
                break;
            case "day":
                result = ((TemporalAccessor) current).get(ChronoField.DAY_OF_MONTH);
                break;
            case "hour":
                result = ((TemporalAccessor) current).get(ChronoField.HOUR_OF_DAY);
                break;
            case "minute":
                result = ((TemporalAccessor) current).get(ChronoField.MINUTE_OF_HOUR);
                break;
            case "second":
                result = ((TemporalAccessor) current).get(ChronoField.SECOND_OF_MINUTE);
                break;
            case "time offset":
                if (((TemporalAccessor) current).isSupported(ChronoField.OFFSET_SECONDS)) {
                    result = Duration.ofSeconds(((TemporalAccessor) current).get(ChronoField.OFFSET_SECONDS));
                } else {
                    result = null;
                }
                break;
            case "timezone":
                ZoneId zoneId = ((TemporalAccessor) current).query(TemporalQueries.zoneId());
                if (zoneId != null) {
                    result = TimeZone.getTimeZone(zoneId).getID();
                    break;
                } else {
                    return PropertyValueResult.notDefined();
                }
            case "weekday":
                result = ((TemporalAccessor) current).get(ChronoField.DAY_OF_WEEK);
                break;
            default:
                return PropertyValueResult.notDefined();
        }
    } else if (current instanceof Range) {
        switch(property) {
            case "start included":
                result = ((Range) current).getLowBoundary() == RangeBoundary.CLOSED ? Boolean.TRUE : Boolean.FALSE;
                break;
            case "start":
                result = ((Range) current).getLowEndPoint();
                break;
            case "end":
                result = ((Range) current).getHighEndPoint();
                break;
            case "end included":
                result = ((Range) current).getHighBoundary() == RangeBoundary.CLOSED ? Boolean.TRUE : Boolean.FALSE;
                break;
            default:
                return PropertyValueResult.notDefined();
        }
    } else {
        Method getter = getGenericAccessor(current.getClass(), property);
        if (getter != null) {
            try {
                result = getter.invoke(current);
                if (result instanceof Character) {
                    result = result.toString();
                } else if (result instanceof java.util.Date) {
                    result = java.time.Instant.ofEpochMilli(((java.util.Date) result).getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
                }
            } 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 : ChronoPeriod(java.time.chrono.ChronoPeriod) TemporalAccessor(java.time.temporal.TemporalAccessor) ZoneId(java.time.ZoneId) Duration(java.time.Duration) Method(java.lang.reflect.Method) Range(org.kie.dmn.feel.runtime.Range) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 52 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project drools by kiegroup.

the class MaxFunctionTest method invokeArrayOfChronoPeriods.

@Test
public void invokeArrayOfChronoPeriods() {
    final ChronoPeriod p1Period = Period.parse("P1Y");
    final ChronoPeriod p1Comparable = ComparablePeriod.parse("P1Y");
    final ChronoPeriod p2Period = Period.parse("P1M");
    final ChronoPeriod p2Comparable = ComparablePeriod.parse("P1M");
    Predicate<ChronoPeriod> assertion = i -> i.get(ChronoUnit.YEARS) == 1 && i.get(ChronoUnit.MONTHS) == 0;
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(new Object[] { p1Period }), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(new Object[] { p1Comparable }), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(new Object[] { p1Period, p2Period }), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(new Object[] { p1Comparable, p2Period }), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(new Object[] { p1Period, p2Comparable }), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(new Object[] { p1Comparable, p2Comparable }), ChronoPeriod.class, assertion);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Period(java.time.Period) BigDecimal(java.math.BigDecimal) Arrays(java.util.Arrays) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) Predicate(java.util.function.Predicate) ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod) Test(org.junit.Test) Collections(java.util.Collections) ChronoPeriod(java.time.chrono.ChronoPeriod) Before(org.junit.Before) Test(org.junit.Test)

Example 53 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project drools by kiegroup.

the class MaxFunctionTest method invokeListOfChronoPeriods.

@Test
public void invokeListOfChronoPeriods() {
    final ChronoPeriod p1Period = Period.parse("P1Y");
    final ChronoPeriod p1Comparable = ComparablePeriod.parse("P1Y");
    final ChronoPeriod p2Period = Period.parse("P1M");
    final ChronoPeriod p2Comparable = ComparablePeriod.parse("P1M");
    Predicate<ChronoPeriod> assertion = i -> i.get(ChronoUnit.YEARS) == 1 && i.get(ChronoUnit.MONTHS) == 0;
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(Collections.singletonList(p1Period)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(Collections.singletonList(p1Comparable)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(Arrays.asList(p1Period, p2Period)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(Arrays.asList(p1Comparable, p2Period)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(Arrays.asList(p1Period, p2Comparable)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(maxFunction.invoke(Arrays.asList(p1Comparable, p2Comparable)), ChronoPeriod.class, assertion);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Period(java.time.Period) BigDecimal(java.math.BigDecimal) Arrays(java.util.Arrays) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) Predicate(java.util.function.Predicate) ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod) Test(org.junit.Test) Collections(java.util.Collections) ChronoPeriod(java.time.chrono.ChronoPeriod) Before(org.junit.Before) Test(org.junit.Test)

Example 54 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project drools by kiegroup.

the class MinFunctionTest method invokeListOfChronoPeriods.

@Test
public void invokeListOfChronoPeriods() {
    final ChronoPeriod p1Period = Period.parse("P1Y");
    final ChronoPeriod p1Comparable = ComparablePeriod.parse("P1Y");
    final ChronoPeriod p2Period = Period.parse("P1M");
    final ChronoPeriod p2Comparable = ComparablePeriod.parse("P1M");
    Predicate<ChronoPeriod> assertion = i -> i.get(ChronoUnit.YEARS) == 0 && i.get(ChronoUnit.MONTHS) == 1;
    FunctionTestUtil.assertPredicateOnResult(minFunction.invoke(Collections.singletonList(p2Period)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(minFunction.invoke(Collections.singletonList(p2Comparable)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(minFunction.invoke(Arrays.asList(p1Period, p2Period)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(minFunction.invoke(Arrays.asList(p1Comparable, p2Period)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(minFunction.invoke(Arrays.asList(p1Period, p2Comparable)), ChronoPeriod.class, assertion);
    FunctionTestUtil.assertPredicateOnResult(minFunction.invoke(Arrays.asList(p1Comparable, p2Comparable)), ChronoPeriod.class, assertion);
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) Period(java.time.Period) BigDecimal(java.math.BigDecimal) Arrays(java.util.Arrays) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) Predicate(java.util.function.Predicate) ComparablePeriod(org.kie.dmn.feel.lang.types.impl.ComparablePeriod) Test(org.junit.Test) Collections(java.util.Collections) ChronoPeriod(java.time.chrono.ChronoPeriod) Before(org.junit.Before) Test(org.junit.Test)

Example 55 with ChronoPeriod

use of java.time.chrono.ChronoPeriod in project drools by kiegroup.

the class DMNRuntimeTest method testYearsAndMonthsDuration.

@Test
public void testYearsAndMonthsDuration() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime("yearMonthDuration.dmn", this.getClass());
    final DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_6eda1490-21ca-441e-8a26-ab3ca800e43c", "Drawing 1");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    final BuiltInType feelType = (BuiltInType) BuiltInType.determineTypeFromName("yearMonthDuration");
    final ChronoPeriod period = (ChronoPeriod) feelType.fromString("P2Y1M");
    final DMNContext context = runtime.newContext();
    context.set("iDuration", period);
    final DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    final DMNContext result = dmnResult.getContext();
    assertThat(result.get("How long"), is("Longer than a year"));
}
Also used : ChronoPeriod(java.time.chrono.ChronoPeriod) DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Aggregations

ChronoPeriod (java.time.chrono.ChronoPeriod)55 Test (org.testng.annotations.Test)26 Test (org.junit.Test)21 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)14 ChronoLocalDate (java.time.chrono.ChronoLocalDate)6 MinguoDate (java.time.chrono.MinguoDate)5 List (java.util.List)5 BigDecimal (java.math.BigDecimal)4 Period (java.time.Period)4 ChronoUnit (java.time.temporal.ChronoUnit)4 Temporal (java.time.temporal.Temporal)4 TemporalAccessor (java.time.temporal.TemporalAccessor)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Predicate (java.util.function.Predicate)4 Before (org.junit.Before)4 ComparablePeriod (org.kie.dmn.feel.lang.types.impl.ComparablePeriod)4 Duration (java.time.Duration)3 ThaiBuddhistDate (java.time.chrono.ThaiBuddhistDate)3 TemporalUnit (java.time.temporal.TemporalUnit)3