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);
}
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);
}
Aggregations