Search in sources :

Example 56 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLTargetFactory method getKiePMMLTargetVariableInitializer.

static MethodCallExpr getKiePMMLTargetVariableInitializer(final TargetField targetField) {
    final MethodDeclaration methodDeclaration = TARGET_TEMPLATE.getMethodsByName(GETKIEPMMLTARGET).get(0).clone();
    final BlockStmt targetBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(targetBody, TARGET).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGET, targetBody)));
    variableDeclarator.setName(targetField.getName());
    final MethodCallExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGET, targetBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", toReturn);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(targetField.getName());
    builder.setArgument(0, nameExpr);
    final ObjectCreationExpr targetFieldInstantiation = getTargetFieldVariableInitializer(targetField);
    builder.setArgument(2, targetFieldInstantiation);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 57 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class KiePMMLTransformationDictionaryFactory method getKiePMMLTransformationDictionaryVariableDeclaration.

/**
 * @param transformationDictionary
 * @return
 */
static BlockStmt getKiePMMLTransformationDictionaryVariableDeclaration(final TransformationDictionary transformationDictionary) {
    final MethodDeclaration methodDeclaration = TRANSFORMATION_DICTIONARY_TEMPLATE.getMethodsByName(GETKIEPMMLTRANSFORMATIONDICTIONARY).get(0).clone();
    final BlockStmt transformationDictionaryBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(transformationDictionaryBody, TRANSFORMATION_DICTIONARY).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TRANSFORMATION_DICTIONARY, transformationDictionaryBody)));
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TRANSFORMATION_DICTIONARY, methodDeclaration))).asMethodCallExpr();
    final BlockStmt toReturn = new BlockStmt();
    if (transformationDictionary.hasDefineFunctions()) {
        NodeList<Expression> defineFunctions = addDefineFunctions(toReturn, transformationDictionary.getDefineFunctions());
        getChainedMethodCallExprFrom("withDefineFunctions", initializer).setArguments(defineFunctions);
    }
    if (transformationDictionary.hasDerivedFields()) {
        NodeList<Expression> derivedFields = addDerivedField(toReturn, transformationDictionary.getDerivedFields());
        getChainedMethodCallExprFrom("withDerivedFields", initializer).setArguments(derivedFields);
    }
    transformationDictionaryBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 58 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException 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)

Example 59 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class CommonCodegenUtils method setExplicitConstructorInvocationStmtArgument.

/**
 * Set the <b>value</b> of the given <b>parameterName</b> in the given
 * <code>ExplicitConstructorInvocationStmt</code>
 * @param constructorInvocationStmt
 * @param parameterName
 * @param value
 * @throws KiePMMLException if the given parameter is not found
 */
public static void setExplicitConstructorInvocationStmtArgument(final ExplicitConstructorInvocationStmt constructorInvocationStmt, final String parameterName, final String value) {
    final NameExpr parameterExpr = getExplicitConstructorInvocationParameter(constructorInvocationStmt, parameterName).orElseThrow(() -> new KiePMMLException(String.format(MISSING_PARAMETER_IN_CONSTRUCTOR_INVOCATION, parameterName, constructorInvocationStmt)));
    parameterExpr.setName(value);
}
Also used : NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 60 with KiePMMLException

use of org.kie.pmml.api.exceptions.KiePMMLException in project drools by kiegroup.

the class CommonCodegenUtils method setAssignExpressionValue.

/**
 * Set the value of the variable with the given <b>assignExpressionName</b> in the given <code>BlockStmt</code>
 * It throws <code>KiePMMLException</code> if variable is not found
 * @param body
 * @param assignExpressionName
 * @param value
 * @throws <code>KiePMMLException</code> if <code>AssignExpr</code> with given <b>assignExpressionName</b> is not
 * found
 */
public static void setAssignExpressionValue(final BlockStmt body, final String assignExpressionName, final Expression value) {
    AssignExpr assignExpr = getAssignExpression(body, assignExpressionName).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, assignExpressionName, body)));
    assignExpr.setValue(value);
}
Also used : KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Aggregations

KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)109 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)49 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)40 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)38 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)37 Expression (com.github.javaparser.ast.expr.Expression)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)33 NameExpr (com.github.javaparser.ast.expr.NameExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)26 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)20 KiePMMLInternalException (org.kie.pmml.api.exceptions.KiePMMLInternalException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)17 NodeList (com.github.javaparser.ast.NodeList)13 HashMap (java.util.HashMap)12 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)10 ConstructorDeclaration (com.github.javaparser.ast.body.ConstructorDeclaration)9 ExplicitConstructorInvocationStmt (com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt)9 Test (org.junit.Test)7 IOException (java.io.IOException)6