Search in sources :

Example 1 with DecisionNode

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

the class DMNRuntimeImpl method evaluateByIdInternal.

private void evaluateByIdInternal(DMNModel model, DMNContext context, DMNResultImpl result, String id) {
    boolean performRuntimeTypeCheck = performRuntimeTypeCheck(model);
    DecisionNode decision = model.getDecisionById(id);
    if (decision != null) {
        evaluateDecision(context, result, decision, performRuntimeTypeCheck);
    } else {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, null, result, null, null, Msg.DECISION_NOT_FOUND_FOR_ID, id);
    }
}
Also used : DecisionNode(org.kie.dmn.api.core.ast.DecisionNode)

Example 2 with DecisionNode

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

the class DMNRuntimeImpl method evaluateByNameInternal.

private void evaluateByNameInternal(DMNModel model, DMNContext context, DMNResultImpl result, String name) {
    boolean performRuntimeTypeCheck = performRuntimeTypeCheck(model);
    DecisionNode decision = model.getDecisionByName(name);
    if (decision != null) {
        evaluateDecision(context, result, decision, performRuntimeTypeCheck);
    } else {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, null, result, null, null, Msg.DECISION_NOT_FOUND_FOR_NAME, name);
    }
}
Also used : DecisionNode(org.kie.dmn.api.core.ast.DecisionNode)

Example 3 with DecisionNode

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

the class DMNCompilerImpl method processDrgElements.

private void processDrgElements(DMNCompilerContext ctx, DMNFEELHelper feel, DMNModelImpl model, Definitions dmndefs) {
    for (DRGElement e : dmndefs.getDrgElement()) {
        boolean foundIt = false;
        for (DRGElementCompiler dc : drgCompilers) {
            if (dc.accept(e)) {
                foundIt = true;
                dc.compileNode(e, this, model);
                continue;
            }
        }
        if (!foundIt) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, e, model, null, null, Msg.UNSUPPORTED_ELEMENT, e.getClass().getSimpleName(), e.getId());
        }
    }
    for (BusinessKnowledgeModelNode bkm : model.getBusinessKnowledgeModels()) {
        BusinessKnowledgeModelNodeImpl bkmi = (BusinessKnowledgeModelNodeImpl) bkm;
        for (DRGElementCompiler dc : drgCompilers) {
            if (bkmi.getEvaluator() == null && dc.accept(bkm)) {
                dc.compileEvaluator(bkm, this, ctx, model);
            }
        }
    }
    for (DecisionNode d : model.getDecisions()) {
        DecisionNodeImpl di = (DecisionNodeImpl) d;
        for (DRGElementCompiler dc : drgCompilers) {
            if (di.getEvaluator() == null && dc.accept(d)) {
                dc.compileEvaluator(d, this, ctx, model);
            }
        }
    }
    detectCycles(model);
}
Also used : BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DRGElement(org.kie.dmn.model.v1_1.DRGElement)

Example 4 with DecisionNode

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

the class DMNMarshallerTest method test_function_java_WB_model.

@Test
public void test_function_java_WB_model() throws IOException {
    final DMNMarshaller m = new DMNMarshaller(new XMLEncoderDiagramMetadataMarshaller(), applicationFactoryManager);
    @SuppressWarnings("unchecked") final Graph<?, Node<?, ?>> g = m.unmarshall(null, this.getClass().getResourceAsStream("/DROOLS-2372.dmn"));
    final Stream<Node<?, ?>> stream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(g.nodes().iterator(), Spliterator.ORDERED), false);
    final Optional<Decision> wbDecision = stream.filter(n -> n.getContent() instanceof ViewImpl).map(n -> (ViewImpl) n.getContent()).filter(n -> n.getDefinition() instanceof Decision).map(n -> (Decision) n.getDefinition()).findFirst();
    wbDecision.ifPresent(d -> {
        assertTrue(d.getExpression() instanceof FunctionDefinition);
        final FunctionDefinition wbFunction = (FunctionDefinition) d.getExpression();
        // This is what the WB expects
        assertTrue(wbFunction.getAdditionalAttributes().containsKey(FunctionDefinition.KIND_QNAME));
        assertEquals("J", wbFunction.getAdditionalAttributes().get(FunctionDefinition.KIND_QNAME));
    });
    final DMNRuntime runtime = roundTripUnmarshalMarshalThenUnmarshalDMN(this.getClass().getResourceAsStream("/DROOLS-2372.dmn"));
    final DMNModel dmnModel = runtime.getModels().get(0);
    final DecisionNode dmnDecision = dmnModel.getDecisions().iterator().next();
    assertTrue(dmnDecision.getDecision().getExpression() instanceof org.kie.dmn.model.v1_1.FunctionDefinition);
    final org.kie.dmn.model.v1_1.FunctionDefinition dmnFunction = (org.kie.dmn.model.v1_1.FunctionDefinition) dmnDecision.getDecision().getExpression();
    assertTrue(dmnFunction.getAdditionalAttributes().containsKey(org.kie.dmn.model.v1_1.FunctionDefinition.KIND_QNAME));
    assertEquals("J", dmnFunction.getAdditionalAttributes().get(org.kie.dmn.model.v1_1.FunctionDefinition.KIND_QNAME));
}
Also used : Arrays(java.util.Arrays) DefinitionUtils(org.kie.workbench.common.stunner.core.util.DefinitionUtils) AuthorityRequirement(org.kie.workbench.common.dmn.api.definition.v1_1.AuthorityRequirement) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNResult(org.kie.dmn.api.core.DMNResult) Spliterators(java.util.Spliterators) Edge(org.kie.workbench.common.stunner.core.graph.Edge) KnowledgeSource(org.kie.workbench.common.dmn.api.definition.v1_1.KnowledgeSource) ViewImpl(org.kie.workbench.common.stunner.core.graph.content.view.ViewImpl) BackendDefinitionSetAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendDefinitionSetAdapter) EdgeFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.EdgeFactoryImpl) BigDecimal(java.math.BigDecimal) DMNModel(org.kie.dmn.api.core.DMNModel) Bounds(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.org.omg.spec.CMMN_20151109_DC.Bounds) DecisionEvaluationStatus(org.kie.dmn.api.core.DMNDecisionResult.DecisionEvaluationStatus) DMNDiagram(org.kie.workbench.common.dmn.api.definition.v1_1.DMNDiagram) Mockito.doAnswer(org.mockito.Mockito.doAnswer) GraphFactory(org.kie.workbench.common.stunner.core.factory.graph.GraphFactory) DefinitionManager(org.kie.workbench.common.stunner.core.api.DefinitionManager) StringInputStream(org.apache.tools.ant.filters.StringInputStream) NodeFactory(org.kie.workbench.common.stunner.core.factory.graph.NodeFactory) InputData(org.kie.workbench.common.dmn.api.definition.v1_1.InputData) Collection(java.util.Collection) BackendPropertySetAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendPropertySetAdapter) Child(org.kie.workbench.common.stunner.core.graph.content.relationship.Child) EdgeFactory(org.kie.workbench.common.stunner.core.factory.graph.EdgeFactory) Metadata(org.kie.workbench.common.stunner.core.diagram.Metadata) BusinessKnowledgeModel(org.kie.workbench.common.dmn.api.definition.v1_1.BusinessKnowledgeModel) Matchers.any(org.mockito.Matchers.any) List(java.util.List) Stream(java.util.stream.Stream) Definitions(org.kie.dmn.model.v1_1.Definitions) Context(org.kie.workbench.common.dmn.api.definition.v1_1.Context) DDExtensionsRegister(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DDExtensionsRegister) Assert.assertFalse(org.junit.Assert.assertFalse) DMNContext(org.kie.dmn.api.core.DMNContext) DMNMarshallerFactory(org.kie.dmn.backend.marshalling.v1_1.DMNMarshallerFactory) MetadataImpl(org.kie.workbench.common.stunner.core.diagram.MetadataImpl) Optional(java.util.Optional) MappingContextSingleton(org.jboss.errai.marshalling.server.MappingContextSingleton) KieHelper(org.kie.dmn.core.util.KieHelper) DMNShape(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DMNShape) Color(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.org.omg.spec.CMMN_20151109_DC.Color) Spliterator(java.util.Spliterator) Decision(org.kie.workbench.common.dmn.api.definition.v1_1.Decision) Node(org.kie.workbench.common.stunner.core.graph.Node) DMNDecisionResult(org.kie.dmn.api.core.DMNDecisionResult) DMNDefinitionSet(org.kie.workbench.common.dmn.api.DMNDefinitionSet) DMNStyle(org.kie.workbench.common.dmn.backend.definition.v1_1.dd.DMNStyle) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.v1_1.FunctionDefinition) Mock(org.mockito.Mock) GraphCommandManager(org.kie.workbench.common.stunner.core.graph.command.GraphCommandManager) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) GraphCommandManagerImpl(org.kie.workbench.common.stunner.core.graph.command.GraphCommandManagerImpl) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Matchers.anyString(org.mockito.Matchers.anyString) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) BackendDefinitionAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendDefinitionAdapter) KieServices(org.kie.api.KieServices) DiagramImpl(org.kie.workbench.common.stunner.core.diagram.DiagramImpl) StreamSupport(java.util.stream.StreamSupport) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) InformationRequirement(org.kie.workbench.common.dmn.api.definition.v1_1.InformationRequirement) GraphFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.GraphFactoryImpl) AdapterManager(org.kie.workbench.common.stunner.core.definition.adapter.AdapterManager) Before(org.junit.Before) BindableAdapterUtils(org.kie.workbench.common.stunner.core.definition.adapter.binding.BindableAdapterUtils) NodeFactoryImpl(org.kie.workbench.common.stunner.core.factory.impl.NodeFactoryImpl) TextAnnotation(org.kie.workbench.common.dmn.api.definition.v1_1.TextAnnotation) XMLEncoderDiagramMetadataMarshaller(org.kie.workbench.common.stunner.core.backend.service.XMLEncoderDiagramMetadataMarshaller) Assert.assertNotNull(org.junit.Assert.assertNotNull) KieContainer(org.kie.api.runtime.KieContainer) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) AdapterRegistry(org.kie.workbench.common.stunner.core.registry.definition.AdapterRegistry) Mockito.when(org.mockito.Mockito.when) UUID(org.uberfire.commons.uuid.UUID) Association(org.kie.workbench.common.dmn.api.definition.v1_1.Association) TestScopeModelFactory(org.kie.workbench.common.stunner.backend.definition.factory.TestScopeModelFactory) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) Consumer(java.util.function.Consumer) GraphCommandFactory(org.kie.workbench.common.stunner.core.graph.command.impl.GraphCommandFactory) Graph(org.kie.workbench.common.stunner.core.graph.Graph) MockitoJUnitRunner(org.mockito.runners.MockitoJUnitRunner) BackendFactoryManager(org.kie.workbench.common.stunner.core.backend.BackendFactoryManager) KnowledgeRequirement(org.kie.workbench.common.dmn.api.definition.v1_1.KnowledgeRequirement) BackendPropertyAdapter(org.kie.workbench.common.stunner.core.backend.definition.adapter.reflect.BackendPropertyAdapter) ElementFactory(org.kie.workbench.common.stunner.core.factory.graph.ElementFactory) RuleManager(org.kie.workbench.common.stunner.core.rule.RuleManager) Assert(org.junit.Assert) BeanManager(javax.enterprise.inject.spi.BeanManager) Assert.assertEquals(org.junit.Assert.assertEquals) InputStream(java.io.InputStream) Node(org.kie.workbench.common.stunner.core.graph.Node) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) Decision(org.kie.workbench.common.dmn.api.definition.v1_1.Decision) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) XMLEncoderDiagramMetadataMarshaller(org.kie.workbench.common.stunner.core.backend.service.XMLEncoderDiagramMetadataMarshaller) ViewImpl(org.kie.workbench.common.stunner.core.graph.content.view.ViewImpl) FunctionDefinition(org.kie.workbench.common.dmn.api.definition.v1_1.FunctionDefinition) DMNModel(org.kie.dmn.api.core.DMNModel) Test(org.junit.Test)

Example 5 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)

Aggregations

DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)32 InputDataNode (org.kie.dmn.api.core.ast.InputDataNode)16 DMNType (org.kie.dmn.api.core.DMNType)12 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)10 DecisionServiceNode (org.kie.dmn.api.core.ast.DecisionServiceNode)9 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)9 DMNMessage (org.kie.dmn.api.core.DMNMessage)7 DMNModel (org.kie.dmn.api.core.DMNModel)7 DMNDecisionResult (org.kie.dmn.api.core.DMNDecisionResult)6 DMNNode (org.kie.dmn.api.core.ast.DMNNode)6 Optional (java.util.Optional)5 DMNContext (org.kie.dmn.api.core.DMNContext)5 DMNResult (org.kie.dmn.api.core.DMNResult)5 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)5 DMNBaseNode (org.kie.dmn.core.ast.DMNBaseNode)5 DecisionServiceNodeImpl (org.kie.dmn.core.ast.DecisionServiceNodeImpl)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 List (java.util.List)4 HashMap (java.util.HashMap)3