use of org.kie.pmml.api.models.MiningField in project drools by kiegroup.
the class PreProcessTest method convertInputDataNotConvertibles.
@Test(expected = KiePMMLException.class)
public void convertInputDataNotConvertibles() {
List<KiePMMLMiningField> miningFields = IntStream.range(0, 3).mapToObj(i -> {
DATA_TYPE dataType = DATA_TYPE.values()[i];
new MiningField("FIELD-" + i, null, null, dataType, null, null, null, null, null, null);
return KiePMMLMiningField.builder("FIELD-" + i, null).withDataType(dataType).build();
}).collect(Collectors.toList());
PMMLRequestData pmmlRequestData = new PMMLRequestData("123", "modelName");
pmmlRequestData.addRequestParam("FIELD-0", 123);
pmmlRequestData.addRequestParam("FIELD-1", true);
pmmlRequestData.addRequestParam("FIELD-2", "123");
PreProcess.convertInputData(miningFields, pmmlRequestData);
}
use of org.kie.pmml.api.models.MiningField 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());
});
}
use of org.kie.pmml.api.models.MiningField in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method init.
/**
* Initialize the given <code>ClassOrInterfaceDeclaration</code> with all the <b>common</b> code needed to
* generate a <code>KiePMMLModel</code>
* @param compilationDTO
* @param modelTemplate
*/
public static void init(final CompilationDTO<? extends Model> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final String name = compilationDTO.getModelName();
final String generatedClassName = compilationDTO.getSimpleClassName();
final List<MiningField> miningFields = compilationDTO.getKieMiningFields();
final List<OutputField> outputFields = compilationDTO.getKieOutputFields();
final List<TargetField> targetFields = compilationDTO.getKieTargetFields();
final Expression miningFunctionExpression;
if (compilationDTO.getMINING_FUNCTION() != null) {
MINING_FUNCTION miningFunction = compilationDTO.getMINING_FUNCTION();
miningFunctionExpression = new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name());
} else {
miningFunctionExpression = new NullLiteralExpr();
}
final PMML_MODEL pmmlModelEnum = compilationDTO.getPMML_MODEL();
final NameExpr pmmlMODELExpression = new NameExpr(pmmlModelEnum.getClass().getName() + "." + pmmlModelEnum.name());
String targetFieldName = compilationDTO.getTargetFieldName();
final Expression targetFieldExpression;
if (targetFieldName != null) {
targetFieldExpression = new StringLiteralExpr(targetFieldName);
} else {
targetFieldExpression = new NullLiteralExpr();
}
setKiePMMLModelConstructor(generatedClassName, constructorDeclaration, name, miningFields, outputFields, targetFields);
addTransformationsInClassOrInterfaceDeclaration(modelTemplate, compilationDTO.getTransformationDictionary(), compilationDTO.getLocalTransformations());
final BlockStmt body = constructorDeclaration.getBody();
CommonCodegenUtils.setAssignExpressionValue(body, "pmmlMODEL", pmmlMODELExpression);
CommonCodegenUtils.setAssignExpressionValue(body, "miningFunction", miningFunctionExpression);
CommonCodegenUtils.setAssignExpressionValue(body, "targetField", targetFieldExpression);
addGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
MethodCallExpr getCreatedKiePMMLMiningFieldsExpr = new MethodCallExpr();
getCreatedKiePMMLMiningFieldsExpr.setScope(new ThisExpr());
getCreatedKiePMMLMiningFieldsExpr.setName(GET_CREATED_KIEPMMLMININGFIELDS);
CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLMiningFields", getCreatedKiePMMLMiningFieldsExpr);
if (compilationDTO.getOutput() != null) {
addGetCreatedKiePMMLOutputFieldsMethod(modelTemplate, compilationDTO.getOutput().getOutputFields());
MethodCallExpr getCreatedKiePMMLOutputFieldsExpr = new MethodCallExpr();
getCreatedKiePMMLOutputFieldsExpr.setScope(new ThisExpr());
getCreatedKiePMMLOutputFieldsExpr.setName(GET_CREATED_KIEPMMLOUTPUTFIELDS);
CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLOutputFields", getCreatedKiePMMLOutputFieldsExpr);
}
}
use of org.kie.pmml.api.models.MiningField in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method setKiePMMLModelConstructor.
@Test
public void setKiePMMLModelConstructor() {
String generatedClassName = "generatedClassName";
String name = "newName";
List<MiningField> miningFields = IntStream.range(0, 3).mapToObj(i -> ModelUtils.convertToKieMiningField(getRandomMiningField(), getRandomDataField())).collect(Collectors.toList());
List<OutputField> outputFields = IntStream.range(0, 2).mapToObj(i -> ModelUtils.convertToKieOutputField(getRandomOutputField(), getRandomDataField())).collect(Collectors.toList());
List<TargetField> targetFields = IntStream.range(0, 2).mapToObj(i -> ModelUtils.convertToKieTargetField(getRandomTarget())).collect(Collectors.toList());
KiePMMLModelFactoryUtils.setKiePMMLModelConstructor(generatedClassName, constructorDeclaration, name, miningFields, outputFields, targetFields);
commonVerifySuperInvocation(generatedClassName, name);
List<MethodCallExpr> retrieved = getMethodCallExprList(constructorDeclaration.getBody(), miningFields.size(), "miningFields", "add");
MethodCallExpr addMethodCall = retrieved.get(0);
NodeList<Expression> arguments = addMethodCall.getArguments();
commonVerifyMiningFieldsObjectCreation(arguments, miningFields);
retrieved = getMethodCallExprList(constructorDeclaration.getBody(), outputFields.size(), "outputFields", "add");
addMethodCall = retrieved.get(0);
arguments = addMethodCall.getArguments();
commonVerifyOutputFieldsObjectCreation(arguments, outputFields);
retrieved = getMethodCallExprList(constructorDeclaration.getBody(), outputFields.size(), "kiePMMLTargets", "add");
addMethodCall = retrieved.get(0);
arguments = addMethodCall.getArguments();
commonVerifyKiePMMLTargetFieldsMethodCallExpr(arguments, targetFields);
}
use of org.kie.pmml.api.models.MiningField in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method getMiningFieldsObjectCreations.
@Test
public void getMiningFieldsObjectCreations() {
List<MiningField> miningFields = IntStream.range(0, 3).mapToObj(i -> ModelUtils.convertToKieMiningField(getRandomMiningField(), getRandomDataField())).collect(Collectors.toList());
List retrieved = KiePMMLModelFactoryUtils.getMiningFieldsObjectCreations(miningFields);
commonVerifyMiningFieldsObjectCreation(retrieved, miningFields);
}
Aggregations