Search in sources :

Example 1 with DMNType

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

the class BusinessKnowledgeModelCompiler method compileNode.

@Override
public void compileNode(DRGElement de, DMNCompilerImpl compiler, DMNModelImpl model) {
    BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) de;
    BusinessKnowledgeModelNodeImpl bkmn = new BusinessKnowledgeModelNodeImpl(bkm);
    DMNType type = null;
    if (bkm.getVariable() == null) {
        DMNCompilerHelper.reportMissingVariable(model, de, bkm, Msg.MISSING_VARIABLE_FOR_BKM);
        return;
    }
    DMNCompilerHelper.checkVariableName(model, bkm, bkm.getName());
    if (bkm.getVariable() != null && bkm.getVariable().getTypeRef() != null) {
        type = compiler.resolveTypeRef(model, bkmn, bkm, bkm.getVariable(), bkm.getVariable().getTypeRef());
    } else {
        // for now the call bellow will return type UNKNOWN
        type = compiler.resolveTypeRef(model, bkmn, bkm, bkm, null);
    }
    bkmn.setResultType(type);
    model.addBusinessKnowledgeModel(bkmn);
}
Also used : BusinessKnowledgeModel(org.kie.dmn.model.v1_1.BusinessKnowledgeModel) BusinessKnowledgeModelNodeImpl(org.kie.dmn.core.ast.BusinessKnowledgeModelNodeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 2 with DMNType

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

the class DMNCompilerImpl method buildTypeDef.

private DMNType buildTypeDef(DMNCompilerContext ctx, DMNFEELHelper feel, DMNModelImpl dmnModel, DMNNode node, ItemDefinition itemDef, boolean topLevel) {
    BaseDMNTypeImpl type = null;
    if (itemDef.getTypeRef() != null) {
        // this is a reference to an existing type, so resolve the reference
        type = (BaseDMNTypeImpl) resolveTypeRef(dmnModel, node, itemDef, itemDef, itemDef.getTypeRef());
        if (type != null) {
            UnaryTests allowedValuesStr = itemDef.getAllowedValues();
            // or if it changes the metadata for the base type
            if (topLevel || allowedValuesStr != null || itemDef.isIsCollection() != type.isCollection()) {
                // we have to clone this type definition into a new one
                BaseDMNTypeImpl baseType = type;
                type = type.clone();
                type.setBaseType(baseType);
                type.setNamespace(dmnModel.getNamespace());
                type.setName(itemDef.getName());
                type.setAllowedValues(null);
                if (allowedValuesStr != null) {
                    List<UnaryTest> av = feel.evaluateUnaryTests(ctx, allowedValuesStr.getText(), dmnModel, itemDef, Msg.ERR_COMPILING_ALLOWED_VALUES_LIST_ON_ITEM_DEF, allowedValuesStr.getText(), node.getName());
                    type.setAllowedValues(av);
                }
                if (itemDef.isIsCollection()) {
                    type.setCollection(itemDef.isIsCollection());
                }
            }
            if (topLevel) {
                DMNType registered = dmnModel.getTypeRegistry().registerType(type);
                if (registered != type) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, itemDef, dmnModel, null, null, Msg.DUPLICATED_ITEM_DEFINITION, itemDef.getName());
                }
            }
        }
    } else {
        // this is a composite type
        DMNCompilerHelper.checkVariableName(dmnModel, itemDef, itemDef.getName());
        CompositeTypeImpl compType = new CompositeTypeImpl(dmnModel.getNamespace(), itemDef.getName(), itemDef.getId(), itemDef.isIsCollection());
        for (ItemDefinition fieldDef : itemDef.getItemComponent()) {
            DMNCompilerHelper.checkVariableName(dmnModel, fieldDef, fieldDef.getName());
            DMNType fieldType = buildTypeDef(ctx, feel, dmnModel, node, fieldDef, false);
            fieldType = fieldType != null ? fieldType : DMNTypeRegistry.UNKNOWN;
            compType.addField(fieldDef.getName(), fieldType);
        }
        type = compType;
        if (topLevel) {
            DMNType registered = dmnModel.getTypeRegistry().registerType(type);
            if (registered != type) {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, itemDef, dmnModel, null, null, Msg.DUPLICATED_ITEM_DEFINITION, itemDef.getName());
            }
        }
    }
    return type;
}
Also used : ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) BaseDMNTypeImpl(org.kie.dmn.core.impl.BaseDMNTypeImpl) UnaryTests(org.kie.dmn.model.v1_1.UnaryTests) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 3 with DMNType

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

the class DMNFEELHelper method compileFeelExpression.

public CompiledExpression compileFeelExpression(DMNCompilerContext ctx, String expression, DMNModelImpl model, DMNElement element, Msg.Message errorMsg, Object... msgParams) {
    CompilerContext feelctx = feel.newCompilerContext();
    for (Map.Entry<String, DMNType> entry : ctx.getVariables().entrySet()) {
        feelctx.addInputVariableType(entry.getKey(), ((BaseDMNTypeImpl) entry.getValue()).getFeelType());
    }
    CompiledExpression ce = feel.compile(expression, feelctx);
    processEvents(model, element, errorMsg, msgParams);
    return ce;
}
Also used : CompilerContext(org.kie.dmn.feel.lang.CompilerContext) HashMap(java.util.HashMap) Map(java.util.Map) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) DMNType(org.kie.dmn.api.core.DMNType)

Example 4 with DMNType

use of org.kie.dmn.api.core.DMNType 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 {
        Map<String, DMNType> importedTypes = new HashMap<>();
        for (DMNNode dep : di.getDependencies().values()) {
            if (dep instanceof DecisionNode) {
                ctx.setVariable(dep.getName(), ((DecisionNode) dep).getResultType());
            } else if (dep instanceof InputDataNode) {
                ctx.setVariable(dep.getName(), ((InputDataNode) dep).getType());
            } else if (dep instanceof BusinessKnowledgeModelNode) {
                if (dep.getModelNamespace().equals(model.getNamespace())) {
                    // might need to create a DMNType for "functions" and replace the type here by that
                    ctx.setVariable(dep.getName(), ((BusinessKnowledgeModelNode) dep).getResultType());
                } else {
                    // then the BKM dependency is an imported BKM.
                    Optional<String> alias = model.getImportAliasFor(dep.getModelNamespace(), dep.getModelName());
                    if (alias.isPresent()) {
                        CompositeTypeImpl importedComposite = (CompositeTypeImpl) importedTypes.computeIfAbsent(alias.get(), a -> new CompositeTypeImpl());
                        importedComposite.addField(dep.getName(), ((BusinessKnowledgeModelNode) dep).getResultType());
                    }
                }
            }
        }
        for (Entry<String, DMNType> importedType : importedTypes.entrySet()) {
            ctx.setVariable(importedType.getKey(), importedType.getValue());
        }
        DMNExpressionEvaluator evaluator = compiler.getEvaluatorCompiler().compileExpression(ctx, model, di, di.getName(), di.getDecision().getExpression());
        di.setEvaluator(evaluator);
    } finally {
        ctx.exitFrame();
    }
}
Also used : DMNType(org.kie.dmn.api.core.DMNType) DMNNode(org.kie.dmn.api.core.ast.DMNNode) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) HashMap(java.util.HashMap) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) Decision(org.kie.dmn.model.v1_1.Decision) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) Map(java.util.Map) DRGElement(org.kie.dmn.model.v1_1.DRGElement) Entry(java.util.Map.Entry) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) Msg(org.kie.dmn.core.util.Msg) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) HashMap(java.util.HashMap) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) DMNNode(org.kie.dmn.api.core.ast.DMNNode) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 5 with DMNType

use of org.kie.dmn.api.core.DMNType 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, dn, decision, decision.getVariable(), decision.getVariable().getTypeRef());
    } else {
        type = compiler.resolveTypeRef(model, dn, decision, decision, null);
    }
    dn.setResultType(type);
    model.addDecision(dn);
}
Also used : DecisionNodeImpl(org.kie.dmn.core.ast.DecisionNodeImpl) Decision(org.kie.dmn.model.v1_1.Decision) DMNType(org.kie.dmn.api.core.DMNType)

Aggregations

DMNType (org.kie.dmn.api.core.DMNType)14 CompositeTypeImpl (org.kie.dmn.core.impl.CompositeTypeImpl)5 Test (org.junit.Test)4 Decision (org.kie.dmn.model.v1_1.Decision)4 Map (java.util.Map)3 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)3 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)3 DMNExpressionEvaluator (org.kie.dmn.core.api.DMNExpressionEvaluator)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 QName (javax.xml.namespace.QName)2 DMNModel (org.kie.dmn.api.core.DMNModel)2 DMNRuntime (org.kie.dmn.api.core.DMNRuntime)2 ItemDefNode (org.kie.dmn.api.core.ast.ItemDefNode)2 DMNContextEvaluator (org.kie.dmn.core.ast.DMNContextEvaluator)2 DecisionNodeImpl (org.kie.dmn.core.ast.DecisionNodeImpl)2 BaseDMNTypeImpl (org.kie.dmn.core.impl.BaseDMNTypeImpl)2 DMNModelImpl (org.kie.dmn.core.impl.DMNModelImpl)2 SimpleTypeImpl (org.kie.dmn.core.impl.SimpleTypeImpl)2