Search in sources :

Example 26 with VariableDeclarator

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

the class CommonCodegenUtils method setVariableDeclaratorValue.

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

Example 27 with VariableDeclarator

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

the class CommonCodegenUtils method getFilteredKiePMMLNameValueExpression.

/**
 * Returns
 * <pre>
 *  Optional<KiePMMLNameValue> kiePMMLNameValue = (<i>kiePMMLNameValueListParam</i>)
 *      .stream()
 *      .filter((KiePMMLNameValue kpmmlnv) -> Objects.equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName()))
 *      .findFirst();
 * </pre>
 * <p>
 * expression, where <b>kiePMMLNameValueListParam</b> is the name of the
 * <code>List&lt;KiePMMLNameValue&gt;</code> parameter, and
 * <b>fieldNameToRef</b> is the name of the field to find, in the containing method
 * @param kiePMMLNameValueListParam
 * @param fieldNameToRef
 * @param stringLiteralComparison if <code>true</code>, equals comparison is made on the String, e.g Objects
 * .equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName())),
 * otherwise, is done on object reference,  e.g Objects.equals((<i>fieldNameToRef</i>), kpmmlnv.getName())). In
 * this latter case, a <i>fieldNameToRef</i> variable is
 * expected to exists
 * @return
 */
public static ExpressionStmt getFilteredKiePMMLNameValueExpression(final String kiePMMLNameValueListParam, final String fieldNameToRef, boolean stringLiteralComparison) {
    // kpmmlnv.getName()
    MethodCallExpr argumentBodyExpressionArgument2 = new MethodCallExpr("getName");
    argumentBodyExpressionArgument2.setScope(new NameExpr(LAMBDA_PARAMETER_NAME));
    // Objects.equals(fieldNameToRef, kpmmlnv.getName())
    MethodCallExpr argumentBodyExpression = new MethodCallExpr("equals");
    Expression equalsComparisonExpression;
    if (stringLiteralComparison) {
        equalsComparisonExpression = new StringLiteralExpr(fieldNameToRef);
    } else {
        equalsComparisonExpression = new NameExpr(fieldNameToRef);
    }
    argumentBodyExpression.setArguments(NodeList.nodeList(equalsComparisonExpression, argumentBodyExpressionArgument2));
    argumentBodyExpression.setScope(new NameExpr(Objects.class.getName()));
    ExpressionStmt argumentBody = new ExpressionStmt(argumentBodyExpression);
    // (KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef, kpmmlnv.getName())
    Parameter argumentParameter = new Parameter(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()), LAMBDA_PARAMETER_NAME);
    LambdaExpr argument = new LambdaExpr();
    // 
    argument.setEnclosingParameters(true).setParameters(NodeList.nodeList(argumentParameter));
    // (KiePMMLNameValue kpmmlnv) ->
    // Objects.equals(fieldNameToRef, kpmmlnv.getName())
    argument.setBody(argumentBody);
    // kiePMMLNameValueListParam.stream()
    MethodCallExpr initializerScopeScope = new MethodCallExpr("stream");
    initializerScopeScope.setScope(new NameExpr(kiePMMLNameValueListParam));
    // kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv)  -> Objects.equals(fieldNameToRef,
    // kpmmlnv.getName()))
    MethodCallExpr initializerScope = new MethodCallExpr("filter");
    initializerScope.setScope(initializerScopeScope);
    initializerScope.setArguments(NodeList.nodeList(argument));
    // kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv)  -> Objects.equals(fieldNameToRef,
    // kpmmlnv.getName())).findFirst()
    MethodCallExpr initializer = new MethodCallExpr("findFirst");
    initializer.setScope(initializerScope);
    // Optional<KiePMMLNameValue> kiePMMLNameValue
    VariableDeclarator variableDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Optional.class.getName(), Collections.singletonList(KiePMMLNameValue.class.getName())), OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME);
    // Optional<KiePMMLNameValue> kiePMMLNameValue = kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue
    // kpmmlnv)  -> Objects.equals(fieldNameToRef, kpmmlnv.getName())).findFirst()
    variableDeclarator.setInitializer(initializer);
    // 
    VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(NodeList.nodeList(variableDeclarator));
    ExpressionStmt toReturn = new ExpressionStmt();
    toReturn.setExpression(variableDeclarationExpr);
    return toReturn;
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 28 with VariableDeclarator

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

the class KiePMMLMiningFieldFactory method getMiningFieldVariableDeclaration.

static BlockStmt getMiningFieldVariableDeclaration(final String variableName, final MiningField miningField, final List<Field<?>> fields) {
    final MethodDeclaration methodDeclaration = MININGFIELD_TEMPLATE.getMethodsByName(GETKIEPMMLMININGFIELD).get(0).clone();
    final BlockStmt miningFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(miningFieldBody, MININGFIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, MININGFIELD, miningFieldBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, MININGFIELD, toReturn))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(miningField.getName().getValue());
    Expression fieldUsageTypeExpr;
    if (miningField.getUsageType() != null) {
        final FIELD_USAGE_TYPE fieldUsageType = FIELD_USAGE_TYPE.byName(miningField.getUsageType().value());
        fieldUsageTypeExpr = new NameExpr(FIELD_USAGE_TYPE.class.getName() + "." + fieldUsageType.name());
    } else {
        fieldUsageTypeExpr = new NullLiteralExpr();
    }
    Expression opTypeExpr;
    if (miningField.getOpType() != null) {
        final OP_TYPE opType = OP_TYPE.byName(miningField.getOpType().value());
        opTypeExpr = new NameExpr(OP_TYPE.class.getName() + "." + opType.name());
    } else {
        opTypeExpr = new NullLiteralExpr();
    }
    final List<Field<?>> mappedFields = getMappedFields(fields, miningField.getName().getValue());
    final DataType dataType = getDataType(mappedFields, miningField.getName().getValue());
    final DATA_TYPE data_TYPE = DATA_TYPE.byName(dataType.value());
    Expression dataTypeExpr = new NameExpr(DATA_TYPE.class.getName() + "." + data_TYPE.name());
    Expression missingValueTreatmentMethodExpr;
    if (miningField.getMissingValueTreatment() != null) {
        final MISSING_VALUE_TREATMENT_METHOD missingValueTreatmentMethod = MISSING_VALUE_TREATMENT_METHOD.byName(miningField.getMissingValueTreatment().value());
        missingValueTreatmentMethodExpr = new NameExpr(MISSING_VALUE_TREATMENT_METHOD.class.getName() + "." + missingValueTreatmentMethod.name());
    } else {
        missingValueTreatmentMethodExpr = new NullLiteralExpr();
    }
    Expression invalidValueTreatmentMethodExpr;
    if (miningField.getInvalidValueTreatment() != null) {
        final INVALID_VALUE_TREATMENT_METHOD invalidValueTreatmentMethod = INVALID_VALUE_TREATMENT_METHOD.byName(miningField.getInvalidValueTreatment().value());
        invalidValueTreatmentMethodExpr = new NameExpr(INVALID_VALUE_TREATMENT_METHOD.class.getName() + "." + invalidValueTreatmentMethod.name());
    } else {
        invalidValueTreatmentMethodExpr = new NullLiteralExpr();
    }
    Expression missingValueReplacementExpr;
    if (miningField.getMissingValueReplacement() != null) {
        final String missingValueReplacement = miningField.getMissingValueReplacement().toString();
        missingValueReplacementExpr = new StringLiteralExpr(missingValueReplacement);
    } else {
        missingValueReplacementExpr = new NullLiteralExpr();
    }
    Expression invalidValueReplacementExpr;
    if (miningField.getInvalidValueReplacement() != null) {
        final String invalidValueReplacement = miningField.getInvalidValueReplacement().toString();
        invalidValueReplacementExpr = new StringLiteralExpr(invalidValueReplacement);
    } else {
        invalidValueReplacementExpr = new NullLiteralExpr();
    }
    DataField dataField = getMappedDataField(mappedFields);
    NodeList<Expression> allowedValuesExpressions = dataField != null ? getAllowedValuesExpressions(dataField) : new NodeList<>();
    NodeList<Expression> intervalsExpressions = dataField != null ? getIntervalsExpressions(dataField) : new NodeList<>();
    builder.setArgument(0, nameExpr);
    getChainedMethodCallExprFrom("withFieldUsageType", initializer).setArgument(0, fieldUsageTypeExpr);
    getChainedMethodCallExprFrom("withOpType", initializer).setArgument(0, opTypeExpr);
    getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpr);
    getChainedMethodCallExprFrom("withMissingValueTreatmentMethod", initializer).setArgument(0, missingValueTreatmentMethodExpr);
    getChainedMethodCallExprFrom("withInvalidValueTreatmentMethod", initializer).setArgument(0, invalidValueTreatmentMethodExpr);
    getChainedMethodCallExprFrom("withMissingValueReplacement", initializer).setArgument(0, missingValueReplacementExpr);
    getChainedMethodCallExprFrom("withInvalidValueReplacement", initializer).setArgument(0, invalidValueReplacementExpr);
    getChainedMethodCallExprFrom("withAllowedValues", initializer).getArgument(0).asMethodCallExpr().setArguments(allowedValuesExpressions);
    getChainedMethodCallExprFrom("withIntervals", initializer).getArgument(0).asMethodCallExpr().setArguments(intervalsExpressions);
    miningFieldBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) OP_TYPE(org.kie.pmml.api.enums.OP_TYPE) INVALID_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.INVALID_VALUE_TREATMENT_METHOD) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Field(org.dmg.pmml.Field) MiningField(org.dmg.pmml.MiningField) DataField(org.dmg.pmml.DataField) MISSING_VALUE_TREATMENT_METHOD(org.kie.pmml.api.enums.MISSING_VALUE_TREATMENT_METHOD) FIELD_USAGE_TYPE(org.kie.pmml.api.enums.FIELD_USAGE_TYPE) DataField(org.dmg.pmml.DataField) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) DataType(org.dmg.pmml.DataType) DATA_TYPE(org.kie.pmml.api.enums.DATA_TYPE) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 29 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator 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 30 with VariableDeclarator

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

the class KiePMMLParameterFieldFactory method getParameterFieldVariableDeclaration.

static BlockStmt getParameterFieldVariableDeclaration(final String variableName, final ParameterField parameterField) {
    final MethodDeclaration methodDeclaration = PARAMETER_FIELD_TEMPLATE.getMethodsByName(GEKIEPMMLPARAMETERFIELD).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, PARAMETER_FIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, PARAMETER_FIELD, toReturn)));
    variableDeclarator.setName(variableName);
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, PARAMETER_FIELD, toReturn))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(0, new StringLiteralExpr(parameterField.getName().getValue()));
    final Expression dataTypeExpression = getExpressionForDataType(parameterField.getDataType());
    final Expression opTypeExpression = getExpressionForOpType(parameterField.getOpType());
    getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpression);
    getChainedMethodCallExprFrom("withOpType", initializer).setArgument(0, opTypeExpression);
    getChainedMethodCallExprFrom("withDisplayName", initializer).setArgument(0, getExpressionForObject(parameterField.getDisplayName()));
    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) 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)

Aggregations

VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)110 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)50 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)44 Expression (com.github.javaparser.ast.expr.Expression)43 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)39 NameExpr (com.github.javaparser.ast.expr.NameExpr)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 Test (org.junit.Test)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)18 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)17 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 NodeList (com.github.javaparser.ast.NodeList)16 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)14 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)13 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)13 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)11 Parameter (com.github.javaparser.ast.body.Parameter)9