Search in sources :

Example 6 with PMML4Compiler

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

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();
}
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 8 with PMML4Compiler

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();
}
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 9 with PMML4Compiler

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);
}
Also used : HashMap(java.util.HashMap) MapVariableResolverFactory(org.mvel2.integration.impl.MapVariableResolverFactory) PMML(org.kie.dmg.pmml.pmml_4_2.descr.PMML) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PMML4Compiler(org.kie.pmml.pmml_4_2.PMML4Compiler) CompiledTemplate(org.mvel2.templates.CompiledTemplate)

Aggregations

Test (org.junit.Test)8 KieSession (org.kie.api.runtime.KieSession)8 DroolsAbstractPMMLTest (org.drools.pmml.pmml_4_2.DroolsAbstractPMMLTest)7 PMML4Compiler (org.drools.pmml.pmml_4_2.PMML4Compiler)7 PMML (org.dmg.pmml.pmml_4_2.descr.PMML)6 TreeModel (org.dmg.pmml.pmml_4_2.descr.TreeModel)6 FactType (org.kie.api.definition.type.FactType)6 BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 KiePackage (org.kie.api.definition.KiePackage)2 PMML4Compiler (org.kie.pmml.pmml_4_2.PMML4Compiler)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HashMap (java.util.HashMap)1 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)1 PMML (org.kie.dmg.pmml.pmml_4_2.descr.PMML)1 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)1 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)1 CompiledTemplate (org.mvel2.templates.CompiledTemplate)1