use of org.kie.pmml.api.models.TargetField 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.TargetField in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method populateGetCreatedKiePMMLTargetsMethod.
@Test
public void populateGetCreatedKiePMMLTargetsMethod() throws IOException {
Random random = new Random();
List<TargetField> kiePMMLTargets = IntStream.range(0, 3).mapToObj(i -> new TargetField(Collections.emptyList(), OP_TYPE.byName(getRandomOpType().value()), "Target-" + i, CAST_INTEGER.byName(getRandomCastInteger().value()), (double) random.nextInt(20), (double) random.nextInt(60) + 20, (double) random.nextInt(100) / 100, (double) random.nextInt(100) / 100)).collect(Collectors.toList());
String opType0 = OP_TYPE.class.getCanonicalName() + "." + kiePMMLTargets.get(0).getOpType().toString();
String castInteger0 = CAST_INTEGER.class.getCanonicalName() + "." + kiePMMLTargets.get(0).getCastInteger().toString();
String opType1 = OP_TYPE.class.getCanonicalName() + "." + kiePMMLTargets.get(1).getOpType().toString();
String castInteger1 = CAST_INTEGER.class.getCanonicalName() + "." + kiePMMLTargets.get(1).getCastInteger().toString();
String opType2 = OP_TYPE.class.getCanonicalName() + "." + kiePMMLTargets.get(2).getOpType().toString();
String castInteger2 = CAST_INTEGER.class.getCanonicalName() + "." + kiePMMLTargets.get(2).getCastInteger().toString();
KiePMMLModelFactoryUtils.populateGetCreatedKiePMMLTargetsMethod(classOrInterfaceDeclaration, kiePMMLTargets);
final MethodDeclaration retrieved = classOrInterfaceDeclaration.getMethodsByName(GET_CREATED_KIEPMMLTARGETS).get(0);
String text = getFileContent(TEST_10_SOURCE);
MethodDeclaration expected = JavaParserUtils.parseMethod(String.format(text, kiePMMLTargets.get(0).getName(), opType0, castInteger0, kiePMMLTargets.get(0).getMin(), kiePMMLTargets.get(0).getMax(), kiePMMLTargets.get(0).getRescaleConstant(), kiePMMLTargets.get(0).getRescaleFactor(), kiePMMLTargets.get(1).getName(), opType1, castInteger1, kiePMMLTargets.get(1).getMin(), kiePMMLTargets.get(1).getMax(), kiePMMLTargets.get(1).getRescaleConstant(), kiePMMLTargets.get(1).getRescaleFactor(), kiePMMLTargets.get(2).getName(), opType2, castInteger2, kiePMMLTargets.get(2).getMin(), kiePMMLTargets.get(2).getMax(), kiePMMLTargets.get(2).getRescaleConstant(), kiePMMLTargets.get(2).getRescaleFactor()));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
}
use of org.kie.pmml.api.models.TargetField in project drools by kiegroup.
the class TargetFieldFactoryTest method getTargetFieldVariableInitializer.
@Test
public void getTargetFieldVariableInitializer() throws IOException {
TargetField kieTargetField = ModelUtils.convertToKieTargetField(getRandomTarget());
ObjectCreationExpr retrieved = TargetFieldFactory.getTargetFieldVariableInitializer(kieTargetField);
String text = getFileContent(TEST_01_SOURCE);
List<TargetValue> kieTargetValues = kieTargetField.getTargetValues();
String opType = OP_TYPE.class.getCanonicalName() + "." + kieTargetField.getOpType().toString();
String castInteger = CAST_INTEGER.class.getCanonicalName() + "." + kieTargetField.getCastInteger().toString();
Expression expected = JavaParserUtils.parseExpression(String.format(text, kieTargetValues.get(0).getValue(), kieTargetValues.get(0).getDisplayValue(), kieTargetValues.get(0).getPriorProbability(), kieTargetValues.get(0).getDefaultValue(), kieTargetValues.get(1).getValue(), kieTargetValues.get(1).getDisplayValue(), kieTargetValues.get(1).getPriorProbability(), kieTargetValues.get(1).getDefaultValue(), kieTargetValues.get(2).getValue(), kieTargetValues.get(2).getDisplayValue(), kieTargetValues.get(2).getPriorProbability(), kieTargetValues.get(2).getDefaultValue(), opType, kieTargetField.getField(), castInteger, kieTargetField.getMin(), kieTargetField.getMax(), kieTargetField.getRescaleConstant(), kieTargetField.getRescaleFactor()));
assertEquals(expected.toString(), retrieved.toString());
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
List<Class<?>> imports = Arrays.asList(Arrays.class, Collections.class, KiePMMLTarget.class, KiePMMLTargetValue.class, TargetField.class, TargetValue.class);
commonValidateCompilationWithImports(retrieved, imports);
}
use of org.kie.pmml.api.models.TargetField in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method commonVerifyKiePMMLTargetFieldsMethodCallExpr.
private void commonVerifyKiePMMLTargetFieldsMethodCallExpr(List<Expression> toVerify, List<TargetField> targetFields) {
toVerify.forEach(argument -> {
assertTrue(argument instanceof MethodCallExpr);
MethodCallExpr mtdfClExpr = (MethodCallExpr) argument;
assertEquals("build", mtdfClExpr.getName().asString());
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", mtdfClExpr);
Optional<TargetField> targetFieldOpt = targetFields.stream().filter(targetField -> targetField.getName().equals(builder.getArgument(0).asStringLiteralExpr().asString())).findFirst();
assertTrue(targetFieldOpt.isPresent());
TargetField targetField = targetFieldOpt.get();
try {
commonVerifyKiePMMLTargetFieldsMethodCallExpr(mtdfClExpr, targetField);
} catch (IOException e) {
fail(e.getMessage());
}
});
}
use of org.kie.pmml.api.models.TargetField in project drools by kiegroup.
the class KiePMMLTargetFactoryTest method getKiePMMLTargetValueVariableInitializer.
@Test
public void getKiePMMLTargetValueVariableInitializer() throws IOException {
TargetField kieTargetField = ModelUtils.convertToKieTargetField(getRandomTarget());
MethodCallExpr retrieved = KiePMMLTargetFactory.getKiePMMLTargetVariableInitializer(kieTargetField);
String text = getFileContent(TEST_01_SOURCE);
List<TargetValue> kieTargetValues = kieTargetField.getTargetValues();
String opType = OP_TYPE.class.getCanonicalName() + "." + kieTargetField.getOpType().toString();
String castInteger = CAST_INTEGER.class.getCanonicalName() + "." + kieTargetField.getCastInteger().toString();
Expression expected = JavaParserUtils.parseExpression(String.format(text, kieTargetField.getName(), kieTargetValues.get(0).getValue(), kieTargetValues.get(0).getDisplayValue(), kieTargetValues.get(0).getPriorProbability(), kieTargetValues.get(0).getDefaultValue(), kieTargetValues.get(1).getValue(), kieTargetValues.get(1).getDisplayValue(), kieTargetValues.get(1).getPriorProbability(), kieTargetValues.get(1).getDefaultValue(), kieTargetValues.get(2).getValue(), kieTargetValues.get(2).getDisplayValue(), kieTargetValues.get(2).getPriorProbability(), kieTargetValues.get(2).getDefaultValue(), opType, kieTargetField.getField(), castInteger, kieTargetField.getMin(), kieTargetField.getMax(), kieTargetField.getRescaleConstant(), kieTargetField.getRescaleFactor()));
assertTrue(JavaParserUtils.equalsNode(expected, retrieved));
List<Class<?>> imports = Arrays.asList(Arrays.class, Collections.class, KiePMMLTarget.class, KiePMMLTargetValue.class, TargetField.class, TargetValue.class);
commonValidateCompilationWithImports(retrieved, imports);
}
Aggregations