Search in sources :

Example 1 with ResourceWithConfiguration

use of org.kie.api.io.ResourceWithConfiguration in project drools by kiegroup.

the class DMNAssemblerService method addResources.

@Override
public void addResources(Object kbuilder, Collection<ResourceWithConfiguration> resources, ResourceType type) throws Exception {
    KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
    DMNCompilerImpl dmnCompiler = (DMNCompilerImpl) kbuilderImpl.getCachedOrCreate(DMN_COMPILER_CACHE_KEY, () -> getCompiler(kbuilderImpl));
    DMNMarshaller dmnMarshaller = dmnCompiler.getMarshaller();
    if (resources.size() == 1) {
        // quick path:
        internalAddResource(kbuilderImpl, dmnCompiler, resources.iterator().next(), Collections.emptyList());
        return;
    }
    List<DMNResource> dmnResources = new ArrayList<>();
    for (ResourceWithConfiguration r : resources) {
        Definitions definitions = dmnMarshaller.unmarshal(r.getResource().getReader());
        QName modelID = new QName(definitions.getNamespace(), definitions.getName());
        DMNResource dmnResource = new DMNResource(modelID, r, definitions);
        dmnResources.add(dmnResource);
    }
    // enrich with imports
    for (DMNResource r : dmnResources) {
        for (Import i : r.getDefinitions().getImport()) {
            if (ImportDMNResolverUtil.whichImportType(i) == ImportType.DMN) {
                Either<String, DMNResource> resolvedResult = ImportDMNResolverUtil.resolveImportDMN(i, dmnResources, DMNResource::getModelID);
                DMNResource located = resolvedResult.getOrElseThrow(RuntimeException::new);
                r.addDependency(located.getModelID());
            }
        }
    }
    List<DMNResource> sortedDmnResources = DMNResourceDependenciesSorter.sort(dmnResources);
    Collection<DMNModel> dmnModels = new ArrayList<>();
    for (DMNResource dmnRes : sortedDmnResources) {
        DMNModel dmnModel = internalAddResource(kbuilderImpl, dmnCompiler, dmnRes.getResAndConfig(), dmnModels);
        dmnModels.add(dmnModel);
    }
}
Also used : DMNCompilerImpl(org.kie.dmn.core.compiler.DMNCompilerImpl) Import(org.kie.dmn.model.v1_1.Import) DMNMarshaller(org.kie.dmn.api.marshalling.v1_1.DMNMarshaller) QName(javax.xml.namespace.QName) Definitions(org.kie.dmn.model.v1_1.Definitions) ArrayList(java.util.ArrayList) ResourceWithConfiguration(org.kie.api.io.ResourceWithConfiguration) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) DMNModel(org.kie.dmn.api.core.DMNModel)

Aggregations

ArrayList (java.util.ArrayList)1 QName (javax.xml.namespace.QName)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 ResourceWithConfiguration (org.kie.api.io.ResourceWithConfiguration)1 DMNModel (org.kie.dmn.api.core.DMNModel)1 DMNMarshaller (org.kie.dmn.api.marshalling.v1_1.DMNMarshaller)1 DMNCompilerImpl (org.kie.dmn.core.compiler.DMNCompilerImpl)1 Definitions (org.kie.dmn.model.v1_1.Definitions)1 Import (org.kie.dmn.model.v1_1.Import)1