use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class PMMLRuntimeFactoryInternalTest method populateNestedKiePackageList.
@Test
public void populateNestedKiePackageList() {
// Setup kiebase
KiePMMLModel kiePMMLModel = getKiePMMLModelWithNested("FAKE");
InternalKnowledgePackage pmmlKnowledgePackage = getKnowledgePackageWithPMMLResourceType(kiePMMLModel);
List<KiePackage> kiePackages = ((HasNestedModels) kiePMMLModel).getNestedModels().stream().map(this::getKnowledgePackageWithPMMLResourceType).collect(Collectors.toList());
InternalKnowledgeBase kieBase = (InternalKnowledgeBase) new KieHelper().build(ExecutableModelProject.class);
kieBase.addPackage(pmmlKnowledgePackage);
kieBase.addPackages(kiePackages);
// Actual test
final List<KiePackage> toPopulate = new ArrayList<>();
PMMLRuntimeFactoryInternal.populateNestedKiePackageList(Collections.singleton(kiePMMLModel), toPopulate, kieBase);
assertFalse(toPopulate.isEmpty());
assertEquals(kiePackages.size(), toPopulate.size());
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class PMMLRuntimeFactoryInternalTest method getKiePackageByFullClassNamePresent.
@Test
public void getKiePackageByFullClassNamePresent() {
// Setup kiebase
KiePMMLModel kiePMMLModel = new KiePMMLModelA("FAKE");
InternalKnowledgePackage pmmlKnowledgePackage = getKnowledgePackageWithPMMLResourceType(kiePMMLModel);
InternalKnowledgeBase kieBase = (InternalKnowledgeBase) new KieHelper().build(ExecutableModelProject.class);
kieBase.addPackage(pmmlKnowledgePackage);
// Actual test
assertNotNull(PMMLRuntimeFactoryInternal.getKiePackageByFullClassName(kiePMMLModel.getClass().getName(), kieBase));
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase 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.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class PMMLRuntimeFactoryInternal method getPMMLRuntime.
public static PMMLRuntime getPMMLRuntime(String pmmlFileName, String pmmlModelName, KieBase kieBase) {
RuleBaseConfiguration ruleBaseConfiguration = new RuleBaseConfiguration(((InternalKnowledgeBase) kieBase).getRootClassLoader());
InternalKnowledgeBase kieBaseNew = KnowledgeBaseFactory.newKnowledgeBase(ruleBaseConfiguration);
KiePackage kiePackage = getKiePackageByModelName(pmmlModelName, kieBase);
kieBaseNew.addPackage(kiePackage);
List<KiePackage> nestedKiePackages = getNestedKiePackages((InternalKnowledgePackage) kiePackage, kieBase);
if (!nestedKiePackages.isEmpty()) {
kieBaseNew.addPackages(nestedKiePackages);
}
((InternalKnowledgePackage) kiePackage).getResourceTypePackages().get(ResourceType.PMML);
return getPMMLRuntime(kieBaseNew);
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class SerializationSecurityPolicyTest method testSerialization.
@Test
public void testSerialization() throws IOException, ClassNotFoundException {
// kpackage serialization is not supported. But leave it for standard-drl.
final String rule = " rule R " + " when " + " then " + " System.out.println(\"consequence!\"); " + " end";
final KieServices kieServices = KieServices.get();
final Resource drlResource = kieServices.getResources().newByteArrayResource(rule.getBytes(StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name());
drlResource.setResourceType(ResourceType.DRL);
drlResource.setTargetPath("test.drl");
final KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.write(drlResource);
final KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
kieBuilder.buildAll();
final KieBase kieBase = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId()).getKieBase();
final Collection<KiePackage> kpkgs = kieBase.getKiePackages();
final Collection<KiePackage> newCollection = new ArrayList<>();
for (KiePackage kpkg : kpkgs) {
kpkg = SerializationHelper.serializeObject(kpkg);
newCollection.add(kpkg);
}
((InternalKnowledgeBase) kieBase).addPackages(newCollection);
final KieSession kieSession = kieBase.newKieSession();
assertThat(kieSession.fireAllRules()).isEqualTo(1);
}
Aggregations