Search in sources :

Example 11 with InvalidParametersEvent

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

the class ZipFunction method invoke.

public FEELFnResult<List> invoke(@ParameterName("attributes") List<?> attributes, @ParameterName("values") Object[] values) {
    if (attributes.isEmpty()) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "attributes", "attributes cannot be empty"));
    } else if (!(attributes.get(0) instanceof String)) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "attributes", "attributes must be a list of string"));
    }
    if (values.length != attributes.size()) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "values must be a list of the same size as of attributes"));
    }
    // spec requires us to return a new list
    final List<Map<Object, Object>> result = new ArrayList<>();
    for (int aIdx = 0; aIdx < values.length; aIdx++) {
        if (!(values[aIdx] instanceof List)) {
            return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "each value must be a list"));
        }
        List<?> value = (List<?>) values[aIdx];
        if (result.isEmpty()) {
            // first time init list
            value.forEach(x -> result.add(new HashMap<>()));
        } else {
            if (value.size() != result.size()) {
                return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "values", "each value must be consistent in size"));
            }
        }
        Object attribute = attributes.get(aIdx);
        for (int vIdx = 0; vIdx < value.size(); vIdx++) {
            result.get(vIdx).put(attribute, value.get(vIdx));
        }
    }
    return FEELFnResult.ofResult(result);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with InvalidParametersEvent

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

the class SecondsDiffFunction 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)

Example 13 with InvalidParametersEvent

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

the class TextOccurrencesFunction method invoke.

public FEELFnResult<BigDecimal> invoke(@ParameterName("find_text") String find_text, @ParameterName("within_text") String within_text) {
    if (within_text == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "within_text", "cannot be null"));
    }
    int idx = 0;
    int occurences = 0;
    while (idx != -1) {
        idx = within_text.indexOf(find_text, idx);
        if (idx != -1) {
            occurences++;
            idx = idx + find_text.length();
        }
    }
    return FEELFnResult.ofResult(EvalHelper.getBigDecimalOrNull(occurences));
}
Also used : InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent)

Example 14 with InvalidParametersEvent

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

the class AllFunction method invoke.

public FEELFnResult<Boolean> invoke(@ParameterName("list") List list) {
    if (list == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null"));
    }
    boolean result = true;
    boolean containsNull = false;
    // Spec. definition: return false if any item is false, else true if all items are true, else null
    for (final Object element : list) {
        if (element != null && !(element instanceof Boolean)) {
            return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "an element in the list is not a Boolean"));
        } else {
            if (element != null) {
                result &= (Boolean) element;
            } else if (!containsNull) {
                containsNull = true;
            }
        }
    }
    if (containsNull && result) {
        return FEELFnResult.ofResult(null);
    } else {
        return FEELFnResult.ofResult(result);
    }
}
Also used : InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent)

Example 15 with InvalidParametersEvent

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

the class AnyFunction method invoke.

public FEELFnResult<Boolean> invoke(@ParameterName("list") List list) {
    if (list == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "list", "cannot be null"));
    }
    boolean result = false;
    boolean containsNull = false;
    // Spec. definition: return true if any item is true, else false if all items are false, else null
    for (final Object element : list) {
        if (element != null && !(element instanceof Boolean)) {
            return FEELFnResult.ofError(new InvalidParametersEvent(Severity.ERROR, "an element in the list is not a Boolean"));
        } else {
            if (element != null) {
                result |= (Boolean) element;
            } else if (!containsNull) {
                containsNull = true;
            }
        }
    }
    if (containsNull && !result) {
        return FEELFnResult.ofResult(null);
    } else {
        return FEELFnResult.ofResult(result);
    }
}
Also used : 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