Search in sources :

Example 1 with PMML4Model

use of org.kie.pmml.pmml_4_2.PMML4Model in project drools by kiegroup.

the class PMML4ModelFactory method getModels.

public List<PMML4Model> getModels(PMML4Unit owner) {
    List<PMML4Model> pmml4Models = new ArrayList<>();
    owner.getRawPMML().getAssociationModelsAndBaselineModelsAndClusteringModels().forEach(serializable -> {
        if (serializable instanceof Scorecard) {
            Scorecard sc = (Scorecard) serializable;
            ScorecardModel model = new ScorecardModel(sc.getModelName(), sc, null, owner);
            pmml4Models.add(model);
        } else if (serializable instanceof RegressionModel) {
            RegressionModel rm = (RegressionModel) serializable;
            Regression model = new Regression(rm.getModelName(), rm, null, owner);
            pmml4Models.add(model);
        } else if (serializable instanceof TreeModel) {
            TreeModel tm = (TreeModel) serializable;
            Treemodel model = new Treemodel(tm.getModelName(), tm, null, owner);
            pmml4Models.add(model);
        } else if (serializable instanceof MiningModel) {
            MiningModel mm = (MiningModel) serializable;
            Miningmodel model = new Miningmodel(mm.getModelName(), mm, null, owner);
            pmml4Models.add(model);
        }
    });
    return pmml4Models;
}
Also used : TreeModel(org.kie.dmg.pmml.pmml_4_2.descr.TreeModel) MiningModel(org.kie.dmg.pmml.pmml_4_2.descr.MiningModel) PMML4Model(org.kie.pmml.pmml_4_2.PMML4Model) ArrayList(java.util.ArrayList) Scorecard(org.kie.dmg.pmml.pmml_4_2.descr.Scorecard) RegressionModel(org.kie.dmg.pmml.pmml_4_2.descr.RegressionModel)

Example 2 with PMML4Model

use of org.kie.pmml.pmml_4_2.PMML4Model in project drools by kiegroup.

the class PMML4UnitImpl method getModels.

@Override
public Map<String, PMML4Model> getModels(PMML4ModelType type, PMML4Model parent) {
    Map<String, PMML4Model> filteredModels = new HashMap<>();
    Map<String, PMML4Model> unfilteredModels = parent == null ? getRootModels() : getChildModels(parent.getModelId());
    if (unfilteredModels != null && !unfilteredModels.isEmpty()) {
        for (PMML4Model model : unfilteredModels.values()) {
            if (model.getModelType() == type) {
                filteredModels.put(model.getModelId(), model);
            }
        }
    }
    return filteredModels;
}
Also used : HashMap(java.util.HashMap) PMML4Model(org.kie.pmml.pmml_4_2.PMML4Model)

Example 3 with PMML4Model

use of org.kie.pmml.pmml_4_2.PMML4Model in project drools by kiegroup.

the class PMML4UnitImpl method addChildModelsToMap.

/**
 * Recursive method that adds children models (i.e. models contained within models)
 * to the internal map of models
 *
 * @param parentModel The model which may contain child models
 */
private void addChildModelsToMap(PMML4Model parentModel) {
    Map<String, PMML4Model> childModels = parentModel.getChildModels();
    if (childModels != null && !childModels.isEmpty()) {
        for (PMML4Model model : childModels.values()) {
            modelsMap.put(model.getModelId(), model);
            addChildModelsToMap(model);
        }
    }
}
Also used : PMML4Model(org.kie.pmml.pmml_4_2.PMML4Model)

Example 4 with PMML4Model

use of org.kie.pmml.pmml_4_2.PMML4Model in project drools by kiegroup.

the class PMML4Compiler method addMissingFieldDefinition.

private void addMissingFieldDefinition(PMML pmml, MiningSegmentation msm, MiningSegment seg) {
    // get the list of models that may contain the field definition
    List<PMML4Model> models = msm.getMiningSegments().stream().filter(s -> s != seg && s.getSegmentIndex() < seg.getSegmentIndex()).map(iseg -> {
        return iseg.getModel();
    }).collect(Collectors.toList());
    seg.getModel().getMiningFields().stream().filter(mf -> !mf.isInDictionary()).forEach(pmf -> {
        String fldName = pmf.getName();
        boolean fieldAdded = false;
        for (Iterator<PMML4Model> iter = models.iterator(); iter.hasNext() && !fieldAdded; ) {
            PMML4Model mdl = iter.next();
            PMMLOutputField outfield = mdl.findOutputField(fldName);
            PMMLMiningField target = (outfield != null && outfield.getTargetField() != null) ? mdl.findMiningField(outfield.getTargetField()) : null;
            if (outfield != null) {
                DataField e = null;
                if (outfield.getRawDataField() != null && outfield.getRawDataField().getDataType() != null) {
                    e = outfield.getRawDataField();
                } else if (target != null) {
                    e = target.getRawDataField();
                }
                if (e != null) {
                    e.setName(fldName);
                    pmml.getDataDictionary().getDataFields().add(e);
                    BigInteger bi = pmml.getDataDictionary().getNumberOfFields();
                    pmml.getDataDictionary().setNumberOfFields(bi.add(BigInteger.ONE));
                    fieldAdded = true;
                }
            }
        }
    });
}
Also used : ResourceFactory(org.kie.internal.io.ResourceFactory) SupportVectorMachineModel(org.kie.dmg.pmml.pmml_4_2.descr.SupportVectorMachineModel) Miningmodel(org.kie.pmml.pmml_4_2.model.Miningmodel) TemplateRegistry(org.mvel2.templates.TemplateRegistry) Map(java.util.Map) BigInteger(java.math.BigInteger) Scorecard(org.kie.dmg.pmml.pmml_4_2.descr.Scorecard) KieSession(org.kie.api.runtime.KieSession) TemplateCompiler(org.mvel2.templates.TemplateCompiler) EventProcessingOption(org.kie.api.conf.EventProcessingOption) PMML4UnitImpl(org.kie.pmml.pmml_4_2.model.PMML4UnitImpl) PMMLMiningField(org.kie.pmml.pmml_4_2.model.PMMLMiningField) TreeModel(org.kie.dmg.pmml.pmml_4_2.descr.TreeModel) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) KieBaseModel(org.kie.api.builder.model.KieBaseModel) Collectors(java.util.stream.Collectors) JAXBException(javax.xml.bind.JAXBException) Resource(org.kie.api.io.Resource) List(java.util.List) PMMLCompiler(org.drools.compiler.compiler.PMMLCompiler) KieSessionModel(org.kie.api.builder.model.KieSessionModel) SAXException(org.xml.sax.SAXException) Writer(java.io.Writer) NaiveBayesModel(org.kie.dmg.pmml.pmml_4_2.descr.NaiveBayesModel) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Marshaller(javax.xml.bind.Marshaller) HashMap(java.util.HashMap) ResourceType(org.kie.api.io.ResourceType) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) MiningSegmentation(org.kie.pmml.pmml_4_2.model.mining.MiningSegmentation) RegressionModel(org.kie.dmg.pmml.pmml_4_2.descr.RegressionModel) ClusteringModel(org.kie.dmg.pmml.pmml_4_2.descr.ClusteringModel) IoUtils(org.drools.core.util.IoUtils) PMMLResource(org.drools.compiler.compiler.PMMLResource) KieServices(org.kie.api.KieServices) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) XMLConstants(javax.xml.XMLConstants) KieBase(org.kie.api.KieBase) SimpleTemplateRegistry(org.mvel2.templates.SimpleTemplateRegistry) JAXBContext(javax.xml.bind.JAXBContext) MiningSegment(org.kie.pmml.pmml_4_2.model.mining.MiningSegment) OutputStream(java.io.OutputStream) Unmarshaller(javax.xml.bind.Unmarshaller) SchemaFactory(javax.xml.validation.SchemaFactory) Iterator(java.util.Iterator) DataField(org.kie.dmg.pmml.pmml_4_2.descr.DataField) KieContainer(org.kie.api.runtime.KieContainer) IOException(java.io.IOException) DataDictionary(org.kie.dmg.pmml.pmml_4_2.descr.DataDictionary) File(java.io.File) PMMLOutputField(org.kie.pmml.pmml_4_2.model.PMMLOutputField) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KieModuleModel(org.kie.api.builder.model.KieModuleModel) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) Collections(java.util.Collections) InputStream(java.io.InputStream) NeuralNetwork(org.kie.dmg.pmml.pmml_4_2.descr.NeuralNetwork) PMMLOutputField(org.kie.pmml.pmml_4_2.model.PMMLOutputField) DataField(org.kie.dmg.pmml.pmml_4_2.descr.DataField) BigInteger(java.math.BigInteger) PMMLMiningField(org.kie.pmml.pmml_4_2.model.PMMLMiningField)

Example 5 with PMML4Model

use of org.kie.pmml.pmml_4_2.PMML4Model 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)

Aggregations

PMML4Model (org.kie.pmml.pmml_4_2.PMML4Model)10 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 PMML (org.kie.dmg.pmml.pmml_4_2.descr.PMML)3 RegressionModel (org.kie.dmg.pmml.pmml_4_2.descr.RegressionModel)3 Scorecard (org.kie.dmg.pmml.pmml_4_2.descr.Scorecard)3 TreeModel (org.kie.dmg.pmml.pmml_4_2.descr.TreeModel)3 PMML4UnitImpl (org.kie.pmml.pmml_4_2.model.PMML4UnitImpl)3 List (java.util.List)2 Map (java.util.Map)2 PMMLResource (org.drools.compiler.compiler.PMMLResource)2 KieServices (org.kie.api.KieServices)2 KieBaseModel (org.kie.api.builder.model.KieBaseModel)2 KieModuleModel (org.kie.api.builder.model.KieModuleModel)2 MiningField (org.kie.dmg.pmml.pmml_4_2.descr.MiningField)2 KnowledgeBuilderResult (org.kie.internal.builder.KnowledgeBuilderResult)2 Miningmodel (org.kie.pmml.pmml_4_2.model.Miningmodel)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1