Search in sources :

Example 1 with PMML

use of org.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.dmg.pmml.pmml_4_2.descr.PMML) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 2 with PMML

use of org.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 ? "PMML-Bayes" : "PMML";
        }
        if (o instanceof NeuralNetwork) {
            if (!neuralLoaded) {
                for (String ntempl : NEURAL_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                neuralLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Neural" : "PMML";
        }
        if (o instanceof ClusteringModel) {
            if (!clusteringLoaded) {
                for (String ntempl : CLUSTERING_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                clusteringLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Cluster" : "PMML";
        }
        if (o instanceof SupportVectorMachineModel) {
            if (!svmLoaded) {
                for (String ntempl : SVM_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                svmLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-SVM" : "PMML";
        }
        if (o instanceof TreeModel) {
            if (!treeLoaded) {
                for (String ntempl : TREE_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                treeLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Tree" : "PMML";
        }
        if (o instanceof RegressionModel) {
            if (!simpleRegLoaded) {
                for (String ntempl : SIMPLEREG_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                simpleRegLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Regression" : "PMML";
        }
        if (o instanceof Scorecard) {
            if (!scorecardLoaded) {
                for (String ntempl : SCORECARD_TEMPLATES) {
                    prepareTemplate(ntempl);
                }
                scorecardLoaded = true;
            }
            chosenKieBase = chosenKieBase == null ? "PMML-Scorecard" : "PMML";
        }
    }
    if (chosenKieBase == null) {
        chosenKieBase = "PMML-Base";
    }
    return kieContainer.getKieBase(chosenKieBase);
}
Also used : TreeModel(org.dmg.pmml.pmml_4_2.descr.TreeModel) KieServices(org.kie.api.KieServices) NaiveBayesModel(org.dmg.pmml.pmml_4_2.descr.NaiveBayesModel) NeuralNetwork(org.dmg.pmml.pmml_4_2.descr.NeuralNetwork) SupportVectorMachineModel(org.dmg.pmml.pmml_4_2.descr.SupportVectorMachineModel) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) KieContainer(org.kie.api.runtime.KieContainer) ClusteringModel(org.dmg.pmml.pmml_4_2.descr.ClusteringModel) RegressionModel(org.dmg.pmml.pmml_4_2.descr.RegressionModel)

Example 3 with PMML

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

the class DecisionTreeTest method testMissingTreeLastChoice.

@Test
public void testMissingTreeLastChoice() throws Exception {
    PMML4Compiler compiler = new PMML4Compiler();
    PMML pmml = compiler.loadModel(PMML, ResourceFactory.newClassPathResource(source2).getInputStream());
    for (Object o : pmml.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (o instanceof TreeModel) {
            TreeModel tree = (TreeModel) o;
            tree.setMissingValueStrategy(MISSINGVALUESTRATEGY.LAST_PREDICTION);
        }
    }
    String theory = compiler.generateTheory(pmml);
    if (VERBOSE) {
        System.out.println(theory);
    }
    KieSession kSession = getSession(theory);
    setKSession(kSession);
    setKbase(getKSession().getKieBase());
    // init model
    kSession.fireAllRules();
    FactType tgt = kSession.getKieBase().getFactType(packageName, "Fld9");
    FactType tok = kSession.getKieBase().getFactType(PMML4Helper.pmmlDefaultPackageName(), "TreeToken");
    kSession.getEntryPoint("in_Fld1").insert(-1.0);
    kSession.getEntryPoint("in_Fld2").insert(-1.0);
    kSession.getEntryPoint("in_Fld3").insert("optA");
    kSession.fireAllRules();
    Object token = getToken(kSession);
    assertEquals(0.8, (Double) tok.get(token, "confidence"), 1e-6);
    assertEquals("null", tok.get(token, "current"));
    assertEquals(50.0, tok.get(token, "totalCount"));
    checkFirstDataFieldOfTypeStatus(tgt, true, false, "Missing", "tgtX");
    checkGeneratedRules();
}
Also used : TreeModel(org.dmg.pmml.pmml_4_2.descr.TreeModel) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) KieSession(org.kie.api.runtime.KieSession) PMML4Compiler(org.drools.pmml.pmml_4_2.PMML4Compiler) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 4 with PMML

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

the class ExternalObjectModelTest method testWithInitialScore.

@Test
public void testWithInitialScore() throws Exception {
    ScorecardCompiler scorecardCompiler2 = new ScorecardCompiler(EXTERNAL_OBJECT_MODEL);
    PMML pmmlDocument2 = null;
    String drl2 = null;
    if (scorecardCompiler2.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls"), "scorecards_initialscore")) {
        pmmlDocument2 = scorecardCompiler2.getPMMLDocument();
        assertNotNull(pmmlDocument2);
        drl2 = scorecardCompiler2.getDRL();
        PMML4Compiler.dumpModel(pmmlDocument2, System.out);
    } else {
        fail("failed to parse scoremodel Excel.");
    }
    assertNotNull(pmmlDocument2);
    assertTrue(drl2 != null && !drl2.isEmpty());
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ks.getResources().newByteArrayResource(drl2.getBytes()).setSourcePath("test_scorecard_rules.drl").setResourceType(ResourceType.DRL));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    Results res = kieBuilder.buildAll().getResults();
    System.err.println(res.getMessages());
    KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
    KieBase kbase = kieContainer.getKieBase();
    KieSession session = kbase.newKieSession();
    Applicant applicant = new Applicant();
    applicant.setAge(10);
    session.insert(applicant);
    // session.addEventListener(new DebugWorkingMemoryEventListener());
    session.fireAllRules();
    session.dispose();
    // occupation = 0, age = 30, validLicence -1, initialScore=100
    assertEquals(129.0, applicant.getTotalScore(), 0.0);
    session = kbase.newKieSession();
    applicant = new Applicant();
    applicant.setOccupation("SKYDIVER");
    applicant.setAge(0);
    session.insert(applicant);
    session.fireAllRules();
    session.dispose();
    // occupation = -10, age = +10, validLicense = -1, initialScore=100;
    assertEquals(99.0, applicant.getTotalScore(), 0.0);
    session = kbase.newKieSession();
    applicant = new Applicant();
    applicant.setResidenceState("AP");
    applicant.setOccupation("TEACHER");
    applicant.setAge(20);
    applicant.setValidLicense(true);
    session.insert(applicant);
    session.fireAllRules();
    session.dispose();
    // occupation = +10, age = +40, state = -10, validLicense = 1, initialScore=100
    assertEquals(141.0, applicant.getTotalScore(), 0.0);
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Applicant(org.drools.scorecards.example.Applicant) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 5 with PMML

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

the class ExternalObjectModelTest method testPMMLCustomOutput.

@Test
public void testPMMLCustomOutput() throws Exception {
    PMML pmmlDocument = null;
    String drl = null;
    if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_externalmodel.xls"))) {
        pmmlDocument = scorecardCompiler.getPMMLDocument();
        assertNotNull(pmmlDocument);
        PMML4Compiler.dumpModel(pmmlDocument, System.out);
        drl = scorecardCompiler.getDRL();
        assertTrue(drl != null && !drl.isEmpty());
    // System.out.println(drl);
    } else {
        fail("failed to parse scoremodel Excel.");
    }
    for (Object serializable : pmmlDocument.getAssociationModelsAndBaselineModelsAndClusteringModels()) {
        if (serializable instanceof Scorecard) {
            Scorecard scorecard = (Scorecard) serializable;
            for (Object obj : scorecard.getExtensionsAndCharacteristicsAndMiningSchemas()) {
                if (obj instanceof Output) {
                    Output output = (Output) obj;
                    final List<OutputField> outputFields = output.getOutputFields();
                    assertEquals(1, outputFields.size());
                    final OutputField outputField = outputFields.get(0);
                    assertNotNull(outputField);
                    assertEquals("totalScore", outputField.getName());
                    assertEquals("Final Score", outputField.getDisplayName());
                    assertEquals("double", outputField.getDataType().value());
                    assertEquals("predictedValue", outputField.getFeature().value());
                    final Extension extension = ScorecardPMMLUtils.getExtension(outputField.getExtensions(), PMMLExtensionNames.EXTERNAL_CLASS);
                    assertNotNull(extension);
                    assertEquals("org.drools.scorecards.example.Applicant", extension.getValue());
                    return;
                }
            }
        }
    }
    fail();
}
Also used : Extension(org.dmg.pmml.pmml_4_2.descr.Extension) Output(org.dmg.pmml.pmml_4_2.descr.Output) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) OutputField(org.dmg.pmml.pmml_4_2.descr.OutputField) Scorecard(org.dmg.pmml.pmml_4_2.descr.Scorecard) Test(org.junit.Test)

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