Search in sources :

Example 1 with IterableRange

use of org.kie.dmn.trisotech.core.util.IterableRange in project drools by kiegroup.

the class DMNFilterEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    DMNResultImpl result = (DMNResultImpl) dmnr;
    if (inEvaluator == null || filterEvaluator == null) {
        return new EvaluatorResultImpl(null, ResultType.FAILURE);
    }
    EvaluatorResult inResult = inEvaluator.evaluate(eventManager, result);
    if (inResult.getResultType() != ResultType.SUCCESS) {
        return inResult;
    }
    Object inObj = inResult.getResult();
    if (inObj instanceof Range) {
        inObj = new IterableRange((Range) inObj);
    } else if (!(inObj instanceof Iterable)) {
        // Can't iterate on null
        if (inObj == null) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.IN_RESULT_NULL, name);
            return new EvaluatorResultImpl(null, ResultType.FAILURE);
        }
        // Coercion to list if a single result is returned
        inObj = Collections.singletonList(inObj);
    }
    DMNContext previousContext = result.getContext();
    DMNContext dmnContext = previousContext.clone();
    LinkedList<Object> returnList = new LinkedList<>();
    try {
        result.setContext(dmnContext);
        boolean first = true;
        for (Object item : (Iterable) inObj) {
            dmnContext.set("item", item);
            if (item instanceof Map) {
                Map<String, Object> complexItem = (Map<String, Object>) item;
                complexItem.forEach((k, v) -> dmnContext.set(k, v));
            }
            EvaluatorResult evaluate = filterEvaluator.evaluate(eventManager, dmnr);
            Object evalReturn = evaluate.getResult();
            // If the evaluation is a boolean result, we add the item based on a return of true
            if (evalReturn instanceof Boolean && ((Boolean) evalReturn).booleanValue() == true) {
                returnList.add(item);
            }
            // If on the first evaluation, a number is returned, we are using an index instead of a boolean filter
            if (first && evalReturn instanceof Number) {
                List list = inObj instanceof List ? (List) inObj : Arrays.asList(inObj);
                int i = ((Number) evalReturn).intValue();
                if (i > 0 && i <= list.size()) {
                    return new EvaluatorResultImpl(list.get(i - 1), ResultType.SUCCESS);
                } else if (i < 0 && Math.abs(i) <= list.size()) {
                    return new EvaluatorResultImpl(list.get(list.size() + i), ResultType.SUCCESS);
                } else {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.INDEX_OUT_OF_BOUND, list.size(), i);
                    return new EvaluatorResultImpl(null, ResultType.FAILURE);
                }
            }
            first = false;
        }
    } finally {
        result.setContext(previousContext);
    }
    return new EvaluatorResultImpl(returnList, ResultType.SUCCESS);
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNContext(org.kie.dmn.api.core.DMNContext) IterableRange(org.kie.dmn.trisotech.core.util.IterableRange) Range(org.kie.dmn.feel.runtime.Range) LinkedList(java.util.LinkedList) EvaluatorResultImpl(org.kie.dmn.core.ast.EvaluatorResultImpl) List(java.util.List) LinkedList(java.util.LinkedList) IterableRange(org.kie.dmn.trisotech.core.util.IterableRange) Map(java.util.Map)

Example 2 with IterableRange

use of org.kie.dmn.trisotech.core.util.IterableRange in project drools by kiegroup.

the class DMNIteratorEvaluator method evaluate.

@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
    DMNResultImpl result = (DMNResultImpl) dmnr;
    EvaluatorResult inResult = inEvaluator.evaluate(eventManager, result);
    if (inResult == null || inResult.getResultType() != ResultType.SUCCESS) {
        return inResult;
    }
    Object inObj = inResult.getResult();
    if (inObj instanceof Range) {
        inObj = new IterableRange((Range) inObj);
    } else if (!(inObj instanceof Iterable)) {
        // Can't iterate on null
        if (inObj == null) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.IN_RESULT_NULL, name);
            return new EvaluatorResultImpl(null, ResultType.FAILURE);
        }
        // Coercion to list if a single result is returned
        inObj = Collections.singletonList(inObj);
    }
    DMNContext previousContext = result.getContext();
    DMNContext dmnContext = previousContext.clone();
    LinkedList<Object> returnList = new LinkedList<>();
    try {
        result.setContext(dmnContext);
        dmnContext.set("partial", returnList);
        for (Object item : (Iterable) inObj) {
            dmnContext.set(variable, item);
            EvaluatorResult evaluate = returnEvaluator.evaluate(eventManager, dmnr);
            returnList.add(evaluate.getResult());
        }
    } finally {
        result.setContext(previousContext);
    }
    switch(type) {
        case EVERY:
            for (Object satisfies : returnList) {
                if (!(satisfies instanceof Boolean) || ((Boolean) satisfies).booleanValue() == false) {
                    return new EvaluatorResultImpl(Boolean.FALSE, ResultType.SUCCESS);
                }
            }
            return new EvaluatorResultImpl(Boolean.TRUE, ResultType.SUCCESS);
        case SOME:
            for (Object satisfies : returnList) {
                if (satisfies instanceof Boolean && ((Boolean) satisfies).booleanValue() == true) {
                    return new EvaluatorResultImpl(Boolean.TRUE, ResultType.SUCCESS);
                }
            }
            return new EvaluatorResultImpl(Boolean.FALSE, ResultType.SUCCESS);
        case FOR:
            return new EvaluatorResultImpl(returnList, ResultType.SUCCESS);
    }
    return new EvaluatorResultImpl(null, ResultType.FAILURE);
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNContext(org.kie.dmn.api.core.DMNContext) EvaluatorResultImpl(org.kie.dmn.core.ast.EvaluatorResultImpl) IterableRange(org.kie.dmn.trisotech.core.util.IterableRange) Range(org.kie.dmn.feel.runtime.Range) IterableRange(org.kie.dmn.trisotech.core.util.IterableRange) LinkedList(java.util.LinkedList)

Aggregations

LinkedList (java.util.LinkedList)2 DMNContext (org.kie.dmn.api.core.DMNContext)2 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)2 EvaluatorResultImpl (org.kie.dmn.core.ast.EvaluatorResultImpl)2 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)2 Range (org.kie.dmn.feel.runtime.Range)2 IterableRange (org.kie.dmn.trisotech.core.util.IterableRange)2 List (java.util.List)1 Map (java.util.Map)1