Search in sources :

Example 1 with DecisionNodeImpl

use of org.kie.dmn.core.ast.DecisionNodeImpl 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 2 with DecisionNodeImpl

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

the class DMNRuntimeTest method testCycleDetectionDeadlyDiamond.

@Test
public void testCycleDetectionDeadlyDiamond() {
    final Definitions defs = buildSimplifiedDefinitions("ns", "a", "b", "c", "d");
    final DecisionNodeImpl a = buildSimplifiedDecisionNode(defs, "a");
    final DecisionNodeImpl b = buildSimplifiedDecisionNode(defs, "b");
    final DecisionNodeImpl c = buildSimplifiedDecisionNode(defs, "c");
    final DecisionNodeImpl d = buildSimplifiedDecisionNode(defs, "d");
    a.addDependency("b", b);
    a.addDependency("c", c);
    b.addDependency("d", d);
    c.addDependency("d", d);
    final DMNModelImpl model = new DMNModelImpl(defs);
    model.setDefinitions(defs);
    model.addDecision(a);
    model.addDecision(b);
    model.addDecision(c);
    model.addDecision(d);
    final DMNRuntime runtime = DMNRuntimeUtil.createRuntime(this.getClass());
    final DMNResult result = runtime.evaluateAll(model, DMNFactory.newContext());
    assertFalse(result.hasErrors());
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) TDefinitions(org.kie.dmn.model.v1_1.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) Test(org.junit.Test)

Example 3 with DecisionNodeImpl

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

the class DecisionCompiler method compileEvaluator.

@Override
public void compileEvaluator(DMNNode node, DMNCompilerImpl compiler, DMNCompilerContext ctx, DMNModelImpl model) {
    DecisionNodeImpl di = (DecisionNodeImpl) node;
    compiler.linkRequirements(model, di);
    ctx.enterFrame();
    try {
        loadInCtx(di, ctx, model);
        DMNExpressionEvaluator evaluator = compiler.getEvaluatorCompiler().compileExpression(ctx, model, di, di.getName(), di.getDecision().getExpression());
        di.setEvaluator(evaluator);
    } finally {
        ctx.exitFrame();
    }
}
Also used : DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl)

Example 4 with DecisionNodeImpl

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

the class DecisionCompiler method compileNode.

@Override
public void compileNode(DRGElement de, DMNCompilerImpl compiler, DMNModelImpl model) {
    Decision decision = (Decision) de;
    DecisionNodeImpl dn = new DecisionNodeImpl(decision);
    DMNType type = null;
    if (decision.getVariable() == null) {
        DMNCompilerHelper.reportMissingVariable(model, de, decision, Msg.MISSING_VARIABLE_FOR_DECISION);
        return;
    }
    DMNCompilerHelper.checkVariableName(model, decision, decision.getName());
    if (decision.getVariable() != null && decision.getVariable().getTypeRef() != null) {
        type = compiler.resolveTypeRef(model, decision, decision.getVariable(), decision.getVariable().getTypeRef());
    } else {
        type = compiler.resolveTypeRef(model, decision, decision, null);
    }
    dn.setResultType(type);
    model.addDecision(dn);
}
Also used : DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) Decision(org.kie.dmn.model.api.Decision) DMNType(org.kie.dmn.api.core.DMNType)

Example 5 with DecisionNodeImpl

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

the class DecisionServiceCompiler method checkFnConsistency.

private void checkFnConsistency(DMNModelImpl model, DecisionServiceNodeImpl ni, DMNType type, List<DecisionNode> outputDecisions) {
    SimpleFnTypeImpl fnType = ((SimpleFnTypeImpl) type);
    FunctionItem fi = fnType.getFunctionItem();
    if (fi.getParameters().size() != ni.getInputParameters().size()) {
        MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.PARAMETER_COUNT_MISMATCH_COMPILING, ni.getName(), fi.getParameters().size(), ni.getInputParameters().size());
        return;
    }
    for (int i = 0; i < fi.getParameters().size(); i++) {
        InformationItem fiII = fi.getParameters().get(i);
        String fpName = ni.getInputParameters().keySet().stream().skip(i).findFirst().orElse(null);
        if (!fiII.getName().equals(fpName)) {
            List<String> fiParamNames = fi.getParameters().stream().map(InformationItem::getName).collect(Collectors.toList());
            List<String> funcDefParamNames = ni.getInputParameters().keySet().stream().collect(Collectors.toList());
            MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.PARAMETER_NAMES_MISMATCH_COMPILING, ni.getName(), fiParamNames, funcDefParamNames);
            return;
        }
        QName fiQname = fiII.getTypeRef();
        QName fdQname = null;
        DMNNode fpDMNNode = ni.getInputParameters().get(fpName);
        if (fpDMNNode instanceof InputDataNodeImpl) {
            fdQname = ((InputDataNodeImpl) fpDMNNode).getInputData().getVariable().getTypeRef();
        } else if (fpDMNNode instanceof DecisionNodeImpl) {
            fdQname = ((DecisionNodeImpl) fpDMNNode).getDecision().getVariable().getTypeRef();
        }
        if (fiQname != null && fdQname != null && !fiQname.equals(fdQname)) {
            MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.PARAMETER_TYPEREF_MISMATCH_COMPILING, ni.getName(), fiII.getName(), fiQname, fdQname);
        }
    }
    QName fiReturnType = fi.getOutputTypeRef();
    if (ni.getDecisionService().getOutputDecision().size() == 1) {
        QName fdReturnType = outputDecisions.get(0).getDecision().getVariable().getTypeRef();
        if (fiReturnType != null && fdReturnType != null && !fiReturnType.equals(fdReturnType)) {
            MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.RETURNTYPE_TYPEREF_MISMATCH_COMPILING, ni.getName(), fiReturnType, fdReturnType);
        }
    } else if (ni.getDecisionService().getOutputDecision().size() > 1) {
        final Function<QName, QName> lookupFn = (in) -> DMNCompilerImpl.getNamespaceAndName(ni.getDecisionService(), model.getImportAliasesForNS(), in, model.getNamespace());
        LinkedHashMap<String, QName> fdComposite = new LinkedHashMap<>();
        for (DecisionNode dn : outputDecisions) {
            fdComposite.put(dn.getName(), lookupFn.apply(dn.getDecision().getVariable().getTypeRef()));
        }
        final QName lookup = lookupFn.apply(fiReturnType);
        Optional<ItemDefNodeImpl> composite = model.getItemDefinitions().stream().filter(id -> id.getModelNamespace().equals(lookup.getNamespaceURI()) && id.getName().equals(lookup.getLocalPart())).map(ItemDefNodeImpl.class::cast).findFirst();
        if (!composite.isPresent()) {
            MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.RETURNTYPE_TYPEREF_MISMATCH_COMPILING, ni.getName(), lookup, fdComposite);
            return;
        }
        LinkedHashMap<String, QName> fiComposite = new LinkedHashMap<>();
        for (ItemDefinition ic : composite.get().getItemDef().getItemComponent()) {
            fiComposite.put(ic.getName(), lookupFn.apply(ic.getTypeRef()));
        }
        if (!fiComposite.equals(fdComposite)) {
            MsgUtil.reportMessage(LOG, DMNMessage.Severity.ERROR, ni.getDecisionService(), model, null, null, Msg.RETURNTYPE_TYPEREF_MISMATCH_COMPILING, ni.getName(), fiComposite, fdComposite);
        }
    }
}
Also used : MsgUtil(org.kie.dmn.core.util.MsgUtil) DMNType(org.kie.dmn.api.core.DMNType) DMNElementReference(org.kie.dmn.model.api.DMNElementReference) DMNMessage(org.kie.dmn.api.core.DMNMessage) SimpleFnTypeImpl(org.kie.dmn.core.impl.SimpleFnTypeImpl) LoggerFactory(org.slf4j.LoggerFactory) ItemDefNodeImpl(org.kie.dmn.core.ast.ItemDefNodeImpl) Function(java.util.function.Function) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) DecisionServiceNodeImpl(org.kie.dmn.core.ast.DecisionServiceNodeImpl) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNDecisionServiceFunctionDefinitionEvaluator(org.kie.dmn.core.ast.DMNDecisionServiceFunctionDefinitionEvaluator) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) InputDataNodeImpl(org.kie.dmn.core.ast.InputDataNodeImpl) Logger(org.slf4j.Logger) DMNNode(org.kie.dmn.api.core.ast.DMNNode) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) Collectors(java.util.stream.Collectors) FunctionItem(org.kie.dmn.model.api.FunctionItem) List(java.util.List) DSFormalParameter(org.kie.dmn.core.ast.DMNDecisionServiceFunctionDefinitionEvaluator.DSFormalParameter) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) QName(javax.xml.namespace.QName) DRGElement(org.kie.dmn.model.api.DRGElement) InformationItem(org.kie.dmn.model.api.InformationItem) Msg(org.kie.dmn.core.util.Msg) DecisionService(org.kie.dmn.model.api.DecisionService) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) Optional(java.util.Optional) QName(javax.xml.namespace.QName) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) InformationItem(org.kie.dmn.model.api.InformationItem) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) SimpleFnTypeImpl(org.kie.dmn.core.impl.SimpleFnTypeImpl) LinkedHashMap(java.util.LinkedHashMap) Function(java.util.function.Function) DMNNode(org.kie.dmn.api.core.ast.DMNNode) FunctionItem(org.kie.dmn.model.api.FunctionItem) InputDataNodeImpl(org.kie.dmn.core.ast.InputDataNodeImpl) ItemDefNodeImpl(org.kie.dmn.core.ast.ItemDefNodeImpl)

Aggregations

DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)21 DMNModelImpl (org.kie.dmn.core.impl.DMNModelImpl)9 Test (org.junit.Test)8 DMNResult (org.kie.dmn.api.core.DMNResult)8 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)8 Definitions (org.kie.dmn.model.api.Definitions)8 TDefinitions (org.kie.dmn.model.v1_1.TDefinitions)8 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)6 DMNNode (org.kie.dmn.api.core.ast.DMNNode)4 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)3 InputDataNode (org.kie.dmn.api.core.ast.InputDataNode)3 BusinessKnowledgeModelNodeImpl (org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 DMNMessage (org.kie.dmn.api.core.DMNMessage)2 DMNType (org.kie.dmn.api.core.DMNType)2 DecisionServiceNode (org.kie.dmn.api.core.ast.DecisionServiceNode)2 DMNBaseNode (org.kie.dmn.core.ast.DMNBaseNode)2 DecisionServiceNodeImpl (org.kie.dmn.core.ast.DecisionServiceNodeImpl)2