Search in sources :

Example 1 with PMML4Data

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

the class PMML4DataFactory method newPMML4Data.

public <T> PMML4Data newPMML4Data(String correlationId, String name, String context, String displayName, Class<T> clazz, T value, Double weight, Boolean valid, Boolean missing) {
    PMML4Data data = null;
    String parmTypeName = clazz.getName();
    if (parmTypeName == null || parmTypeName.trim().isEmpty()) {
        String errMsg = "PMML4DataFactory::Parameter: " + name + " - type is null or blank";
        throw new IllegalArgumentException(errMsg);
    }
    if (!mapOfKnownTypes.containsKey(parmTypeName)) {
        String errMsg = "PMML4DataFactory::Parameter: " + name + " - is of unregistered type: " + parmTypeName;
        throw new RuntimeException(errMsg);
    }
    Class<? extends PMML4Data> pmmlDataClass = mapOfKnownTypes.get(parmTypeName);
    if (pmmlDataClass != null) {
        try {
            data = pmmlDataClass.getDeclaredConstructor(String.class, String.class, String.class, String.class, clazz, Double.class, Boolean.class, Boolean.class).newInstance(correlationId, name, context, name, value, weight, valid, missing);
        } catch (Exception rx) {
            String errMsg = "PMML4DataFactory::Unable create data object from ParameterInfo::Parameter: " + name;
            throw new RuntimeException(errMsg, rx);
        }
    }
    return data;
}
Also used : PMML4Data(org.kie.api.pmml.PMML4Data)

Example 2 with PMML4Data

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

the class DecisionTreeTest method testMissingTreeWeighted2.

@Test
public void testMissingTreeWeighted2() 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, "miss"));
    PMML4Result resultHolder = new PMML4Result();
    DataSource<PMMLRequestData> data = executor.newDataSource("request");
    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);
    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 token = (AbstractTreeToken) resultHolder.getResultValue("MissingTreeToken", null);
    assertNotNull(token);
    assertEquals(0.6, token.getConfidence(), 1e-6);
    assertEquals("null", token.getCurrent());
    assertEquals(100.0, token.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 3 with PMML4Data

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

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

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

the class PMML4DataFactory method copy.

public PMML4DataType copy(PMML4Data source) {
    PMML4Data data = null;
    Class<?> srcValueClass = source.getValueClass();
    if (srcValueClass == null) {
        String errMsg = "PMML4DataFactory::Copying " + source.getName() + " - Unable to determine the class of the source's value";
        throw new RuntimeException(errMsg);
    }
    String className = source.getValueClass().getName();
    if (!mapOfKnownTypes.containsKey(className)) {
        String errMsg = "PMML4DataFactory::Copying " + source.getName() + " - value is of unregistered type: " + className;
        throw new RuntimeException(errMsg);
    }
    Class<? extends PMML4Data> pmmlDataClass = mapOfKnownTypes.get(className);
    if (pmmlDataClass != null) {
        try {
            data = pmmlDataClass.getDeclaredConstructor(String.class, String.class, String.class, String.class, srcValueClass, Double.class, Boolean.class, Boolean.class).newInstance(source.getCorrelationId(), source.getName(), source.getContext(), source.getDisplayValue(), source.getValue(), source.getWeight(), source.isValid(), source.isMissing());
        } catch (Exception rx) {
            String errMsg = "PMML4DataFactory::Copying - Unable to create copy from source " + source.getName();
            throw new RuntimeException(errMsg, rx);
        }
    }
    return data;
}
Also used : PMML4Data(org.kie.api.pmml.PMML4Data)

Aggregations

PMML4Data (org.kie.api.pmml.PMML4Data)5 InternalRuleUnitExecutor (org.drools.core.impl.InternalRuleUnitExecutor)3 KieRuntimeLogger (org.kie.api.logger.KieRuntimeLogger)3 PMML4Result (org.kie.api.pmml.PMML4Result)3 PMMLRequestData (org.kie.api.pmml.PMMLRequestData)3 RuleUnitExecutor (org.kie.api.runtime.rule.RuleUnitExecutor)3 Test (org.junit.Test)2 KieBase (org.kie.api.KieBase)2 Resource (org.kie.api.io.Resource)2 KieHelper (org.kie.internal.utils.KieHelper)2 DroolsAbstractPMMLTest (org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)2 AbstractTreeToken (org.kie.pmml.pmml_4_2.model.tree.AbstractTreeToken)2