Search in sources :

Example 26 with Import

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

the class ImportDMNResolverUtilTest method testNSandModelNameWithAlias.

@Test
public void testNSandModelNameWithAlias() {
    final Import i = makeImport("ns1", "aliased", "m1");
    final List<QName> available = Arrays.asList(new QName("ns1", "m1"), new QName("ns2", "m2"), new QName("ns3", "m3"));
    final Either<String, QName> result = ImportDMNResolverUtil.resolveImportDMN(i, available, Function.identity());
    assertTrue(result.isRight());
    assertEquals(new QName("ns1", "m1"), result.getOrElse(null));
}
Also used : TImport(org.kie.dmn.model.v1_1.TImport) Import(org.kie.dmn.model.api.Import) QName(javax.xml.namespace.QName) Test(org.junit.Test)

Example 27 with Import

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

the class DMNCompilerImpl method compile.

public DMNModel compile(Definitions dmndefs, Collection<DMNModel> dmnModels, Resource resource, Function<String, Reader> relativeResolver) {
    if (dmndefs == null) {
        return null;
    }
    DMNModelImpl model = new DMNModelImpl(dmndefs, resource);
    model.setRuntimeTypeCheck(((DMNCompilerConfigurationImpl) dmnCompilerConfig).getOption(RuntimeTypeCheckOption.class).isRuntimeTypeCheck());
    DMNCompilerConfigurationImpl cc = (DMNCompilerConfigurationImpl) dmnCompilerConfig;
    List<FEELProfile> helperFEELProfiles = cc.getFeelProfiles();
    DMNFEELHelper feel = new DMNFEELHelper(cc.getRootClassLoader(), helperFEELProfiles);
    DMNCompilerContext ctx = new DMNCompilerContext(feel);
    ctx.setRelativeResolver(relativeResolver);
    if (!dmndefs.getImport().isEmpty()) {
        for (Import i : dmndefs.getImport()) {
            if (ImportDMNResolverUtil.whichImportType(i) == ImportType.DMN) {
                Either<String, DMNModel> resolvedResult = ImportDMNResolverUtil.resolveImportDMN(i, dmnModels, (DMNModel m) -> new QName(m.getNamespace(), m.getName()));
                DMNModel located = resolvedResult.cata(msg -> {
                    MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, i, model, null, null, Msg.IMPORT_NOT_FOUND_FOR_NODE, msg, i);
                    return null;
                }, Function.identity());
                if (located != null) {
                    String iAlias = Optional.ofNullable(i.getName()).orElse(located.getName());
                    model.setImportAliasForNS(iAlias, located.getNamespace(), located.getName());
                    importFromModel(model, located, iAlias);
                }
            } else if (ImportDMNResolverUtil.whichImportType(i) == ImportType.PMML) {
                processPMMLImport(model, i, relativeResolver);
                model.setImportAliasForNS(i.getName(), i.getNamespace(), i.getName());
            } else {
                MsgUtil.reportMessage(logger, DMNMessage.Severity.ERROR, null, model, null, null, Msg.IMPORT_TYPE_UNKNOWN, i.getImportType());
            }
        }
    }
    processItemDefinitions(ctx, model, dmndefs);
    processDrgElements(ctx, model, dmndefs);
    return model;
}
Also used : Import(org.kie.dmn.model.api.Import) FEELProfile(org.kie.dmn.feel.lang.FEELProfile) QName(javax.xml.namespace.QName) DMNModelImpl(org.kie.dmn.core.impl.DMNModelImpl) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 28 with Import

use of org.kie.dmn.model.api.Import 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 29 with Import

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

the class DMNAssemblerService method enrichDMNResourcesWithImportsDependencies.

public static void enrichDMNResourcesWithImportsDependencies(List<DMNResource> dmnResources, Collection<DMNModel> dmnModels) {
    for (DMNResource r : dmnResources) {
        for (Import i : r.getDefinitions().getImport()) {
            if (ImportDMNResolverUtil.whichImportType(i) == ImportType.DMN) {
                Either<String, DMNModel> inAlreadyCompiled = ImportDMNResolverUtil.resolveImportDMN(i, dmnModels, x -> new QName(x.getNamespace(), x.getName()));
                if (inAlreadyCompiled.isLeft()) {
                    // the DMN Model is not already available in the KieBuilder and needs to be compiled.
                    Either<String, DMNResource> resolvedResult = ImportDMNResolverUtil.resolveImportDMN(i, dmnResources, DMNResource::getModelID);
                    DMNResource located = resolvedResult.getOrElseThrow(RuntimeException::new);
                    r.addDependency(located.getModelID());
                } else {
                // do nothing: the DMN Model is already available in the KieBuilder.
                }
            }
        }
    }
}
Also used : Import(org.kie.dmn.model.api.Import) QName(javax.xml.namespace.QName) DMNModel(org.kie.dmn.api.core.DMNModel)

Example 30 with Import

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

the class DefinitionsConverter method writeChildren.

@Override
protected void writeChildren(HierarchicalStreamWriter writer, MarshallingContext context, Object parent) {
    super.writeChildren(writer, context, parent);
    Definitions def = (Definitions) parent;
    for (Import i : def.getImport()) {
        writeChildrenNode(writer, context, i, IMPORT);
    }
    for (ItemDefinition id : def.getItemDefinition()) {
        writeChildrenNode(writer, context, id, ITEM_DEFINITION);
    }
    for (DRGElement e : def.getDrgElement()) {
        String nodeName = DRG_ELEMENT;
        if (e instanceof BusinessKnowledgeModel) {
            nodeName = "businessKnowledgeModel";
        } else if (e instanceof Decision) {
            nodeName = "decision";
        } else if (e instanceof InputData) {
            nodeName = "inputData";
        } else if (e instanceof KnowledgeSource) {
            nodeName = "knowledgeSource";
        }
        writeChildrenNode(writer, context, e, nodeName);
    }
    for (Artifact a : def.getArtifact()) {
        String nodeName = ARTIFACT;
        if (a instanceof Association) {
            nodeName = "association";
        } else if (a instanceof TextAnnotation) {
            nodeName = "textAnnotation";
        }
        writeChildrenNode(writer, context, a, nodeName);
    }
    for (ElementCollection ec : def.getElementCollection()) {
        writeChildrenNode(writer, context, ec, ELEMENT_COLLECTION);
    }
    for (BusinessContextElement bce : def.getBusinessContextElement()) {
        String nodeName = BUSINESS_CONTEXT_ELEMENT;
        if (bce instanceof OrganizationUnit) {
            nodeName = "organizationUnit";
        } else if (bce instanceof PerformanceIndicator) {
            nodeName = "performanceIndicator";
        }
        writeChildrenNode(writer, context, bce, nodeName);
    }
}
Also used : Import(org.kie.dmn.model.api.Import) PerformanceIndicator(org.kie.dmn.model.api.PerformanceIndicator) TDefinitions(org.kie.dmn.model.v1_1.TDefinitions) Definitions(org.kie.dmn.model.api.Definitions) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) BusinessContextElement(org.kie.dmn.model.api.BusinessContextElement) Decision(org.kie.dmn.model.api.Decision) Artifact(org.kie.dmn.model.api.Artifact) Association(org.kie.dmn.model.api.Association) KnowledgeSource(org.kie.dmn.model.api.KnowledgeSource) OrganizationUnit(org.kie.dmn.model.api.OrganizationUnit) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) ElementCollection(org.kie.dmn.model.api.ElementCollection) InputData(org.kie.dmn.model.api.InputData) TextAnnotation(org.kie.dmn.model.api.TextAnnotation) DRGElement(org.kie.dmn.model.api.DRGElement)

Aggregations

Import (org.kie.dmn.model.api.Import)42 Test (org.junit.Test)22 QName (javax.xml.namespace.QName)19 TImport (org.kie.dmn.model.v1_1.TImport)15 Definitions (org.kie.dmn.model.api.Definitions)13 HashMap (java.util.HashMap)7 DRGElement (org.kie.dmn.model.api.DRGElement)7 ItemDefinition (org.kie.dmn.model.api.ItemDefinition)6 ArrayList (java.util.ArrayList)5 BusinessKnowledgeModel (org.kie.dmn.model.api.BusinessKnowledgeModel)5 Decision (org.kie.dmn.model.api.Decision)5 PMMLDocumentMetadata (org.kie.workbench.common.dmn.api.editors.included.PMMLDocumentMetadata)5 Artifact (org.kie.dmn.model.api.Artifact)4 Association (org.kie.dmn.model.api.Association)4 BusinessContextElement (org.kie.dmn.model.api.BusinessContextElement)4 ElementCollection (org.kie.dmn.model.api.ElementCollection)4 InputData (org.kie.dmn.model.api.InputData)4 KnowledgeSource (org.kie.dmn.model.api.KnowledgeSource)4 OrganizationUnit (org.kie.dmn.model.api.OrganizationUnit)4 PerformanceIndicator (org.kie.dmn.model.api.PerformanceIndicator)4