Search in sources :

Example 11 with ArrayCreationExpr

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

the class DMNAlphaNetworkCompiler method initPropertyNames.

private void initPropertyNames(List<InputClause> input) {
    NodeList<Expression> propertyNamesArray = input.stream().map(inputClause -> inputClause.getInputExpression().getText()).map(StringLiteralExpr::new).collect(Collectors.toCollection(NodeList::new));
    ArrayCreationExpr array = new ArrayCreationExpr().setElementType(new ArrayType(parseType(String.class.getCanonicalName()))).setInitializer(new ArrayInitializerExpr(propertyNamesArray));
    template.findAll(StringLiteralExpr.class, n -> n.asString().equals("PROPERTY_NAMES")).forEach(r -> r.replace(array));
}
Also used : ArrayType(com.github.javaparser.ast.type.ArrayType) NodeList(com.github.javaparser.ast.NodeList) DecisionTable(org.kie.dmn.model.api.DecisionTable) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) Logger(org.slf4j.Logger) ArrayType(com.github.javaparser.ast.type.ArrayType) InputClause(org.kie.dmn.model.api.InputClause) CodegenStringUtil.replaceStringLiteralExprWith(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil.replaceStringLiteralExprWith) LoggerFactory(org.slf4j.LoggerFactory) CodegenStringUtil.replaceSimpleNameWith(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil.replaceSimpleNameWith) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CodegenStringUtil.parseJavaClassTemplateFromResources(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil.parseJavaClassTemplateFromResources) CodegenStringUtil(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil) HitPolicy(org.kie.dmn.model.api.HitPolicy) CodegenStringUtil.blockHasComment(org.kie.dmn.feel.codegen.feel11.CodegenStringUtil.blockHasComment) List(java.util.List) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) BuiltinAggregator(org.kie.dmn.model.api.BuiltinAggregator) Expression(com.github.javaparser.ast.expr.Expression) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr)

Example 12 with ArrayCreationExpr

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

the class KiePMMLRowFactory method getRowVariableDeclaration.

static BlockStmt getRowVariableDeclaration(final String variableName, final Row row) {
    final MethodDeclaration methodDeclaration = ROW_TEMPLATE.getMethodsByName(GETKIEPMMLROW).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final String columnValuesVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, COLUMN_VALUES);
    final VariableDeclarator columnValuesVariableDeclarator = getVariableDeclarator(toReturn, COLUMN_VALUES).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ROW, toReturn)));
    columnValuesVariableDeclarator.setName(columnValuesVariableName);
    final MethodCallExpr columnValuesVariableInit = columnValuesVariableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asMethodCallExpr();
    final MethodCallExpr columnValuesVariableScope = columnValuesVariableInit.getScope().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asMethodCallExpr();
    final ArrayCreationExpr columnValuesVariableArray = columnValuesVariableScope.getArguments().get(0).asArrayCreationExpr();
    final ArrayInitializerExpr columnValuesVariableArrayInit = columnValuesVariableArray.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COLUMN_VALUES, toReturn))).asArrayInitializerExpr();
    Map<String, Object> rowDataMap = getRowDataMap(row);
    NodeList<Expression> arguments = new NodeList<>();
    rowDataMap.entrySet().forEach(entry -> {
        ArrayInitializerExpr argument = new ArrayInitializerExpr();
        NodeList<Expression> values = NodeList.nodeList(new StringLiteralExpr(entry.getKey()), getExpressionForObject(entry.getValue()));
        argument.setValues(values);
        arguments.add(argument);
    });
    columnValuesVariableArrayInit.setValues(arguments);
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, ROW).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ROW, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, ROW, toReturn))).asObjectCreationExpr();
    final NameExpr nameExpr = new NameExpr(columnValuesVariableName);
    objectCreationExpr.getArguments().set(0, nameExpr);
    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) NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) ArrayInitializerExpr(com.github.javaparser.ast.expr.ArrayInitializerExpr) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getExpressionForObject(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject) ArrayCreationExpr(com.github.javaparser.ast.expr.ArrayCreationExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

ArrayCreationExpr (com.github.javaparser.ast.expr.ArrayCreationExpr)12 Expression (com.github.javaparser.ast.expr.Expression)5 NodeList (com.github.javaparser.ast.NodeList)4 ArrayInitializerExpr (com.github.javaparser.ast.expr.ArrayInitializerExpr)4 NameExpr (com.github.javaparser.ast.expr.NameExpr)4 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)4 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)4 Test (org.junit.Test)4 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)3 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)3 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)3 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)3 List (java.util.List)3 StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)2 Node (com.github.javaparser.ast.Node)2 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)2 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)2 ArrayAccessExpr (com.github.javaparser.ast.expr.ArrayAccessExpr)2 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)2 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)2