use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLTargetFactory method getKiePMMLTargetVariableInitializer.
static MethodCallExpr getKiePMMLTargetVariableInitializer(final TargetField targetField) {
final MethodDeclaration methodDeclaration = TARGET_TEMPLATE.getMethodsByName(GETKIEPMMLTARGET).get(0).clone();
final BlockStmt targetBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(targetBody, TARGET).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGET, targetBody)));
variableDeclarator.setName(targetField.getName());
final MethodCallExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGET, targetBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", toReturn);
final StringLiteralExpr nameExpr = new StringLiteralExpr(targetField.getName());
builder.setArgument(0, nameExpr);
final ObjectCreationExpr targetFieldInstantiation = getTargetFieldVariableInitializer(targetField);
builder.setArgument(2, targetFieldInstantiation);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.
the class KiePMMLTransformationDictionaryFactory method getKiePMMLTransformationDictionaryVariableDeclaration.
/**
* @param transformationDictionary
* @return
*/
static BlockStmt getKiePMMLTransformationDictionaryVariableDeclaration(final TransformationDictionary transformationDictionary) {
final MethodDeclaration methodDeclaration = TRANSFORMATION_DICTIONARY_TEMPLATE.getMethodsByName(GETKIEPMMLTRANSFORMATIONDICTIONARY).get(0).clone();
final BlockStmt transformationDictionaryBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(transformationDictionaryBody, TRANSFORMATION_DICTIONARY).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TRANSFORMATION_DICTIONARY, transformationDictionaryBody)));
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TRANSFORMATION_DICTIONARY, methodDeclaration))).asMethodCallExpr();
final BlockStmt toReturn = new BlockStmt();
if (transformationDictionary.hasDefineFunctions()) {
NodeList<Expression> defineFunctions = addDefineFunctions(toReturn, transformationDictionary.getDefineFunctions());
getChainedMethodCallExprFrom("withDefineFunctions", initializer).setArguments(defineFunctions);
}
if (transformationDictionary.hasDerivedFields()) {
NodeList<Expression> derivedFields = addDerivedField(toReturn, transformationDictionary.getDerivedFields());
getChainedMethodCallExprFrom("withDerivedFields", initializer).setArguments(derivedFields);
}
transformationDictionaryBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.body.VariableDeclarator 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.body.VariableDeclarator 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.body.VariableDeclarator 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