Search in sources :

Example 1 with ResourceTypePackage

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

the class DMNWeaverService method merge.

@Override
public void merge(KieBase kieBase, KiePackage kiePkg, DMNPackage dmnpkg) {
    Map<ResourceType, ResourceTypePackage> map = ((InternalKnowledgePackage) kiePkg).getResourceTypePackages();
    DMNPackageImpl existing = (DMNPackageImpl) map.get(ResourceType.DMN);
    if (existing == null) {
        existing = new DMNPackageImpl(dmnpkg.getNamespace());
        map.put(ResourceType.DMN, existing);
    }
    for (Map.Entry<String, DMNModel> entry : dmnpkg.getAllModels().entrySet()) {
        existing.addModel(entry.getKey(), entry.getValue());
    }
    existing.addProfiles(((DMNPackageImpl) dmnpkg).getProfiles());
}
Also used : DMNPackageImpl(org.kie.dmn.core.impl.DMNPackageImpl) ResourceType(org.kie.api.io.ResourceType) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Map(java.util.Map) DMNModel(org.kie.dmn.api.core.DMNModel) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with ResourceTypePackage

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

the class KnowledgeBaseImpl method mergePackage.

/**
 * Merge a new package with an existing package.
 * Most of the work is done by the concrete implementations,
 * but this class does some work (including combining imports, compilation data, globals,
 * and the actual Rule objects into the package).
 */
private void mergePackage(InternalKnowledgePackage pkg, InternalKnowledgePackage newPkg) {
    // Merge imports
    final Map<String, ImportDeclaration> imports = pkg.getImports();
    imports.putAll(newPkg.getImports());
    // Merge static imports
    for (String staticImport : newPkg.getStaticImports()) {
        pkg.addStaticImport(staticImport);
    }
    String lastIdent = null;
    String lastType = null;
    try {
        // merge globals
        if (newPkg.getGlobals() != null && newPkg.getGlobals() != Collections.EMPTY_MAP) {
            Map<String, String> globals = pkg.getGlobals();
            // Add globals
            for (final Map.Entry<String, String> entry : newPkg.getGlobals().entrySet()) {
                final String identifier = entry.getKey();
                final String type = entry.getValue();
                lastIdent = identifier;
                lastType = type;
                if (globals.containsKey(identifier) && !globals.get(identifier).equals(type)) {
                    throw new RuntimeException(pkg.getName() + " cannot be integrated");
                } else {
                    pkg.addGlobal(identifier, this.rootClassLoader.loadClass(type));
                    // this isn't a package merge, it's adding to the rulebase, but I've put it here for convienience
                    addGlobal(identifier, this.rootClassLoader.loadClass(type));
                }
            }
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("Unable to resolve class '" + lastType + "' for global '" + lastIdent + "'");
    }
    // merge entry point declarations
    if (newPkg.getEntryPointIds() != null) {
        for (String ep : newPkg.getEntryPointIds()) {
            pkg.addEntryPointId(ep);
        }
    }
    // merge the type declarations
    if (newPkg.getTypeDeclarations() != null) {
        // add type declarations
        for (TypeDeclaration type : newPkg.getTypeDeclarations().values()) {
            // @TODO should we allow overrides? only if the class is not in use.
            if (!pkg.getTypeDeclarations().containsKey(type.getTypeName())) {
                // add to package list of type declarations
                pkg.addTypeDeclaration(type);
            }
        }
    }
    // merge window declarations
    if (newPkg.getWindowDeclarations() != null) {
        // add window declarations
        for (WindowDeclaration window : newPkg.getWindowDeclarations().values()) {
            if (!pkg.getWindowDeclarations().containsKey(window.getName()) || pkg.getWindowDeclarations().get(window.getName()).equals(window)) {
                pkg.addWindowDeclaration(window);
            } else {
                throw new RuntimeException("Unable to merge two conflicting window declarations for window named: " + window.getName());
            }
        }
    }
    // Merge rules into the RuleBase package
    // as this is needed for individual rule removal later on
    List<RuleImpl> rulesToBeRemoved = new ArrayList<RuleImpl>();
    for (Rule newRule : newPkg.getRules()) {
        // remove the rule if it already exists
        RuleImpl oldRule = pkg.getRule(newRule.getName());
        if (oldRule != null) {
            rulesToBeRemoved.add(oldRule);
        }
    }
    if (!rulesToBeRemoved.isEmpty()) {
        removeRules(rulesToBeRemoved);
    }
    for (Rule newRule : newPkg.getRules()) {
        pkg.addRule((RuleImpl) newRule);
    }
    // Merge The Rule Flows
    if (newPkg.getRuleFlows() != null) {
        for (Process flow : newPkg.getRuleFlows().values()) {
            pkg.addProcess(flow);
        }
    }
    if (!newPkg.getResourceTypePackages().isEmpty()) {
        for (ResourceTypePackage rtkKpg : newPkg.getResourceTypePackages().values()) {
            ResourceType rt = rtkKpg.getResourceType();
            KieWeavers weavers = ServiceRegistry.getInstance().get(KieWeavers.class);
            KieWeaverService weaver = weavers.getWeavers().get(rt);
            weaver.merge(this, pkg, rtkKpg);
        }
    }
}
Also used : KieWeaverService(org.kie.api.internal.weaver.KieWeaverService) ArrayList(java.util.ArrayList) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Process(org.kie.api.definition.process.Process) ResourceType(org.kie.api.io.ResourceType) KieWeavers(org.kie.api.internal.weaver.KieWeavers) WindowDeclaration(org.drools.core.rule.WindowDeclaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) Rule(org.kie.api.definition.rule.Rule) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 3 with ResourceTypePackage

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

the class BayesWeaverService method merge.

@Override
public void merge(KieBase kieBase, KiePackage kiePkg, BayesPackage bayesPkg) {
    Map<ResourceType, ResourceTypePackage> map = ((InternalKnowledgePackage) kiePkg).getResourceTypePackages();
    BayesPackage existing = (BayesPackage) map.get(ResourceType.BAYES);
    if (existing == null) {
        existing = new BayesPackage();
        map.put(ResourceType.BAYES, existing);
    }
    for (String name : bayesPkg.listJunctionTrees()) {
        existing.addJunctionTree(name, bayesPkg.getJunctionTree(name));
    }
}
Also used : BayesPackage(org.drools.beliefs.bayes.assembler.BayesPackage) ResourceType(org.kie.api.io.ResourceType) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 4 with ResourceTypePackage

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

the class DMNAssemblerService method compileResourceToModel.

private DMNModel compileResourceToModel(KnowledgeBuilderImpl kbuilderImpl, DMNCompiler dmnCompiler, Resource resource, Collection<DMNModel> dmnModels) {
    DMNModel model = dmnCompiler.compile(resource, dmnModels);
    if (model != null) {
        String namespace = model.getNamespace();
        PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(namespace));
        InternalKnowledgePackage kpkgs = pkgReg.getPackage();
        kpkgs.addCloningResource(DMN_COMPILER_CACHE_KEY, dmnCompiler);
        Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
        DMNPackageImpl dmnpkg = (DMNPackageImpl) rpkg.get(ResourceType.DMN);
        if (dmnpkg == null) {
            dmnpkg = new DMNPackageImpl(namespace);
            rpkg.put(ResourceType.DMN, dmnpkg);
        } else {
            if (dmnpkg.getModel(model.getName()) != null) {
                kbuilderImpl.addBuilderResult(new DMNKnowledgeBuilderError(ResultSeverity.ERROR, resource, namespace, "Duplicate model name " + model.getName() + " in namespace " + namespace));
                logger.error("Duplicate model name {} in namespace {}", model.getName(), namespace);
            }
        }
        dmnpkg.addModel(model.getName(), model);
        dmnpkg.addProfiles(kbuilderImpl.getCachedOrCreate(DMN_PROFILES_CACHE_KEY, () -> getDMNProfiles(kbuilderImpl)));
    } else {
        kbuilderImpl.addBuilderResult(new DMNKnowledgeBuilderError(ResultSeverity.ERROR, resource, "Unable to compile DMN model for the resource"));
        logger.error("Unable to compile DMN model for resource {}", resource.getSourcePath());
    }
    return model;
}
Also used : PackageRegistry(org.drools.compiler.compiler.PackageRegistry) DMNPackageImpl(org.kie.dmn.core.impl.DMNPackageImpl) ResourceType(org.kie.api.io.ResourceType) DMNKnowledgeBuilderError(org.kie.dmn.core.impl.DMNKnowledgeBuilderError) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) DMNModel(org.kie.dmn.api.core.DMNModel) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 5 with ResourceTypePackage

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

the class PMMLRuntimeFactoryInternalTest method createKieBaseFromKnowledgeBuilder.

@Test
public void createKieBaseFromKnowledgeBuilder() {
    KnowledgeBuilderImpl knowledgeBuilder = new KnowledgeBuilderImpl();
    knowledgeBuilder.addPackage(CoreComponentFactory.get().createKnowledgePackage("namespace_1"));
    knowledgeBuilder.addPackage(CoreComponentFactory.get().createKnowledgePackage("namespace_2"));
    PMMLPackage pmmlPkg = new PMMLPackageImpl();
    pmmlPkg.addAll(Collections.singleton(new KiePMMLTestingModel("FAKE", Collections.emptyList())));
    InternalKnowledgePackage pmmlKnowledgePackage = CoreComponentFactory.get().createKnowledgePackage("pmmled_package");
    pmmlKnowledgePackage.getResourceTypePackages().put(ResourceType.PMML, pmmlPkg);
    KieBase retrieved = PMMLRuntimeFactoryInternal.createKieBase(knowledgeBuilder);
    assertNotNull(retrieved);
    assertFalse(retrieved.getKiePackages().isEmpty());
    assertEquals(knowledgeBuilder.getKnowledgePackages().size(), retrieved.getKiePackages().size());
    knowledgeBuilder.getKnowledgePackages().forEach(kBuilderPackage -> {
        assertNotNull(retrieved.getKiePackage(kBuilderPackage.getName()));
        ResourceTypePackage knowledgeBuilderResourceTypePackage = ((InternalKnowledgePackage) kBuilderPackage).getResourceTypePackages().get(ResourceType.PMML);
        if (((InternalKnowledgePackage) kBuilderPackage).getResourceTypePackages().get(ResourceType.PMML) != null) {
            InternalKnowledgePackage retrievedKiePackage = (InternalKnowledgePackage) retrieved.getKiePackage(kBuilderPackage.getName());
            ResourceTypePackage retrievedResourceTypePackage = retrievedKiePackage.getResourceTypePackages().get(ResourceType.PMML);
            assertNotNull(retrievedKiePackage.getResourceTypePackages().get(ResourceType.PMML));
            assertEquals(knowledgeBuilderResourceTypePackage, retrievedResourceTypePackage);
        }
    });
}
Also used : PMMLPackage(org.kie.pmml.evaluator.api.container.PMMLPackage) PMMLPackageImpl(org.kie.pmml.evaluator.assembler.container.PMMLPackageImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) KieBase(org.kie.api.KieBase) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Aggregations

ResourceTypePackage (org.kie.api.internal.io.ResourceTypePackage)13 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)9 ResourceType (org.kie.api.io.ResourceType)7 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)5 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)5 DMNModel (org.kie.dmn.api.core.DMNModel)5 DMNPackageImpl (org.kie.dmn.core.impl.DMNPackageImpl)5 HashMap (java.util.HashMap)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 WindowDeclaration (org.drools.core.rule.WindowDeclaration)4 Process (org.kie.api.definition.process.Process)4 KieWeavers (org.kie.api.internal.weaver.KieWeavers)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 Rule (org.kie.api.definition.rule.Rule)3 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 ResourceTypePackageRegistry (org.drools.core.definitions.ResourceTypePackageRegistry)2 Function (org.drools.core.rule.Function)2 ImportDeclaration (org.drools.core.rule.ImportDeclaration)2