use of org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD in project drools by kiegroup.
the class BooleanFunctionsTest method getIsMissingValueCorrectInput.
@Test
public void getIsMissingValueCorrectInput() {
Object[] input1 = { null };
Object retrieved = BooleanFunctions.IS_MISSING.getValue(input1, EMPTY_MINING_FIELD);
assertTrue((boolean) retrieved);
Object[] input2 = { 35 };
retrieved = BooleanFunctions.IS_MISSING.getValue(input2, EMPTY_MINING_FIELD);
assertFalse((boolean) retrieved);
for (INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod : INVALID_VALUE_TREATMENT_METHOD.values()) {
MiningField referredByFieldRef = getReferredByFieldRef(invalidValueTreatmentMethod, null, Arrays.asList(new Interval(20, 29), new Interval(41, 50)));
boolean expected = INVALID_VALUE_TREATMENT_METHOD.AS_MISSING.equals(invalidValueTreatmentMethod);
assertEquals(expected, BooleanFunctions.IS_MISSING.getValue(input2, referredByFieldRef));
}
}
use of org.kie.pmml.api.enums.INVALID_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;
}
use of org.kie.pmml.api.enums.INVALID_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();
}
use of org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD in project drools by kiegroup.
the class PreProcess method manageInvalidValues.
/**
* Manage the <b>invalid value</b> of the given <code>ParameterInfo</code> depending on the
* <code>INVALID_VALUE_TREATMENT_METHOD</code>
* of the given <code>MiningField</code>, <b>eventually adding the ParameterInfo to the list of the ones to be
* removed from input data</b>
* @param miningField
* @param parameterInfo
* @param toRemove
*/
static void manageInvalidValues(final KiePMMLMiningField miningField, final ParameterInfo parameterInfo, final List<ParameterInfo> toRemove) {
INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod = miningField.getInvalidValueTreatmentMethod() != null ? miningField.getInvalidValueTreatmentMethod() : INVALID_VALUE_TREATMENT_METHOD.RETURN_INVALID;
Object originalValue = parameterInfo.getValue();
switch(invalidValueTreatmentMethod) {
case RETURN_INVALID:
throw new KiePMMLInputDataException("Invalid value " + originalValue + " for " + miningField.getName());
case AS_MISSING:
toRemove.add(parameterInfo);
break;
case AS_IS:
break;
case AS_VALUE:
String invalidValueReplacement = miningField.getInvalidValueReplacement();
if (invalidValueReplacement == null) {
throw new KiePMMLInputDataException("Missing required invalidValueReplacement for " + miningField.getName());
} else {
Object requiredValue = miningField.getDataType().getActualValue(invalidValueReplacement);
parameterInfo.setType(miningField.getDataType().getMappedClass());
parameterInfo.setValue(requiredValue);
}
break;
default:
throw new KiePMMLException("Unmanaged INVALID_VALUE_TREATMENT_METHOD " + invalidValueTreatmentMethod);
}
}
use of org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD in project drools by kiegroup.
the class BooleanFunctionsTest method getIsNotMissingValueCorrectInput.
@Test
public void getIsNotMissingValueCorrectInput() {
Object[] input1 = { 35 };
Object retrieved = BooleanFunctions.IS_NOT_MISSING.getValue(input1, EMPTY_MINING_FIELD);
assertTrue((boolean) retrieved);
Object[] input2 = { null };
retrieved = BooleanFunctions.IS_NOT_MISSING.getValue(input2, EMPTY_MINING_FIELD);
assertFalse((boolean) retrieved);
for (INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod : INVALID_VALUE_TREATMENT_METHOD.values()) {
MiningField referredByFieldRef = getReferredByFieldRef(invalidValueTreatmentMethod, null, Arrays.asList(new Interval(20, 29), new Interval(41, 50)));
boolean expected = !INVALID_VALUE_TREATMENT_METHOD.AS_MISSING.equals(invalidValueTreatmentMethod);
assertEquals(expected, BooleanFunctions.IS_NOT_MISSING.getValue(input1, referredByFieldRef));
}
}
Aggregations