Search in sources :

Example 1 with KiePMMLNameValue

use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.

the class CommonCodegenUtils method getFilteredKiePMMLNameValueExpression.

/**
 * Returns
 * <pre>
 *  Optional<KiePMMLNameValue> kiePMMLNameValue = (<i>kiePMMLNameValueListParam</i>)
 *      .stream()
 *      .filter((KiePMMLNameValue kpmmlnv) -> Objects.equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName()))
 *      .findFirst();
 * </pre>
 * <p>
 * expression, where <b>kiePMMLNameValueListParam</b> is the name of the
 * <code>List&lt;KiePMMLNameValue&gt;</code> parameter, and
 * <b>fieldNameToRef</b> is the name of the field to find, in the containing method
 * @param kiePMMLNameValueListParam
 * @param fieldNameToRef
 * @param stringLiteralComparison if <code>true</code>, equals comparison is made on the String, e.g Objects
 * .equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName())),
 * otherwise, is done on object reference,  e.g Objects.equals((<i>fieldNameToRef</i>), kpmmlnv.getName())). In
 * this latter case, a <i>fieldNameToRef</i> variable is
 * expected to exists
 * @return
 */
public static ExpressionStmt getFilteredKiePMMLNameValueExpression(final String kiePMMLNameValueListParam, final String fieldNameToRef, boolean stringLiteralComparison) {
    // kpmmlnv.getName()
    MethodCallExpr argumentBodyExpressionArgument2 = new MethodCallExpr("getName");
    argumentBodyExpressionArgument2.setScope(new NameExpr(LAMBDA_PARAMETER_NAME));
    // Objects.equals(fieldNameToRef, kpmmlnv.getName())
    MethodCallExpr argumentBodyExpression = new MethodCallExpr("equals");
    Expression equalsComparisonExpression;
    if (stringLiteralComparison) {
        equalsComparisonExpression = new StringLiteralExpr(fieldNameToRef);
    } else {
        equalsComparisonExpression = new NameExpr(fieldNameToRef);
    }
    argumentBodyExpression.setArguments(NodeList.nodeList(equalsComparisonExpression, argumentBodyExpressionArgument2));
    argumentBodyExpression.setScope(new NameExpr(Objects.class.getName()));
    ExpressionStmt argumentBody = new ExpressionStmt(argumentBodyExpression);
    // (KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef, kpmmlnv.getName())
    Parameter argumentParameter = new Parameter(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()), LAMBDA_PARAMETER_NAME);
    LambdaExpr argument = new LambdaExpr();
    // 
    argument.setEnclosingParameters(true).setParameters(NodeList.nodeList(argumentParameter));
    // (KiePMMLNameValue kpmmlnv) ->
    // Objects.equals(fieldNameToRef, kpmmlnv.getName())
    argument.setBody(argumentBody);
    // kiePMMLNameValueListParam.stream()
    MethodCallExpr initializerScopeScope = new MethodCallExpr("stream");
    initializerScopeScope.setScope(new NameExpr(kiePMMLNameValueListParam));
    // kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv)  -> Objects.equals(fieldNameToRef,
    // kpmmlnv.getName()))
    MethodCallExpr initializerScope = new MethodCallExpr("filter");
    initializerScope.setScope(initializerScopeScope);
    initializerScope.setArguments(NodeList.nodeList(argument));
    // kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv)  -> Objects.equals(fieldNameToRef,
    // kpmmlnv.getName())).findFirst()
    MethodCallExpr initializer = new MethodCallExpr("findFirst");
    initializer.setScope(initializerScope);
    // Optional<KiePMMLNameValue> kiePMMLNameValue
    VariableDeclarator variableDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Optional.class.getName(), Collections.singletonList(KiePMMLNameValue.class.getName())), OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME);
    // Optional<KiePMMLNameValue> kiePMMLNameValue = kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue
    // kpmmlnv)  -> Objects.equals(fieldNameToRef, kpmmlnv.getName())).findFirst()
    variableDeclarator.setInitializer(initializer);
    // 
    VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(NodeList.nodeList(variableDeclarator));
    ExpressionStmt toReturn = new ExpressionStmt();
    toReturn.setExpression(variableDeclarationExpr);
    return toReturn;
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 2 with KiePMMLNameValue

use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.

the class PreProcessTest method executeTransformations.

@Test
public void executeTransformations() {
    // <DefineFunction name="CUSTOM_FUNCTION" optype="continuous" dataType="double">
    // <ParameterField name="PARAM_1" />
    // <ParameterField name="PARAM_2" />
    // <Apply function="/">
    // <FieldRef>PARAM_1</FieldRef>
    // <FieldRef>PARAM_2</FieldRef>
    // </Apply>
    // </DefineFunction>
    final KiePMMLParameterField kiePMMLParameterField1 = KiePMMLParameterField.builder(PARAM_1, Collections.emptyList()).build();
    final KiePMMLParameterField kiePMMLParameterField2 = KiePMMLParameterField.builder(PARAM_2, Collections.emptyList()).build();
    final KiePMMLFieldRef kiePMMLFieldRef1 = new KiePMMLFieldRef(PARAM_1, Collections.emptyList(), null);
    final KiePMMLFieldRef kiePMMLFieldRef2 = new KiePMMLFieldRef(PARAM_2, Collections.emptyList(), null);
    final KiePMMLApply kiePMMLApplyRef = KiePMMLApply.builder("NAMEREF", Collections.emptyList(), "/").withKiePMMLExpressions(Arrays.asList(kiePMMLFieldRef1, kiePMMLFieldRef2)).build();
    final KiePMMLDefineFunction defineFunction = new KiePMMLDefineFunction(CUSTOM_FUNCTION, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, Arrays.asList(kiePMMLParameterField1, kiePMMLParameterField2), kiePMMLApplyRef);
    // <DerivedField name="CUSTOM_REF_FIELD" optype="continuous" dataType="double">
    // <Apply function="CUSTOM_FUNCTION">
    // <FieldRef>INPUT_FIELD</FieldRef>
    // <Constant>5.0</Constant>
    // </Apply>
    // </DerivedField>
    final KiePMMLFieldRef kiePMMLFieldRef3 = new KiePMMLFieldRef(INPUT_FIELD, Collections.emptyList(), null);
    final KiePMMLConstant kiePMMLConstant1 = new KiePMMLConstant(PARAM_2, Collections.emptyList(), value2, null);
    final KiePMMLApply kiePMMLApply = KiePMMLApply.builder("NAME", Collections.emptyList(), CUSTOM_FUNCTION).withKiePMMLExpressions(Arrays.asList(kiePMMLFieldRef3, kiePMMLConstant1)).build();
    final KiePMMLDerivedField derivedField = KiePMMLDerivedField.builder(CUSTOM_REF_FIELD, Collections.emptyList(), DATA_TYPE.DOUBLE, OP_TYPE.CONTINUOUS, kiePMMLApply).build();
    // From TransformationDictionary
    KiePMMLTransformationDictionary transformationDictionary = KiePMMLTransformationDictionary.builder("transformationDictionary", Collections.emptyList()).withDefineFunctions(Collections.singletonList(defineFunction)).withDerivedFields(Collections.singletonList(derivedField)).build();
    KiePMMLTestingModel kiePMMLModel = KiePMMLTestingModel.builder("TESTINGMODEL", Collections.emptyList(), MINING_FUNCTION.REGRESSION).withKiePMMLTransformationDictionary(transformationDictionary).build();
    // 
    final PMMLRequestData pmmlRequestData = new PMMLRequestData();
    pmmlRequestData.addRequestParam(INPUT_FIELD, value1);
    Map<String, ParameterInfo> mappedRequestParams = pmmlRequestData.getMappedRequestParams();
    final List<KiePMMLNameValue> kiePMMLNameValues = PreProcess.getKiePMMLNameValuesFromParameterInfos(mappedRequestParams.values());
    Optional<KiePMMLNameValue> retrieved = kiePMMLNameValues.stream().filter(kiePMMLNameValue -> kiePMMLNameValue.getName().equals(INPUT_FIELD)).findFirst();
    assertTrue(retrieved.isPresent());
    assertEquals(value1, retrieved.get().getValue());
    ProcessingDTO processingDTO = new ProcessingDTO(kiePMMLModel, kiePMMLNameValues);
    PreProcess.executeTransformations(processingDTO, pmmlRequestData);
    mappedRequestParams = pmmlRequestData.getMappedRequestParams();
    Object expected = value1 / value2;
    assertTrue(mappedRequestParams.containsKey(CUSTOM_REF_FIELD));
    assertEquals(expected, mappedRequestParams.get(CUSTOM_REF_FIELD).getValue());
    retrieved = kiePMMLNameValues.stream().filter(kiePMMLNameValue -> kiePMMLNameValue.getName().equals(CUSTOM_REF_FIELD)).findFirst();
    assertTrue(retrieved.isPresent());
    assertEquals(expected, retrieved.get().getValue());
}
Also used : IntStream(java.util.stream.IntStream) CLOSURE(org.kie.pmml.api.enums.CLOSURE) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) Arrays(java.util.Arrays) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) KiePMMLInterval(org.kie.pmml.commons.model.expressions.KiePMMLInterval) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) ArrayList(java.util.ArrayList) MiningField(org.kie.pmml.api.models.MiningField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) Map(java.util.Map) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) ParameterInfo(org.kie.api.pmml.ParameterInfo) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) List(java.util.List) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) Assert.assertNull(org.junit.Assert.assertNull) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) Optional(java.util.Optional) Collections(java.util.Collections) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Assert.assertEquals(org.junit.Assert.assertEquals) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) KiePMMLApply(org.kie.pmml.commons.model.expressions.KiePMMLApply) KiePMMLTestingModel(org.kie.pmml.commons.testingutility.KiePMMLTestingModel) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) ParameterInfo(org.kie.api.pmml.ParameterInfo) KiePMMLConstant(org.kie.pmml.commons.model.expressions.KiePMMLConstant) KiePMMLTransformationDictionary(org.kie.pmml.commons.transformations.KiePMMLTransformationDictionary) ProcessingDTO(org.kie.pmml.commons.model.ProcessingDTO) KiePMMLDefineFunction(org.kie.pmml.commons.transformations.KiePMMLDefineFunction) KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLParameterField(org.kie.pmml.commons.transformations.KiePMMLParameterField) KiePMMLFieldRef(org.kie.pmml.commons.model.expressions.KiePMMLFieldRef) Test(org.junit.Test)

Example 3 with KiePMMLNameValue

use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.

the class PreProcess method executeTransformations.

/**
 * Execute <b>transformations</b> on input data.
 * @param processingDTO
 * @param requestData
 * @see <a href="http://dmg.org/pmml/v4-4/Transformations.html">Transformations</a>
 * @see
 * <a href="http://dmg.org/pmml/v4-4/Transformations.html#xsdElement_LocalTransformations">LocalTransformations</a>
 */
static void executeTransformations(final ProcessingDTO processingDTO, final PMMLRequestData requestData) {
    logger.debug("executeTransformations {} {}", processingDTO, requestData);
    for (KiePMMLDerivedField derivedField : processingDTO.getDerivedFields()) {
        Object derivedValue = derivedField.evaluate(processingDTO);
        if (derivedValue != null) {
            requestData.addRequestParam(derivedField.getName(), derivedValue);
            processingDTO.addKiePMMLNameValue(new KiePMMLNameValue(derivedField.getName(), derivedValue));
        }
    }
}
Also used : KiePMMLDerivedField(org.kie.pmml.commons.transformations.KiePMMLDerivedField) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue)

Example 4 with KiePMMLNameValue

use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.

the class PMMLMiningModelEvaluator method getPMML4Result.

PMML4Result getPMML4Result(final KiePMMLMiningModel toEvaluate, final LinkedHashMap<String, KiePMMLNameValueProbabilityMapTuple> inputData, final PMMLContext pmmlContext) {
    final MULTIPLE_MODEL_METHOD multipleModelMethod = toEvaluate.getSegmentation().getMultipleModelMethod();
    Object result = null;
    LinkedHashMap<String, Double> probabilityResultMap = null;
    ResultCode resultCode = OK;
    final LinkedHashMap<String, KiePMMLNameValue> toUseForPrediction = new LinkedHashMap<>();
    final LinkedHashMap<String, List<KiePMMLNameValue>> toUseForProbability = new LinkedHashMap<>();
    inputData.forEach((key, value) -> {
        toUseForPrediction.put(key, value.predictionValue);
        toUseForProbability.put(key, value.probabilityValues);
    });
    try {
        if (MINING_FUNCTION.CLASSIFICATION.equals(toEvaluate.getMiningFunction())) {
            result = multipleModelMethod.applyClassification(toUseForPrediction);
            probabilityResultMap = multipleModelMethod.applyProbability(toUseForProbability);
        } else {
            result = multipleModelMethod.applyPrediction(toUseForPrediction);
        }
    } catch (KieEnumException e) {
        logger.warn(e.getMessage());
        resultCode = FAIL;
    }
    pmmlContext.setProbabilityResultMap(probabilityResultMap);
    PMML4Result toReturn = new PMML4Result();
    toReturn.addResultVariable(toEvaluate.getTargetField(), result);
    toReturn.setResultObjectName(toEvaluate.getTargetField());
    toReturn.setResultCode(resultCode.getName());
    return toReturn;
}
Also used : PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) KieEnumException(org.kie.pmml.api.exceptions.KieEnumException) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) ResultCode(org.kie.pmml.api.enums.ResultCode) MULTIPLE_MODEL_METHOD(org.kie.pmml.models.mining.model.enums.MULTIPLE_MODEL_METHOD)

Example 5 with KiePMMLNameValue

use of org.kie.pmml.commons.model.tuples.KiePMMLNameValue in project drools by kiegroup.

the class PMMLMiningModelEvaluatorTest method getKiePMMLNameRawObject.

@Test
public void getKiePMMLNameRawObject() {
    final Object rawObject = "OBJ";
    final PMML4Result pmml4Result = getPMML4Result(rawObject);
    RAW_OBJECT_METHODS.forEach(multipleModelMethod -> {
        KiePMMLNameValue retrieved = evaluator.getKiePMMLNameValue(pmml4Result, multipleModelMethod, 34.2);
        assertEquals(pmml4Result.getResultObjectName(), retrieved.getName());
        assertNotNull(retrieved.getValue());
        assertEquals(rawObject, retrieved.getValue());
    });
}
Also used : PMML4Result(org.kie.api.pmml.PMML4Result) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) PMMLContextTest(org.kie.pmml.commons.testingutility.PMMLContextTest) Test(org.junit.Test)

Aggregations

KiePMMLNameValue (org.kie.pmml.commons.model.tuples.KiePMMLNameValue)44 Test (org.junit.Test)37 CommonTestingUtility.getProcessingDTO (org.kie.pmml.commons.CommonTestingUtility.getProcessingDTO)19 ProcessingDTO (org.kie.pmml.commons.model.ProcessingDTO)19 LinkedHashMap (java.util.LinkedHashMap)16 ArrayList (java.util.ArrayList)8 List (java.util.List)8 HashMap (java.util.HashMap)7 PMML4Result (org.kie.api.pmml.PMML4Result)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 IntStream (java.util.stream.IntStream)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 KiePMMLFieldRef (org.kie.pmml.commons.model.expressions.KiePMMLFieldRef)6 KiePMMLDerivedField (org.kie.pmml.commons.transformations.KiePMMLDerivedField)6 Collections (java.util.Collections)5 Assert.assertNotNull (org.junit.Assert.assertNotNull)5 Arrays (java.util.Arrays)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 PMMLContextTest (org.kie.pmml.commons.testingutility.PMMLContextTest)4