Search in sources :

Example 1 with SegmentExecution

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

the class MiningmodelTest method testSelectAll.

@Test
public void testSelectAll() {
    RuleUnitExecutor executor = createExecutor(source4);
    // KieRuntimeLogger console = ((InternalRuleUnitExecutor)executor).addConsoleLogger();
    PMMLRequestData request = new PMMLRequestData("1234", "SampleSelectAllMine");
    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("SampleSelectAllMine");
    Class<? extends RuleUnit> ruleUnitClass = this.getStartingRuleUnit("Start Mining - SampleSelectAllMine", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(ruleUnitClass);
    data.insert(request);
    resultData.insert(resultHolder);
    executor.run(ruleUnitClass);
    // console.close();
    resultData.forEach(rd -> {
        assertEquals("OK", rd.getResultCode());
        assertEquals(request.getCorrelationId(), rd.getCorrelationId());
        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") || ranking.containsKey("LC00"));
        if (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());
            assertEquals(41.345, sc.getScore(), 1e-6);
        } else {
            assertTrue(ranking.containsKey("RST"));
            assertTrue(ranking.containsKey("DX2"));
            assertEquals(-1.0, ranking.get("LC00"));
            assertEquals(10.0, ranking.get("RST"));
            assertEquals(-30.0, ranking.get("DX2"));
            Iterator iter = ranking.keySet().iterator();
            assertEquals("RST", iter.next());
            assertEquals("LC00", iter.next());
            assertEquals("DX2", iter.next());
            assertEquals(21.345, sc.getScore(), 1e-6);
        }
    });
    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(2, 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 2 with SegmentExecution

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

the class MiningmodelTest method testSimpleModelChain.

@Test
public void testSimpleModelChain() {
    RuleUnitExecutor executor = createExecutor(source5);
    assertNotNull(executor);
    // KieRuntimeLogger console = ((InternalRuleUnitExecutor)executor).addConsoleLogger();
    PMMLRequestData request = new PMMLRequestData("1234", "SampleModelChainMine");
    request.addRequestParam("age", 33.0);
    request.addRequestParam("occupation", "TEACHER");
    request.addRequestParam("residenceState", "TN");
    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("SampleModelChainMine");
    Class<? extends RuleUnit> ruleUnitClass = this.getStartingRuleUnit("Start Mining - SampleModelChainMine", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(ruleUnitClass);
    data.insert(request);
    resultData.insert(resultHolder);
    executor.run(ruleUnitClass);
    // console.close();
    assertEquals("OK", resultHolder.getResultCode());
    Map<String, Object> resultVars = resultHolder.getResultVariables();
    assertNotNull(resultVars);
    assertTrue(resultVars.containsKey("QualificationLevel"));
    assertTrue(resultVars.containsKey("OverallScore"));
    String qual = resultHolder.getResultValue("QualificationLevel", "value", String.class).orElse(null);
    Double oscore = resultHolder.getResultValue("OverallScore", "value", Double.class).orElse(null);
    assertNotNull(qual);
    assertNotNull(oscore);
    assertEquals("Well", qual);
    assertEquals(56.345, oscore, 1e-6);
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) SegmentExecution(org.kie.pmml.pmml_4_2.model.mining.SegmentExecution) 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)

Example 3 with SegmentExecution

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

the class MiningmodelTest method testSelectFirstSegmentFirst.

@Test
public void testSelectFirstSegmentFirst() {
    RuleUnitExecutor executor = createExecutor(source1);
    // KieRuntimeLogger console = ((InternalRuleUnitExecutor)executor).addConsoleLogger();
    PMMLRequestData request = new PMMLRequestData("1234", "SampleMine");
    request.addRequestParam("fld1", 30.0);
    request.addRequestParam("fld2", 60.0);
    request.addRequestParam("fld3", "false");
    request.addRequestParam("fld4", "optA");
    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("SampleMine");
    Class<? extends RuleUnit> ruleUnitClass = this.getStartingRuleUnit("Start Mining - SampleMine", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(ruleUnitClass);
    data.insert(request);
    resultData.insert(resultHolder);
    executor.run(ruleUnitClass);
    // console.close();
    Collection<?> objects = ((InternalRuleUnitExecutor) executor).getSessionObjects();
    objects.forEach(o -> {
        System.out.println(o);
    });
    miningModelPojo.forEach(mmp -> {
        System.out.println(mmp);
    });
    resultData.iterator().forEachRemaining(rd -> {
        assertEquals(request.getCorrelationId(), rd.getCorrelationId());
        if (rd.getSegmentationId() == null) {
            assertEquals("OK", rd.getResultCode());
            assertNotNull(rd.getResultValue("Fld5", null));
            String value = rd.getResultValue("Fld5", "value", String.class).orElse(null);
            assertEquals("tgtY", value);
        }
    });
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) SegmentExecution(org.kie.pmml.pmml_4_2.model.mining.SegmentExecution) 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)

Example 4 with SegmentExecution

use of org.kie.pmml.pmml_4_2.model.mining.SegmentExecution 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 5 with SegmentExecution

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

the class MiningmodelTest method testWithRegression.

@Test
public void testWithRegression() {
    RuleUnitExecutor executor = createExecutor(source2);
    // KieRuntimeLogger console = ((InternalRuleUnitExecutor)executor).addConsoleLogger();
    PMMLRequestData request = new PMMLRequestData("123", "SampleScorecardMine");
    request.addRequestParam("fld1r", 1.0);
    request.addRequestParam("fld2r", 1.0);
    request.addRequestParam("fld3r", "x");
    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) {
            System.out.println(rd);
            assertNotNull(rd.getResultValue("RegOut", null));
            String regOutValue = rd.getResultValue("RegOut", "value", String.class).orElse(null);
            assertEquals("catC", regOutValue);
            assertNotNull(rd.getResultValue("RegProb", null));
            Double regProbValue = rd.getResultValue("RegProb", "value", Double.class).orElse(null);
            assertEquals(0.709228, regProbValue, 1e-6);
            assertNotNull(rd.getResultValue("RegProbA", null));
            Double regProbValueA = rd.getResultValue("RegProbA", "value", Double.class).orElse(null);
            assertEquals(0.010635, regProbValueA, 1e-6);
        }
    });
    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) 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

InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)6 Test (org.junit.Test)6 PMML4Result (org.kie.api.pmml.PMML4Result)6 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)6 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)6 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)6 SegmentExecution (org.kie.pmml.pmml_4_2.model.mining.SegmentExecution)6 Iterator (java.util.Iterator)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 ScoreCard (org.kie.pmml.pmml_4_2.model.ScoreCard)2 AbstractTreeToken (org.kie.pmml.pmml_4_2.model.tree.AbstractTreeToken)1