Search in sources :

Example 41 with PMMLRequestData

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

the class MiningmodelTest method testSelectSecondSegmentFirst.

@Test
public void testSelectSecondSegmentFirst() {
    RuleUnitExecutor executor = createExecutor(source1);
    // KieRuntimeLogger console = ((InternalRuleUnitExecutor)executor).addConsoleLogger();
    PMMLRequestData request = new PMMLRequestData("1234", "SampleMine");
    request.addRequestParam(new ParameterInfo<>("1234", "fld1", Double.class, 45.0));
    request.addRequestParam(new ParameterInfo<>("1234", "fld2", Double.class, 60.0));
    request.addRequestParam(new ParameterInfo<>("1234", "fld6", String.class, "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();
    resultData.forEach(rd -> {
        assertEquals(request.getCorrelationId(), rd.getCorrelationId());
        assertEquals("OK", rd.getResultCode());
        if (rd.getSegmentationId() == null) {
            assertNotNull(rd.getResultValue("Fld5", null));
            String value = rd.getResultValue("Fld5", "value", String.class).orElse(null);
            assertEquals("tgtZ", value);
            AbstractTreeToken token = rd.getResultValue("MissingTreeToken", null, AbstractTreeToken.class).orElse(null);
            assertNotNull(token);
            assertEquals(0.6, token.getConfidence().doubleValue(), 0.0);
            assertEquals("null", token.getCurrent());
        }
    });
    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) AbstractTreeToken(org.kie.pmml.pmml_4_2.model.tree.AbstractTreeToken) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 42 with PMMLRequestData

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

the class ScorecardTest method createRequest.

protected PMMLRequestData createRequest(String correlationId, String model, Double age, String occupation, String residenceState, boolean validLicense) {
    PMMLRequestData data = new PMMLRequestData(correlationId, model);
    data.addRequestParam("age", age);
    data.addRequestParam("occupation", occupation);
    data.addRequestParam("residenceState", residenceState);
    data.addRequestParam("validLicense", validLicense);
    return data;
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData)

Example 43 with PMMLRequestData

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

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

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

the class ScorecardTest method testScorecardOutputs.

@Test
public void testScorecardOutputs() throws Exception {
    // RuleUnitExecutor.create().bind(kbase);
    RuleUnitExecutor executor = createExecutor(source2);
    PMMLRequestData requestData = new PMMLRequestData("123", "SampleScorecard");
    requestData.addRequestParam("cage", "engineering");
    requestData.addRequestParam("age", 25);
    requestData.addRequestParam("wage", 500.0);
    PMML4Result resultHolder = new PMML4Result();
    List<String> possiblePackages = calculatePossiblePackageNames("SampleScorecard");
    Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(unitClass);
    executor.run(unitClass);
    data.insert(requestData);
    resultData.insert(resultHolder);
    executor.run(unitClass);
    assertEquals("OK", resultHolder.getResultCode());
    assertEquals(6, resultHolder.getResultVariables().size());
    assertNotNull(resultHolder.getResultValue("OutRC1", null));
    assertNotNull(resultHolder.getResultValue("OutRC2", null));
    assertNotNull(resultHolder.getResultValue("OutRC3", null));
    assertEquals("RC2", resultHolder.getResultValue("OutRC1", "value"));
    assertEquals("RC1", resultHolder.getResultValue("OutRC2", "value"));
    assertEquals("RC1", resultHolder.getResultValue("OutRC3", "value"));
}
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

PMMLRequestData (org.kie.api.pmml.PMMLRequestData)68 Test (org.junit.Test)53 PMML4Result (org.kie.api.pmml.PMML4Result)30 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)28 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)17 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)17 KieBase (org.kie.api.KieBase)16 KiePMMLMiningField (org.kie.pmml.commons.model.KiePMMLMiningField)16 ParameterInfo (org.kie.api.pmml.ParameterInfo)14 KiePMMLInterval (org.kie.pmml.commons.model.expressions.KiePMMLInterval)14 Map (java.util.Map)13 PMMLExecutor (org.kie.pmml.pmml_4_2.PMMLExecutor)11 ArrayList (java.util.ArrayList)9 Arrays (java.util.Arrays)9 Collections (java.util.Collections)9 List (java.util.List)9 Optional (java.util.Optional)9 IntStream (java.util.stream.IntStream)9 Assert.assertTrue (org.junit.Assert.assertTrue)9 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)9