Search in sources :

Example 16 with PMML4Result

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

Example 17 with PMML4Result

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

the class DecisionTreeTest method testMissingTreeWeighted1.

@Test
public void testMissingTreeWeighted1() throws Exception {
    Resource res = ResourceFactory.newClassPathResource(source2);
    KieBase kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    KieRuntimeLogger console = ((InternalRuleUnitExecutor) executor).addConsoleLogger();
    PMMLRequestData requestData = new PMMLRequestData("123", "Missing");
    requestData.addRequestParam(new ParameterInfo<>("123", "fld1", Double.class, -1.0));
    requestData.addRequestParam(new ParameterInfo<>("123", "fld2", Double.class, -1.0));
    requestData.addRequestParam(new ParameterInfo<>("123", "fld3", String.class, "optA"));
    PMML4Result resultHolder = new PMML4Result();
    // , requestData);
    DataSource<PMMLRequestData> data = executor.newDataSource("request");
    // , resultHolder);
    DataSource<PMML4Result> results = executor.newDataSource("results");
    DataSource<PMML4Data> pmmlData = executor.newDataSource("pmmlData");
    List<String> possiblePackages = calculatePossiblePackageNames("Missing");
    Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(unitClass);
    int x = executor.run(unitClass);
    data.insert(requestData);
    results.insert(resultHolder);
    int y = executor.run(unitClass);
    console.close();
    System.out.println(resultHolder);
    Collection<?> objects = ((InternalRuleUnitExecutor) executor).getSessionObjects();
    objects.forEach(o -> {
        System.out.println(o);
    });
    pmmlData.forEach(pd -> {
        System.out.println(pd);
    });
    AbstractTreeToken missingTreeToken = (AbstractTreeToken) resultHolder.getResultValue("MissingTreeToken", null);
    assertNotNull(missingTreeToken);
    assertEquals(0.8, missingTreeToken.getConfidence(), 0.0);
    assertEquals("null", missingTreeToken.getCurrent());
    assertEquals(50.0, missingTreeToken.getTotalCount(), 0.0);
    Object fld9 = resultHolder.getResultValue("Fld9", null);
    assertNotNull(fld9);
    String value = (String) resultHolder.getResultValue("Fld9", "value");
    assertNotNull(value);
    assertEquals("tgtX", value);
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) Resource(org.kie.api.io.Resource) PMML4Data(org.kie.api.pmml.PMML4Data) KieHelper(org.kie.internal.utils.KieHelper) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) AbstractTreeToken(org.kie.pmml.pmml_4_2.model.tree.AbstractTreeToken) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 18 with PMML4Result

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

the class DecisionTreeTest method testLastPredictionMissingValueStrategy.

@Test
public void testLastPredictionMissingValueStrategy() {
    KieBase kieBase = PMMLKieBaseUtil.createKieBaseWithPMML(TREE_LAST_CHILD_MISSING_STRATEGY);
    PMMLExecutor executor = new PMMLExecutor(kieBase);
    PMMLRequestData request = new PMMLRequestData("123", "TreeTest");
    request.addRequestParam("fld1", 30.0);
    PMML4Result resultHolder = executor.run(request);
    Assertions.assertThat(resultHolder).isNotNull();
    String targetValue = resultHolder.getResultValue("Fld3", "value", String.class).orElse(null);
    Assertions.assertThat(targetValue).isEqualTo("tgtY");
    request = new PMMLRequestData("123", "TreeTest");
    request.addRequestParam("fld1", 100.0);
    resultHolder = executor.run(request);
    Assertions.assertThat(resultHolder).isNotNull();
    targetValue = resultHolder.getResultValue("Fld3", "value", String.class).orElse(null);
    Assertions.assertThat(targetValue).isEqualTo("tgtA");
}
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 19 with PMML4Result

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

the class DecisionTreeTest method testReturnNullNoTrueChildPredictionStrategy.

@Test
public void testReturnNullNoTrueChildPredictionStrategy() {
    KieBase kieBase = PMMLKieBaseUtil.createKieBaseWithPMML(TREE_RETURN_NULL_NOTRUECHILD_STRATEGY);
    PMMLExecutor executor = new PMMLExecutor(kieBase);
    PMMLRequestData request = new PMMLRequestData("123", "TreeTest");
    request.addRequestParam("fld1", 30.0);
    PMML4Result resultHolder = executor.run(request);
    Assertions.assertThat(resultHolder).isNotNull();
    String targetValue = resultHolder.getResultValue("Fld2", "value", String.class).orElse(null);
    Assertions.assertThat(targetValue).isEqualTo("tgtY");
    request = new PMMLRequestData("123", "TreeTest");
    request.addRequestParam("fld1", 50.0);
    resultHolder = executor.run(request);
    Assertions.assertThat(resultHolder).isNotNull();
    Assertions.assertThat(resultHolder.getResultValue("Fld2", "value", String.class)).isEmpty();
}
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 20 with PMML4Result

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

the class DecisionTreeTest method testSimpleTree.

@Test
public void testSimpleTree() throws Exception {
    RuleUnitExecutor executor = createExecutor(source1);
    PMMLRequestData request = new PMMLRequestData("123", "TreeTest");
    request.addRequestParam("fld1", 30.0);
    request.addRequestParam("fld2", 60.0);
    request.addRequestParam("fld3", "false");
    request.addRequestParam("fld4", "optA");
    PMML4Result resultHolder = new PMML4Result();
    List<String> possiblePackages = calculatePossiblePackageNames("TreeTest");
    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());
    Object obj = resultHolder.getResultValue("Fld5", null);
    assertNotNull(obj);
    String targetValue = resultHolder.getResultValue("Fld5", "value", String.class).orElse(null);
    assertEquals("tgtY", targetValue);
}
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