Search in sources :

Example 16 with NodeList

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

the class KiePMMLMiningFieldFactory method getIntervalsExpressions.

private static NodeList<Expression> getIntervalsExpressions(final DataField dataField) {
    final NodeList<Expression> toReturn = new NodeList<>();
    if (dataField.hasIntervals()) {
        dataField.getIntervals().forEach(interval -> {
            BlockStmt intervalStmt = getIntervalVariableDeclaration("name", interval);
            Expression toAdd = intervalStmt.getStatement(0).asExpressionStmt().getExpression().asVariableDeclarationExpr().getVariable(0).getInitializer().orElseThrow(() -> new KiePMMLInternalException(String.format("Failed to create initializer " + "for " + "Interval %s", interval)));
            toReturn.add(toAdd);
        });
    }
    return toReturn;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException)

Example 17 with NodeList

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

the class KiePMMLNormContinuousFactory method getNormContinuousVariableDeclaration.

static BlockStmt getNormContinuousVariableDeclaration(final String variableName, final NormContinuous normContinuous) {
    final MethodDeclaration methodDeclaration = NORMCONTINUOUS_TEMPLATE.getMethodsByName(GETKIEPMMLNORMCONTINUOUS).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, NORM_CONTINUOUS).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, NORM_CONTINUOUS, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NORM_CONTINUOUS, toReturn))).asObjectCreationExpr();
    final StringLiteralExpr nameExpr = new StringLiteralExpr(normContinuous.getField().getValue());
    final OUTLIER_TREATMENT_METHOD outlierTreatmentMethod = OUTLIER_TREATMENT_METHOD.byName(normContinuous.getOutliers().value());
    final NameExpr outlierTreatmentMethodExpr = new NameExpr(OUTLIER_TREATMENT_METHOD.class.getName() + "." + outlierTreatmentMethod.name());
    NodeList<Expression> arguments = new NodeList<>();
    int counter = 0;
    for (LinearNorm linearNorm : normContinuous.getLinearNorms()) {
        arguments.add(getNewKiePMMLLinearNormExpression(linearNorm, "LinearNorm-" + counter));
    }
    final Expression mapMissingToExpr = getExpressionForObject(normContinuous.getMapMissingTo());
    objectCreationExpr.getArguments().set(0, nameExpr);
    objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
    objectCreationExpr.getArguments().set(3, outlierTreatmentMethodExpr);
    objectCreationExpr.getArguments().set(4, mapMissingToExpr);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) OUTLIER_TREATMENT_METHOD(org.kie.pmml.api.enums.OUTLIER_TREATMENT_METHOD) KiePMMLLinearNorm(org.kie.pmml.commons.model.expressions.KiePMMLLinearNorm) LinearNorm(org.dmg.pmml.LinearNorm) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException)

Example 18 with NodeList

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

the class KiePMMLNormContinuousFactory method getNewKiePMMLLinearNormExpression.

static Expression getNewKiePMMLLinearNormExpression(LinearNorm linearNorm, String name) {
    ObjectCreationExpr toReturn = new ObjectCreationExpr();
    toReturn.setType(KiePMMLLinearNorm.class);
    NodeList<Expression> arguments = new NodeList<>();
    arguments.add(new StringLiteralExpr(name));
    arguments.add(new MethodCallExpr(new NameExpr("Collections"), "emptyList"));
    arguments.add(getExpressionForObject(linearNorm.getOrig()));
    arguments.add(getExpressionForObject(linearNorm.getNorm()));
    toReturn.setArguments(arguments);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 19 with NodeList

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

the class KiePMMLSimpleSetPredicateFactory method getSimpleSetPredicateVariableDeclaration.

static BlockStmt getSimpleSetPredicateVariableDeclaration(final String variableName, final SimpleSetPredicate simpleSetPredicate) {
    final MethodDeclaration methodDeclaration = SIMPLESET_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLSIMPLESETPREDICATE).get(0).clone();
    final BlockStmt simpleSetPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(simpleSetPredicateBody, SIMPLESET_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, SIMPLESET_PREDICATE, simpleSetPredicateBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final NodeList<Expression> arguments = new NodeList<>();
    List<Object> values = getObjectsFromArray(simpleSetPredicate.getArray());
    for (Object value : values) {
        arguments.add(getExpressionForObject(value));
    }
    final ARRAY_TYPE arrayType = ARRAY_TYPE.byName(simpleSetPredicate.getArray().getType().value());
    final NameExpr arrayTypeExpr = new NameExpr(ARRAY_TYPE.class.getName() + "." + arrayType.name());
    final IN_NOTIN inNotIn = IN_NOTIN.byName(simpleSetPredicate.getBooleanOperator().value());
    final NameExpr inNotInExpr = new NameExpr(IN_NOTIN.class.getName() + "." + inNotIn.name());
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, SIMPLESET_PREDICATE, simpleSetPredicateBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(0, new StringLiteralExpr(simpleSetPredicate.getField().getValue()));
    builder.setArgument(2, arrayTypeExpr);
    builder.setArgument(3, inNotInExpr);
    getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
    simpleSetPredicateBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) ARRAY_TYPE(org.kie.pmml.api.enums.ARRAY_TYPE) 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) Expression(com.github.javaparser.ast.expr.Expression) IN_NOTIN(org.kie.pmml.api.enums.IN_NOTIN) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getExpressionForObject(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 20 with NodeList

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

the class KiePMMLModelFactoryUtils method commonPopulateGetCreatedKiePMMLOutputFieldsMethod.

static void commonPopulateGetCreatedKiePMMLOutputFieldsMethod(final MethodDeclaration methodDeclaration, final List<org.dmg.pmml.OutputField> outputFields) {
    BlockStmt body = new BlockStmt();
    NodeList<Expression> arguments = new NodeList<>();
    for (org.dmg.pmml.OutputField outputField : outputFields) {
        String outputFieldVariableName = getSanitizedVariableName(outputField.getName().getValue()).toLowerCase();
        BlockStmt toAdd = getOutputFieldVariableDeclaration(outputFieldVariableName, outputField);
        toAdd.getStatements().forEach(body::addStatement);
        arguments.add(new NameExpr(outputFieldVariableName));
    }
    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)

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