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;
}
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);
}
});
}
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;
}
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());
}
}
}
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;
}
Aggregations