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);
}
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);
}
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);
}
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);
}
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"));
}
Aggregations