Search in sources :

Example 6 with TargetValue

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);
}
Also used : KiePMMLTargetValue(org.kie.pmml.commons.model.KiePMMLTargetValue) TargetValue(org.kie.pmml.api.models.TargetValue) Expression(com.github.javaparser.ast.expr.Expression) TargetField(org.kie.pmml.api.models.TargetField) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 7 with TargetValue

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;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) TargetValue(org.kie.pmml.api.models.TargetValue) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CAST_INTEGER(org.kie.pmml.api.enums.CAST_INTEGER)

Aggregations

TargetValue (org.kie.pmml.api.models.TargetValue)7 Expression (com.github.javaparser.ast.expr.Expression)6 KiePMMLTargetValue (org.kie.pmml.commons.model.KiePMMLTargetValue)5 Test (org.junit.Test)4 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)3 TargetField (org.kie.pmml.api.models.TargetField)3 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)2 CAST_INTEGER (org.kie.pmml.api.enums.CAST_INTEGER)2 OP_TYPE (org.kie.pmml.api.enums.OP_TYPE)2 PMMLModelTestUtils.getRandomTargetValue (org.kie.pmml.compiler.api.testutils.PMMLModelTestUtils.getRandomTargetValue)2 ModelUtils.convertToKieTargetValue (org.kie.pmml.compiler.api.utils.ModelUtils.convertToKieTargetValue)2 NodeList (com.github.javaparser.ast.NodeList)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 BeforeClass (org.junit.BeforeClass)1 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)1 KiePMMLTarget (org.kie.pmml.commons.model.KiePMMLTarget)1