Search in sources :

Example 11 with ContextEntry

use of org.kie.dmn.model.api.ContextEntry in project drools by kiegroup.

the class ValidatorContextTest method testCONTEXT_ENTRY_MISSING_VARIABLE_FileInput.

@Test
public void testCONTEXT_ENTRY_MISSING_VARIABLE_FileInput() {
    final List<DMNMessage> validate = validator.validate(getFile("context/CONTEXT_ENTRY_MISSING_VARIABLE.dmn"), VALIDATE_SCHEMA, VALIDATE_MODEL, VALIDATE_COMPILATION);
    assertThat(ValidatorUtil.formatMessages(validate), validate.size(), is(1));
    assertTrue(validate.stream().anyMatch(p -> p.getMessageType().equals(DMNMessageType.MISSING_VARIABLE)));
    // check that it reports and error for the second context entry, but not for the last one
    final ContextEntry ce = (ContextEntry) validate.get(0).getSourceReference();
    assertThat(((Context) ce.getParent()).getContextEntry().indexOf(ce), is(1));
}
Also used : AbstractValidatorTest(org.kie.dmn.validation.AbstractValidatorTest) CoreMatchers.is(org.hamcrest.CoreMatchers.is) DMNMessage(org.kie.dmn.api.core.DMNMessage) DMNMessageType(org.kie.dmn.api.core.DMNMessageType) ContextEntry(org.kie.dmn.model.api.ContextEntry) VALIDATE_COMPILATION(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_COMPILATION) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Reader(java.io.Reader) ValidatorUtil(org.kie.dmn.validation.ValidatorUtil) List(java.util.List) VALIDATE_SCHEMA(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_SCHEMA) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Context(org.kie.dmn.model.api.Context) VALIDATE_MODEL(org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_MODEL) Context(org.kie.dmn.model.api.Context) DMNMessage(org.kie.dmn.api.core.DMNMessage) ContextEntry(org.kie.dmn.model.api.ContextEntry) AbstractValidatorTest(org.kie.dmn.validation.AbstractValidatorTest) Test(org.junit.Test)

Example 12 with ContextEntry

use of org.kie.dmn.model.api.ContextEntry in project drools by kiegroup.

the class ContextConverter method writeChildren.

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
    super.writeChildren(writer, context, parent);
    Context c = (Context) parent;
    for (ContextEntry ce : c.getContextEntry()) {
        writeChildrenNode(writer, context, ce, CONTEXT_ENTRY);
    }
}
Also used : TContext(org.kie.dmn.model.v1_3.TContext) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Context(org.kie.dmn.model.api.Context) ContextEntry(org.kie.dmn.model.api.ContextEntry)

Example 13 with ContextEntry

use of org.kie.dmn.model.api.ContextEntry in project drools by kiegroup.

the class ContextConverter method writeChildren.

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
    super.writeChildren(writer, context, parent);
    Context c = (Context) parent;
    for (ContextEntry ce : c.getContextEntry()) {
        writeChildrenNode(writer, context, ce, CONTEXT_ENTRY);
    }
}
Also used : TContext(org.kie.dmn.model.v1_2.TContext) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Context(org.kie.dmn.model.api.Context) ContextEntry(org.kie.dmn.model.api.ContextEntry)

Example 14 with ContextEntry

use of org.kie.dmn.model.api.ContextEntry in project drools by kiegroup.

the class DMNEvaluatorCompiler method compileFunctionDefinitionPMML.

private DMNExpressionEvaluator compileFunctionDefinitionPMML(DMNCompilerContext ctx, DMNModelImpl model, DMNBaseNode node, String functionName, FunctionDefinition funcDef) {
    if (funcDef.getExpression() instanceof Context) {
        Context context = (Context) funcDef.getExpression();
        String pmmlDocument = null;
        String pmmlModel = null;
        for (ContextEntry ce : context.getContextEntry()) {
            if (ce.getVariable() != null && ce.getVariable().getName() != null && ce.getExpression() instanceof LiteralExpression) {
                LiteralExpression ceLitExpr = (LiteralExpression) ce.getExpression();
                if (ce.getVariable().getName().equals("document")) {
                    if (ceLitExpr.getText() != null) {
                        pmmlDocument = stripQuotes(ceLitExpr.getText().trim());
                    }
                } else if (ce.getVariable().getName().equals("model")) {
                    if (ceLitExpr.getText() != null) {
                        pmmlModel = stripQuotes(ceLitExpr.getText().trim());
                    }
                }
            }
        }
        final String nameLookup = pmmlDocument;
        Optional<Import> lookupImport = model.getDefinitions().getImport().stream().filter(x -> x.getName().equals(nameLookup)).findFirst();
        if (lookupImport.isPresent()) {
            Import theImport = lookupImport.get();
            logger.trace("theImport: {}", theImport);
            Resource pmmlResource = DMNCompilerImpl.resolveRelativeResource(getRootClassLoader(), model, theImport, funcDef, ctx.getRelativeResolver());
            logger.trace("pmmlResource: {}", pmmlResource);
            DMNImportPMMLInfo pmmlInfo = model.getPmmlImportInfo().get(pmmlDocument);
            logger.trace("pmmlInfo: {}", pmmlInfo);
            if (pmmlModel == null || pmmlModel.isEmpty()) {
                List<String> pmmlModelNames = pmmlInfo.getModels().stream().map(PMMLModelInfo::getName).filter(x -> x != null).collect(Collectors.toList());
                if (pmmlModelNames.size() > 0) {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.WARN, funcDef, model, null, null, Msg.FUNC_DEF_PMML_MISSING_MODEL_NAME, pmmlModelNames.stream().collect(Collectors.joining(",")));
                }
            }
            AbstractPMMLInvocationEvaluator invoker = PMMLInvocationEvaluatorFactory.newInstance(model, getRootClassLoader(), funcDef, pmmlResource, pmmlModel, pmmlInfo);
            DMNFunctionDefinitionEvaluator func = new DMNFunctionDefinitionEvaluator(node, funcDef);
            for (InformationItem p : funcDef.getFormalParameter()) {
                DMNCompilerHelper.checkVariableName(model, p, p.getName());
                DMNType dmnType = compiler.resolveTypeRef(model, p, p, p.getTypeRef());
                func.addParameter(p.getName(), dmnType);
                invoker.addParameter(p.getName(), dmnType);
            }
            func.setEvaluator(invoker);
            return func;
        } else {
            MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_PMML_MISSING_ENTRY, functionName, node.getIdentifierString());
        }
    } else {
        // error, PMML function definitions require a context
        MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, funcDef, model, null, null, Msg.FUNC_DEF_BODY_NOT_CONTEXT, node.getIdentifierString());
    }
    return new DMNFunctionDefinitionEvaluator(node, funcDef);
}
Also used : Context(org.kie.dmn.model.api.Context) PMMLModelInfo(org.kie.dmn.core.pmml.PMMLModelInfo) DMNConditionalEvaluator(org.kie.dmn.core.ast.DMNConditionalEvaluator) DecisionTable(org.kie.dmn.model.api.DecisionTable) DMNMessage(org.kie.dmn.api.core.DMNMessage) Quantified(org.kie.dmn.model.api.Quantified) LoggerFactory(org.slf4j.LoggerFactory) DMNExpressionEvaluator(org.kie.dmn.core.api.DMNExpressionEvaluator) LiteralExpression(org.kie.dmn.model.api.LiteralExpression) DMNElement(org.kie.dmn.model.api.DMNElement) DTDecisionRule(org.kie.dmn.feel.runtime.decisiontables.DTDecisionRule) EvaluatorResult(org.kie.dmn.core.api.EvaluatorResult) DMNIteratorEvaluator(org.kie.dmn.core.ast.DMNIteratorEvaluator) UnaryTest(org.kie.dmn.feel.runtime.UnaryTest) DMNNode(org.kie.dmn.api.core.ast.DMNNode) BaseDMNTypeImpl(org.kie.dmn.core.impl.BaseDMNTypeImpl) OutputClause(org.kie.dmn.model.api.OutputClause) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) Import(org.kie.dmn.model.api.Import) UUID(java.util.UUID) FunctionKind(org.kie.dmn.model.api.FunctionKind) DMNRelationEvaluator(org.kie.dmn.core.ast.DMNRelationEvaluator) Collectors(java.util.stream.Collectors) BusinessKnowledgeModelNode(org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode) HitPolicy(org.kie.dmn.model.api.HitPolicy) Objects(java.util.Objects) Resource(org.kie.api.io.Resource) List(java.util.List) DMNDTExpressionEvaluator(org.kie.dmn.core.ast.DMNDTExpressionEvaluator) Filter(org.kie.dmn.model.api.Filter) CompiledExpression(org.kie.dmn.feel.lang.CompiledExpression) Expression(org.kie.dmn.model.api.Expression) Entry(java.util.Map.Entry) Optional(java.util.Optional) QName(javax.xml.namespace.QName) InformationItem(org.kie.dmn.model.api.InformationItem) Iterator(org.kie.dmn.model.api.Iterator) DMNLiteralExpressionEvaluator(org.kie.dmn.core.ast.DMNLiteralExpressionEvaluator) RootExecutionFrame(org.kie.dmn.feel.lang.impl.RootExecutionFrame) Relation(org.kie.dmn.model.api.Relation) 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) Binding(org.kie.dmn.model.api.Binding) InputClause(org.kie.dmn.model.api.InputClause) 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) DecisionRule(org.kie.dmn.model.api.DecisionRule) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNFilterEvaluator(org.kie.dmn.core.ast.DMNFilterEvaluator) FEELFunction(org.kie.dmn.feel.runtime.FEELFunction) For(org.kie.dmn.model.api.For) DMNBaseNode(org.kie.dmn.core.ast.DMNBaseNode) Decision(org.kie.dmn.model.api.Decision) FunctionDefinition(org.kie.dmn.model.api.FunctionDefinition) DMNInvocationEvaluator(org.kie.dmn.core.ast.DMNInvocationEvaluator) Logger(org.slf4j.Logger) DMNListEvaluator(org.kie.dmn.core.ast.DMNListEvaluator) AbstractPMMLInvocationEvaluator(org.kie.dmn.core.pmml.AbstractPMMLInvocationEvaluator) DTInvokerFunction(org.kie.dmn.feel.runtime.functions.DTInvokerFunction) ContextEntry(org.kie.dmn.model.api.ContextEntry) Invocation(org.kie.dmn.model.api.Invocation) DMNAlphaNetworkEvaluatorCompiler(org.kie.dmn.core.compiler.alphanetbased.DMNAlphaNetworkEvaluatorCompiler) Collectors.toList(java.util.stream.Collectors.toList) PMMLInvocationEvaluatorFactory(org.kie.dmn.core.pmml.AbstractPMMLInvocationEvaluator.PMMLInvocationEvaluatorFactory) DMNImportPMMLInfo(org.kie.dmn.core.pmml.DMNImportPMMLInfo) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) DMNFunctionDefinitionEvaluator(org.kie.dmn.core.ast.DMNFunctionDefinitionEvaluator) UnaryTests(org.kie.dmn.model.api.UnaryTests) CompositeTypeImpl(org.kie.dmn.core.impl.CompositeTypeImpl) Conditional(org.kie.dmn.model.api.Conditional) Msg(org.kie.dmn.core.util.Msg) DecisionTableImpl(org.kie.dmn.feel.runtime.decisiontables.DecisionTableImpl) Collections(java.util.Collections) Context(org.kie.dmn.model.api.Context) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) Import(org.kie.dmn.model.api.Import) LiteralExpression(org.kie.dmn.model.api.LiteralExpression) Resource(org.kie.api.io.Resource) InformationItem(org.kie.dmn.model.api.InformationItem) ContextEntry(org.kie.dmn.model.api.ContextEntry) AbstractPMMLInvocationEvaluator(org.kie.dmn.core.pmml.AbstractPMMLInvocationEvaluator) PMMLModelInfo(org.kie.dmn.core.pmml.PMMLModelInfo) DMNFunctionDefinitionEvaluator(org.kie.dmn.core.ast.DMNFunctionDefinitionEvaluator) DMNImportPMMLInfo(org.kie.dmn.core.pmml.DMNImportPMMLInfo) DMNType(org.kie.dmn.api.core.DMNType)

Example 15 with ContextEntry

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

Aggregations

ContextEntry (org.kie.dmn.model.api.ContextEntry)18 Context (org.kie.dmn.model.api.Context)13 List (java.util.List)8 DMNMessage (org.kie.dmn.api.core.DMNMessage)8 IOException (java.io.IOException)6 Reader (java.io.Reader)6 CoreMatchers.is (org.hamcrest.CoreMatchers.is)6 Test (org.junit.Test)6 DMNMessageType (org.kie.dmn.api.core.DMNMessageType)6 VALIDATE_COMPILATION (org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_COMPILATION)6 VALIDATE_MODEL (org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_MODEL)6 VALIDATE_SCHEMA (org.kie.dmn.validation.DMNValidator.Validation.VALIDATE_SCHEMA)6 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)4 QName (javax.xml.namespace.QName)3 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)3 Assert (org.junit.Assert)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 DMNType (org.kie.dmn.api.core.DMNType)3 BusinessKnowledgeModelNode (org.kie.dmn.api.core.ast.BusinessKnowledgeModelNode)3 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)3