Search in sources :

Example 16 with PMML

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

the class DecisionTreeTest method testMissingAggregate.

@Test
public void testMissingAggregate() 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.AGGREGATE_NODES);
        }
    }
    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(45.0);
    kSession.getEntryPoint("in_Fld2").insert(90.0);
    kSession.getEntryPoint("in_Fld3").insert("miss");
    kSession.fireAllRules();
    Object token = getToken(kSession);
    assertEquals(0.47, (Double) tok.get(token, "confidence"), 1e-2);
    assertEquals("null", tok.get(token, "current"));
    assertEquals(60.0, tok.get(token, "totalCount"));
    checkFirstDataFieldOfTypeStatus(tgt, true, false, "Missing", "tgtY");
    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 17 with PMML

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

the class DecisionTreeTest method testMissingTreeNull.

@Test
public void testMissingTreeNull() 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.NULL_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.0, (Double) tok.get(token, "confidence"), 1e-6);
    assertEquals("null", tok.get(token, "current"));
    assertEquals(0.0, tok.get(token, "totalCount"));
    assertEquals(0, getKSession().getObjects(new ClassObjectFilter(tgt.getFactClass())).size());
    checkGeneratedRules();
}
Also used : TreeModel(org.dmg.pmml.pmml_4_2.descr.TreeModel) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) 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 18 with PMML

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

the class DrlFromPMMLTest method setUp.

@Before
public void setUp() throws Exception {
    ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
    if (scorecardCompiler.compileFromExcel(PMMLDocumentTest.class.getResourceAsStream("/scoremodel_c.xls"))) {
        PMML pmmlDocument = scorecardCompiler.getPMMLDocument();
        assertNotNull(pmmlDocument);
        PMML4Compiler.dumpModel(pmmlDocument, System.out);
        drl = scorecardCompiler.getDRL();
    } else {
        fail("failed to parse scoremodel Excel.");
    }
}
Also used : PMML(org.dmg.pmml.pmml_4_2.descr.PMML) Before(org.junit.Before)

Example 19 with PMML

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

the class ExternalObjectModelTest method testDRLExecution.

@Test
public void testDRLExecution() 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.");
    }
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("test_scorecard_rules.drl").setResourceType(ResourceType.DRL));
    KieBuilder kieBuilder = ks.newKieBuilder(kfs);
    Results res = kieBuilder.buildAll().getResults();
    System.err.print(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.fireAllRules();
    session.dispose();
    // occupation = 0, age = 30, validLicence -1
    assertEquals(29.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;
    assertEquals(-1.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
    assertEquals(41.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 20 with PMML

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

the class ExternalObjectModelTest method testWithReasonCodes.

@Test
public void testWithReasonCodes() 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_reasoncode")) {
        pmmlDocument2 = scorecardCompiler2.getPMMLDocument();
        PMML4Compiler.dumpModel(pmmlDocument2, System.out);
        assertNotNull(pmmlDocument2);
        drl2 = scorecardCompiler2.getDRL();
    // System.out.println(drl2);
    } else {
        for (ScorecardError error : scorecardCompiler2.getScorecardParseErrors()) {
            System.out.println(error.getErrorLocation() + ":" + error.getErrorMessage());
        }
        fail("failed to parse scoremodel Excel (scorecards_reasoncode).");
    }
    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();
    KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
    KieBase kbase = kieContainer.getKieBase();
    KieSession session = kbase.newKieSession();
    FactType scorecardInternalsType = kbase.getFactType(PMML4Helper.pmmlDefaultPackageName(), "ScoreCard");
    Applicant applicant = new Applicant();
    applicant.setAge(10);
    session.insert(applicant);
    // session.addEventListener(new DebugWorkingMemoryEventListener());
    session.fireAllRules();
    // occupation = 0, age = 30, validLicence -1, initialScore=100
    assertEquals(129.0, applicant.getTotalScore(), 0.0);
    assertEquals("VL0099", applicant.getReasonCodes());
    Object scorecardInternals = session.getObjects(new ClassObjectFilter(scorecardInternalsType.getFactClass())).iterator().next();
    Assert.assertEquals(129.0, scorecardInternalsType.get(scorecardInternals, "score"));
    Map reasonCodesMap = (Map) scorecardInternalsType.get(scorecardInternals, "ranking");
    Assert.assertNotNull(reasonCodesMap);
    Assert.assertEquals(Arrays.asList("VL0099", "AGE02"), new ArrayList(reasonCodesMap.keySet()));
    session.dispose();
    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) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) FactType(org.kie.api.definition.type.FactType) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) Results(org.kie.api.builder.Results) KieBase(org.kie.api.KieBase) PMML(org.dmg.pmml.pmml_4_2.descr.PMML) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Applicant(org.drools.scorecards.example.Applicant) Map(java.util.Map) KieContainer(org.kie.api.runtime.KieContainer) 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