Search in sources :

Example 11 with PMMLRequestData

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

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

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

the class UserDefinedFunctionsTest method testFunctions0.

@Test
public void testFunctions0() throws Exception {
    setKSession(getModelSession(source0, VERBOSE));
    setKbase(getKSession().getKieBase());
    FactType userAge1 = getKbase().getFactType(packageName, "UserAge");
    PMMLRequestData request = new PMMLRequestData("TestSimple");
    request.addRequestParam("Age", 2.2);
    getKSession().insert(request);
    // getKSession().getEntryPoint( "in_Age" ).insert( 2.2 );
    getKSession().fireAllRules();
    System.out.println(reportWMObjects(getKSession()));
    checkFirstDataFieldOfTypeStatus(userAge1, true, false, null, 22.0);
    checkGeneratedRules();
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) FactType(org.kie.api.definition.type.FactType) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest) Test(org.junit.Test)

Example 14 with PMMLRequestData

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

Example 15 with PMMLRequestData

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

the class PMMLExecutor method run.

private PMML4Result run(PMMLRequestData requestData, Optional<PMML4Data> data) {
    KieRuntimeLogger logger = null;
    RuleUnitExecutor ruleUnitExecutor = RuleUnitExecutor.create().bind(kieBase);
    if (runWithLogging) {
        if (loggingFileName != null) {
            logger = ((InternalRuleUnitExecutor) ruleUnitExecutor).addFileLogger(loggingFileName);
        } else {
            logger = ((InternalRuleUnitExecutor) ruleUnitExecutor).addConsoleLogger();
        }
    }
    DataSource<PMMLRequestData> requestDataSource = ruleUnitExecutor.newDataSource("request");
    ;
    DataSource<PMML4Result> resultDataSource = ruleUnitExecutor.newDataSource("results");
    DataSource<PMML4Data> pmmlDataSource = ruleUnitExecutor.newDataSource("pmmlData");
    requestDataSource.insert(requestData);
    PMML4Result resultHandler = new PMML4Result();
    resultDataSource.insert(resultHandler);
    if (data.isPresent()) {
        pmmlDataSource.insert(data.get());
    }
    try {
        ruleUnitExecutor.run(startingRuleUnit("RuleUnitIndicator", requestData.getModelName(), "org.drools.scorecards.example"));
    } finally {
        if (logger != null) {
            logger.close();
        }
    }
    return resultHandler;
}
Also used : PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) PMML4Data(org.kie.api.pmml.PMML4Data)

Aggregations

PMMLRequestData (org.kie.api.pmml.PMMLRequestData)32 Test (org.junit.Test)29 PMML4Result (org.kie.api.pmml.PMML4Result)28 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)13 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 FactType (org.kie.api.definition.type.FactType)1 ParameterInfo (org.kie.api.pmml.ParameterInfo)1