Search in sources :

Example 16 with InvalidParametersEvent

use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.

the class AppendFunction method invoke.

public FEELFnResult<List<Object>> invoke(@ParameterName("list") Object appendTo, @ParameterName("item") Object[] items) {
    if (appendTo == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null"));
    }
    if (items == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "item", "cannot be null"));
    }
    // spec requires us to return a new list
    final List<Object> result = new ArrayList<>();
    if (appendTo instanceof Collection) {
        result.addAll((Collection) appendTo);
    } else {
        result.add(appendTo);
    }
    result.addAll(Arrays.asList(items));
    return FEELFnResult.ofResult(result);
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent)

Example 17 with InvalidParametersEvent

use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.

the class MatchesFunction method invoke.

public FEELFnResult<Boolean> invoke(@ParameterName("input") String input, @ParameterName("pattern") String pattern, @ParameterName("flags") String flags) {
    if (input == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "input", "cannot be null"));
    }
    if (pattern == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "pattern", "cannot be null"));
    }
    try {
        int f = processFlags(flags);
        Pattern p = Pattern.compile(pattern, f);
        Matcher m = p.matcher(input);
        return FEELFnResult.ofResult(m.find());
    } catch (PatternSyntaxException e) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "pattern", "is invalid and can not be compiled", e));
    } catch (IllegalArgumentException t) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "flags", "contains unknown flags", t));
    } catch (Throwable t) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "pattern", "is invalid and can not be compiled", t));
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 18 with InvalidParametersEvent

use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.

the class MeanFunction method invoke.

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);
}
Also used : BigDecimal(java.math.BigDecimal) Arrays(java.util.Arrays) List(java.util.List) Severity(org.kie.dmn.api.feel.runtime.events.FEELEvent.Severity) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) MathContext(java.math.MathContext) Function(java.util.function.Function) EvalHelper(org.kie.dmn.feel.util.EvalHelper) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) BigDecimal(java.math.BigDecimal)

Example 19 with InvalidParametersEvent

use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.

the class ReverseFunction method invoke.

public FEELFnResult<List<Object>> invoke(@ParameterName("list") List list) {
    if (list == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null"));
    }
    // spec requires us to return a new list
    final List<Object> result = new ArrayList<>(list);
    Collections.reverse(result);
    return FEELFnResult.ofResult(result);
}
Also used : ArrayList(java.util.ArrayList) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent)

Example 20 with InvalidParametersEvent

use of org.kie.dmn.feel.runtime.events.InvalidParametersEvent in project drools by kiegroup.

the class YearsAndMonthsFunction method invoke.

public FEELFnResult<TemporalAmount> invoke(@ParameterName("from") Temporal from, @ParameterName("to") Temporal to) {
    if (from == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "cannot be null"));
    }
    if (to == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "to", "cannot be null"));
    }
    final LocalDate fromDate = getLocalDateFromTemporal(from);
    if (fromDate == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "from", "is of type not suitable for years and months function"));
    }
    final LocalDate toDate = getLocalDateFromTemporal(to);
    if (toDate == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "to", "is of type not suitable for years and months function"));
    }
    return FEELFnResult.ofResult(Period.between(fromDate, toDate).withDays(0));
}
Also used : InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) LocalDate(java.time.LocalDate)

Aggregations

InvalidParametersEvent (org.kie.dmn.feel.runtime.events.InvalidParametersEvent)27 DateTimeException (java.time.DateTimeException)7 TemporalAccessor (java.time.temporal.TemporalAccessor)6 ArrayList (java.util.ArrayList)6 BigDecimal (java.math.BigDecimal)4 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)3 List (java.util.List)2 Map (java.util.Map)2 Pattern (java.util.regex.Pattern)2 PatternSyntaxException (java.util.regex.PatternSyntaxException)2 FEELEventBase (org.kie.dmn.feel.runtime.events.FEELEventBase)2 MathContext (java.math.MathContext)1 DecimalFormat (java.text.DecimalFormat)1 LocalDate (java.time.LocalDate)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Function (java.util.function.Function)1 Matcher (java.util.regex.Matcher)1 DMNContext (org.kie.dmn.api.core.DMNContext)1