Search in sources :

Example 1 with MISSING_VALUE_TREATMENT_METHOD

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

the class KiePMMLMiningFieldFactory method getMiningFieldVariableDeclaration.

static BlockStmt getMiningFieldVariableDeclaration(final String variableName, final MiningField miningField, final List<Field<?>> fields) {
    final MethodDeclaration methodDeclaration = MININGFIELD_TEMPLATE.getMethodsByName(GETKIEPMMLMININGFIELD).get(0).clone();
    final BlockStmt miningFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(miningFieldBody, MININGFIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, MININGFIELD, miningFieldBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, MININGFIELD, toReturn))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(miningField.getName().getValue());
    Expression fieldUsageTypeExpr;
    if (miningField.getUsageType() != null) {
        final FIELD_USAGE_TYPE fieldUsageType = FIELD_USAGE_TYPE.byName(miningField.getUsageType().value());
        fieldUsageTypeExpr = new NameExpr(FIELD_USAGE_TYPE.class.getName() + "." + fieldUsageType.name());
    } else {
        fieldUsageTypeExpr = new NullLiteralExpr();
    }
    Expression opTypeExpr;
    if (miningField.getOpType() != null) {
        final OP_TYPE opType = OP_TYPE.byName(miningField.getOpType().value());
        opTypeExpr = new NameExpr(OP_TYPE.class.getName() + "." + opType.name());
    } else {
        opTypeExpr = new NullLiteralExpr();
    }
    final List<Field<?>> mappedFields = getMappedFields(fields, miningField.getName().getValue());
    final DataType dataType = getDataType(mappedFields, miningField.getName().getValue());
    final DATA_TYPE data_TYPE = DATA_TYPE.byName(dataType.value());
    Expression dataTypeExpr = new NameExpr(DATA_TYPE.class.getName() + "." + data_TYPE.name());
    Expression missingValueTreatmentMethodExpr;
    if (miningField.getMissingValueTreatment() != null) {
        final MISSING_VALUE_TREATMENT_METHOD missingValueTreatmentMethod = MISSING_VALUE_TREATMENT_METHOD.byName(miningField.getMissingValueTreatment().value());
        missingValueTreatmentMethodExpr = new NameExpr(MISSING_VALUE_TREATMENT_METHOD.class.getName() + "." + missingValueTreatmentMethod.name());
    } else {
        missingValueTreatmentMethodExpr = new NullLiteralExpr();
    }
    Expression invalidValueTreatmentMethodExpr;
    if (miningField.getInvalidValueTreatment() != null) {
        final INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod = INVALID_VALUE_TREATMENT_METHOD.byName(miningField.getInvalidValueTreatment().value());
        invalidValueTreatmentMethodExpr = new NameExpr(INVALID_VALUE_TREATMENT_METHOD.class.getName() + "." + invalidValueTreatmentMethod.name());
    } else {
        invalidValueTreatmentMethodExpr = new NullLiteralExpr();
    }
    Expression missingValueReplacementExpr;
    if (miningField.getMissingValueReplacement() != null) {
        final String missingValueReplacement = miningField.getMissingValueReplacement().toString();
        missingValueReplacementExpr = new StringLiteralExpr(missingValueReplacement);
    } else {
        missingValueReplacementExpr = new NullLiteralExpr();
    }
    Expression invalidValueReplacementExpr;
    if (miningField.getInvalidValueReplacement() != null) {
        final String invalidValueReplacement = miningField.getInvalidValueReplacement().toString();
        invalidValueReplacementExpr = new StringLiteralExpr(invalidValueReplacement);
    } else {
        invalidValueReplacementExpr = new NullLiteralExpr();
    }
    DataField dataField = getMappedDataField(mappedFields);
    NodeList<Expression> allowedValuesExpressions = dataField != null ? getAllowedValuesExpressions(dataField) : new NodeList<>();
    NodeList<Expression> intervalsExpressions = dataField != null ? getIntervalsExpressions(dataField) : new NodeList<>();
    builder.setArgument(0, nameExpr);
    getChainedMethodCallExprFrom("withFieldUsageType", initializer).setArgument(0, fieldUsageTypeExpr);
    getChainedMethodCallExprFrom("withOpType", initializer).setArgument(0, opTypeExpr);
    getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpr);
    getChainedMethodCallExprFrom("withMissingValueTreatmentMethod", initializer).setArgument(0, missingValueTreatmentMethodExpr);
    getChainedMethodCallExprFrom("withInvalidValueTreatmentMethod", initializer).setArgument(0, invalidValueTreatmentMethodExpr);
    getChainedMethodCallExprFrom("withMissingValueReplacement", initializer).setArgument(0, missingValueReplacementExpr);
    getChainedMethodCallExprFrom("withInvalidValueReplacement", initializer).setArgument(0, invalidValueReplacementExpr);
    getChainedMethodCallExprFrom("withAllowedValues", initializer).getArgument(0).asMethodCallExpr().setArguments(allowedValuesExpressions);
    getChainedMethodCallExprFrom("withIntervals", initializer).getArgument(0).asMethodCallExpr().setArguments(intervalsExpressions);
    miningFieldBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Field(org.dmg.pmml.Field) MiningField(org.dmg.pmml.MiningField) DataField(org.dmg.pmml.DataField) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) DataField(org.dmg.pmml.DataField) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) DataType(org.dmg.pmml.DataType) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with MISSING_VALUE_TREATMENT_METHOD

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

the class KiePMMLMiningFieldInstanceFactory method getKiePMMLMiningField.

public static KiePMMLMiningField getKiePMMLMiningField(final MiningField toConvert, final Field<?> field) {
    String name = toConvert.getName() != null ? toConvert.getName().getValue() : "" + toConvert.hashCode();
    final FIELD_USAGE_TYPE fieldUsageType = toConvert.getUsageType() != null ? FIELD_USAGE_TYPE.byName(toConvert.getUsageType().value()) : null;
    final OP_TYPE opType = toConvert.getOpType() != null ? OP_TYPE.byName(toConvert.getOpType().value()) : null;
    final DATA_TYPE dataType = field.getDataType() != null ? DATA_TYPE.byName(field.getDataType().value()) : null;
    final MISSING_VALUE_TREATMENT_METHOD missingValueTreatmentMethod = toConvert.getMissingValueTreatment() != null ? MISSING_VALUE_TREATMENT_METHOD.byName(toConvert.getMissingValueTreatment().value()) : null;
    final INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod = toConvert.getInvalidValueTreatment() != null ? INVALID_VALUE_TREATMENT_METHOD.byName(toConvert.getInvalidValueTreatment().value()) : null;
    final String missingValueReplacement = toConvert.getMissingValueReplacement() != null ? toConvert.getMissingValueReplacement().toString() : null;
    final String invalidValueReplacement = toConvert.getInvalidValueReplacement() != null ? toConvert.getInvalidValueReplacement().toString() : null;
    final List<String> allowedValues = field instanceof DataField ? convertDataFieldValues(((DataField) field).getValues()) : Collections.emptyList();
    final List<KiePMMLInterval> intervals = field instanceof DataField ? getKiePMMLIntervals(((DataField) field).getIntervals()) : Collections.emptyList();
    final KiePMMLMiningField.Builder builder = KiePMMLMiningField.builder(name, Collections.emptyList()).withFieldUsageType(fieldUsageType).withOpType(opType).withDataType(dataType).withMissingValueTreatmentMethod(missingValueTreatmentMethod).withInvalidValueTreatmentMethod(invalidValueTreatmentMethod).withMissingValueReplacement(missingValueReplacement).withInvalidValueReplacement(invalidValueReplacement).withAllowedValues(allowedValues).withIntervals(intervals);
    return builder.build();
}
Also used : MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) DataField(org.dmg.pmml.DataField) KiePMMLInterval(org.kie.pmml.commons.model.expressions.KiePMMLInterval) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD)

Example 3 with MISSING_VALUE_TREATMENT_METHOD

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

the class PreProcessTest method manageMissingValuesNotReturnInvalid.

@Test
public void manageMissingValuesNotReturnInvalid() {
    List<MISSING_VALUE_TREATMENT_METHOD> missingValueTreatmentMethods = Arrays.stream(MISSING_VALUE_TREATMENT_METHOD.values()).filter(treatmentMethod -> !treatmentMethod.equals(MISSING_VALUE_TREATMENT_METHOD.RETURN_INVALID)).collect(Collectors.toList());
    final String fieldName = "FIELD";
    missingValueTreatmentMethods.forEach(missingValueTreatmentMethod -> {
        KiePMMLMiningField miningField = KiePMMLMiningField.builder(fieldName, null).withMissingValueTreatmentMethod(missingValueTreatmentMethod).build();
        PMMLRequestData pmmlRequestData = new PMMLRequestData();
        PreProcess.manageMissingValues(miningField, pmmlRequestData);
        assertTrue(pmmlRequestData.getRequestParams().isEmpty());
        String missingValueReplacement = "REPLACEMENT";
        miningField = KiePMMLMiningField.builder(fieldName, null).withDataType(DATA_TYPE.STRING).withMissingValueTreatmentMethod(missingValueTreatmentMethod).withMissingValueReplacement(missingValueReplacement).build();
        pmmlRequestData = new PMMLRequestData();
        PreProcess.manageMissingValues(miningField, pmmlRequestData);
        assertEquals(1, pmmlRequestData.getRequestParams().size());
        assertTrue(pmmlRequestData.getMappedRequestParams().containsKey(fieldName));
        ParameterInfo parameterInfo = pmmlRequestData.getMappedRequestParams().get(fieldName);
        assertEquals(missingValueReplacement, parameterInfo.getValue());
        assertEquals(String.class, parameterInfo.getType());
    });
}
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) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) ParameterInfo(org.kie.api.pmml.ParameterInfo) KiePMMLMiningField(org.kie.pmml.commons.model.KiePMMLMiningField) Test(org.junit.Test)

Example 4 with MISSING_VALUE_TREATMENT_METHOD

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

the class PreProcess method manageMissingValues.

/**
 * Manage the <b>missing value</b> depending on the <code>INVALID_VALUE_TREATMENT_METHOD</code>
 * of the given <code>MiningField</code>, <b>eventually adding default ont to input data</b>
 * @param miningField
 * @param requestData
 */
static void manageMissingValues(final KiePMMLMiningField miningField, final PMMLRequestData requestData) {
    MISSING_VALUE_TREATMENT_METHOD missingValueTreatmentMethod = miningField.getMissingValueTreatmentMethod() != null ? miningField.getMissingValueTreatmentMethod() : MISSING_VALUE_TREATMENT_METHOD.RETURN_INVALID;
    switch(missingValueTreatmentMethod) {
        case RETURN_INVALID:
            throw new KiePMMLInputDataException("Missing required value for " + miningField.getName());
        case AS_IS:
        case AS_MEAN:
        case AS_MODE:
        case AS_MEDIAN:
        case AS_VALUE:
            String missingValueReplacement = miningField.getMissingValueReplacement();
            if (missingValueReplacement != null) {
                Object requiredValue = miningField.getDataType().getActualValue(missingValueReplacement);
                requestData.addRequestParam(miningField.getName(), requiredValue);
            }
            break;
        default:
            throw new KiePMMLException("Unmanaged INVALID_VALUE_TREATMENT_METHOD " + missingValueTreatmentMethod);
    }
}
Also used : MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) KiePMMLInputDataException(org.kie.pmml.api.exceptions.KiePMMLInputDataException) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 5 with MISSING_VALUE_TREATMENT_METHOD

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

the class ModelUtils method convertToKieMiningField.

/**
 * Return a <code>org.kie.pmml.api.models.MiningField</code> out of a <code>org.dmg.pmml.MiningField</code> and
 * relative <code>org.dmg.pmml.DataField</code> ones
 * @param toConvert
 * @param field
 * @return
 */
public static org.kie.pmml.api.models.MiningField convertToKieMiningField(final MiningField toConvert, final Field<?> field) {
    final String name = toConvert.getName() != null ? toConvert.getName().getValue() : null;
    final FIELD_USAGE_TYPE fieldUsageType = toConvert.getUsageType() != null ? FIELD_USAGE_TYPE.byName(toConvert.getUsageType().value()) : null;
    final OP_TYPE opType = toConvert.getOpType() != null ? OP_TYPE.byName(toConvert.getOpType().value()) : null;
    final DATA_TYPE dataType = field.getDataType() != null ? DATA_TYPE.byName(field.getDataType().value()) : null;
    final MISSING_VALUE_TREATMENT_METHOD missingValueTreatmentMethod = toConvert.getMissingValueTreatment() != null ? MISSING_VALUE_TREATMENT_METHOD.byName(toConvert.getMissingValueTreatment().value()) : null;
    final INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod = toConvert.getInvalidValueTreatment() != null ? INVALID_VALUE_TREATMENT_METHOD.byName(toConvert.getInvalidValueTreatment().value()) : null;
    final String missingValueReplacement = toConvert.getMissingValueReplacement() != null ? toConvert.getMissingValueReplacement().toString() : null;
    final String invalidValueReplacement = toConvert.getInvalidValueReplacement() != null ? toConvert.getInvalidValueReplacement().toString() : null;
    final List<String> allowedValues = field instanceof DataField ? convertDataFieldValues(((DataField) field).getValues()) : Collections.emptyList();
    final List<org.kie.pmml.api.models.Interval> intervals = field instanceof DataField ? convertDataFieldIntervals(((DataField) field).getIntervals()) : Collections.emptyList();
    return new org.kie.pmml.api.models.MiningField(name, fieldUsageType, opType, dataType, missingValueTreatmentMethod, invalidValueTreatmentMethod, missingValueReplacement, invalidValueReplacement, allowedValues, intervals);
}
Also used : MiningField(org.dmg.pmml.MiningField) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) DataField(org.dmg.pmml.DataField) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) Interval(org.dmg.pmml.Interval)

Aggregations

MISSING_VALUE_TREATMENT_METHOD (org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD)5 DATA_TYPE (org.kie.pmml.api.enums.DATA_TYPE)4 INVALID_VALUE_TREATMENT_METHOD (org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD)4 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)4 DataField (org.dmg.pmml.DataField)3 FIELD_USAGE_TYPE (org.kie.pmml.api.enums.FIELD_USAGE_TYPE)3 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)3 MiningField (org.dmg.pmml.MiningField)2 KiePMMLMiningField (org.kie.pmml.commons.model.KiePMMLMiningField)2 KiePMMLInterval (org.kie.pmml.commons.model.expressions.KiePMMLInterval)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1