use of org.kie.dmn.core.impl.DMNResultImpl in project drools by kiegroup.
the class DMNListEvaluator method evaluate.
@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
DMNResultImpl result = (DMNResultImpl) dmnr;
List<Object> results = new ArrayList<>();
DMNContext previousContext = result.getContext();
DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
result.setContext(dmnContext);
try {
int index = 0;
for (DMNExpressionEvaluator ee : elements) {
try {
EvaluatorResult er = ee.evaluate(eventManager, result);
if (er.getResultType() == ResultType.SUCCESS) {
results.add(er.getResult());
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.ERR_EVAL_LIST_ELEMENT_ON_POSITION_ON_LIST, index + 1, name);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
} catch (Exception e) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, e, null, Msg.ERR_EVAL_LIST_ELEMENT_ON_POSITION_ON_LIST, index + 1, name);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
} finally {
index++;
}
}
} finally {
result.setContext(previousContext);
}
return new EvaluatorResultImpl(results, ResultType.SUCCESS);
}
use of org.kie.dmn.core.impl.DMNResultImpl in project drools by kiegroup.
the class DMNRelationEvaluator method evaluate.
@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
DMNResultImpl result = (DMNResultImpl) dmnr;
List<Map<String, Object>> results = new ArrayList<>();
DMNContext previousContext = result.getContext();
DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
result.setContext(dmnContext);
try {
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
List<DMNExpressionEvaluator> row = rows.get(rowIndex);
Map<String, Object> element = new HashMap<>();
for (int i = 0; i < columns.size(); i++) {
try {
EvaluatorResult er = row.get(i).evaluate(eventManager, result);
if (er.getResultType() == ResultType.SUCCESS) {
element.put(columns.get(i), er.getResult());
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.ERR_EVAL_ROW_ELEMENT_ON_POSITION_ON_ROW_OF_RELATION, i + 1, rowIndex + 1, name);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
} catch (Exception e) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, e, null, Msg.ERR_EVAL_ROW_ELEMENT_ON_POSITION_ON_ROW_OF_RELATION, i + 1, rowIndex + 1, name);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
}
results.add(element);
}
} finally {
result.setContext(previousContext);
}
return new EvaluatorResultImpl(results, ResultType.SUCCESS);
}
use of org.kie.dmn.core.impl.DMNResultImpl in project drools by kiegroup.
the class DMNContextEvaluator method evaluate.
@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
DMNResultImpl result = (DMNResultImpl) dmnr;
// when this evaluator is executed, it should either return a Map of key/value pairs
// where keys are the name of the entries and values are the result of the evaluations
// OR if a default result is implemented, it should return the result instead
Map<String, Object> results = new HashMap<>();
DMNContext previousContext = result.getContext();
DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
result.setContext(dmnContext);
try {
for (ContextEntryDef ed : entries) {
try {
EvaluatorResult er = ed.getEvaluator().evaluate(eventManager, result);
if (er.getResultType() == ResultType.SUCCESS) {
Object value = er.getResult();
if (!ed.getType().isCollection() && value instanceof Collection && ((Collection) value).size() == 1) {
// spec defines that "a=[a]", i.e., singleton collections should be treated as the single element
// and vice-versa
value = ((Collection) value).toArray()[0];
}
if (((DMNRuntimeImpl) eventManager.getRuntime()).performRuntimeTypeCheck(result.getModel())) {
if (!(ed.getContextEntry().getExpression() instanceof FunctionDefinition)) {
// checking directly the result type...
if (ed.getType() != null && !ed.getType().isAssignableValue(value)) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERROR_EVAL_NODE_RESULT_WRONG_TYPE, ed.getName(), ed.getType(), value);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
} else {
// TODO ...will need calculation/inference of function return type.
}
}
results.put(ed.getName(), value);
dmnContext.set(ed.getName(), value);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, contextDef, result, null, null, Msg.ERR_EVAL_CTX_ENTRY_ON_CTX, ed.getName(), name);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
} catch (Exception e) {
logger.error("Error invoking expression for node '" + name + "'.", e);
return new EvaluatorResultImpl(results, ResultType.FAILURE);
}
}
} finally {
result.setContext(previousContext);
}
if (results.containsKey(RESULT_ENTRY)) {
return new EvaluatorResultImpl(results.get(RESULT_ENTRY), ResultType.SUCCESS);
} else {
return new EvaluatorResultImpl(results, ResultType.SUCCESS);
}
}
use of org.kie.dmn.core.impl.DMNResultImpl in project drools by kiegroup.
the class DMNDTExpressionEvaluator method evaluate.
@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager dmrem, DMNResult dmnr) {
final List<FEELEvent> events = new ArrayList<>();
DMNResultImpl result = (DMNResultImpl) dmnr;
EventResults r = null;
try {
DMNRuntimeEventManagerUtils.fireBeforeEvaluateDecisionTable(dmrem, node.getName(), dt.getName(), result);
List<String> paramNames = dt.getParameterNames().get(0);
Object[] params = new Object[paramNames.size()];
EvaluationContextImpl ctx = feel.newEvaluationContext(Arrays.asList(events::add), Collections.emptyMap());
ctx.setPerformRuntimeTypeCheck(((DMNRuntimeImpl) dmrem.getRuntime()).performRuntimeTypeCheck(result.getModel()));
ctx.enterFrame();
// need to set the values for in context variables...
for (Map.Entry<String, Object> entry : result.getContext().getAll().entrySet()) {
ctx.setValue(entry.getKey(), entry.getValue());
}
for (int i = 0; i < params.length; i++) {
EvaluationContextImpl evalCtx = feel.newEvaluationContext(Arrays.asList(events::add), Collections.emptyMap());
evalCtx.setValues(result.getContext().getAll());
params[i] = feel.evaluate(paramNames.get(i), evalCtx);
ctx.setValue(paramNames.get(i), params[i]);
}
Object dtr = dt.invoke(ctx, params).cata(e -> {
events.add(e);
return null;
}, Function.identity());
// since ctx is a local variable that will be discarded, no need for a try/finally,
// but still wanted to match the enter/exit frame for future maintainability purposes
ctx.exitFrame();
r = processEvents(events, dmrem, result);
return new EvaluatorResultImpl(dtr, r.hasErrors ? ResultType.FAILURE : ResultType.SUCCESS);
} finally {
DMNRuntimeEventManagerUtils.fireAfterEvaluateDecisionTable(dmrem, node.getName(), dt.getName(), result, (r != null ? r.matchedRules : null), (r != null ? r.fired : null));
}
}
use of org.kie.dmn.core.impl.DMNResultImpl in project drools by kiegroup.
the class DMNFunctionDefinitionEvaluator method evaluate.
@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
DMNResultImpl result = (DMNResultImpl) dmnr;
// when this evaluator is executed, it should return a "FEEL function" to register in the context
DMNFunction function = new DMNFunction(name, parameters, functionDefinition, evaluator, eventManager, result);
return new EvaluatorResultImpl(function, ResultType.SUCCESS);
}
Aggregations