use of org.kie.pmml.api.models.TargetValue 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);
}
use of org.kie.pmml.api.models.TargetValue in project drools by kiegroup.
the class TargetFieldFactory method getTargetFieldVariableInitializer.
static ObjectCreationExpr getTargetFieldVariableInitializer(final TargetField targetField) {
final MethodDeclaration methodDeclaration = TARGET_TEMPLATE.getMethodsByName(GETTARGET_FIELD).get(0).clone();
final BlockStmt targetBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(targetBody, TARGET_FIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGET_FIELD, targetBody)));
variableDeclarator.setName(targetField.getName());
final ObjectCreationExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGET_FIELD, targetBody))).asObjectCreationExpr();
final NodeList<Expression> arguments = new NodeList<>();
if (targetField.getTargetValues() != null) {
for (TargetValue targetValue : targetField.getTargetValues()) {
arguments.add(getTargetValueVariableInitializer(targetValue));
}
}
toReturn.getArgument(0).asMethodCallExpr().setArguments(arguments);
OP_TYPE oPT = targetField.getOpType();
Expression opType = oPT != null ? new NameExpr(oPT.getClass().getName() + "." + oPT.name()) : new NullLiteralExpr();
toReturn.setArgument(1, opType);
toReturn.setArgument(2, getExpressionForObject(targetField.getField()));
CAST_INTEGER cstInt = targetField.getCastInteger();
Expression castInteger = cstInt != null ? new NameExpr(cstInt.getClass().getName() + "." + cstInt.name()) : new NullLiteralExpr();
toReturn.setArgument(3, castInteger);
toReturn.setArgument(4, getExpressionForObject(targetField.getMin()));
toReturn.setArgument(5, getExpressionForObject(targetField.getMax()));
toReturn.setArgument(6, getExpressionForObject(targetField.getRescaleConstant()));
toReturn.setArgument(7, getExpressionForObject(targetField.getRescaleFactor()));
return toReturn;
}
Aggregations