use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.
the class KnowledgeBuilderImpl method generatedResourcesToPackageDescr.
private List<PackageDescr> generatedResourcesToPackageDescr(Resource resource, List<PMMLResource> resources) throws DroolsParserException {
List<PackageDescr> pkgDescrs = new ArrayList<>();
DrlParser parser = new DrlParser(configuration.getLanguageLevel());
for (PMMLResource res : resources) {
for (String key : res.getRules().keySet()) {
String src = res.getRules().get(key);
PackageDescr descr = null;
descr = parser.parse(false, src);
if (descr != null) {
descr.setResource(resource);
pkgDescrs.add(descr);
dumpGeneratedRule(descr, key, src);
} else {
addBuilderResult(new ParserError(resource, "Parser returned a null Package", 0, 0));
}
}
}
return pkgDescrs;
}
use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.
the class KnowledgeBuilderTest method testResourceMapping.
@Ignore
@Test
public void testResourceMapping() throws Exception {
ServiceRegistryImpl serviceRegistry = (ServiceRegistryImpl) ServiceRegistry.getInstance();
serviceRegistry.reset();
ServiceDiscoveryImpl.getInstance().addService(PMMLCompiler.class.getCanonicalName(), new PMMLCompiler() {
public String compile(InputStream stream, ClassLoader cl) {
return "rule R2 when then end";
}
@Override
public List<KnowledgeBuilderResult> getResults() {
return Collections.emptyList();
}
@Override
public void clearResults() {
// To change body of implemented methods use File | Settings | File Templates.
}
public Resource[] transform(Resource input, ClassLoader classLoader) {
return new Resource[0];
}
public String getCompilerVersion() {
return "KnowledgeBuilderTest";
}
@Override
public List<PMMLResource> precompile(InputStream stream, ClassLoader classLoader, KieBaseModel rootModel) {
return Collections.emptyList();
}
@Override
public List<PMMLResource> precompile(String fileName, ClassLoader classLoader, KieBaseModel rootModel) {
return Collections.emptyList();
}
@Override
public Map<String, String> getJavaClasses(InputStream stream) {
return Collections.emptyMap();
}
@Override
public Map<String, String> getJavaClasses(String fileName) {
return Collections.emptyMap();
}
});
serviceRegistry.reload();
String rule = "package org.drools.compiler.test\n" + "rule R1 when\n" + " \n" + "then\n" + "end\n";
Resource res1 = ResourceFactory.newByteArrayResource(rule.getBytes());
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(res1, ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
KiePackage kp1 = kbuilder.getKnowledgePackages().iterator().next();
assertEquals(1, kp1.getRules().size());
Rule r = kp1.getRules().iterator().next();
assertEquals(res1, ((RuleImpl) r).getResource());
String pmml = "<PMML version=\"4.0\"><Header/></PMML>";
Resource res2 = ResourceFactory.newByteArrayResource(pmml.getBytes());
KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder2.add(res2, ResourceType.PMML);
assertFalse(kbuilder2.getErrors().toString(), kbuilder2.hasErrors());
KiePackage kp2 = kbuilder2.getKnowledgePackages().iterator().next();
assertEquals(1, kp2.getRules().size());
Rule r2 = kp2.getRules().iterator().next();
assertEquals(res2, ((RuleImpl) r2).getResource());
}
use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.
the class PMML4Compiler method buildResourcesFromModel.
protected List<PMMLResource> buildResourcesFromModel(PMML pmml, Miningmodel miningModel, List<PMMLResource> resourcesList, ClassLoader classLoader, KieModuleModel module) {
if (resourcesList == null) {
resourcesList = new ArrayList<>();
}
// new PMMLResource(PMML_DROOLS+".mining.model_"+miningModel.getModelId());
PMMLResource resource = new PMMLResource(miningModel.getModelPackageName());
KieBaseModel rootKieBaseModel = module.newKieBaseModel(resource.getPackageName());
rootKieBaseModel.addPackage(resource.getPackageName());
rootKieBaseModel.setDefault(true);
resource.setKieBaseModel(rootKieBaseModel);
resource.addRules(miningModel.getModelId(), miningModel.generateRules());
resourcesList.add(resource);
getChildResources(pmml, miningModel, resourcesList, classLoader, module);
return resourcesList;
}
use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.
the class PMML4Compiler method precompile.
public List<PMMLResource> precompile(InputStream stream, ClassLoader classLoader, KieBaseModel rootKieBaseModel) {
List<PMMLResource> resources = new ArrayList<>();
KieServices services = KieServices.Factory.get();
KieModuleModel module = services.newKieModuleModel();
this.results = new ArrayList<KnowledgeBuilderResult>();
PMML pmml = loadModel(PMML, stream);
helper.setResolver(classLoader);
PMML4Unit unit = new PMML4UnitImpl(pmml);
if (unit.containsMiningModel()) {
Miningmodel rootModel = unit.getRootMiningModel();
resources = buildResourcesFromModel(pmml, rootModel, null, classLoader, module);
} else {
PMML4Model rootModel = unit.getRootModel();
if (rootModel != null) {
helper.setPack(rootModel.getModelPackageName());
KieBaseModel kbm = module.newKieBaseModel(rootModel.getModelId());
kbm.addPackage(helper.getPack()).setDefault(true).setEventProcessingMode(EventProcessingOption.CLOUD);
PMMLResource resource = new PMMLResource(helper.getPack());
resource.setKieBaseModel(kbm);
resource.addRules(rootModel.getModelId(), this.compile(pmml, classLoader));
resources.add(resource);
}
}
return resources;
}
use of org.drools.compiler.compiler.PMMLResource in project drools by kiegroup.
the class PMML4Compiler method buildResourceFromSegment.
protected PMMLResource buildResourceFromSegment(PMML pmml_origin, MiningSegment segment, ClassLoader classLoader, KieModuleModel module) {
PMML pmml = new PMML();
DataDictionary dd = pmml_origin.getDataDictionary();
pmml.setDataDictionary(dd);
pmml.setHeader(pmml_origin.getHeader());
pmml.getAssociationModelsAndBaselineModelsAndClusteringModels().add(segment.getModel().getRawModel());
addMissingFieldDefinition(pmml, segment.getOwner(), segment);
// PMML4Helper.pmmlDefaultPackageName()+".mining.segment_"+segment.getSegmentId());
helper.setPack(segment.getModel().getModelPackageName());
String rules = this.compile(pmml, classLoader);
KieBaseModel kbModel = module.newKieBaseModel(segment.getOwner().getOwner().getModelId() + "_" + segment.getOwner().getSegmentationId() + "_SEGMENT_" + segment.getSegmentId());
kbModel.addPackage(helper.getPack()).setDefault(false).setEventProcessingMode(EventProcessingOption.CLOUD);
KieSessionModel ksm = kbModel.newKieSessionModel("SEGMENT_" + segment.getSegmentId());
ksm.setDefault(true);
PMMLResource resource = new PMMLResource(helper.getPack());
resource.setKieBaseModel(kbModel);
resource.addRules(segment.getModel().getModelId(), rules);
return resource;
}
Aggregations