use of org.kie.dmn.core.api.EvaluatorResult 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.api.EvaluatorResult 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.api.EvaluatorResult in project drools by kiegroup.
the class DMNRuntimeImpl method evaluateBKM.
private void evaluateBKM(DMNContext context, DMNResultImpl result, BusinessKnowledgeModelNode b, boolean typeCheck) {
BusinessKnowledgeModelNodeImpl bkm = (BusinessKnowledgeModelNodeImpl) b;
if (isNodeValueDefined(result, bkm)) {
// TODO: do we need to check if the defined variable is a function as it should?
return;
}
// TODO: do we need to check/resolve dependencies?
if (bkm.getEvaluator() == null) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, bkm.getSource(), result, null, null, Msg.MISSING_EXPRESSION_FOR_BKM, getIdentifier(bkm));
return;
}
try {
DMNRuntimeEventManagerUtils.fireBeforeEvaluateBKM(eventManager, bkm, result);
for (DMNNode dep : bkm.getDependencies().values()) {
if (typeCheck && !checkDependencyValueIsValid(dep, result)) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, null, null, Msg.ERROR_EVAL_NODE_DEP_WRONG_TYPE, getIdentifier(bkm), getIdentifier(dep), MsgUtil.clipString(result.getContext().get(dep.getName()).toString(), 50), ((DMNBaseNode) dep).getType());
return;
}
if (!isNodeValueDefined(result, dep)) {
if (dep instanceof BusinessKnowledgeModelNode) {
evaluateBKM(context, result, (BusinessKnowledgeModelNode) dep, typeCheck);
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, null, null, Msg.REQ_DEP_NOT_FOUND_FOR_NODE, getIdentifier(dep), getIdentifier(bkm));
return;
}
}
}
EvaluatorResult er = bkm.getEvaluator().evaluate(this, result);
if (er.getResultType() == EvaluatorResult.ResultType.SUCCESS) {
FEELFunction resultFn = (FEELFunction) er.getResult();
if (bkm.getModelNamespace().equals(result.getModel().getNamespace())) {
// TODO check of the return type will need calculation/inference of function return type.
result.getContext().set(bkm.getBusinessKnowledModel().getVariable().getName(), resultFn);
} else {
DMNModelImpl model = (DMNModelImpl) result.getModel();
Optional<String> importAlias = model.getImportAliasFor(bkm.getModelNamespace(), bkm.getModelName());
Map<String, Object> aliasContext = (Map) result.getContext().getAll().computeIfAbsent(importAlias.get(), x -> new LinkedHashMap<>());
aliasContext.put(bkm.getBusinessKnowledModel().getVariable().getName(), resultFn);
}
}
} catch (Throwable t) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, bkm.getSource(), result, t, null, Msg.ERROR_EVAL_BKM_NODE, getIdentifier(bkm), t.getMessage());
} finally {
DMNRuntimeEventManagerUtils.fireAfterEvaluateBKM(eventManager, bkm, result);
}
}
use of org.kie.dmn.core.api.EvaluatorResult 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.api.EvaluatorResult in project drools by kiegroup.
the class DMNInvocationEvaluator method evaluate.
@Override
public EvaluatorResult evaluate(DMNRuntimeEventManager eventManager, DMNResult dmnr) {
final List<FEELEvent> events = new ArrayList<>();
DMNResultImpl result = (DMNResultImpl) dmnr;
DMNContext previousContext = result.getContext();
DMNContextImpl dmnContext = (DMNContextImpl) previousContext.clone();
result.setContext(dmnContext);
Object invocationResult = null;
try {
FEELFunction function = this.functionLocator.apply(previousContext, functionName);
if (function == null) {
// check if it is a configured/built-in function
Object r = null;
if (feel != null) {
r = ((FEELImpl) feel).newEvaluationContext(Collections.emptyList(), Collections.emptyMap()).getValue(functionName);
} else {
r = RootExecutionFrame.INSTANCE.getValue(functionName);
}
if (r != null && r instanceof FEELFunction) {
function = (FEELFunction) r;
}
}
if (function == null) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.FUNCTION_NOT_FOUND, functionName, nodeName);
return new EvaluatorResultImpl(null, ResultType.FAILURE);
}
Object[] namedParams = new Object[parameters.size()];
int index = 0;
for (ActualParameter param : parameters) {
try {
EvaluatorResult value = param.expression.evaluate(eventManager, result);
if (value.getResultType() == ResultType.SUCCESS) {
namedParams[index++] = new NamedParameter(param.name, value.getResult());
} else {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, null, null, Msg.ERR_EVAL_PARAM_FOR_INVOCATION_ON_NODE, param.name, functionName, nodeName);
return new EvaluatorResultImpl(null, ResultType.FAILURE);
}
} catch (Exception e) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, e, null, Msg.ERR_INVOKING_PARAM_EXPR_FOR_PARAM_ON_NODE, param.name, nodeName);
return new EvaluatorResultImpl(null, ResultType.FAILURE);
}
}
FEELEventListenersManager listenerMgr = new FEELEventListenersManager();
listenerMgr.addListener(events::add);
EvaluationContextImpl ctx = new EvaluationContextImpl(listenerMgr, eventManager.getRuntime());
invocationResult = function.invokeReflectively(ctx, namedParams);
boolean hasErrors = hasErrors(events, eventManager, result);
return new EvaluatorResultImpl(invocationResult, hasErrors ? ResultType.FAILURE : ResultType.SUCCESS);
} catch (Throwable t) {
MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, node, result, t, null, Msg.ERR_INVOKING_FUNCTION_ON_NODE, functionName, nodeName);
return new EvaluatorResultImpl(null, ResultType.FAILURE);
} finally {
result.setContext(previousContext);
}
}
Aggregations