Search in sources :

Example 6 with DecisionNode

use of org.kie.dmn.api.core.ast.DecisionNode in project tck by dmn-tck.

the class DroolsTCKTest method executeTest.

@Override
public TestResult executeTest(Description description, TestSuiteContext context, TestCases.TestCase testCase) {
    DroolsContext ctx = (DroolsContext) context;
    logger.info("Executing test '{} / {}'\n", description.getClassName(), description.getMethodName());
    DMNContext dmnctx = ctx.runtime.newContext();
    testCase.getInputNode().forEach(in -> {
        InputDataNode input = ctx.dmnmodel.getInputByName(in.getName());
        DecisionNode decision = ctx.dmnmodel.getDecisionByName(in.getName());
        if (input != null) {
            // normally data input from file, should be pointing at a InputData in the model
            dmnctx.set(in.getName(), parseValue(in, input));
        } else if (decision != null) {
            // the test case offers a pre-evaluated Decision
            dmnctx.set(in.getName(), parseValue(in, decision));
        } else {
            throw new RuntimeException("Unable to locate InputData node or a Decision node for name: " + in.getName());
        }
    });
    DMNContext resultctx = dmnctx;
    List<String> failures = new ArrayList<>();
    for (TestCases.TestCase.ResultNode rn : testCase.getResultNode()) {
        try {
            String name = rn.getName();
            DMNResult dmnResult = ctx.runtime.evaluateDecisionByName(ctx.dmnmodel, name, resultctx);
            if (!dmnResult.getMessages().isEmpty()) {
                logger.info("Messages: \n-----\n{}-----\n", dmnResult.getMessages().stream().map(m -> m.toString()).collect(Collectors.joining("\n")));
            }
            resultctx = dmnResult.getContext();
            Object expected = parseValue(rn, ctx.dmnmodel.getDecisionByName(name));
            Object actual = resultctx.get(name);
            if (rn.isErrorResult()) {
                if (actual != null) {
                    failures.add("FAILURE: '" + name + "' expected error but found='" + actual + "'");
                }
            } else {
                if (dmnResult.hasErrors()) {
                    for (DMNMessage msg : dmnResult.getMessages(DMNMessage.Severity.ERROR)) {
                        failures.add(msg.toString());
                    }
                }
                if (!isEquals(expected, actual)) {
                    failures.add("FAILURE: '" + name + "' expected='" + expected + "' but found='" + actual + "'");
                }
            }
        } catch (Throwable t) {
            failures.add("FAILURE: unnexpected exception executing test case '" + description.getClassName() + " / " + description.getMethodName() + "': " + t.getClass().getName());
            logger.error("FAILURE: unnexpected exception executing test case '{} / {}'", description.getClassName(), description.getMethodName(), t);
        }
    }
    // DMNResult dmnResult = ctx.runtime.evaluateAll( ctx.dmnmodel, dmnctx );
    logger.info("Result context: {}\n", resultctx);
    // if( ! dmnResult.getMessages().isEmpty() ) {
    // logger.info( "Messages: \n-----\n{}-----\n", dmnResult.getMessages().stream().map( m -> m.toString() ).collect( Collectors.joining( "\n" ) ) );
    // }
    // 
    // List<String> failures = new ArrayList<>();
    // if( dmnResult.hasErrors() ) {
    // for( DMNMessage msg : dmnResult.getMessages( DMNMessage.Severity.ERROR ) ) {
    // failures.add( msg.toString() );
    // }
    // }
    // testCase.getResultNode().forEach( rn -> {
    // try {
    // String name = rn.getName();
    // Object expected = parseValue( rn, ctx.dmnmodel.getDecisionByName( name ) );
    // Object actual = resultctx.get( name );
    // if( ! isEquals( expected, actual ) ) {
    // failures.add( "FAILURE: '"+name+"' expected='"+expected+"' but found='"+actual+"'" );
    // }
    // } catch ( Throwable t ) {
    // failures.add( "FAILURE: unnexpected exception executing test case '"+description.getClassName()+" / " +description.getMethodName()+"': "+t.getClass().getName() );
    // logger.error( "FAILURE: unnexpected exception executing test case '{} / {}'", description.getClassName(), description.getMethodName(), t );
    // }
    // } );
    TestResult.Result r = !failures.isEmpty() ? TestResult.Result.ERROR : TestResult.Result.SUCCESS;
    return new TestResult(r, failures.stream().collect(Collectors.joining("\n")));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) ArrayList(java.util.ArrayList) TestResult(org.omg.dmn.tck.runner.junit4.TestResult) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNMessage(org.kie.dmn.api.core.DMNMessage) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode)

Example 7 with DecisionNode

use of org.kie.dmn.api.core.ast.DecisionNode in project kie-wb-common by kiegroup.

the class DMNMarshallerTest method test_wrong_context.

@Test
public void test_wrong_context() throws IOException {
    // DROOLS-2217
    // SPECIAL CASE: to represent a partially edited DMN file.
    // consider a LiteralExpression with null text as missing expression altogether.
    final DMNRuntime runtime = roundTripUnmarshalMarshalThenUnmarshalDMN(this.getClass().getResourceAsStream("/wrong_context.dmn"));
    DMNModel dmnModel = runtime.getModels().get(0);
    // the DMN file is schema valid but is not a valid-DMN (a context-entry value is a literal expression missing text, which is null)
    assertTrue(dmnModel.hasErrors());
    // identify the error message for context-entry "ciao":
    DMNMessage m0 = dmnModel.getMessages(DMNMessage.Severity.ERROR).get(0);
    assertTrue("expected a message identifying the problem on a context entry for 'ciao'", m0.getMessage().startsWith("No expression defined for name 'ciao'"));
    DecisionNode d0 = dmnModel.getDecisionById("_653b3426-933a-4050-9568-ab2a66b43c36");
    // the identified DMN Decision is composed of a DMN Context where the first context-entry value is a literal expression missing text (text is null).
    org.kie.dmn.model.v1_1.Context d0c = (org.kie.dmn.model.v1_1.Context) d0.getDecision().getExpression();
    org.kie.dmn.model.v1_1.Expression contextEntryValue = d0c.getContextEntry().get(0).getExpression();
    assertTrue(contextEntryValue instanceof org.kie.dmn.model.v1_1.LiteralExpression);
    assertEquals(null, ((org.kie.dmn.model.v1_1.LiteralExpression) contextEntryValue).getText());
    // -- Stunner side.
    DMNMarshaller m = new DMNMarshaller(new XMLEncoderDiagramMetadataMarshaller(), applicationFactoryManager);
    Graph<?, ?> g = m.unmarshall(null, this.getClass().getResourceAsStream("/wrong_context.dmn"));
    DiagramImpl diagram = new DiagramImpl("", null);
    Node<?, ?> decisionNode = g.getNode("_653b3426-933a-4050-9568-ab2a66b43c36");
    assertNodeContentDefinitionIs(decisionNode, Decision.class);
    View<Decision> view = ((View<Decision>) decisionNode.getContent());
    // the identified DMN Decision is composed of a DMN Context where the first context-entry has missing Expression.
    Context expression = (Context) view.getDefinition().getExpression();
    assertEquals("a literalexpression with null text is threated as a missing expression altogether.", null, expression.getContextEntry().get(0).getExpression());
}
Also used : Context(org.kie.workbench.common.dmn.api.definition.v1_1.Context) DMNContext(org.kie.dmn.api.core.DMNContext) DiagramImpl(org.kie.workbench.common.stunner.core.diagram.DiagramImpl) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Decision(org.kie.workbench.common.dmn.api.definition.v1_1.Decision) DMNMessage(org.kie.dmn.api.core.DMNMessage) XMLEncoderDiagramMetadataMarshaller(org.kie.workbench.common.stunner.core.backend.service.XMLEncoderDiagramMetadataMarshaller) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 8 with DecisionNode

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

the class DMNCompilerImpl method linkRequirements.

public void linkRequirements(DMNModelImpl model, DMNBaseNode node) {
    for (InformationRequirement ir : node.getInformationRequirement()) {
        if (ir.getRequiredInput() != null) {
            String id = getId(ir.getRequiredInput());
            InputDataNode input = model.getInputById(id);
            if (input != null) {
                node.addDependency(input.getName(), input);
            } else {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ir.getRequiredInput(), model, null, null, Msg.REQ_INPUT_NOT_FOUND_FOR_NODE, id, node.getName());
            }
        } else if (ir.getRequiredDecision() != null) {
            String id = getId(ir.getRequiredDecision());
            DecisionNode dn = model.getDecisionById(id);
            if (dn != null) {
                node.addDependency(dn.getName(), dn);
            } else {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, ir.getRequiredDecision(), model, null, null, Msg.REQ_DECISION_NOT_FOUND_FOR_NODE, id, node.getName());
            }
        }
    }
    for (KnowledgeRequirement kr : node.getKnowledgeRequirement()) {
        if (kr.getRequiredKnowledge() != null) {
            String id = getId(kr.getRequiredKnowledge());
            BusinessKnowledgeModelNode bkmn = model.getBusinessKnowledgeModelById(id);
            if (bkmn != null) {
                node.addDependency(bkmn.getName(), bkmn);
            } else {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, kr.getRequiredKnowledge(), model, null, null, Msg.REQ_BKM_NOT_FOUND_FOR_NODE, id, node.getName());
            }
        }
    }
}
Also used : KnowledgeRequirement(org.kie.dmn.model.v1_1.KnowledgeRequirement) InformationRequirement(org.kie.dmn.model.v1_1.InformationRequirement) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)

Example 9 with DecisionNode

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

the class DMNCompilerImpl method detectCycles.

private void detectCycles(DMNModelImpl model) {
    /*
        Boolean.TRUE = node is either safe or already reported for having a cyclic dependency
        Boolean.FALSE = node is being checked at the moment
         */
    final Map<DecisionNodeImpl, Boolean> registry = new HashMap<>();
    for (DecisionNode decision : model.getDecisions()) {
        final DecisionNodeImpl decisionNode = (DecisionNodeImpl) decision;
        detectCycles(decisionNode, registry, model);
    }
}
Also used : HashMap(java.util.HashMap) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl)

Example 10 with DecisionNode

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

the class DMNEvaluatorCompiler method compileContext.

private DMNExpressionEvaluator compileContext(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String contextName, Context expression) {
    Context ctxDef = expression;
    DMNContextEvaluator ctxEval = new DMNContextEvaluator(node.getName(), ctxDef);
    ctx.enterFrame();
    try {
        for (ContextEntry ce : ctxDef.getContextEntry()) {
            if (ce.getVariable() != null) {
                String entryName = ce.getVariable().getName();
                DMNCompilerHelper.checkVariableName(model, node.getSource(), entryName);
                DMNType entryType = compiler.resolveTypeRef(model, node, ce.getVariable(), ce.getVariable(), ce.getVariable().getTypeRef());
                // add context entry to the list of available variables for the following entries
                ctx.setVariable(entryName, entryType);
                DMNExpressionEvaluator evaluator = compileExpression(ctx, model, node, entryName, ce.getExpression());
                ctxEval.addEntry(entryName, entryType, evaluator, ce);
            } else {
                // if the variable is not defined, then it should be the last
                // entry in the context and the result of this context evaluation is the
                // result of this expression itself
                // TODO: if it is not the last entry, raise error message
                DMNType type = null;
                if (ctxDef.getParent() instanceof ContextEntry && ((ContextEntry) ctxDef.getParent()).getVariable() != null) {
                    ContextEntry parentEntry = (ContextEntry) ctxDef.getParent();
                    type = compiler.resolveTypeRef(model, node, parentEntry.getVariable(), parentEntry.getVariable(), parentEntry.getVariable().getTypeRef());
                } else if (node instanceof BusinessKnowledgeModelNode) {
                    type = ((BusinessKnowledgeModelNode) node).getResultType();
                } else if (node instanceof DecisionNode) {
                    type = ((DecisionNode) node).getResultType();
                }
                ctxEval.addEntry(DMNContextEvaluator.RESULT_ENTRY, type, compileExpression(ctx, model, node, contextName, ce.getExpression()), ce);
            }
        }
    } finally {
        ctx.exitFrame();
    }
    return ctxEval;
}
Also used : Context(org.kie.dmn.model.v1_1.Context) DMNContextEvaluator(org.kie.dmn.core.ast.DMNContextEvaluator) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) ContextEntry(org.kie.dmn.model.v1_1.ContextEntry) DMNType(org.kie.dmn.api.core.DMNType)

Aggregations

DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)13 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)5 DMNMessage (org.kie.dmn.api.core.DMNMessage)4 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)4 HashMap (java.util.HashMap)3 DMNContext (org.kie.dmn.api.core.DMNContext)3 InputDataNode (org.kie.dmn.api.core.ast.InputDataNode)3 Collection (java.util.Collection)2 Optional (java.util.Optional)2 Test (org.junit.Test)2 DMNDecisionResult (org.kie.dmn.api.core.DMNDecisionResult)2 DMNModel (org.kie.dmn.api.core.DMNModel)2 DMNResult (org.kie.dmn.api.core.DMNResult)2 DRGElement (org.kie.dmn.model.v1_1.DRGElement)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1