Search in sources :

Example 46 with PMMLRequestData

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

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

the class ScorecardTest method testMultipleInputData.

@Test
public void testMultipleInputData() throws Exception {
    RuleUnitExecutor[] executor = new RuleUnitExecutor[3];
    PMMLRequestData[] requestData = new PMMLRequestData[3];
    PMML4Result[] resultHolder = new PMML4Result[3];
    Resource res = ResourceFactory.newClassPathResource(source1);
    kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
    executor[0] = RuleUnitExecutor.create().bind(kbase);
    executor[1] = RuleUnitExecutor.create().bind(kbase);
    executor[2] = RuleUnitExecutor.create().bind(kbase);
    DataSource<PMMLRequestData>[] requests = new DataSource[3];
    DataSource<PMML4Result>[] results = new DataSource[3];
    DataSource<PMML4Data>[] pmmlDatas = new DataSource[3];
    Double[] expectedScores = new Double[3];
    expectedScores[0] = 41.345;
    expectedScores[1] = 26.345;
    expectedScores[2] = 39.345;
    LinkedHashMap<String, Double>[] expectedResults = new LinkedHashMap[3];
    expectedResults[0] = new LinkedHashMap<>();
    expectedResults[0].put("LX00", -1.0);
    expectedResults[0].put("RES", -10.0);
    expectedResults[0].put("CX2", -30.0);
    expectedResults[1] = new LinkedHashMap<>();
    expectedResults[1].put("RES", 10.0);
    expectedResults[1].put("LX00", -1.0);
    expectedResults[1].put("OCC", -10.0);
    expectedResults[1].put("ABZ", -25.0);
    expectedResults[2] = new LinkedHashMap<>();
    expectedResults[2].put("LX00", 1.0);
    expectedResults[2].put("OCC", -5.0);
    expectedResults[2].put("RES", -5.0);
    expectedResults[2].put("CX1", -30.0);
    requestData[0] = createRequest("123", "Sample Score", 33.0, "SKYDIVER", "KN", true);
    requestData[1] = createRequest("124", "Sample Score", 50.0, "TEACHER", "AP", true);
    requestData[2] = createRequest("125", "Sample Score", 10.0, "STUDENT", "TN", false);
    for (int x = 0; x < 3; x++) {
        requests[x] = executor[x].newDataSource("request");
        results[x] = executor[x].newDataSource("results");
        pmmlDatas[x] = executor[x].newDataSource("pmmlData");
        resultHolder[x] = new PMML4Result(requestData[x].getCorrelationId());
    }
    List<String> possiblePackages = calculatePossiblePackageNames("Sample Score", "org.drools.scorecards.example");
    Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(unitClass);
    for (int x = 0; x < 3; x++) {
        executor[x].run(unitClass);
    }
    for (int y = 0; y < 3; y++) {
        requests[y].insert(requestData[y]);
        results[y].insert(resultHolder[y]);
    }
    for (int z = 0; z < 3; z++) {
        executor[z].run(unitClass);
    }
    for (int p = 0; p < 3; p++) {
        checkResult(resultHolder[p], expectedScores[p], expectedResults[p]);
    }
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) Resource(org.kie.api.io.Resource) KieHelper(org.kie.internal.utils.KieHelper) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DataSource(org.kie.api.runtime.rule.DataSource) LinkedHashMap(java.util.LinkedHashMap) 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 48 with PMMLRequestData

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

the class SimpleRegressionTest method testRegression.

@Test
public void testRegression() throws Exception {
    RuleUnitExecutor executor = createExecutor(source1);
    PMMLRequestData request = new PMMLRequestData("123", "LinReg");
    request.addRequestParam("fld1", 0.9);
    request.addRequestParam("fld2", 0.3);
    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);
    assertEquals("OK", resultHolder.getResultCode());
    assertNotNull(resultHolder.getResultValue("Fld4", null));
    Double value = resultHolder.getResultValue("Fld4", "value", Double.class).orElse(null);
    assertNotNull(value);
    double chkVal = 0.5 + 5 * 0.9 * 0.9 + 2 * 0.3 - 3.0 + 0.4 * 0.9 * 0.3;
    chkVal = 1.0 / (1.0 + Math.exp(-chkVal));
    assertEquals(chkVal, value, 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)

Example 49 with PMMLRequestData

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

the class MiningSegmentTransfer method getOutboundRequest.

public PMMLRequestData getOutboundRequest() {
    if (outboundRequest == null) {
        outboundRequest = new PMMLRequestData(this.correlationId);
        outboundRequest.setSource("MiningSegmentTransfer:" + this.fromSegmentId + "-" + this.toSegmentId);
        for (String requestField : requestFromResultMap.keySet()) {
            String resultFieldName = requestFromResultMap.get(requestField);
            Object resultFieldValue = getValueFromResult(resultFieldName);
            if (resultFieldValue != null) {
                outboundRequest.addRequestParam(requestField, resultFieldValue);
            }
        }
    }
    return outboundRequest;
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData)

Example 50 with PMMLRequestData

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

the class PMMLCommandExecutorImpl method execute.

/**
 * Evaluate the given <code>PMMLRequestData<code>
 * @param pmmlRequestData : it must contain the pmml file name (in the <i>source</i> property)
 * @param context and the model name
 * @return
 */
@Override
public PMML4Result execute(final PMMLRequestData pmmlRequestData, final Context context) {
    final PMMLRequestData cleanedRequestData = getCleanedRequestData(pmmlRequestData);
    validate(cleanedRequestData);
    final String pmmlFileName = cleanedRequestData.getSource();
    final String pmmlModelName = cleanedRequestData.getModelName();
    final PMMLRuntime pmmlRuntime = getPMMLRuntime(pmmlFileName, pmmlModelName, ((RegistryContext) context).lookup(KieBase.class));
    return evaluate(cleanedRequestData, pmmlRuntime);
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) KieBase(org.kie.api.KieBase) PMMLRuntime(org.kie.pmml.api.runtime.PMMLRuntime)

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