Search in sources :

Example 11 with ObjectCreationExpr

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

the class KiePMMLModelFactoryUtils method setKiePMMLModelConstructor.

/**
 * Set the <b>name</b> parameter on <b>super</b> invocation and populate the <b>miningFields/outputFields</b>
 * @param generatedClassName
 * @param constructorDeclaration
 * @param name
 * @param miningFields
 * @param outputFields
 * @param targetFields
 */
public static void setKiePMMLModelConstructor(final String generatedClassName, final ConstructorDeclaration constructorDeclaration, final String name, final List<MiningField> miningFields, final List<OutputField> outputFields, final List<TargetField> targetFields) {
    setConstructorSuperNameInvocation(generatedClassName, constructorDeclaration, name);
    final BlockStmt body = constructorDeclaration.getBody();
    final List<ObjectCreationExpr> miningFieldsObjectCreations = getMiningFieldsObjectCreations(miningFields);
    addListPopulationByObjectCreationExpr(miningFieldsObjectCreations, body, "miningFields");
    final List<ObjectCreationExpr> outputFieldsObjectCreations = getOutputFieldsObjectCreations(outputFields);
    addListPopulationByObjectCreationExpr(outputFieldsObjectCreations, body, "outputFields");
    final List<MethodCallExpr> kiePMMLTargetFieldsObjectCreations = getKiePMMLTargetFieldsObjectCreations(targetFields);
    addListPopulationByMethodCallExpr(kiePMMLTargetFieldsObjectCreations, body, "kiePMMLTargets");
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) CommonCodegenUtils.addListPopulationByObjectCreationExpr(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByObjectCreationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) CommonCodegenUtils.addListPopulationByMethodCallExpr(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByMethodCallExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 12 with ObjectCreationExpr

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

the class KiePMMLModelFactoryUtils method getObjectCreationExprFromInterval.

static ObjectCreationExpr getObjectCreationExprFromInterval(Interval source) {
    ObjectCreationExpr toReturn = new ObjectCreationExpr();
    toReturn.setType(Interval.class.getCanonicalName());
    NodeList<Expression> arguments = new NodeList<>();
    if (source.getLeftMargin() != null) {
        arguments.add(new NameExpr(source.getLeftMargin().toString()));
    } else {
        arguments.add(new NullLiteralExpr());
    }
    if (source.getRightMargin() != null) {
        arguments.add(new NameExpr(source.getRightMargin().toString()));
    } else {
        arguments.add(new NullLiteralExpr());
    }
    toReturn.setArguments(arguments);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) CommonCodegenUtils.addListPopulationByObjectCreationExpr(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByObjectCreationExpr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) Interval(org.kie.pmml.api.models.Interval)

Example 13 with ObjectCreationExpr

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

the class KiePMMLFieldRefFactory method getFieldRefVariableDeclaration.

static BlockStmt getFieldRefVariableDeclaration(final String variableName, final FieldRef fieldRef) {
    final MethodDeclaration methodDeclaration = FIELDREF_TEMPLATE.getMethodsByName(GETKIEPMMLFIELDREF).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, FIELD_REF).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, FIELD_REF, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, FIELD_REF, toReturn))).asObjectCreationExpr();
    final StringLiteralExpr nameExpr = new StringLiteralExpr(fieldRef.getField().getValue());
    final Expression mapMissingToExpr = getExpressionForObject(fieldRef.getMapMissingTo());
    objectCreationExpr.getArguments().set(0, nameExpr);
    objectCreationExpr.getArguments().set(2, mapMissingToExpr);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) 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)

Example 14 with ObjectCreationExpr

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

the class KiePMMLIntervalFactory method getIntervalVariableDeclaration.

static BlockStmt getIntervalVariableDeclaration(final String variableName, final Interval interval) {
    final MethodDeclaration methodDeclaration = INTERVAL_TEMPLATE.getMethodsByName(GETKIEPMMLINTERVAL).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, INTERVAL).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, INTERVAL, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, INTERVAL, toReturn))).asObjectCreationExpr();
    final Expression leftMarginExpr = getExpressionForObject(interval.getLeftMargin());
    final Expression rightMarginExpr = getExpressionForObject(interval.getRightMargin());
    final CLOSURE closure = CLOSURE.byName(interval.getClosure().value());
    final NameExpr closureExpr = new NameExpr(CLOSURE.class.getName() + "." + closure.name());
    objectCreationExpr.getArguments().set(0, leftMarginExpr);
    objectCreationExpr.getArguments().set(1, rightMarginExpr);
    objectCreationExpr.getArguments().set(2, closureExpr);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CLOSURE(org.kie.pmml.api.enums.CLOSURE) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 15 with ObjectCreationExpr

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

the class TargetValueFactory method getTargetValueVariableInitializer.

static ObjectCreationExpr getTargetValueVariableInitializer(final TargetValue targetValueField) {
    final MethodDeclaration methodDeclaration = TARGETVALUE_TEMPLATE.getMethodsByName(GETTARGETVALUE).get(0).clone();
    final BlockStmt targetValueBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(targetValueBody, TARGETVALUE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGETVALUE, targetValueBody)));
    final ObjectCreationExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGETVALUE, targetValueBody))).asObjectCreationExpr();
    toReturn.setArgument(0, getExpressionForObject(targetValueField.getValue()));
    toReturn.setArgument(1, getExpressionForObject(targetValueField.getDisplayValue()));
    toReturn.setArgument(2, getExpressionForObject(targetValueField.getPriorProbability()));
    toReturn.setArgument(3, getExpressionForObject(targetValueField.getDefaultValue()));
    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) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Aggregations

ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)83 Expression (com.github.javaparser.ast.expr.Expression)59 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)44 NameExpr (com.github.javaparser.ast.expr.NameExpr)41 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)38 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)31 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)30 NodeList (com.github.javaparser.ast.NodeList)24 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)24 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)21 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)19 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14 List (java.util.List)13 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)12 Type (com.github.javaparser.ast.type.Type)12 Test (org.junit.Test)12 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)11 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)10