Search in sources :

Example 61 with NodeList

use of com.github.javaparser.ast.NodeList in project drools by kiegroup.

the class KiePMMLTransformationDictionaryFactory method addDefineFunctions.

static NodeList<Expression> addDefineFunctions(final BlockStmt body, final List<DefineFunction> defineFunctions) {
    NodeList<Expression> arguments = new NodeList<>();
    for (DefineFunction defineFunction : defineFunctions) {
        arguments.add(new NameExpr(defineFunction.getName()));
        BlockStmt toAdd = getDefineFunctionVariableDeclaration(defineFunction);
        toAdd.getStatements().forEach(body::addStatement);
    }
    return getArraysAsListInvocation(arguments);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) DefineFunction(org.dmg.pmml.DefineFunction)

Example 62 with NodeList

use of com.github.javaparser.ast.NodeList in project drools by kiegroup.

the class KiePMMLTransformationDictionaryFactory method addDerivedField.

static NodeList<Expression> addDerivedField(final BlockStmt body, final List<DerivedField> derivedFields) {
    NodeList<Expression> arguments = new NodeList<>();
    int counter = 0;
    for (DerivedField derivedField : derivedFields) {
        String nestedVariableName = String.format("transformationDictionaryDerivedField_%s", counter);
        arguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getDerivedFieldVariableDeclaration(nestedVariableName, derivedField);
        toAdd.getStatements().forEach(body::addStatement);
        counter++;
    }
    return getArraysAsListInvocation(arguments);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) DerivedField(org.dmg.pmml.DerivedField)

Example 63 with NodeList

use of com.github.javaparser.ast.NodeList 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 64 with NodeList

use of com.github.javaparser.ast.NodeList in project drools by kiegroup.

the class CommonCodegenUtils method createArraysAsListFromList.

/**
 * Create a populated <b>Arrays.asList(?... a)</b> <code>ExpressionStmt</code>
 * @param source
 * @return
 */
public static ExpressionStmt createArraysAsListFromList(List<?> source) {
    ExpressionStmt toReturn = createArraysAsListExpression();
    MethodCallExpr arraysCallExpression = toReturn.getExpression().asMethodCallExpr();
    NodeList<Expression> arguments = new NodeList<>();
    source.forEach(value -> arguments.add(getExpressionForObject(value)));
    arraysCallExpression.setArguments(arguments);
    toReturn.setExpression(arraysCallExpression);
    return toReturn;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 65 with NodeList

use of com.github.javaparser.ast.NodeList in project drools by kiegroup.

the class CommonCodegenUtils method getMethodDeclaration.

/**
 * Returns
 * <pre>
 *     empty (<i>methodName</i>)((list of <i>parameterType</i> <i>parameter name</i>)) {
 * }
 * </pre>
 * <p>
 * <p>
 * a <b>multi-parameters</b> <code>MethodDeclaration</code> whose names are the <b>key</b>s of the given
 * <code>Map</code>
 * and <b>methodArity</b>, and whose parameters types are the <b>value</b>s
 *
 * <b>The </b>
 * @param methodName
 * @param parameterNameTypeMap expecting an <b>ordered</b> map here, since parameters order matter for
 * <i>caller</i> code
 * @return
 */
public static MethodDeclaration getMethodDeclaration(final String methodName, final Map<String, ClassOrInterfaceType> parameterNameTypeMap) {
    MethodDeclaration toReturn = getMethodDeclaration(methodName);
    NodeList<Parameter> typeParameters = new NodeList<>();
    parameterNameTypeMap.forEach((parameterName, classOrInterfaceType) -> {
        Parameter toAdd = new Parameter();
        toAdd.setName(parameterName);
        toAdd.setType(classOrInterfaceType);
        typeParameters.add(toAdd);
    });
    toReturn.setParameters(typeParameters);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NodeList(com.github.javaparser.ast.NodeList) Parameter(com.github.javaparser.ast.body.Parameter)

Aggregations

NodeList (com.github.javaparser.ast.NodeList)83 Expression (com.github.javaparser.ast.expr.Expression)48 NameExpr (com.github.javaparser.ast.expr.NameExpr)40 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)37 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)36 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)29 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 List (java.util.List)22 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)18 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)18 CompilationUnit (com.github.javaparser.ast.CompilationUnit)17 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)17 ArrayList (java.util.ArrayList)17 Collectors (java.util.stream.Collectors)17 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)16 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)16 Parameter (com.github.javaparser.ast.body.Parameter)15 Test (org.junit.Test)15 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14