use of org.drools.pmml.pmml_4_2.PMML4Compiler in project drools by kiegroup.
the class DecisionTreeTest method testMissingTreeNone.
@Test
public void testMissingTreeNone() 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.NONE);
}
}
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("miss");
kSession.fireAllRules();
Object token = getToken(kSession);
assertEquals(0.6, (Double) tok.get(token, "confidence"), 1e-6);
assertEquals("null", tok.get(token, "current"));
assertEquals(100.0, tok.get(token, "totalCount"));
checkFirstDataFieldOfTypeStatus(tgt, true, false, "Missing", "tgtX");
checkGeneratedRules();
}
use of org.drools.pmml.pmml_4_2.PMML4Compiler 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.drools.pmml.pmml_4_2.PMML4Compiler 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.drools.pmml.pmml_4_2.PMML4Compiler in project drools by kiegroup.
the class MiningSegment method generateSegmentRules.
public String generateSegmentRules(String segmentationAgenda, int segmentIndex) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CompiledTemplate template = getLaunchTemplate();
if (template != null) {
Map<String, Object> vars = new HashMap<>();
vars.put("segmentId", getSegmentId());
vars.put("segmentationAgendaId", segmentationAgenda);
vars.put("segmentSalience", new Integer(1000 - segmentIndex));
if (predicateRuleProducer instanceof CompoundSegmentPredicate) {
CompoundSegmentPredicate predProd = (CompoundSegmentPredicate) predicateRuleProducer;
if (predProd.hasSurrogation()) {
vars.put("segmentPredicate", getSurrogationPredicateText(predProd, -1));
} else {
vars.put("segmentPredicate", predProd.getPredicateRule());
}
} else {
vars.put("segmentPredicate", predicateRuleProducer.getPredicateRule());
}
vars.put("miningPojoClass", getOwner().getOwner().getMiningPojoClassName());
TemplateRuntime.execute(template, null, new MapVariableResolverFactory(vars), baos);
}
PMML pmml = new PMML();
pmml.setDataDictionary(this.internalModel.getDataDictionary());
pmml.getAssociationModelsAndBaselineModelsAndClusteringModels().add(this.internalModel.getRawModel());
PMML4Compiler compiler = new PMML4Compiler();
String innerRules = compiler.generateTheory(pmml);
return (new String(baos.toByteArray())).concat(innerRules);
}
Aggregations