Search in sources :

Example 1 with PMMLOutputField

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

the class MiningSegmentation method getSegmentTransfer.

public MiningSegmentTransfer getSegmentTransfer(MiningSegment targetSegment, String targetFieldName) {
    MiningSegmentTransfer xfer = null;
    int lastIndex = targetSegment.getSegmentIndex() - 1;
    if (lastIndex < 0) {
        throw new IndexOutOfBoundsException("Cannot have an undefined mining field in the first segment");
    }
    for (int idx = lastIndex; idx >= 0; idx--) {
        MiningSegment seg = miningSegments.get(idx);
        if (seg != null && seg.getInternalModel() != null) {
            List<PMMLOutputField> outputs = seg.getInternalModel().getOutputFields();
            if (outputs != null) {
                for (PMMLOutputField field : outputs) {
                    if (field.getName().equals(targetFieldName)) {
                        xfer = new MiningSegmentTransfer(this.segmentationId, seg.getSegmentId(), targetSegment.getSegmentId());
                        xfer.addResultToRequestMapping(targetFieldName, targetFieldName);
                        return xfer;
                    }
                }
            }
        }
    }
    return xfer;
}
Also used : PMMLOutputField(org.kie.pmml.pmml_4_2.model.PMMLOutputField)

Example 2 with PMMLOutputField

use of org.kie.pmml.pmml_4_2.model.PMMLOutputField 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 3 with PMMLOutputField

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

the class AbstractModel method getMiningFields.

@Override
public List<PMMLMiningField> getMiningFields() {
    List<PMMLMiningField> fields = new ArrayList<>();
    Map<String, MiningField> excludesTargetMap = getFilteredMiningFieldMap(false, FIELDUSAGETYPE.TARGET);
    Map<String, PMMLDataField> dataDictionary = getOwner().getDataDictionaryMap();
    for (String key : excludesTargetMap.keySet()) {
        PMMLDataField df = dataDictionary.get(key);
        MiningField mf = miningFieldMap.get(key);
        if (df != null) {
            fields.add(new PMMLMiningField(mf, df.getRawDataField(), this.getModelId(), true));
        } else {
            PMMLMiningField fld = new PMMLMiningField(mf, this.getModelId());
            if (this.getParentModel() != null) {
                PMML4Model ultimateParentModel = this.getParentModel();
                if (ultimateParentModel instanceof Miningmodel) {
                    while (ultimateParentModel.getParentModel() != null) {
                        ultimateParentModel = ultimateParentModel.getParentModel();
                    }
                    PMMLOutputField ofld = ((Miningmodel) ultimateParentModel).findOutputField(fld.getName());
                    if (ofld != null) {
                        fld.setType(ofld.getType());
                        fields.add(fld);
                    }
                }
            }
        }
    }
    return fields;
}
Also used : MiningField(org.kie.dmg.pmml.pmml_4_2.descr.MiningField) ArrayList(java.util.ArrayList) PMML4Model(org.kie.pmml.pmml_4_2.PMML4Model)

Example 4 with PMMLOutputField

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

the class Miningmodel method getChildOutputField.

private PMMLOutputField getChildOutputField(PMML4Model parentModel, String fieldName) {
    PMMLOutputField output = null;
    for (Iterator<PMML4Model> childIter = parentModel.getChildModels().values().iterator(); childIter.hasNext() && output == null; ) {
        PMML4Model child = childIter.next();
        List<PMMLOutputField> fields = child.getOutputFields();
        if (fields != null && !fields.isEmpty()) {
            for (Iterator<PMMLOutputField> fieldIter = fields.iterator(); fieldIter.hasNext() && output == null; ) {
                PMMLOutputField fld = fieldIter.next();
                if (fieldName.equals(fld.getName())) {
                    output = fld;
                }
            }
        }
        if (child instanceof Miningmodel && output == null) {
            output = getChildOutputField(child, fieldName);
        }
    }
    return output;
}
Also used : PMML4Model(org.kie.pmml.pmml_4_2.PMML4Model)

Aggregations

ArrayList (java.util.ArrayList)2 PMML4Model (org.kie.pmml.pmml_4_2.PMML4Model)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Writer (java.io.Writer)1 BigInteger (java.math.BigInteger)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 XMLConstants (javax.xml.XMLConstants)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1