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();
}
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();
}
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.");
}
}
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);
}
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);
}
Aggregations