Search in sources :

Example 81 with VariableDeclarator

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

the class BoxedParameters method getBoxedParametersWithUnboxedAssignment.

// Types in the executable model are promoted to boxed to type check the Java DSL.
// We add such promoted types as _<PARAMETER_NAME> (with the underscore prefix)
// and then we downcast to the original unboxed type in the body of the function (methodBody)
public NodeList<Parameter> getBoxedParametersWithUnboxedAssignment(Collection<String> declarationUsedInRHS, BlockStmt methodBody) {
    NodeList<Parameter> parameters = NodeList.nodeList();
    for (String parameterName : declarationUsedInRHS) {
        DeclarationSpec declaration = context.getDeclarationByIdWithException(parameterName);
        Parameter boxedParameter;
        Type boxedType = declaration.getBoxedType();
        if (declaration.isBoxed()) {
            String boxedParameterName = "_" + parameterName;
            boxedParameter = new Parameter(boxedType, boxedParameterName);
            Expression unboxedTypeDowncast = new VariableDeclarationExpr(new VariableDeclarator(declaration.getRawType(), parameterName, new NameExpr(boxedParameterName)));
            methodBody.addStatement(0, unboxedTypeDowncast);
        } else {
            boxedParameter = new Parameter(boxedType, parameterName);
        }
        parameters.add(boxedParameter);
    }
    return parameters;
}
Also used : Type(com.github.javaparser.ast.type.Type) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) Parameter(com.github.javaparser.ast.body.Parameter) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 82 with VariableDeclarator

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

the class DMNAlphaNetworkEvaluatorCompiler method createAlphaNetworkCompiler.

private ObjectTypeNodeCompiler createAlphaNetworkCompiler(ObjectTypeNode firstObjectTypeNodeOfRete) {
    ANCConfiguration ancConfiguration = new ANCConfiguration();
    ancConfiguration.setDisableContextEntry(true);
    ancConfiguration.setPrettyPrint(true);
    ancConfiguration.setEnableModifyObject(false);
    ObjectTypeNodeCompiler objectTypeNodeCompiler = new ObjectTypeNodeCompiler(ancConfiguration, firstObjectTypeNodeOfRete, true);
    VariableDeclarator variableDeclarator = new VariableDeclarator(StaticJavaParser.parseType(AlphaNetworkEvaluationContext.class.getCanonicalName()), "ctx");
    objectTypeNodeCompiler.addAdditionalFields(new FieldDeclaration(NodeList.nodeList(), NodeList.nodeList(variableDeclarator)));
    return objectTypeNodeCompiler;
}
Also used : ANCConfiguration(org.drools.ancompiler.ANCConfiguration) ObjectTypeNodeCompiler(org.drools.ancompiler.ObjectTypeNodeCompiler) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 83 with VariableDeclarator

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

the class KiePMMLCompoundPredicateFactory method getCompoundPredicateVariableDeclaration.

static BlockStmt getCompoundPredicateVariableDeclaration(final String variableName, final CompoundPredicate compoundPredicate, final List<Field<?>> fields) {
    final MethodDeclaration methodDeclaration = COMPOUND_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLCOMPOUNDPREDICATE).get(0).clone();
    final BlockStmt compoundPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(compoundPredicateBody, COMPOUND_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, COMPOUND_PREDICATE, compoundPredicateBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    int counter = 0;
    final NodeList<Expression> arguments = new NodeList<>();
    for (Predicate predicate : compoundPredicate.getPredicates()) {
        String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
        arguments.add(new NameExpr(nestedVariableName));
        BlockStmt toAdd = getKiePMMLPredicate(nestedVariableName, predicate, fields);
        toAdd.getStatements().forEach(toReturn::addStatement);
        counter++;
    }
    final BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value());
    final NameExpr booleanOperatorExpr = new NameExpr(BOOLEAN_OPERATOR.class.getName() + "." + booleanOperator.name());
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COMPOUND_PREDICATE, compoundPredicateBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(1, booleanOperatorExpr);
    getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
    compoundPredicateBody.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) NameExpr(com.github.javaparser.ast.expr.NameExpr) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CompoundPredicate(org.dmg.pmml.CompoundPredicate) Predicate(org.dmg.pmml.Predicate) KiePMMLPredicateFactory.getKiePMMLPredicate(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLPredicateFactory.getKiePMMLPredicate) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 84 with VariableDeclarator

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

the class KiePMMLOutputFieldFactory method getOutputFieldVariableDeclaration.

static BlockStmt getOutputFieldVariableDeclaration(final String variableName, final OutputField outputField) {
    final MethodDeclaration methodDeclaration = OUTPUTFIELD_TEMPLATE.getMethodsByName(GETKIEPMMLOUTPUTFIELD).get(0).clone();
    final BlockStmt outputFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(outputFieldBody, OUTPUTFIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, OUTPUTFIELD, outputFieldBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final Expression expressionExpr;
    if (outputField.getExpression() != null) {
        String nestedVariableName = String.format("%s_Expression", variableName);
        BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, outputField.getExpression());
        toAdd.getStatements().forEach(toReturn::addStatement);
        expressionExpr = new NameExpr(nestedVariableName);
    } else {
        expressionExpr = new NullLiteralExpr();
    }
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, OUTPUTFIELD, toReturn))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(outputField.getName().getValue());
    final RESULT_FEATURE resultFeature = RESULT_FEATURE.byName(outputField.getResultFeature().value());
    final NameExpr resultFeatureExpr = new NameExpr(RESULT_FEATURE.class.getName() + "." + resultFeature.name());
    final Expression targetFieldExpr = outputField.getTargetField() != null ? getExpressionForObject(outputField.getTargetField().getValue()) : new NullLiteralExpr();
    final Expression valueExpr = outputField.getValue() != null ? getExpressionForObject(outputField.getValue()) : new NullLiteralExpr();
    final Expression dataTypeExpression = getExpressionForDataType(outputField.getDataType());
    final Expression rankExpr = outputField.getRank() != null ? getExpressionForObject(outputField.getRank()) : new NullLiteralExpr();
    builder.setArgument(0, nameExpr);
    getChainedMethodCallExprFrom("withResultFeature", initializer).setArgument(0, resultFeatureExpr);
    getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, targetFieldExpr);
    getChainedMethodCallExprFrom("withValue", initializer).setArgument(0, valueExpr);
    getChainedMethodCallExprFrom("withDataType", initializer).setArgument(0, dataTypeExpression);
    getChainedMethodCallExprFrom("withRank", initializer).setArgument(0, rankExpr);
    getChainedMethodCallExprFrom("withKiePMMLExpression", initializer).setArgument(0, expressionExpr);
    outputFieldBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) RESULT_FEATURE(org.kie.pmml.api.enums.RESULT_FEATURE) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) 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)

Example 85 with VariableDeclarator

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

the class KiePMMLSimplePredicateFactory method getSimplePredicateVariableDeclaration.

static BlockStmt getSimplePredicateVariableDeclaration(final String variableName, final SimplePredicate simplePredicate, final List<Field<?>> fields) {
    final MethodDeclaration methodDeclaration = SIMPLE_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLSIMPLEPREDICATE).get(0).clone();
    final BlockStmt simplePredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(simplePredicateBody, SIMPLE_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, SIMPLE_PREDICATE, simplePredicateBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final OPERATOR operator = OPERATOR.byName(simplePredicate.getOperator().value());
    final NameExpr operatorExpr = new NameExpr(OPERATOR.class.getName() + "." + operator.name());
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, SIMPLE_PREDICATE, simplePredicateBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(0, new StringLiteralExpr(simplePredicate.getField().getValue()));
    builder.setArgument(2, operatorExpr);
    DataType dataType = getDataType(fields, simplePredicate.getField().getValue());
    Object actualValue = DATA_TYPE.byName(dataType.value()).getActualValue(simplePredicate.getValue());
    getChainedMethodCallExprFrom("withValue", initializer).setArgument(0, getExpressionForObject(actualValue));
    simplePredicateBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : OPERATOR(org.kie.pmml.api.enums.OPERATOR) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) DataType(org.dmg.pmml.DataType) ModelUtils.getDataType(org.kie.pmml.compiler.api.utils.ModelUtils.getDataType) CommonCodegenUtils.getExpressionForObject(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject) 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