Search in sources :

Example 71 with NodeList

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

the class KiePMMLClusteringModelFactory method clusterCreationExprFrom.

private static ObjectCreationExpr clusterCreationExprFrom(Cluster cluster) {
    NodeList<Expression> arguments = new NodeList<>();
    arguments.add(literalExprFrom(cluster.getId()));
    arguments.add(literalExprFrom(cluster.getName()));
    if (cluster.getArray() != null && cluster.getArray().getType() == Array.Type.REAL) {
        String arrayStringValue = (String) cluster.getArray().getValue();
        String[] arrayStringChunks = arrayStringValue.split(" ");
        try {
            Arrays.stream(arrayStringChunks).map(Double::parseDouble).map(DoubleLiteralExpr::new).forEach(arguments::add);
        } catch (NumberFormatException e) {
            logger.error("Can't parse \"real\" cluster with value \"" + arrayStringValue + "\"", e);
        }
    }
    return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLCluster.class.getCanonicalName()), arguments);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 72 with NodeList

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

the class KiePMMLClusteringModelFactory method clusteringFieldCreationExprFrom.

private static ObjectCreationExpr clusteringFieldCreationExprFrom(ClusteringField clusteringField) {
    double fieldWeight = clusteringField.getFieldWeight() == null ? 1.0 : clusteringField.getFieldWeight().doubleValue();
    boolean isCenterField = clusteringField.getCenterField() == null || clusteringField.getCenterField() == ClusteringField.CenterField.TRUE;
    NodeList<Expression> arguments = new NodeList<>();
    arguments.add(literalExprFrom(clusteringField.getField().getValue()));
    arguments.add(new DoubleLiteralExpr(fieldWeight));
    arguments.add(new BooleanLiteralExpr(isCenterField));
    arguments.add(clusteringField.getCompareFunction() == null ? new NullLiteralExpr() : literalExprFrom(compareFunctionFrom(clusteringField.getCompareFunction())));
    arguments.add(new NullLiteralExpr());
    return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLClusteringField.class.getCanonicalName()), arguments);
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) NodeList(com.github.javaparser.ast.NodeList) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 73 with NodeList

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

the class KiePMMLModelFactoryUtils method createIntervalsExpression.

static Expression createIntervalsExpression(List<Interval> intervals) {
    ExpressionStmt arraysAsListStmt = CommonCodegenUtils.createArraysAsListExpression();
    MethodCallExpr arraysCallExpression = arraysAsListStmt.getExpression().asMethodCallExpr();
    NodeList<Expression> arguments = new NodeList<>();
    intervals.forEach(value -> arguments.add(getObjectCreationExprFromInterval(value)));
    arraysCallExpression.setArguments(arguments);
    arraysAsListStmt.setExpression(arraysCallExpression);
    return arraysAsListStmt.getExpression();
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) CommonCodegenUtils.addListPopulationByMethodCallExpr(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByMethodCallExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 74 with NodeList

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

the class KiePMMLModelFactoryUtils method commonPopulateGetCreatedKiePMMLMiningFieldsMethod.

/**
 * Populate the <code>getCreatedKiePMMLMiningFields</code> method
 * @param methodDeclaration
 * @param miningFields
 * @param fields
 */
static void commonPopulateGetCreatedKiePMMLMiningFieldsMethod(final MethodDeclaration methodDeclaration, final List<org.dmg.pmml.MiningField> miningFields, final List<org.dmg.pmml.Field<?>> fields) {
    BlockStmt body = new BlockStmt();
    NodeList<Expression> arguments = new NodeList<>();
    for (org.dmg.pmml.MiningField miningField : miningFields) {
        String miningFieldVariableName = getSanitizedVariableName(miningField.getName().getValue()).toLowerCase();
        BlockStmt toAdd = getMiningFieldVariableDeclaration(miningFieldVariableName, miningField, fields);
        toAdd.getStatements().forEach(body::addStatement);
        arguments.add(new NameExpr(miningFieldVariableName));
    }
    MethodCallExpr methodCallExpr = new MethodCallExpr();
    methodCallExpr.setScope(new NameExpr(Arrays.class.getSimpleName()));
    methodCallExpr.setName("asList");
    methodCallExpr.setArguments(arguments);
    ReturnStmt returnStmt = new ReturnStmt();
    returnStmt.setExpression(methodCallExpr);
    body.addStatement(returnStmt);
    methodDeclaration.setBody(body);
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) CommonCodegenUtils.getReturnStmt(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getReturnStmt) CommonCodegenUtils.addListPopulationByMethodCallExpr(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByMethodCallExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 75 with NodeList

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

the class KiePMMLApplyFactory method getApplyVariableDeclaration.

static BlockStmt getApplyVariableDeclaration(final String variableName, final Apply apply) {
    final MethodDeclaration methodDeclaration = APPLY_TEMPLATE.getMethodsByName(GETKIEPMMLAPPLY).get(0).clone();
    final BlockStmt applyBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(applyBody, APPLY).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, APPLY, applyBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    int counter = 0;
    final NodeList<Expression> arguments = new NodeList<>();
    for (org.dmg.pmml.Expression expression : apply.getExpressions()) {
        String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
        arguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, expression);
        toAdd.getStatements().forEach(toReturn::addStatement);
        counter++;
    }
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, APPLY, toReturn))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
    final StringLiteralExpr functionExpr = new StringLiteralExpr(apply.getFunction());
    builder.setArgument(0, nameExpr);
    builder.setArgument(2, functionExpr);
    getChainedMethodCallExprFrom("withDefaultValue", initializer).setArgument(0, getExpressionForObject(apply.getDefaultValue()));
    getChainedMethodCallExprFrom("withMapMissingTo", initializer).setArgument(0, getExpressionForObject(apply.getMapMissingTo()));
    final Expression invalidTreatmentExpr = apply.getInvalidValueTreatment() != null ? new StringLiteralExpr(apply.getInvalidValueTreatment().value()) : new NullLiteralExpr();
    getChainedMethodCallExprFrom("withInvalidValueTreatmentMethod", initializer).setArgument(0, invalidTreatmentExpr);
    getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
    applyBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) 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) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

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