use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class TargetFieldFactory method getTargetFieldVariableInitializer.
static ObjectCreationExpr getTargetFieldVariableInitializer(final TargetField targetField) {
final MethodDeclaration methodDeclaration = TARGET_TEMPLATE.getMethodsByName(GETTARGET_FIELD).get(0).clone();
final BlockStmt targetBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(targetBody, TARGET_FIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGET_FIELD, targetBody)));
variableDeclarator.setName(targetField.getName());
final ObjectCreationExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGET_FIELD, targetBody))).asObjectCreationExpr();
final NodeList<Expression> arguments = new NodeList<>();
if (targetField.getTargetValues() != null) {
for (TargetValue targetValue : targetField.getTargetValues()) {
arguments.add(getTargetValueVariableInitializer(targetValue));
}
}
toReturn.getArgument(0).asMethodCallExpr().setArguments(arguments);
OP_TYPE oPT = targetField.getOpType();
Expression opType = oPT != null ? new NameExpr(oPT.getClass().getName() + "." + oPT.name()) : new NullLiteralExpr();
toReturn.setArgument(1, opType);
toReturn.setArgument(2, getExpressionForObject(targetField.getField()));
CAST_INTEGER cstInt = targetField.getCastInteger();
Expression castInteger = cstInt != null ? new NameExpr(cstInt.getClass().getName() + "." + cstInt.name()) : new NullLiteralExpr();
toReturn.setArgument(3, castInteger);
toReturn.setArgument(4, getExpressionForObject(targetField.getMin()));
toReturn.setArgument(5, getExpressionForObject(targetField.getMax()));
toReturn.setArgument(6, getExpressionForObject(targetField.getRescaleConstant()));
toReturn.setArgument(7, getExpressionForObject(targetField.getRescaleFactor()));
return toReturn;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class CommonCodegenUtils method createMap.
private static void createMap(final BlockStmt body, final String mapName, final List<String> mapTypes, final Class<? extends Map> mapClass) {
final VariableDeclarator mapDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Map.class.getName(), mapTypes), mapName);
final ObjectCreationExpr mapInitializer = new ObjectCreationExpr();
mapInitializer.setType(getTypedClassOrInterfaceTypeByTypeNames(mapClass.getName(), mapTypes));
mapDeclarator.setInitializer(mapInitializer);
final VariableDeclarationExpr mapDeclarationExpr = new VariableDeclarationExpr(mapDeclarator);
body.addStatement(mapDeclarationExpr);
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr 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;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLTruePredicateFactory method getTruePredicateVariableDeclaration.
static BlockStmt getTruePredicateVariableDeclaration(final String variableName, final True truePredicate) {
final MethodDeclaration methodDeclaration = TRUEPREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLTRUEPREDICATE).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, TRUEPREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TRUEPREDICATE, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TRUEPREDICATE, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
objectCreationExpr.getArguments().set(0, nameExpr);
return toReturn;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLFieldColumnPairFactory method getFieldColumnPairVariableDeclaration.
static BlockStmt getFieldColumnPairVariableDeclaration(final String variableName, final FieldColumnPair fieldColumnPair) {
final MethodDeclaration methodDeclaration = FIELDCOLUMNPAIR_TEMPLATE.getMethodsByName(GETKIEPMMLFIELDCOLUMNPAIR).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, FIELDCOLUMNPAIR).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, FIELDCOLUMNPAIR, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, FIELDCOLUMNPAIR, toReturn))).asObjectCreationExpr();
objectCreationExpr.getArguments().set(0, new StringLiteralExpr(fieldColumnPair.getField().getValue()));
objectCreationExpr.getArguments().set(2, new StringLiteralExpr(fieldColumnPair.getColumn()));
return toReturn;
}
Aggregations