Search in sources :

Example 6 with PMML

use of org.kie.dmg.pmml.pmml_4_2.descr.PMML in project drools by kiegroup.

the class ScorecardReasonCodeTest method testBaselineScores.

@Test
public void testBaselineScores() throws Exception {
    ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
    boolean compileResult = scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_reasoncodes.xls"));
    if (!compileResult) {
        assertErrors(scorecardCompiler);
    }
    final PMML pmmlDocument = scorecardCompiler.getPMMLDocument();
    for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (serializable instanceof Scorecard) {
            for (Object obj : ((Scorecard) serializable).getExtensionsAndCharacteristicsAndMiningSchemas()) {
                if (obj instanceof Characteristics) {
                    Characteristics characteristics = (Characteristics) obj;
                    assertEquals(4, characteristics.getCharacteristics().size());
                    assertEquals(10.0, characteristics.getCharacteristics().get(0).getBaselineScore(), 0.0);
                    assertEquals(99.0, characteristics.getCharacteristics().get(1).getBaselineScore(), 0.0);
                    assertEquals(12.0, characteristics.getCharacteristics().get(2).getBaselineScore(), 0.0);
                    assertEquals(15.0, characteristics.getCharacteristics().get(3).getBaselineScore(), 0.0);
                    assertEquals(25.0, ((Scorecard) serializable).getBaselineScore(), 0.0);
                    return;
                }
            }
        }
    }
    fail();
}
Also used : Characteristics(org.dmg.pmml.pmml_4_2.descr.Characteristics) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) Test(org.junit.Test)

Example 7 with PMML

use of org.kie.dmg.pmml.pmml_4_2.descr.PMML in project drools by kiegroup.

the class ScorecardReasonCodeTest method testAbsenceOfReasonCodes.

@Test
public void testAbsenceOfReasonCodes() throws Exception {
    ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
    scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls"));
    PMML pmml = scorecardCompiler.getPMMLDocument();
    for (Object serializable : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (serializable instanceof Scorecard) {
            assertFalse(((Scorecard) serializable).getUseReasonCodes());
        }
    }
}
Also used : PMML(org.dmg.pmml.pmml_4_2.descr.PMML) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) Test(org.junit.Test)

Example 8 with PMML

use of org.kie.dmg.pmml.pmml_4_2.descr.PMML in project drools by kiegroup.

the class PMML4Compiler method checkBuildingResources.

private static KieBase checkBuildingResources(PMML pmml) throws IOException {
    KieServices ks = KieServices.Factory.get();
    KieContainer kieContainer = ks.getKieClasspathContainer();
    if (registry == null) {
        initRegistry();
    }
    String chosenKieBase = null;
    for (Object o : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (o instanceof NaiveBayesModel) {
            if (!naiveBayesLoaded) {
                for (String ntempl : NAIVE_BAYES_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                naiveBayesLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "KiePMML-Bayes" : "KiePMML";
        }
        if (o instanceof NeuralNetwork) {
            if (!neuralLoaded) {
                for (String ntempl : NEURAL_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                neuralLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "KiePMML-Neural" : "KiePMML";
        }
        if (o instanceof ClusteringModel) {
            if (!clusteringLoaded) {
                for (String ntempl : CLUSTERING_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                clusteringLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "KiePMML-Cluster" : "KiePMML";
        }
        if (o instanceof SupportVectorMachineModel) {
            if (!svmLoaded) {
                for (String ntempl : SVM_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                svmLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "KiePMML-SVM" : "KiePMML";
        }
        if (o instanceof TreeModel) {
            if (!treeLoaded) {
                for (String ntempl : TREE_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                treeLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "KiePMML-Tree" : "KiePMML";
        }
        if (o instanceof RegressionModel) {
            if (!simpleRegLoaded) {
                for (String ntempl : SIMPLEREG_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                simpleRegLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "KiePMML-Regression" : "KiePMML";
        }
        if (o instanceof Scorecard) {
            if (!scorecardLoaded) {
                for (String ntempl : SCORECARD_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                scorecardLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "KiePMML-Scorecard" : "KiePMML";
        }
    }
    if (chosenKieBase == null) {
        chosenKieBase = "KiePMML-Base";
    }
    return kieContainer.getKieBase(chosenKieBase);
}
Also used : TreeModel(org.kie.dmg.pmml.pmml_4_2.descr.TreeModel) KieServices(org.kie.api.KieServices) NaiveBayesModel(org.kie.dmg.pmml.pmml_4_2.descr.NaiveBayesModel) NeuralNetwork(org.kie.dmg.pmml.pmml_4_2.descr.NeuralNetwork) SupportVectorMachineModel(org.kie.dmg.pmml.pmml_4_2.descr.SupportVectorMachineModel) Scorecard(org.kie.dmg.pmml.pmml_4_2.descr.Scorecard) KieContainer(org.kie.api.runtime.KieContainer) ClusteringModel(org.kie.dmg.pmml.pmml_4_2.descr.ClusteringModel) RegressionModel(org.kie.dmg.pmml.pmml_4_2.descr.RegressionModel)

Example 9 with PMML

use of org.kie.dmg.pmml.pmml_4_2.descr.PMML in project drools by kiegroup.

the class PMML4Compiler method loadModel.

/**
 * Imports a PMML source file, returning a Java descriptor
 * @param model            the PMML package name (classes derived from a specific schema)
 * @param source        the name of the PMML resource storing the predictive model
 * @return                the Java Descriptor of the PMML resource
 */
public PMML loadModel(String model, InputStream source) {
    try {
        if (schema == null) {
            visitorBuildResults.add(new PMMLWarning(ResourceFactory.newInputStreamResource(source), "Could not validate PMML document, schema not available"));
        }
        JAXBContext jc = JAXBContext.newInstance(model);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        if (schema != null) {
            unmarshaller.setSchema(schema);
        }
        return (PMML) unmarshaller.unmarshal(source);
    } catch (JAXBException e) {
        this.results.add(new PMMLError(e.toString()));
        return null;
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 10 with PMML

use of org.kie.dmg.pmml.pmml_4_2.descr.PMML in project drools by kiegroup.

the class PMML4UnitImpl method initDataDictionaryMap.

/**
 * Initializes the internal structure that holds data dictionary information.
 * This initializer should be called prior to any other initializers, since
 * many other structures may have a dependency on the data dictionary.
 */
private void initDataDictionaryMap() {
    DataDictionary dd = rawPmml.getDataDictionary();
    if (dd != null) {
        dataDictionaryMap = new HashMap<>();
        for (DataField dataField : dd.getDataFields()) {
            PMMLDataField df = new PMMLDataField(dataField);
            dataDictionaryMap.put(df.getName(), df);
        }
    } else {
        throw new IllegalStateException("BRMS-PMML requires a data dictionary section in the definition file");
    }
}
Also used : DataField(org.kie.dmg.pmml.pmml_4_2.descr.DataField) DataDictionary(org.kie.dmg.pmml.pmml_4_2.descr.DataDictionary)

Aggregations

PMML (org.dmg.pmml.pmml_4_2.descr.PMML)20 Test (org.junit.Test)17 KieSession (org.kie.api.runtime.KieSession)10 PMML (org.kie.dmg.pmml.pmml_4_2.descr.PMML)8 FactType (org.kie.api.definition.type.FactType)7 Scorecard (org.dmg.pmml.pmml_4_2.descr.Scorecard)6 TreeModel (org.dmg.pmml.pmml_4_2.descr.TreeModel)6 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 KieBase (org.kie.api.KieBase)4 KnowledgeBuilderResult (org.kie.internal.builder.KnowledgeBuilderResult)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3