public static Number sum(List<Boolean> list) { return sumFunction.invoke(list).cata(e -> null, Function.identity()); }
public static Number mean(List<Number> list) { return meanFunction.invoke(list).cata(e -> null, Function.identity()); }
public static Number median(List<Number> list) { return medianFunction.invoke(list).cata(e -> null, Function.identity()); }
public static Number count(List<Number> list) { return countFunction.invoke(list).cata(e -> null, Function.identity()); }
public static Object min(List<Number> list) { return minFunction.invoke(list).cata(e -> null, Function.identity()); }
public static List mode(List<Number> list) { return modeFunction.invoke(list).cata(e -> null, Function.identity()); }
public static Number stddev(List<Number> list) { return stddevFunction.invoke(list).cata(e -> null, Function.identity()); }
public static Boolean all(List<Boolean> list) { return allFunction.invoke(list).cata(e -> Boolean.FALSE, Function.identity()); }
public static Boolean any(List<Boolean> list) { return anyFunction.invoke(list).cata(e -> Boolean.FALSE, Function.identity()); }
public static Object max(List<Number> list) { return maxFunction.invoke(list).cata(e -> null, Function.identity()); }
/** * Marshalls the give FEEL value into a String. The result is similar to * calling the string() function in a FEEL expression, with the difference * that a null value is returned as the "null" string instead of the null * value itself. * * @param value the FEEL value to be marshalled * * @return the string representation of the value */ @Override public String marshall(Object value) { if( value == null ) { return "null"; } return BuiltInFunctions.getFunction( StringFunction.class ).invoke( value ).cata( justNull(), Function.identity()); }
/** * Marshalls the given value into FEEL code that can be executed to * reconstruct the value. For instance, here are some examples of the marshalling process: * * * number 10 marshalls as: 10 * * string foo marshalls as: "foo" * * duration P1D marshalls as: duration( "P1D" ) * * context { x : 10, y : foo } marshalls as: { x : 10, y : "foo" } * * @param value the FEEL value to be marshalled * * @return a string representing the FEEL code that needs to be executed to reconstruct the value */ @Override public String marshall(Object value) { if( value == null ) { return "null"; } return KieExtendedDMNFunctions.getFunction(CodeFunction.class).invoke(value).cata(justNull(), Function.identity()); }
public static <T> void assertResultNull(final FEELFnResult<T> result) { assertResultNotError(result); Assert.assertThat(result.cata(left -> false, right -> right), Matchers.nullValue()); }
public static <T> void assertResultList(final FEELFnResult<List<T>> result, final List<Object> expectedResult) { assertResultNotError(result); final List<T> resultList = result.cata(left -> null, right -> right); Assert.assertThat(resultList, Matchers.hasSize(expectedResult.size())); if (expectedResult.isEmpty()) { Assert.assertThat(resultList, Matchers.empty()); } else { Assert.assertThat(resultList, Matchers.contains(expectedResult.toArray(new Object[]{}))); } }
public FEELFnResult<BigDecimal> invoke(@ParameterName( "list" ) List list) { if ( list == null ) { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null")); } FEELFnResult<BigDecimal> s = sum.invoke( list ); Function<FEELEvent, FEELFnResult<BigDecimal>> ifLeft = (event) -> { return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "unable to sum the elements which is required to calculate the mean")); }; Function<BigDecimal, FEELFnResult<BigDecimal>> ifRight = (sum) -> { try { return FEELFnResult.ofResult( sum.divide( BigDecimal.valueOf( list.size() ), MathContext.DECIMAL128 ) ); } catch (Exception e) { return FEELFnResult.ofError( new InvalidParametersEvent(Severity.ERROR, "unable to perform division to calculate the mean", e) ); } }; return s.cata(ifLeft, ifRight); }
public static <T> void assertResult(final FEELFnResult<T> result, final T expectedResult) { if (expectedResult == null) { assertResultNull(result); } else { assertResultNotError(result); final T resultValue = result.cata(left -> null, right -> right); Assert.assertThat(resultValue, Matchers.notNullValue()); Assert.assertThat(resultValue, Matchers.equalTo(expectedResult)); } }
public static <T> void assertResultError(final FEELFnResult<T> result, final Class expectedErrorEventClass) { Assert.assertThat(result, Matchers.notNullValue()); Assert.assertThat(result.isLeft(), Matchers.is(true)); final FEELEvent resultEvent = result.cata(left -> left, right -> null); checkErrorEvent(resultEvent, expectedErrorEventClass); }
public FEELFnResult<TemporalAccessor> invoke(@ParameterName("from") String val) { if (val == null) { return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "cannot be null")); } if (!BEGIN_YEAR.matcher(val).find()) { // please notice the regex strictly requires the beginning, so we can use find. return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "year not compliant with XML Schema Part 2 Datatypes")); } try { return FEELFnResult.ofResult(LocalDate.from(FEEL_DATE.parse(val))); } catch (DateTimeException e) { // try to parse it as a date time and extract the date component // NOTE: this is an extension to the standard return BuiltInFunctions.getFunction(DateAndTimeFunction.class).invoke(val) .cata(overrideLeft -> FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "from", "date-parsing exception", e)), this::invoke ); } }
public static void assertResultBigDecimal(final FEELFnResult<BigDecimal> result, final BigDecimal expectedResult) { assertResultNotError(result); final BigDecimal resultValue = result.cata(left -> null, right -> right); Assert.assertThat(resultValue, Matchers.notNullValue()); Assert.assertThat(resultValue, Matchers.comparesEqualTo(expectedResult)); }
@Test public void invoke() { // The current time that we need to compare will almost never be the same as another one we get for comparison purposes, // because there is some execution between them, so the comparison assertion doesn't make sense. // Note: We cannot guarantee any part of the date to be the same. E.g. in case when the test is executed // at the exact moment when the year is flipped to the next one, we cannot guarantee the year will be the same. final FEELFnResult<TemporalAccessor> nowResult = nowFunction.invoke(); Assert.assertThat(nowResult.isRight(), Matchers.is(true)); final TemporalAccessor result = nowResult.cata(left -> null, right -> right); Assert.assertThat(result, Matchers.notNullValue()); Assert.assertThat(result.getClass(), Matchers.typeCompatibleWith(ZonedDateTime.class)); }