use of org.kie.dmn.feel.lang.impl.EvaluationContextImpl in project drools by kiegroup.
the class DMNFEELHelper method valueMatchesInUnaryTests.
public static boolean valueMatchesInUnaryTests(List<UnaryTest> unaryTests, Object value, DMNContext dmnContext) {
FEELEventListenersManager manager = new FEELEventListenersManager();
FEELEventsListenerImpl listener = new FEELEventsListenerImpl();
manager.addListener(listener);
EvaluationContextImpl ctx = new EvaluationContextImpl(manager);
try {
ctx.enterFrame();
if (dmnContext != null) {
// need to set the values for in context variables...
for (Map.Entry<String, Object> entry : dmnContext.getAll().entrySet()) {
ctx.setValue(entry.getKey(), entry.getValue());
}
}
for (UnaryTest t : unaryTests) {
try {
Boolean applyT = t.apply(ctx, value);
// the unary test above can actually return null, so we have to handle it here
if (applyT == null) {
return false;
} else if (applyT) {
return true;
}
} catch (Throwable e) {
StringBuilder message = new StringBuilder();
for (FEELEvent feelEvent : listener.getFeelEvents()) {
message.append(feelEvent.getMessage());
message.append("\n");
}
throw new RuntimeException(message.toString(), e);
}
}
} finally {
ctx.exitFrame();
}
return false;
}
use of org.kie.dmn.feel.lang.impl.EvaluationContextImpl 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.feel.lang.impl.EvaluationContextImpl 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);
}
}
use of org.kie.dmn.feel.lang.impl.EvaluationContextImpl in project drools by kiegroup.
the class DMNCompilerTest method testItemDefAllowedValuesString.
@Test
public void testItemDefAllowedValuesString() {
DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0003-input-data-string-allowed-values.dmn", this.getClass());
DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/kie-dmn", "0003-input-data-string-allowed-values");
assertThat(dmnModel, notNullValue());
ItemDefNode itemDef = dmnModel.getItemDefinitionByName("tEmploymentStatus");
assertThat(itemDef.getName(), is("tEmploymentStatus"));
assertThat(itemDef.getId(), is(nullValue()));
DMNType type = itemDef.getType();
assertThat(type, is(notNullValue()));
assertThat(type.getName(), is("tEmploymentStatus"));
assertThat(type.getId(), is(nullValue()));
assertThat(type, is(instanceOf(SimpleTypeImpl.class)));
SimpleTypeImpl feelType = (SimpleTypeImpl) type;
EvaluationContext ctx = new EvaluationContextImpl(null);
assertThat(feelType.getFeelType(), is(BuiltInType.STRING));
assertThat(feelType.getAllowedValuesFEEL().size(), is(4));
assertThat(feelType.getAllowedValuesFEEL().get(0).apply(ctx, "UNEMPLOYED"), is(true));
assertThat(feelType.getAllowedValuesFEEL().get(1).apply(ctx, "EMPLOYED"), is(true));
assertThat(feelType.getAllowedValuesFEEL().get(2).apply(ctx, "SELF-EMPLOYED"), is(true));
assertThat(feelType.getAllowedValuesFEEL().get(3).apply(ctx, "STUDENT"), is(true));
}
Aggregations