Search in sources :

Example 1 with DMNContext

use of org.kie.dmn.api.core.DMNContext 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);
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) ArrayList(java.util.ArrayList) DMNContext(org.kie.dmn.api.core.DMNContext) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl)

Example 2 with DMNContext

use of org.kie.dmn.api.core.DMNContext 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);
}
Also used : DMNResultImpl(org.kie.dmn.core.impl.DMNResultImpl) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) DMNContext(org.kie.dmn.api.core.DMNContext) DMNContextImpl(org.kie.dmn.core.impl.DMNContextImpl)

Example 3 with DMNContext

use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.

the class InvokeFunction method invoke.

public FEELFnResult<Object> invoke(@ParameterName("ctx") EvaluationContext ctx, @ParameterName("namespace") String namespace, @ParameterName("model name") String modelName, @ParameterName("decision name") String decisionName, @ParameterName("parameters") Map<String, Object> parameters) {
    DMNRuntime dmnRuntime = ctx.getDMNRuntime();
    if (namespace == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "namespace", "cannot be null"));
    }
    if (modelName == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "model name", "cannot be null"));
    }
    if (decisionName == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "decision name", "cannot be null"));
    }
    if (parameters == null) {
        return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "parameters", "cannot be null"));
    }
    FEELEvent capturedException = null;
    try {
        ctx.enterFrame();
        DMNModel dmnModel = dmnRuntime.getModel(namespace, modelName);
        if (dmnModel == null) {
            return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Cannot find model '" + modelName + "' in namespace " + namespace, null));
        }
        if (dmnModel.getDecisionByName(decisionName) == null) {
            return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Cannot find decision '" + decisionName + "' in the model", null));
        }
        DMNContext dmnContext = dmnRuntime.newContext();
        dmnContext.getAll().putAll(parameters);
        DMNResult requiredDecisionResult = dmnRuntime.evaluateByName(dmnModel, dmnContext, decisionName);
        if (requiredDecisionResult.hasErrors()) {
            return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Errors occurred while invoking the external decision: " + requiredDecisionResult.getMessages(), null));
        }
        return FEELFnResult.ofResult(requiredDecisionResult.getContext().get(decisionName));
    } catch (Exception e) {
        capturedException = new FEELEventBase(FEELEvent.Severity.ERROR, "Error invoking function", new RuntimeException("Error invoking function " + getName() + ".", e));
    } finally {
        ctx.exitFrame();
    }
    return FEELFnResult.ofError(capturedException);
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) FEELEventBase(org.kie.dmn.feel.runtime.events.FEELEventBase) DMNContext(org.kie.dmn.api.core.DMNContext) InvalidParametersEvent(org.kie.dmn.feel.runtime.events.InvalidParametersEvent) FEELEvent(org.kie.dmn.api.feel.runtime.events.FEELEvent) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 4 with DMNContext

use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.

the class DMNRuntimeTest method testEx_4_3simplified.

@Test
public void testEx_4_3simplified() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("Ex_4_3simplified.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_5c5a9c72-627e-4666-ae85-31356fed3658", "Ex_4_3simplified");
    assertThat(dmnModel, notNullValue());
    assertThat(DMNRuntimeUtil.formatMessages(dmnModel.getMessages()), dmnModel.hasErrors(), is(false));
    DMNContext context = DMNFactory.newContext();
    context.set("number", 123.123456d);
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    System.out.println(dmnResult);
    assertThat(DMNRuntimeUtil.formatMessages(dmnResult.getMessages()), dmnResult.hasErrors(), is(false));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("Formatted Monthly Payment"), is("€123.12"));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 5 with DMNContext

use of org.kie.dmn.api.core.DMNContext in project drools by kiegroup.

the class DMNRuntimeTest method testInvalidFunction.

@Test
public void testInvalidFunction() {
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntimeWithAdditionalResources("InvalidFunction.dmn", this.getClass());
    final DMNModel model = runtime.getModel("http://www.trisotech.com/definitions/_84453b71-5d23-479f-9481-5196d92bacae", "0003-iteration-augmented");
    assertThat(model, notNullValue());
    final DMNContext context = DMNFactory.newContext();
    context.set("Loans", new HashMap<>());
    final DMNResult result = runtime.evaluateAll(model, context);
    final List<DMNDecisionResult> decisionResults = result.getDecisionResults();
    FEELStringMarshaller.INSTANCE.marshall(Arrays.asList(decisionResults.get(0).getResult(), decisionResults.get(1).getResult()));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Aggregations

DMNContext (org.kie.dmn.api.core.DMNContext)142 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)130 DMNModel (org.kie.dmn.api.core.DMNModel)129 Test (org.junit.Test)127 DMNResult (org.kie.dmn.api.core.DMNResult)123 HashMap (java.util.HashMap)24 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)24 BigDecimal (java.math.BigDecimal)23 ArrayList (java.util.ArrayList)20 List (java.util.List)20 Map (java.util.Map)20 KieServices (org.kie.api.KieServices)19 KieContainer (org.kie.api.runtime.KieContainer)19 DMNMessage (org.kie.dmn.api.core.DMNMessage)13 DMNRuntimeEventListener (org.kie.dmn.api.core.event.DMNRuntimeEventListener)11 CoreMatchers.is (org.hamcrest.CoreMatchers.is)10 CoreMatchers.notNullValue (org.hamcrest.CoreMatchers.notNullValue)10 AfterEvaluateDecisionTableEvent (org.kie.dmn.api.core.event.AfterEvaluateDecisionTableEvent)10 Collection (java.util.Collection)8 UUID (java.util.UUID)8