Search in sources :

Example 26 with PMML

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);
}
Also used : PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 27 with PMML

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;
}
Also used : HashMap(java.util.HashMap) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) Map(java.util.Map) HashMap(java.util.HashMap) PMML4UnitImpl(org.kie.pmml.pmml_4_2.model.PMML4UnitImpl)

Example 28 with PMML

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;
}
Also used : ArrayList(java.util.ArrayList) Miningmodel(org.kie.pmml.pmml_4_2.model.Miningmodel) KieServices(org.kie.api.KieServices) PMMLResource(org.drools.compiler.compiler.PMMLResource) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieModuleModel(org.kie.api.builder.model.KieModuleModel) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) PMML4UnitImpl(org.kie.pmml.pmml_4_2.model.PMML4UnitImpl) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 29 with PMML

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;
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) PMMLResource(org.drools.compiler.compiler.PMMLResource) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) DataDictionary(org.kie.dmg.pmml.pmml_4_2.descr.DataDictionary) KieSessionModel(org.kie.api.builder.model.KieSessionModel)

Example 30 with PMML

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);
}
Also used : HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PMML4Compiler(org.kie.pmml.pmml_4_2.PMML4Compiler) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

PMML (org.dmg.pmml.pmml_4_2.descr.PMML)20 Test (org.junit.Test)17 KieSession (org.kie.api.runtime.KieSession)10 Scorecard (org.dmg.pmml.pmml_4_2.descr.Scorecard)8 PMML (org.kie.dmg.pmml.pmml_4_2.descr.PMML)8 TreeModel (org.dmg.pmml.pmml_4_2.descr.TreeModel)7 FactType (org.kie.api.definition.type.FactType)7 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)6 PMML4Compiler (org.drools.pmml.pmml_4_2.PMML4Compiler)6 KieServices (org.kie.api.KieServices)6 JAXBContext (javax.xml.bind.JAXBContext)5 JAXBException (javax.xml.bind.JAXBException)5 KieContainer (org.kie.api.runtime.KieContainer)5 ArrayList (java.util.ArrayList)4 KieBase (org.kie.api.KieBase)4 KnowledgeBuilderResult (org.kie.internal.builder.KnowledgeBuilderResult)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3