Search in sources :

Example 6 with DMNType

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

the class DMNTypeTest method testDROOLS2147.

@Test
public void testDROOLS2147() {
    // DROOLS-2147
    final String testNS = "testDROOLS2147";
    Map<String, DMNType> personPrototype = prototype(entry("name", FEEL_STRING), entry("age", FEEL_NUMBER));
    DMNType dmnPerson = typeRegistry.registerType(new CompositeTypeImpl(testNS, "person", null, false, personPrototype, null, null));
    DMNType dmnPersonList = typeRegistry.registerType(new CompositeTypeImpl(testNS, "personList", null, true, null, dmnPerson, null));
    DMNType dmnListOfPersonsGrouped = typeRegistry.registerType(new CompositeTypeImpl(testNS, "groups", null, true, null, dmnPersonList, null));
    Map<String, Object> instanceBob = prototype(entry("name", "Bob"), entry("age", 42));
    Map<String, Object> instanceJohn = prototype(entry("name", "John"), entry("age", 47));
    Map<String, Object> instanceNOTaPerson = prototype(entry("name", "NOTAPERSON"));
    assertTrue(dmnPerson.isAssignableValue(instanceBob));
    assertTrue(dmnPerson.isAssignableValue(instanceJohn));
    assertFalse(dmnPerson.isAssignableValue(instanceNOTaPerson));
    List<Map<String, Object>> onlyBob = Arrays.asList(instanceBob);
    List<Map<String, Object>> bobANDjohn = Arrays.asList(instanceBob, instanceJohn);
    List<Map<String, Object>> johnANDnotAPerson = Arrays.asList(instanceJohn, instanceNOTaPerson);
    assertTrue(dmnPersonList.isAssignableValue(onlyBob));
    assertTrue(dmnPersonList.isAssignableValue(bobANDjohn));
    assertFalse(dmnPersonList.isAssignableValue(johnANDnotAPerson));
    // because accordingly to FEEL spec, bob=[bob]
    assertTrue(dmnPersonList.isAssignableValue(instanceBob));
    List<List<Map<String, Object>>> the2ListsThatContainBob = Arrays.asList(onlyBob, bobANDjohn);
    assertTrue(dmnListOfPersonsGrouped.isAssignableValue(the2ListsThatContainBob));
    List<List<Map<String, Object>>> the3Lists = Arrays.asList(onlyBob, bobANDjohn, johnANDnotAPerson);
    assertFalse(dmnListOfPersonsGrouped.isAssignableValue(the3Lists));
    List<Object> groupsOfBobAndBobHimself = Arrays.asList(instanceBob, onlyBob, bobANDjohn);
    // [bob, [bob], [bob, john]] because for the property of FEEL spec a=[a] is equivalent to [[bob], [bob], [bob, john]]
    assertTrue(dmnListOfPersonsGrouped.isAssignableValue(groupsOfBobAndBobHimself));
    DMNType listOfGroups = typeRegistry.registerType(new CompositeTypeImpl(testNS, "listOfGroups", null, true, null, dmnListOfPersonsGrouped, null));
    List<Object> groupsContainingBobPartitionedBySize = Arrays.asList(the2ListsThatContainBob, Arrays.asList(bobANDjohn));
    // [ [[B], [B, J]], [[B, J]] ]
    assertTrue(listOfGroups.isAssignableValue(groupsContainingBobPartitionedBySize));
}
Also used : List(java.util.List) Map(java.util.Map) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DMNType(org.kie.dmn.api.core.DMNType) Test(org.junit.Test)

Example 7 with DMNType

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

the class DMNCompilerImpl method processItemDefinitions.

private void processItemDefinitions(DMNCompilerContext ctx, DMNFEELHelper feel, DMNModelImpl model, Definitions dmndefs) {
    Definitions.normalize(dmndefs);
    List<ItemDefinition> ordered = new ItemDefinitionDependenciesSorter(model.getNamespace()).sort(dmndefs.getItemDefinition());
    for (ItemDefinition id : ordered) {
        ItemDefNodeImpl idn = new ItemDefNodeImpl(id);
        DMNType type = buildTypeDef(ctx, feel, model, idn, id, true);
        idn.setType(type);
        model.addItemDefinition(idn);
    }
}
Also used : ItemDefinition(org.kie.dmn.model.v1_1.ItemDefinition) ItemDefNodeImpl(org.kie.dmn.core.ast.ItemDefNodeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 8 with DMNType

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

the class DMNCompilerImpl method resolveTypeRef.

public DMNType resolveTypeRef(DMNModelImpl dmnModel, DMNNode node, NamedElement model, DMNModelInstrumentedBase localElement, QName typeRef) {
    if (typeRef != null) {
        QName nsAndName = getNamespaceAndName(localElement, dmnModel.getImportAliasesForNS(), typeRef);
        DMNType type = dmnModel.getTypeRegistry().resolveType(nsAndName.getNamespaceURI(), nsAndName.getLocalPart());
        if (type == null && DMNModelInstrumentedBase.URI_FEEL.equals(nsAndName.getNamespaceURI())) {
            if (model instanceof Decision && ((Decision) model).getExpression() instanceof DecisionTable) {
                DecisionTable dt = (DecisionTable) ((Decision) model).getExpression();
                if (dt.getOutput().size() > 1) {
                    // implicitly define a type for the decision table result
                    CompositeTypeImpl compType = new CompositeTypeImpl(dmnModel.getNamespace(), model.getName() + "_Type", model.getId(), dt.getHitPolicy().isMultiHit());
                    for (OutputClause oc : dt.getOutput()) {
                        DMNType fieldType = resolveTypeRef(dmnModel, node, model, oc, oc.getTypeRef());
                        compType.addField(oc.getName(), fieldType);
                    }
                    dmnModel.getTypeRegistry().registerType(compType);
                    return compType;
                } else if (dt.getOutput().size() == 1) {
                    return resolveTypeRef(dmnModel, node, model, dt.getOutput().get(0), dt.getOutput().get(0).getTypeRef());
                }
            }
        } else if (type == null) {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, localElement, dmnModel, null, null, Msg.UNKNOWN_TYPE_REF_ON_NODE, typeRef.toString(), localElement.getParentDRDElement().getIdentifierString());
        }
        return type;
    }
    return dmnModel.getTypeRegistry().resolveType(DMNModelInstrumentedBase.URI_FEEL, BuiltInType.UNKNOWN.getName());
}
Also used : OutputClause(org.kie.dmn.model.v1_1.OutputClause) DecisionTable(org.kie.dmn.model.v1_1.DecisionTable) QName(javax.xml.namespace.QName) Decision(org.kie.dmn.model.v1_1.Decision) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) DMNType(org.kie.dmn.api.core.DMNType)

Example 9 with DMNType

use of org.kie.dmn.api.core.DMNType 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)

Example 10 with DMNType

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

the class DMNEvaluatorCompiler method compileFunctionDefinition.

private DMNExpressionEvaluator compileFunctionDefinition(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String functionName, FunctionDefinition expression) {
    FunctionDefinition funcDef = expression;
    String kindStr = funcDef.getAdditionalAttributes().get(FunctionDefinition.KIND_QNAME);
    FunctionDefinition.Kind kind = kindStr != null ? FunctionDefinition.Kind.determineFromString(kindStr) : FunctionDefinition.Kind.FEEL;
    if (kind == null) {
        // unknown function kind
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_INVALID_KIND, kindStr, node.getIdentifierString());
        return new DMNFunctionDefinitionEvaluator(node.getName(), funcDef);
    } else if (kind.equals(FunctionDefinition.Kind.FEEL)) {
        ctx.enterFrame();
        try {
            DMNFunctionDefinitionEvaluator func = new DMNFunctionDefinitionEvaluator(node.getName(), funcDef);
            for (InformationItem p : funcDef.getFormalParameter()) {
                DMNCompilerHelper.checkVariableName(model, p, p.getName());
                DMNType dmnType = compiler.resolveTypeRef(model, node, p, p, p.getTypeRef());
                func.addParameter(p.getName(), dmnType);
                ctx.setVariable(p.getName(), dmnType);
            }
            DMNExpressionEvaluator eval = compileExpression(ctx, model, node, functionName, funcDef.getExpression());
            if (eval instanceof DMNLiteralExpressionEvaluator && ((DMNLiteralExpressionEvaluator) eval).isFunctionDefinition()) {
                // we need to resolve the function and eliminate the indirection
                CompiledExpression fexpr = ((DMNLiteralExpressionEvaluator) eval).getExpression();
                FEELFunction feelFunction = feel.evaluateFunctionDef(ctx, fexpr, model, funcDef, Msg.FUNC_DEF_COMPILATION_ERR, functionName, node.getIdentifierString());
                DMNInvocationEvaluator invoker = new DMNInvocationEvaluator(node.getName(), node.getSource(), functionName, new Invocation(), (fctx, fname) -> feelFunction, // feel can be null as anyway is hardcoded to `feelFunction`
                null);
                for (InformationItem p : funcDef.getFormalParameter()) {
                    invoker.addParameter(p.getName(), func.getParameterType(p.getName()), (em, dr) -> new EvaluatorResultImpl(dr.getContext().get(p.getName()), EvaluatorResult.ResultType.SUCCESS));
                }
                eval = invoker;
            }
            func.setEvaluator(eval);
            return func;
        } finally {
            ctx.exitFrame();
        }
    } else if (kind.equals(FunctionDefinition.Kind.JAVA)) {
        if (funcDef.getExpression() instanceof Context) {
            // proceed
            Context context = (Context) funcDef.getExpression();
            String clazz = null;
            String method = null;
            for (ContextEntry ce : context.getContextEntry()) {
                if (ce.getVariable() != null && ce.getVariable().getName() != null && ce.getExpression() != null && ce.getExpression() instanceof LiteralExpression) {
                    if (ce.getVariable().getName().equals("class")) {
                        clazz = stripQuotes(((LiteralExpression) ce.getExpression()).getText().trim());
                    } else if (ce.getVariable().getName().equals("method signature")) {
                        method = stripQuotes(((LiteralExpression) ce.getExpression()).getText().trim());
                    }
                }
            }
            if (clazz != null && method != null) {
                String params = funcDef.getFormalParameter().stream().map(p -> p.getName()).collect(Collectors.joining(","));
                String expr = String.format("function(%s) external { java: { class: \"%s\", method signature: \"%s\" }}", params, clazz, method);
                try {
                    FEELFunction feelFunction = feel.evaluateFunctionDef(ctx, expr, model, funcDef, Msg.FUNC_DEF_COMPILATION_ERR, functionName, node.getIdentifierString());
                    if (feelFunction != null) {
                        ((BaseFEELFunction) feelFunction).setName(functionName);
                    }
                    DMNInvocationEvaluator invoker = new DMNInvocationEvaluator(node.getName(), node.getSource(), functionName, new Invocation(), (fctx, fname) -> feelFunction, // feel can be null as anyway is hardcoded to `feelFunction`
                    null);
                    DMNFunctionDefinitionEvaluator func = new DMNFunctionDefinitionEvaluator(node.getName(), funcDef);
                    for (InformationItem p : funcDef.getFormalParameter()) {
                        DMNCompilerHelper.checkVariableName(model, p, p.getName());
                        DMNType dmnType = compiler.resolveTypeRef(model, node, p, p, p.getTypeRef());
                        func.addParameter(p.getName(), dmnType);
                        invoker.addParameter(p.getName(), dmnType, (em, dr) -> new EvaluatorResultImpl(dr.getContext().get(p.getName()), EvaluatorResult.ResultType.SUCCESS));
                    }
                    func.setEvaluator(invoker);
                    return func;
                } catch (Throwable e) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, expression, model, e, null, Msg.FUNC_DEF_COMPILATION_ERR, functionName, node.getIdentifierString(), "Exception raised: " + e.getClass().getSimpleName());
                }
            } else {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, expression, model, null, null, Msg.FUNC_DEF_MISSING_ENTRY, functionName, node.getIdentifierString());
            }
        } else {
            // error, java function definitions require a context
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_BODY_NOT_CONTEXT, node.getIdentifierString());
        }
    } else if (kind.equals(FunctionDefinition.Kind.PMML)) {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, funcDef, model, null, null, Msg.FUNC_DEF_PMML_NOT_SUPPORTED, node.getIdentifierString());
    } else {
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_INVALID_KIND, kindStr, node.getIdentifierString());
    }
    return new DMNFunctionDefinitionEvaluator(node.getName(), funcDef);
}
Also used : InformationItem(org.kie.dmn.model.v1_1.InformationItem) DMNMessage(org.kie.dmn.api.core.DMNMessage) ContextEntry(org.kie.dmn.model.v1_1.ContextEntry) OutputClause(org.kie.dmn.model.v1_1.OutputClause) FunctionDefinition(org.kie.dmn.model.v1_1.FunctionDefinition) LoggerFactory(org.slf4j.LoggerFactory) LiteralExpression(org.kie.dmn.model.v1_1.LiteralExpression) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) Binding(org.kie.dmn.model.v1_1.Binding) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) DMNModelInstrumentedBase(org.kie.dmn.model.v1_1.DMNModelInstrumentedBase) BaseDMNTypeImpl(org.kie.dmn.core.impl.BaseDMNTypeImpl) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) DecisionTable(org.kie.dmn.model.v1_1.DecisionTable) DMNRelationEvaluator(org.kie.dmn.core.ast.DMNRelationEvaluator) Collectors(java.util.stream.Collectors) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) Context(org.kie.dmn.model.v1_1.Context) List(java.util.List) DMNDTExpressionEvaluator(org.kie.dmn.core.ast.DMNDTExpressionEvaluator) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Invocation(org.kie.dmn.model.v1_1.Invocation) Optional(java.util.Optional) QName(javax.xml.namespace.QName) DMNLiteralExpressionEvaluator(org.kie.dmn.core.ast.DMNLiteralExpressionEvaluator) BusinessKnowledgeModel(org.kie.dmn.model.v1_1.BusinessKnowledgeModel) DMNElement(org.kie.dmn.model.v1_1.DMNElement) FEEL(org.kie.dmn.feel.FEEL) MsgUtil(org.kie.dmn.core.util.MsgUtil) DMNType(org.kie.dmn.api.core.DMNType) DMNContextEvaluator(org.kie.dmn.core.ast.DMNContextEvaluator) DTOutputClause(org.kie.dmn.feel.runtime.decisiontables.DTOutputClause) EvaluatorResultImpl(org.kie.dmn.core.ast.EvaluatorResultImpl) DTInputClause(org.kie.dmn.feel.runtime.decisiontables.DTInputClause) ArrayList(java.util.ArrayList) HitPolicy(org.kie.dmn.model.v1_1.HitPolicy) Relation(org.kie.dmn.model.v1_1.Relation) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) DecisionRule(org.kie.dmn.model.v1_1.DecisionRule) UnaryTests(org.kie.dmn.model.v1_1.UnaryTests) Expression(org.kie.dmn.model.v1_1.Expression) DMNInvocationEvaluator(org.kie.dmn.core.ast.DMNInvocationEvaluator) Logger(org.slf4j.Logger) DMNListEvaluator(org.kie.dmn.core.ast.DMNListEvaluator) DTInvokerFunction(org.kie.dmn.feel.runtime.functions.DTInvokerFunction) Decision(org.kie.dmn.model.v1_1.Decision) Collectors.toList(java.util.stream.Collectors.toList) DMNFunctionDefinitionEvaluator(org.kie.dmn.core.ast.DMNFunctionDefinitionEvaluator) InputClause(org.kie.dmn.model.v1_1.InputClause) Msg(org.kie.dmn.core.util.Msg) DecisionTableImpl(org.kie.dmn.feel.runtime.decisiontables.DecisionTableImpl) Collections(java.util.Collections) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) Context(org.kie.dmn.model.v1_1.Context) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) Invocation(org.kie.dmn.model.v1_1.Invocation) DMNLiteralExpressionEvaluator(org.kie.dmn.core.ast.DMNLiteralExpressionEvaluator) LiteralExpression(org.kie.dmn.model.v1_1.LiteralExpression) InformationItem(org.kie.dmn.model.v1_1.InformationItem) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) ContextEntry(org.kie.dmn.model.v1_1.ContextEntry) DMNFunctionDefinitionEvaluator(org.kie.dmn.core.ast.DMNFunctionDefinitionEvaluator) FunctionDefinition(org.kie.dmn.model.v1_1.FunctionDefinition) EvaluatorResultImpl(org.kie.dmn.core.ast.EvaluatorResultImpl) DMNInvocationEvaluator(org.kie.dmn.core.ast.DMNInvocationEvaluator) 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