Search in sources :

Example 1 with PMMLPackage

use of org.kie.pmml.evaluator.api.container.PMMLPackage in project drools by kiegroup.

the class PMMLRuntimeFactoryInternalTest method getKnowledgePackageWithPMMLResourceType.

private InternalKnowledgePackage getKnowledgePackageWithPMMLResourceType(KiePMMLModel kiePMMLModel) {
    PMMLPackage pmmlPkg = new PMMLPackageImpl();
    pmmlPkg.addAll(Collections.singleton(kiePMMLModel));
    InternalKnowledgePackage pmmlKnowledgePackage = CoreComponentFactory.get().createKnowledgePackage(kiePMMLModel.getKModulePackageName());
    pmmlKnowledgePackage.getResourceTypePackages().put(ResourceType.PMML, pmmlPkg);
    return pmmlKnowledgePackage;
}
Also used : PMMLPackage(org.kie.pmml.evaluator.api.container.PMMLPackage) PMMLPackageImpl(org.kie.pmml.evaluator.assembler.container.PMMLPackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 2 with PMMLPackage

use of org.kie.pmml.evaluator.api.container.PMMLPackage 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)

Example 3 with PMMLPackage

use of org.kie.pmml.evaluator.api.container.PMMLPackage in project drools by kiegroup.

the class PMMLRuntimeFactoryInternal method createKieBase.

protected static KieBase createKieBase(KnowledgeBuilderImpl kbuilderImpl) {
    KieHelper kieHelper = new KieHelper();
    kbuilderImpl.getPackageNames().stream().flatMap(name -> kbuilderImpl.getPackageDescrs(name).stream()).forEach(pDescr -> kieHelper.addResource(createDescrResource(pDescr)));
    KieBase kieBase = kieHelper.build(ExecutableModelProject.class);
    // Temporarily because all this path has to be removed
    // Needed for non-drools models
    kbuilderImpl.getKnowledgePackages().forEach(kBuilderPackage -> {
        if (kieBase.getKiePackage(kBuilderPackage.getName()) == null) {
            ((InternalKnowledgeBase) kieBase).addPackage(kBuilderPackage);
        }
    });
    // Temporarily because all this path has to be removed
    // Basically, get PMMLPackage from kbuilderImpl' InternalKnowledgePackages and put inside kieBase' ones
    kieBase.getKiePackages().forEach(kiePackage -> {
        PMMLPackage pmmlPackage = (PMMLPackage) ((InternalKnowledgePackage) kiePackage).getResourceTypePackages().get(ResourceType.PMML);
        if (pmmlPackage == null) {
            InternalKnowledgePackage kBuilderPackage = kbuilderImpl.getPackage(kiePackage.getName());
            if (kBuilderPackage.getResourceTypePackages().get(ResourceType.PMML) != null) {
                ((InternalKnowledgePackage) kiePackage).getResourceTypePackages().put(ResourceType.PMML, kBuilderPackage.getResourceTypePackages().get(ResourceType.PMML));
            }
        }
    });
    return kieBase;
}
Also used : InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) DescrResource(org.drools.core.io.impl.DescrResource) KnowledgeBuilderFactory(org.kie.internal.builder.KnowledgeBuilderFactory) PMMLRuntime(org.kie.pmml.api.runtime.PMMLRuntime) LoggerFactory(org.slf4j.LoggerFactory) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) FileSystemResource(org.drools.core.io.impl.FileSystemResource) ResourceType(org.kie.api.io.ResourceType) ExternalException(org.kie.pmml.api.exceptions.ExternalException) ArrayList(java.util.ArrayList) ReleaseId(org.kie.api.builder.ReleaseId) PMMLPackage(org.kie.pmml.evaluator.api.container.PMMLPackage) KieRuntimeFactory(org.kie.api.runtime.KieRuntimeFactory) Map(java.util.Map) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) KieServices(org.kie.api.KieServices) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) KieBase(org.kie.api.KieBase) KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) KieHelper(org.kie.internal.utils.KieHelper) Logger(org.slf4j.Logger) InternalResource(org.drools.core.io.internal.InternalResource) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExecutableModelProject(org.drools.modelcompiler.ExecutableModelProject) HasNestedModels(org.kie.pmml.commons.model.HasNestedModels) Collection(java.util.Collection) FileOutputStream(java.io.FileOutputStream) UUID(java.util.UUID) File(java.io.File) KnowledgeBaseFactory(org.drools.kiesession.rulebase.KnowledgeBaseFactory) List(java.util.List) FileUtils.getFile(org.apache.commons.io.FileUtils.getFile) KieContainerImpl(org.drools.compiler.kie.builder.impl.KieContainerImpl) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) PMMLAssemblerService(org.kie.pmml.evaluator.assembler.service.PMMLAssemblerService) InputStream(java.io.InputStream) PMMLPackage(org.kie.pmml.evaluator.api.container.PMMLPackage) KieBase(org.kie.api.KieBase) KieHelper(org.kie.internal.utils.KieHelper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 4 with PMMLPackage

use of org.kie.pmml.evaluator.api.container.PMMLPackage in project drools by kiegroup.

the class PMMLAssemblerService method addModels.

protected void addModels(KnowledgeBuilderImpl kbuilderImpl, List<KiePMMLModel> toAdd) {
    for (KiePMMLModel kiePMMLModel : toAdd) {
        PackageDescr pkgDescr = new PackageDescr(kiePMMLModel.getKModulePackageName());
        PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(pkgDescr);
        InternalKnowledgePackage kpkgs = pkgReg.getPackage();
        ResourceTypePackageRegistry rpkg = kpkgs.getResourceTypePackages();
        PMMLPackage pmmlPkg = rpkg.computeIfAbsent(ResourceType.PMML, rtp -> new PMMLPackageImpl());
        pmmlPkg.addAll(Collections.singletonList(kiePMMLModel));
        if (kiePMMLModel instanceof HasNestedModels) {
            addModels(kbuilderImpl, ((HasNestedModels) kiePMMLModel).getNestedModels());
        }
    }
}
Also used : PMMLPackage(org.kie.pmml.evaluator.api.container.PMMLPackage) ResourceTypePackageRegistry(org.drools.core.definitions.ResourceTypePackageRegistry) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) PMMLPackageImpl(org.kie.pmml.evaluator.assembler.container.PMMLPackageImpl) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) HasNestedModels(org.kie.pmml.commons.model.HasNestedModels) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ResourceTypePackageRegistry(org.drools.core.definitions.ResourceTypePackageRegistry) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 5 with PMMLPackage

use of org.kie.pmml.evaluator.api.container.PMMLPackage in project drools by kiegroup.

the class KnowledgeBaseUtils method getModels.

public static List<KiePMMLModel> getModels(final KieBase knowledgeBase) {
    List<KiePMMLModel> models = new ArrayList<>();
    knowledgeBase.getKiePackages().forEach(kpkg -> {
        PMMLPackage pmmlPackage = (PMMLPackage) ((InternalKnowledgePackage) kpkg).getResourceTypePackages().get(ResourceType.PMML);
        if (pmmlPackage != null) {
            models.addAll(pmmlPackage.getAllModels().values());
        }
    });
    return models;
}
Also used : PMMLPackage(org.kie.pmml.evaluator.api.container.PMMLPackage) KiePMMLModel(org.kie.pmml.commons.model.KiePMMLModel) ArrayList(java.util.ArrayList) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

PMMLPackage (org.kie.pmml.evaluator.api.container.PMMLPackage)6 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)5 KiePMMLModel (org.kie.pmml.commons.model.KiePMMLModel)4 ArrayList (java.util.ArrayList)3 PMMLPackageImpl (org.kie.pmml.evaluator.assembler.container.PMMLPackageImpl)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 PackageDescr (org.drools.drl.ast.descr.PackageDescr)2 KieBase (org.kie.api.KieBase)2 KiePackage (org.kie.api.definition.KiePackage)2 HasNestedModels (org.kie.pmml.commons.model.HasNestedModels)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 FileUtils.getFile (org.apache.commons.io.FileUtils.getFile)1 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)1 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)1