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));
}
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;
}
Aggregations