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