use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.
the class DMNRuntimeTest method testCompositeItemDefinition.
@Test
public void testCompositeItemDefinition() {
DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0008-LX-arithmetic.dmn", this.getClass());
DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0008-LX-arithmetic");
assertThat(dmnModel, notNullValue());
DMNContext context = DMNFactory.newContext();
Map loan = new HashMap();
loan.put("principal", 600000);
loan.put("rate", 0.0375);
loan.put("termMonths", 360);
context.set("loan", loan);
DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
DMNContext result = dmnResult.getContext();
assertThat(result.get("payment"), is(new BigDecimal("2778.693549432766768088520383236299")));
}
use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.
the class DMNRuntimeTest method testDateAllowedValues.
@Test
public void testDateAllowedValues() {
DMNRuntime runtime = DMNRuntimeUtil.createRuntime("date_allowed_values.dmn", this.getClass());
DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_fbf002a3-615b-4f02-98e4-c28d4676225a", "Error with constraints verification");
assertThat(dmnModel, notNullValue());
assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
DMNContext ctx = runtime.newContext();
Object duration = BuiltInType.DURATION.fromString("P20Y");
ctx.set("yearsMonth", duration);
Object dateTime = BuiltInType.DATE_TIME.fromString("2017-05-16T17:58:00.000");
ctx.set("dateTime", dateTime);
DMNResult dmnResult = runtime.evaluateAll(dmnModel, ctx);
DMNContext result = dmnResult.getContext();
assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
assertThat((Map<String, Object>) result.get("Decision Logic 1"), hasEntry("years and months", duration));
assertThat((Map<String, Object>) result.get("Decision Logic 1"), hasEntry("Date Time", dateTime));
}
use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.
the class DMNRuntimeTest method testVerifyExtendedKieFEELFunction_today.
@Test
public void testVerifyExtendedKieFEELFunction_today() {
// DROOLS-2322
DMNRuntime runtime = DMNRuntimeUtil.createRuntime("just_today.dmn", this.getClass());
DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_56fd6445-ff6a-4c28-8206-71fce7f80436", "just today");
assertThat(dmnModel, notNullValue());
assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
DMNContext emptyContext = DMNFactory.newContext();
DMNResult dmnResult = runtime.evaluateAll(dmnModel, emptyContext);
assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
assertThat(dmnResult.getDecisionResultByName("a decision just today").getResult(), notNullValue());
}
use of org.kie.dmn.api.core.DMNContext 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.api.core.DMNContext 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