use of org.dmg.pmml.pmml_4_2.descr.PMML in project drools by kiegroup.
the class PMML4Compiler method compile.
public String compile(InputStream source, ClassLoader classLoader) {
this.results = new ArrayList<KnowledgeBuilderResult>();
PMML pmml = loadModel(PMML, source);
return compile(pmml, classLoader);
}
use of org.dmg.pmml.pmml_4_2.descr.PMML in project drools by kiegroup.
the class PMML4Compiler method getJavaClasses.
@Override
public Map<String, String> getJavaClasses(InputStream stream) {
Map<String, String> javaClasses = new HashMap<>();
PMML pmml = loadModel(PMML, stream);
if (pmml != null) {
PMML4Unit unit = new PMML4UnitImpl(pmml);
if (unit != null) {
List<PMML4Model> models = unit.getModels();
models.forEach(model -> {
Map.Entry<String, String> inputPojo = model.getMappedMiningPojo();
Map.Entry<String, String> ruleUnit = model.getMappedRuleUnit();
if (inputPojo != null)
javaClasses.put(inputPojo.getKey(), inputPojo.getValue());
if (ruleUnit != null)
javaClasses.put(ruleUnit.getKey(), ruleUnit.getValue());
});
}
}
return javaClasses;
}
use of org.dmg.pmml.pmml_4_2.descr.PMML 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.dmg.pmml.pmml_4_2.descr.PMML 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;
}
use of org.dmg.pmml.pmml_4_2.descr.PMML in project drools by kiegroup.
the class MiningSegment method generateSegmentRules.
public String generateSegmentRules(String segmentationAgenda, int segmentIndex) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CompiledTemplate template = getLaunchTemplate();
if (template != null) {
Map<String, Object> vars = new HashMap<>();
vars.put("segmentId", getSegmentId());
vars.put("segmentationAgendaId", segmentationAgenda);
vars.put("segmentSalience", new Integer(1000 - segmentIndex));
if (predicateRuleProducer instanceof CompoundSegmentPredicate) {
CompoundSegmentPredicate predProd = (CompoundSegmentPredicate) predicateRuleProducer;
if (predProd.hasSurrogation()) {
vars.put("segmentPredicate", getSurrogationPredicateText(predProd, -1));
} else {
vars.put("segmentPredicate", predProd.getPredicateRule());
}
} else {
vars.put("segmentPredicate", predicateRuleProducer.getPredicateRule());
}
vars.put("miningPojoClass", getOwner().getOwner().getMiningPojoClassName());
TemplateRuntime.execute(template, null, new MapVariableResolverFactory(vars), baos);
}
PMML pmml = new PMML();
pmml.setDataDictionary(this.internalModel.getDataDictionary());
pmml.getAssociationModelsAndBaselineModelsAndClusteringModels().add(this.internalModel.getRawModel());
PMML4Compiler compiler = new PMML4Compiler();
String innerRules = compiler.generateTheory(pmml);
return (new String(baos.toByteArray())).concat(innerRules);
}
Aggregations