Search in sources :

Example 16 with Range

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

the class StartsFunctionTest method invokeParamIsNull.

@Test
public void invokeParamIsNull() {
    FunctionTestUtil.assertResultError(startsFunction.invoke((Comparable) null, (Range) new RangeImpl()), InvalidParametersEvent.class);
    FunctionTestUtil.assertResultError(startsFunction.invoke((Comparable) "a", (Range) null), InvalidParametersEvent.class);
}
Also used : Range(org.kie.dmn.feel.runtime.Range) RangeImpl(org.kie.dmn.feel.runtime.impl.RangeImpl) Test(org.junit.Test)

Example 17 with Range

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

the class IncludesFunctionTest method invokeParamIsNull.

@Test
public void invokeParamIsNull() {
    FunctionTestUtil.assertResultError(includesFunction.invoke((Range) null, (Comparable) "b"), InvalidParametersEvent.class);
    FunctionTestUtil.assertResultError(includesFunction.invoke((Range) new RangeImpl(), (Comparable) null), InvalidParametersEvent.class);
}
Also used : Range(org.kie.dmn.feel.runtime.Range) RangeImpl(org.kie.dmn.feel.runtime.impl.RangeImpl) Test(org.junit.Test)

Example 18 with Range

use of org.kie.dmn.feel.runtime.Range 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 19 with Range

use of org.kie.dmn.feel.runtime.Range 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)

Example 20 with Range

use of org.kie.dmn.feel.runtime.Range 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)) {
        if (inObj == null) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.IN_RESULT_NULL, name);
            return new EvaluatorResultImpl(null, ResultType.FAILURE);
        }
        // 10.3.2.9.4 Type conversions "to singleton list"
        inObj = Collections.singletonList(inObj);
    }
    DMNContext previousContext = result.getContext();
    DMNContext dmnContext = previousContext.clone();
    List<Object> returnList = new ArrayList<>();
    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);
    }
    if (type instanceof 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);
    }
    if (type instanceof 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);
    }
    if (type instanceof 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) ArrayList(java.util.ArrayList) For(org.kie.dmn.model.api.For) Range(org.kie.dmn.feel.runtime.Range) IterableRange(org.kie.dmn.core.util.IterableRange) Some(org.kie.dmn.model.api.Some) Every(org.kie.dmn.model.api.Every) IterableRange(org.kie.dmn.core.util.IterableRange)

Aggregations

Range (org.kie.dmn.feel.runtime.Range)23 RangeImpl (org.kie.dmn.feel.runtime.impl.RangeImpl)14 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Map (java.util.Map)6 DMNContext (org.kie.dmn.api.core.DMNContext)4 EvaluatorResult (org.kie.dmn.core.api.EvaluatorResult)4 DMNResultImpl (org.kie.dmn.core.impl.DMNResultImpl)4 ChronoPeriod (java.time.chrono.ChronoPeriod)3 TemporalAccessor (java.time.temporal.TemporalAccessor)3 Duration (java.time.Duration)2 ZoneId (java.time.ZoneId)2 LinkedList (java.util.LinkedList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 EvaluatorResultImpl (org.kie.dmn.core.ast.EvaluatorResultImpl)2 IterableRange (org.kie.dmn.core.util.IterableRange)2 FEELFunction (org.kie.dmn.feel.runtime.FEELFunction)2 UnaryTest (org.kie.dmn.feel.runtime.UnaryTest)2 IterableRange (org.kie.dmn.trisotech.core.util.IterableRange)2