Search in sources :

Example 11 with PMML4Result

use of org.kie.api.pmml.PMML4Result in project drools by kiegroup.

the class MiningmodelTest method testWithScorecard.

@Test
public void testWithScorecard() {
    RuleUnitExecutor executor = createExecutor(source2);
    // KieRuntimeLogger console = ((InternalRuleUnitExecutor)executor).addConsoleLogger();
    PMMLRequestData request = new PMMLRequestData("1234", "SampleScorecardMine");
    request.addRequestParam("age", 33.0);
    request.addRequestParam("occupation", "SKYDIVER");
    request.addRequestParam("residenceState", "KN");
    request.addRequestParam("validLicense", true);
    PMML4Result resultHolder = new PMML4Result();
    resultHolder.setCorrelationId(request.getCorrelationId());
    DataSource<PMMLRequestData> childModelRequest = executor.newDataSource("childModelRequest");
    DataSource<PMML4Result> childModelResults = executor.newDataSource("childModelResults");
    DataSource<SegmentExecution> childModelSegments = executor.newDataSource("childModelSegments");
    DataSource<? extends AbstractPMMLData> miningModelPojo = executor.newDataSource("miningModelPojo");
    List<String> possiblePackages = this.calculatePossiblePackageNames("SampleScorecardMine");
    Class<? extends RuleUnit> ruleUnitClass = this.getStartingRuleUnit("Start Mining - SampleScorecardMine", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(ruleUnitClass);
    data.insert(request);
    resultData.insert(resultHolder);
    executor.run(ruleUnitClass);
    // console.close();
    resultData.forEach(rd -> {
        assertEquals(request.getCorrelationId(), rd.getCorrelationId());
        assertEquals("OK", rd.getResultCode());
        if (rd.getSegmentationId() == null) {
            ScoreCard sc = rd.getResultValue("ScoreCard", null, ScoreCard.class).orElse(null);
            assertNotNull(sc);
            Map map = sc.getRanking();
            assertNotNull(map);
            assertTrue(map instanceof LinkedHashMap);
            LinkedHashMap ranking = (LinkedHashMap) map;
            assertTrue(ranking.containsKey("LX00"));
            assertTrue(ranking.containsKey("RES"));
            assertTrue(ranking.containsKey("CX2"));
            assertEquals(-1.0, ranking.get("LX00"));
            assertEquals(-10.0, ranking.get("RES"));
            assertEquals(-30.0, ranking.get("CX2"));
            Iterator iter = ranking.keySet().iterator();
            assertEquals("LX00", iter.next());
            assertEquals("RES", iter.next());
            assertEquals("CX2", iter.next());
        }
    });
    int segmentsExecuted = 0;
    for (Iterator<SegmentExecution> iter = childModelSegments.iterator(); iter.hasNext(); ) {
        SegmentExecution cms = iter.next();
        assertEquals(request.getCorrelationId(), cms.getCorrelationId());
        if (cms.getState() == SegmentExecutionState.COMPLETE)
            segmentsExecuted++;
    }
    assertEquals(1, segmentsExecuted);
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) SegmentExecution(org.kie.pmml.pmml_4_2.model.mining.SegmentExecution) ScoreCard(org.kie.pmml.pmml_4_2.model.ScoreCard) LinkedHashMap(java.util.LinkedHashMap) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) Iterator(java.util.Iterator) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 12 with PMML4Result

use of org.kie.api.pmml.PMML4Result in project drools by kiegroup.

the class ScorecardTest method testScorecardWithSimpleSetPredicateWithSpaceValue.

@Test
public void testScorecardWithSimpleSetPredicateWithSpaceValue() {
    KieBase kieBase = PMMLKieBaseUtil.createKieBaseWithPMML(SOURCE_SIMPLE_SET_SPACE_VALUE_SCORECARD);
    PMMLExecutor executor = new PMMLExecutor(kieBase);
    PMMLRequestData requestData = new PMMLRequestData("123", "SimpleSetScorecardWithSpaceValue");
    requestData.addRequestParam("param", "optA");
    PMML4Result resultHolder = executor.run(requestData);
    Assertions.assertThat(resultHolder).isNotNull();
    double score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(13);
}
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 13 with PMML4Result

use of org.kie.api.pmml.PMML4Result in project drools by kiegroup.

the class ScorecardTest method testScorecardWithSimpleSetPredicate.

@Test
public void testScorecardWithSimpleSetPredicate() {
    KieBase kieBase = PMMLKieBaseUtil.createKieBaseWithPMML(SOURCE_SIMPLE_SET_SCORECARD);
    PMMLExecutor executor = new PMMLExecutor(kieBase);
    PMMLRequestData requestData = new PMMLRequestData("123", "SimpleSetScorecard");
    requestData.addRequestParam("param1", 4);
    requestData.addRequestParam("param2", "optA");
    PMML4Result resultHolder = executor.run(requestData);
    Assertions.assertThat(resultHolder).isNotNull();
    double score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(113);
    requestData = new PMMLRequestData("123", "SimpleSetScorecard");
    requestData.addRequestParam("param1", 5);
    requestData.addRequestParam("param2", "optA");
    resultHolder = executor.run(requestData);
    Assertions.assertThat(resultHolder).isNotNull();
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(33);
    requestData = new PMMLRequestData("123", "SimpleSetScorecard");
    requestData.addRequestParam("param1", -5);
    requestData.addRequestParam("param2", "optC");
    resultHolder = executor.run(requestData);
    Assertions.assertThat(resultHolder).isNotNull();
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(123);
    requestData = new PMMLRequestData("123", "SimpleSetScorecard");
    requestData.addRequestParam("param1", -5);
    requestData.addRequestParam("param2", "optA");
    resultHolder = executor.run(requestData);
    Assertions.assertThat(resultHolder).isNotNull();
    score = resultHolder.getResultValue("ScoreCard", "score", Double.class).get();
    Assertions.assertThat(score).isEqualTo(113);
}
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 14 with PMML4Result

use of org.kie.api.pmml.PMML4Result 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 15 with PMML4Result

use of org.kie.api.pmml.PMML4Result in project drools by kiegroup.

the class SimpleRegressionTest method testClassification.

@Test
public void testClassification() throws Exception {
    RuleUnitExecutor executor = createExecutor(source2);
    PMMLRequestData request = new PMMLRequestData("123", "LinReg");
    request.addRequestParam("fld1", 1.0);
    request.addRequestParam("fld2", 1.0);
    request.addRequestParam("fld3", "x");
    PMML4Result resultHolder = new PMML4Result();
    List<String> possiblePackages = calculatePossiblePackageNames("LinReg");
    Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(unitClass);
    int x = executor.run(unitClass);
    data.insert(request);
    resultData.insert(resultHolder);
    executor.run(unitClass);
    assertNotNull(resultHolder.getResultValue("RegOut", null));
    assertNotNull(resultHolder.getResultValue("RegProb", null));
    assertNotNull(resultHolder.getResultValue("RegProbA", null));
    String regOut = resultHolder.getResultValue("RegOut", "value", String.class).orElse(null);
    Double regProb = resultHolder.getResultValue("RegProb", "value", Double.class).orElse(null);
    Double regProbA = resultHolder.getResultValue("RegProbA", "value", Double.class).orElse(null);
    assertEquals("catC", regOut);
    assertEquals(0.709228, regProb, 1e-6);
    assertEquals(0.010635, regProbA, 1e-6);
}
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) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Aggregations

PMML4Result (org.kie.api.pmml.PMML4Result)30 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)28 Test (org.junit.Test)27 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)27 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)17 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)17 KieBase (org.kie.api.KieBase)14 PMMLExecutor (org.kie.pmml.pmml_4_2.PMMLExecutor)11 LinkedHashMap (java.util.LinkedHashMap)6 SegmentExecution (org.kie.pmml.pmml_4_2.model.mining.SegmentExecution)6 Map (java.util.Map)4 AbstractTreeToken (org.kie.pmml.pmml_4_2.model.tree.AbstractTreeToken)4 Iterator (java.util.Iterator)3 Resource (org.kie.api.io.Resource)3 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)3 PMML4Data (org.kie.api.pmml.PMML4Data)3 KieHelper (org.kie.internal.utils.KieHelper)3 ScoreCard (org.kie.pmml.pmml_4_2.model.ScoreCard)2 RequestContextImpl (org.drools.core.command.RequestContextImpl)1 RegistryContext (org.drools.core.command.impl.RegistryContext)1