Search in sources :

Example 6 with ScoreCard

use of org.kie.pmml.pmml_4_2.model.ScoreCard in project drools by kiegroup.

the class ScorecardTest method testScorecardWithComplexPartialScore.

@Test
public void testScorecardWithComplexPartialScore() {
    KieBase kieBase = PMMLKieBaseUtil.createKieBaseWithPMML(SOURCE_COMPLEX_PARTIAL_SCORE_SCORECARD);
    PMMLExecutor executor = new PMMLExecutor(kieBase);
    PMMLRequestData requestData = new PMMLRequestData("123", "ComplexPartialScoreScorecard");
    requestData.addRequestParam("param", 5.0);
    PMML4Result resultHolder = executor.run(requestData);
    Assertions.assertThat(resultHolder).isNotNull();
    double score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(20);
    requestData = new PMMLRequestData("123", "ComplexPartialScoreScorecard");
    requestData.addRequestParam("param", 40.0);
    resultHolder = executor.run(requestData);
    Assertions.assertThat(resultHolder).isNotNull();
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(150);
    requestData = new PMMLRequestData("123", "ComplexPartialScoreScorecard");
    requestData.addRequestParam("param", 100.0);
    resultHolder = executor.run(requestData);
    Assertions.assertThat(resultHolder).isNotNull();
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(205);
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) KieBase(org.kie.api.KieBase) PMMLExecutor(org.kie.pmml.pmml_4_2.PMMLExecutor) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 7 with ScoreCard

use of org.kie.pmml.pmml_4_2.model.ScoreCard in project drools by kiegroup.

the class ScorecardTest method testSimpleScorecard.

@Test
public void testSimpleScorecard() {
    KieBase kieBase = PMMLKieBaseUtil.createKieBaseWithPMML(SOURCE_SIMPLE_SCORECARD);
    PMMLExecutor executor = new PMMLExecutor(kieBase);
    PMMLRequestData requestData = new PMMLRequestData("123", "SimpleScorecard");
    requestData.addRequestParam("param1", 10.0);
    requestData.addRequestParam("param2", 15.0);
    PMML4Result resultHolder = executor.run(requestData);
    double score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(40.8);
    Map<String, Double> rankingMap = (Map<String, Double>) resultHolder.getResultValue("ScoreCard", "ranking");
    Assertions.assertThat(rankingMap.get("reasonCh1")).isEqualTo(5);
    Assertions.assertThat(rankingMap.get("reasonCh2")).isEqualTo(-6);
    requestData = new PMMLRequestData("123", "SimpleScorecard");
    requestData.addRequestParam("param1", 51.0);
    requestData.addRequestParam("param2", 12.0);
    resultHolder = executor.run(requestData);
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(120.8);
    rankingMap = (Map<String, Double>) resultHolder.getResultValue("ScoreCard", "ranking");
    Assertions.assertThat(rankingMap.get("reasonCh1")).isEqualTo(-75);
    Assertions.assertThat(rankingMap.get("reasonCh2")).isEqualTo(-6);
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) KieBase(org.kie.api.KieBase) PMMLExecutor(org.kie.pmml.pmml_4_2.PMMLExecutor) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 8 with ScoreCard

use of org.kie.pmml.pmml_4_2.model.ScoreCard in project drools by kiegroup.

the class ScorecardTest method testScorecard.

@Test
public void testScorecard() throws Exception {
    RuleUnitExecutor executor = createExecutor(source1);
    PMMLRequestData requestData = createRequest("123", "Sample Score", 33.0, "SKYDIVER", "KN", true);
    PMML4Result resultHolder = new PMML4Result();
    List<String> possiblePackages = calculatePossiblePackageNames("Sample Score", "org.drools.scorecards.example");
    Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(unitClass);
    executor.run(unitClass);
    Collection<? extends EntryPoint> eps = ((InternalRuleUnitExecutor) executor).getKieSession().getEntryPoints();
    eps.forEach(ep -> {
        System.out.println(ep);
    });
    data.insert(requestData);
    resultData.insert(resultHolder);
    executor.run(unitClass);
    assertEquals(3, resultHolder.getResultVariables().size());
    Object scorecard = resultHolder.getResultValue("ScoreCard", null);
    assertNotNull(scorecard);
    Double score = resultHolder.getResultValue("ScoreCard", "score", Double.class).orElse(null);
    assertEquals(41.345, score, 0.000);
    Object ranking = resultHolder.getResultValue("ScoreCard", "ranking");
    assertNotNull(ranking);
    assertTrue(ranking instanceof LinkedHashMap);
    LinkedHashMap map = (LinkedHashMap) ranking;
    assertTrue(map.containsKey("LX00"));
    assertTrue(map.containsKey("RES"));
    assertTrue(map.containsKey("CX2"));
    assertEquals(-1.0, map.get("LX00"));
    assertEquals(-10.0, map.get("RES"));
    assertEquals(-30.0, map.get("CX2"));
    Iterator iter = map.keySet().iterator();
    assertEquals("LX00", iter.next());
    assertEquals("RES", iter.next());
    assertEquals("CX2", iter.next());
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Iterator(java.util.Iterator) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 9 with ScoreCard

use of org.kie.pmml.pmml_4_2.model.ScoreCard in project drools by kiegroup.

the class ScorecardTest method testScorecardWithCompoundPredicate.

@Test
public void testScorecardWithCompoundPredicate() {
    KieBase kieBase = PMMLKieBaseUtil.createKieBaseWithPMML(SOURCE_COMPOUND_PREDICATE_SCORECARD);
    PMMLExecutor executor = new PMMLExecutor(kieBase);
    PMMLRequestData requestData = new PMMLRequestData("123", "ScorecardCompoundPredicate");
    requestData.addRequestParam("param1", 41.0);
    requestData.addRequestParam("param2", 21.0);
    PMML4Result resultHolder = executor.run(requestData);
    double score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(120.8);
    Map<String, Double> rankingMap = (Map<String, Double>) resultHolder.getResultValue("ScoreCard", "ranking");
    Assertions.assertThat(rankingMap.get("reasonCh1")).isEqualTo(50);
    Assertions.assertThat(rankingMap.get("reasonCh2")).isEqualTo(5);
    requestData = new PMMLRequestData("123", "ScorecardCompoundPredicate");
    requestData.addRequestParam("param1", 40.0);
    requestData.addRequestParam("param2", 25.0);
    resultHolder = executor.run(requestData);
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(120.8);
    requestData = new PMMLRequestData("123", "ScorecardCompoundPredicate");
    requestData.addRequestParam("param1", 40.0);
    requestData.addRequestParam("param2", 55.0);
    resultHolder = executor.run(requestData);
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(210.8);
    requestData = new PMMLRequestData("123", "ScorecardCompoundPredicate");
    requestData.addRequestParam("param1", 4.0);
    requestData.addRequestParam("param2", -25.0);
    resultHolder = executor.run(requestData);
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(30.8);
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) KieBase(org.kie.api.KieBase) PMMLExecutor(org.kie.pmml.pmml_4_2.PMMLExecutor) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 10 with ScoreCard

use of org.kie.pmml.pmml_4_2.model.ScoreCard in project drools by kiegroup.

the class PMML4ModelFactory method getModel.

public PMML4Model getModel(Segment segment, MiningSegmentation segmentation) {
    PMML4Model model = null;
    if (segment.getMiningModel() != null) {
        MiningModel mm = segment.getMiningModel();
        model = new Miningmodel(mm.getModelName(), mm, segmentation.getOwner(), null);
    } else if (segment.getRegressionModel() != null) {
        RegressionModel rm = segment.getRegressionModel();
        model = new Regression(rm.getModelName(), rm, segmentation.getOwner(), null);
    } else if (segment.getScorecard() != null) {
        Scorecard sc = segment.getScorecard();
        model = new ScorecardModel(sc.getModelName(), sc, segmentation.getOwner(), null);
    } else if (segment.getTreeModel() != null) {
        TreeModel tm = segment.getTreeModel();
        model = new Treemodel(tm.getModelName(), tm, segmentation.getOwner(), null);
    }
    return model;
}
Also used : TreeModel(org.kie.dmg.pmml.pmml_4_2.descr.TreeModel) MiningModel(org.kie.dmg.pmml.pmml_4_2.descr.MiningModel) PMML4Model(org.kie.pmml.pmml_4_2.PMML4Model) Scorecard(org.kie.dmg.pmml.pmml_4_2.descr.Scorecard) RegressionModel(org.kie.dmg.pmml.pmml_4_2.descr.RegressionModel)

Aggregations

Test (org.junit.Test)8 PMML4Result (org.kie.api.pmml.PMML4Result)8 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)8 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)8 LinkedHashMap (java.util.LinkedHashMap)5 KieBase (org.kie.api.KieBase)5 PMMLExecutor (org.kie.pmml.pmml_4_2.PMMLExecutor)5 Map (java.util.Map)4 Iterator (java.util.Iterator)3 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)3 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)3 MiningModel (org.kie.dmg.pmml.pmml_4_2.descr.MiningModel)2 RegressionModel (org.kie.dmg.pmml.pmml_4_2.descr.RegressionModel)2 Scorecard (org.kie.dmg.pmml.pmml_4_2.descr.Scorecard)2 TreeModel (org.kie.dmg.pmml.pmml_4_2.descr.TreeModel)2 PMML4Model (org.kie.pmml.pmml_4_2.PMML4Model)2 ScoreCard (org.kie.pmml.pmml_4_2.model.ScoreCard)2 SegmentExecution (org.kie.pmml.pmml_4_2.model.mining.SegmentExecution)2 ArrayList (java.util.ArrayList)1