Search in sources :

Example 51 with DMNMessage

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

the class ValidatorTest method testINVOCATION_MISSING_TARGETRbis.

@Ignore("known current limitation")
@Test
public void testINVOCATION_MISSING_TARGETRbis() {
    Definitions definitions = utilDefinitions("INVOCATION_MISSING_TARGETbis.dmn", "INVOCATION_MISSING_TARGETbis");
    List<DMNMessage> validate = validator.validate(definitions);
// assertTrue( validate.stream().anyMatch( p -> p.getMessageType().equals( DMNMessageType.INVOCATION_MISSING_TARGET ) ) );
}
Also used : DMNMessage(org.kie.dmn.api.core.DMNMessage) Definitions(org.kie.dmn.model.v1_1.Definitions) Ignore(org.junit.Ignore) Test(org.junit.Test) DMNInputRuntimeTest(org.kie.dmn.core.DMNInputRuntimeTest)

Example 52 with DMNMessage

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

the class ValidatorTest method testMACDInputDefinitions.

@Test
public void testMACDInputDefinitions() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("MACD-enhanced_iteration.dmn", DMNInputRuntimeTest.class);
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/definitions/_6cfe7d88-6741-45d1-968c-b61a597d0964", "MACD-enhanced iteration");
    assertThat(dmnModel, notNullValue());
    Definitions definitions = dmnModel.getDefinitions();
    assertThat(definitions, notNullValue());
    List<DMNMessage> messages = DMNValidatorFactory.newValidator().validate(definitions, VALIDATE_MODEL, VALIDATE_COMPILATION);
    assertThat(messages.toString(), messages.size(), is(0));
}
Also used : DMNMessage(org.kie.dmn.api.core.DMNMessage) Definitions(org.kie.dmn.model.v1_1.Definitions) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test) DMNInputRuntimeTest(org.kie.dmn.core.DMNInputRuntimeTest)

Example 53 with DMNMessage

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

the class ValidatorTypeRefTest method testTYPEREF_NO_NS_ReaderInput.

@Test
public void testTYPEREF_NO_NS_ReaderInput() throws IOException {
    try (final Reader reader = getReader("typeref/TYPEREF_NO_NS.dmn")) {
        final List<DMNMessage> validate = validator.validate(reader, VALIDATE_SCHEMA, VALIDATE_MODEL, VALIDATE_COMPILATION);
        assertThat(ValidatorUtil.formatMessages(validate), validate.size(), is(2));
        assertTrue(validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.FAILED_XML_VALIDATION)));
        assertTrue(validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.TYPE_DEF_NOT_FOUND)));
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assert.assertThat(org.junit.Assert.assertThat) List(java.util.List) DMNMessage(org.kie.dmn.api.core.DMNMessage) VALIDATE_SCHEMA(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_SCHEMA) DMNMessageType(org.kie.dmn.api.core.DMNMessageType) VALIDATE_COMPILATION(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_COMPILATION) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Reader(java.io.Reader) VALIDATE_MODEL(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_MODEL) DMNMessage(org.kie.dmn.api.core.DMNMessage) Reader(java.io.Reader) Test(org.junit.Test)

Example 54 with DMNMessage

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

the class DMNRuntimeImpl method evaluateDecision.

private boolean evaluateDecision(DMNContext context, DMNResultImpl result, DecisionNode d, boolean typeCheck) {
    DecisionNodeImpl decision = (DecisionNodeImpl) d;
    if (result.getContext().isDefined(decision.getName())) {
        // already resolved
        return true;
    } else {
        // check if the decision was already evaluated before and returned error
        DMNDecisionResult.DecisionEvaluationStatus status = result.getDecisionResultById(decision.getId()).getEvaluationStatus();
        if (FAILED == status || SKIPPED == status || EVALUATING == status) {
            return false;
        }
    }
    try {
        DMNRuntimeEventManagerUtils.fireBeforeEvaluateDecision(eventManager, decision, result);
        boolean missingInput = false;
        DMNDecisionResultImpl dr = (DMNDecisionResultImpl) result.getDecisionResultById(decision.getId());
        dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.EVALUATING);
        for (DMNNode dep : decision.getDependencies().values()) {
            try {
                if (typeCheck && !checkDependencyValueIsValid(dep, result)) {
                    missingInput = true;
                    DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, null, null, Msg.ERROR_EVAL_NODE_DEP_WRONG_TYPE, getIdentifier(decision), getIdentifier(dep), MsgUtil.clipString(result.getContext().get(dep.getName()).toString(), 50), ((DMNBaseNode) dep).getType());
                    reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
                }
            } catch (Exception e) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ((DMNBaseNode) dep).getSource(), result, e, null, Msg.ERROR_CHECKING_ALLOWED_VALUES, getIdentifier(dep), e.getMessage());
            }
            if (!result.getContext().isDefined(dep.getName())) {
                if (dep instanceof DecisionNode) {
                    if (!evaluateDecision(context, result, (DecisionNode) dep, typeCheck)) {
                        missingInput = true;
                        DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.UNABLE_TO_EVALUATE_DECISION_REQ_DEP, getIdentifier(decision), getIdentifier(dep));
                        reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
                    }
                } else if (dep instanceof BusinessKnowledgeModelNode) {
                    evaluateBKM(context, result, (BusinessKnowledgeModelNode) dep, typeCheck);
                } else {
                    missingInput = true;
                    DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.REQ_DEP_NOT_FOUND_FOR_NODE, getIdentifier(dep), getIdentifier(decision));
                    reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
                }
            }
        }
        if (missingInput) {
            return false;
        }
        if (decision.getEvaluator() == null) {
            DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, decision.getSource(), result, null, null, Msg.MISSING_EXPRESSION_FOR_DECISION, getIdentifier(decision));
            reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.SKIPPED);
            return false;
        }
        try {
            EvaluatorResult er = decision.getEvaluator().evaluate(this, result);
            if (er.getResultType() == EvaluatorResult.ResultType.SUCCESS) {
                Object value = er.getResult();
                if (!decision.getResultType().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];
                }
                try {
                    if (typeCheck && !d.getResultType().isAssignableValue(value)) {
                        DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, null, null, Msg.ERROR_EVAL_NODE_RESULT_WRONG_TYPE, getIdentifier(decision), decision.getResultType(), value);
                        reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.FAILED);
                        return false;
                    }
                } catch (Exception e) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, e, null, Msg.ERROR_CHECKING_ALLOWED_VALUES, getIdentifier(decision), e.getMessage());
                    return false;
                }
                result.getContext().set(decision.getDecision().getVariable().getName(), value);
                dr.setResult(value);
                dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.SUCCEEDED);
            } else {
                dr.setEvaluationStatus(DMNDecisionResult.DecisionEvaluationStatus.FAILED);
                return false;
            }
        } catch (Throwable t) {
            DMNMessage message = MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, decision.getSource(), result, t, null, Msg.ERROR_EVAL_DECISION_NODE, getIdentifier(decision), t.getMessage());
            reportFailure(dr, message, DMNDecisionResult.DecisionEvaluationStatus.FAILED);
        }
        return true;
    } finally {
        DMNRuntimeEventManagerUtils.fireAfterEvaluateDecision(eventManager, decision, result);
    }
}
Also used : EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) Collection(java.util.Collection) DMNNode(org.kie.dmn.api.core.ast.DMNNode)

Example 55 with DMNMessage

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

the class DMNRuntimeTest method testEmptyDecision2.

@Test
public void testEmptyDecision2() {
    DMNRuntime runtime = DMNRuntimeUtil.createRuntime("empty_decision.dmn", this.getClass());
    DMNModel dmnModel = runtime.getModel("http://www.trisotech.com/dmn/definitions/_ba9fc4b1-5ced-4d00-9b61-290de4bf3213", "Solution 3");
    assertThat(dmnModel, notNullValue());
    DMNContext context = DMNFactory.newContext();
    Map shipInfo = new HashMap();
    shipInfo.put("Size", BigDecimal.valueOf(70));
    shipInfo.put("Is Double Hulled", Boolean.FALSE);
    shipInfo.put("Residual Cargo Size", BigDecimal.valueOf(0.1));
    context.set("Ship Info", shipInfo);
    context.set("Ship Size", BigDecimal.valueOf(70));
    context.set("IsDoubleHulled", Boolean.FALSE);
    context.set("Residual Cargo Size", BigDecimal.valueOf(0.1));
    // check that if all the input data is available, but the
    // decision expression is empty, the model returns a warning
    DMNResult dmnResult = runtime.evaluateAll(dmnModel, context);
    List<DMNMessage> messages = dmnResult.getMessages(DMNMessage.Severity.WARN);
    assertThat(messages.size(), is(1));
    assertThat(messages.get(0).getSeverity(), is(DMNMessage.Severity.WARN));
    assertThat(messages.get(0).getSourceId(), is("_42806504-8ed5-488f-b274-de98c1bc67b9"));
    DMNContext result = dmnResult.getContext();
    assertThat(result.get("Ship Can Enter v2"), is(true));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNMessage(org.kie.dmn.api.core.DMNMessage) HashMap(java.util.HashMap) DMNContext(org.kie.dmn.api.core.DMNContext) Map(java.util.Map) HashMap(java.util.HashMap) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Aggregations

DMNMessage (org.kie.dmn.api.core.DMNMessage)59 Test (org.junit.Test)58 Reader (java.io.Reader)52 IOException (java.io.IOException)46 List (java.util.List)46 CoreMatchers.is (org.hamcrest.CoreMatchers.is)46 DMNMessageType (org.kie.dmn.api.core.DMNMessageType)46 VALIDATE_COMPILATION (org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_COMPILATION)46 VALIDATE_MODEL (org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_MODEL)46 VALIDATE_SCHEMA (org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_SCHEMA)46 Assert.assertThat (org.junit.Assert.assertThat)33 Assert.assertTrue (org.junit.Assert.assertTrue)33 Assert (org.junit.Assert)13 Context (org.kie.dmn.model.v1_1.Context)7 ContextEntry (org.kie.dmn.model.v1_1.ContextEntry)7 DMNInputRuntimeTest (org.kie.dmn.core.DMNInputRuntimeTest)5 Definitions (org.kie.dmn.model.v1_1.Definitions)4 DMNModel (org.kie.dmn.api.core.DMNModel)3 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)3 Ignore (org.junit.Ignore)2