use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.
the class TimeFunction method invoke.
public FEELFnResult<TemporalAccessor> invoke(@ParameterName("hour") Number hour, @ParameterName("minute") Number minute, @ParameterName("second") Number seconds, @ParameterName("offset") Duration offset) {
if (hour == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "hour", "cannot be null"));
}
if (minute == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "minute", "cannot be null"));
}
if (seconds == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "seconds", "cannot be null"));
}
try {
int nanosecs = 0;
if (seconds instanceof BigDecimal) {
BigDecimal secs = (BigDecimal) seconds;
nanosecs = secs.subtract(secs.setScale(0, BigDecimal.ROUND_DOWN)).multiply(NANO_MULT).intValue();
}
if (offset == null) {
return FEELFnResult.ofResult(LocalTime.of(hour.intValue(), minute.intValue(), seconds.intValue(), nanosecs));
} else {
return FEELFnResult.ofResult(OffsetTime.of(hour.intValue(), minute.intValue(), seconds.intValue(), nanosecs, ZoneOffset.ofTotalSeconds((int) offset.getSeconds())));
}
} catch (DateTimeException e) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "time-parsing exception", e));
}
}
use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.
the class DayDiffFunction method invoke.
public FEELFnResult<BigDecimal> invoke(@ParameterName("datetime1") String datetime1, @ParameterName("datetime2") String datetime2) {
if (datetime1 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime1", "cannot be null"));
}
if (datetime2 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime2", "cannot be null"));
}
try {
TemporalAccessor dt1 = BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(datetime1).cata(BuiltInType.justNull(), Function.identity());
TemporalAccessor dt2 = BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(datetime2).cata(BuiltInType.justNull(), Function.identity());
return invoke(dt1, dt2);
} catch (DateTimeException e) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime", "invalid 'date' or 'date and time' parameter", e));
}
}
use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.
the class HourDiffFunction method invoke.
public FEELFnResult<BigDecimal> invoke(@ParameterName("datetime1") String datetime1, @ParameterName("datetime2") String datetime2) {
if (datetime1 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime1", "cannot be null"));
}
if (datetime2 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime2", "cannot be null"));
}
try {
TemporalAccessor dt1 = BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(datetime1).cata(BuiltInType.justNull(), Function.identity());
TemporalAccessor dt2 = BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(datetime2).cata(BuiltInType.justNull(), Function.identity());
return invoke(dt1, dt2);
} catch (DateTimeException e) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime", "invalid 'date' or 'date and time' parameter", e));
}
}
use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.
the class MedianFunction method invoke.
public FEELFnResult<BigDecimal> invoke(@ParameterName("list") List<?> list) {
if (list == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null"));
}
if (list.isEmpty()) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be empty"));
}
List<?> sorted = list.stream().sorted().collect(Collectors.toList());
if (sorted.size() % 2 == 0) {
// even sized list
int medianPos0 = (sorted.size() / 2) - 1;
int medianPos1 = medianPos0 + 1;
if (!(sorted.get(medianPos0) instanceof BigDecimal) || !(sorted.get(medianPos1) instanceof BigDecimal)) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "does not contain number"));
}
BigDecimal median0 = (BigDecimal) sorted.get(medianPos0);
BigDecimal median1 = (BigDecimal) sorted.get(medianPos1);
BigDecimal medianAvg = median0.add(median1).divide(new BigDecimal(2, MathContext.DECIMAL128), MathContext.DECIMAL128);
return FEELFnResult.ofResult(medianAvg);
} else {
int medianPos = sorted.size() / 2;
Object median = sorted.get(medianPos);
if (!(median instanceof BigDecimal)) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "does not contain number"));
}
return FEELFnResult.ofResult((BigDecimal) median);
}
}
use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.
the class MinutesDiffFunction method invoke.
public FEELFnResult<BigDecimal> invoke(@ParameterName("datetime1") String datetime1, @ParameterName("datetime2") String datetime2) {
if (datetime1 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime1", "cannot be null"));
}
if (datetime2 == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime2", "cannot be null"));
}
try {
TemporalAccessor dt1 = BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(datetime1).cata(BuiltInType.justNull(), Function.identity());
TemporalAccessor dt2 = BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(datetime2).cata(BuiltInType.justNull(), Function.identity());
return invoke(dt1, dt2);
} catch (DateTimeException e) {
return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "datetime", "invalid 'date' or 'date and time' parameter", e));
}
}
Aggregations