Search in sources :

Example 1 with ParameterName

use of org.kie.dmn.feel.runtime.functions.ParameterName in project drools by kiegroup.

the class ContextPutFunction method invoke.

public FEELFnResult<Map<String, Object>> invoke(@ParameterName("context") Object context, @ParameterName("keys") List keys, @ParameterName("value") Object value) {
    if (context == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "context", "cannot be null"));
    }
    if (keys == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "keys", "cannot be null"));
    } else if (keys.isEmpty()) {
        return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "keys", "cannot be empty"));
    }
    Object head = keys.get(0);
    if (!(head instanceof String)) {
        return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "keys", "an element is not a key: " + head));
    }
    final String key0 = (String) head;
    if (keys.size() == 1) {
        return invoke(context, key0, value);
    }
    final List keysTail = keys.subList(1, keys.size());
    final FEELFnResult<Map<String, Object>> result = toMap(context).flatMap(r -> invoke(r.get(key0), keysTail, value).map(rv -> put(r, key0, rv)));
    return result;
}
Also used : List(java.util.List) FEELFnResult(org.kie.dmn.feel.runtime.functions.FEELFnResult) ParameterName(org.kie.dmn.feel.runtime.functions.ParameterName) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) Map(java.util.Map) BuiltInType(org.kie.dmn.feel.lang.types.BuiltInType) Entry(java.util.Map.Entry) ImmutableFPAWrappingPOJO(org.kie.dmn.feel.lang.types.impl.ImmutableFPAWrappingPOJO) HashMap(java.util.HashMap) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) List(java.util.List) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 FEELEvent (org.kie.dmn.api.feel.runtime.events.FEELEvent)1 BuiltInType (org.kie.dmn.feel.lang.types.BuiltInType)1 ImmutableFPAWrappingPOJO (org.kie.dmn.feel.lang.types.impl.ImmutableFPAWrappingPOJO)1 InvalidParametersEvent (org.kie.dmn.feel.runtime.events.InvalidParametersEvent)1 BaseFEELFunction (org.kie.dmn.feel.runtime.functions.BaseFEELFunction)1 FEELFnResult (org.kie.dmn.feel.runtime.functions.FEELFnResult)1 ParameterName (org.kie.dmn.feel.runtime.functions.ParameterName)1