Search in sources :

Example 6 with InvalidParametersEvent

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

the class ModeFunction method invoke.

public FEELFnResult<Object> 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"));
    }
    Map<?, Long> collect = list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
    long maxFreq = collect.values().stream().mapToLong(Long::longValue).max().orElse(-1);
    List<?> mostFrequents = collect.entrySet().stream().filter(kv -> kv.getValue() == maxFreq).map(Map.Entry::getKey).collect(Collectors.toList());
    if (mostFrequents.size() == 1) {
        return FEELFnResult.ofResult(mostFrequents.get(0));
    } else {
        return FEELFnResult.ofResult(mostFrequents.stream().sorted().collect(Collectors.toList()));
    }
}
Also used : InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) Map(java.util.Map)

Example 7 with InvalidParametersEvent

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

the class RemoveAllFunction method invoke.

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

Example 8 with InvalidParametersEvent

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

the class SignavioRemoveFunction method invoke.

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

Example 9 with InvalidParametersEvent

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

the class TextFunction method invoke.

public FEELFnResult<String> invoke(@ParameterName("num") BigDecimal num, @ParameterName("format_text") String format_text) {
    if (num == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "num", "cannot be null"));
    }
    if (format_text == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "format_text", "cannot be null"));
    }
    DecimalFormat df = null;
    try {
        df = new DecimalFormat(format_text);
    } catch (IllegalArgumentException e) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "format_text", "illegal specific format: " + format_text + " because: " + e.getMessage()));
    }
    String result = df.format(num);
    return FEELFnResult.ofResult(result);
}
Also used : DecimalFormat(java.text.DecimalFormat) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent)

Example 10 with InvalidParametersEvent

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

the class YearDiffFunction 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));
    }
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) DateTimeException(java.time.DateTimeException) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent)

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