use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLTransformationDictionaryFactory method addDefineFunctions.
static NodeList<Expression> addDefineFunctions(final BlockStmt body, final List<DefineFunction> defineFunctions) {
NodeList<Expression> arguments = new NodeList<>();
for (DefineFunction defineFunction : defineFunctions) {
arguments.add(new NameExpr(defineFunction.getName()));
BlockStmt toAdd = getDefineFunctionVariableDeclaration(defineFunction);
toAdd.getStatements().forEach(body::addStatement);
}
return getArraysAsListInvocation(arguments);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLTransformationDictionaryFactory method addDerivedField.
static NodeList<Expression> addDerivedField(final BlockStmt body, final List<DerivedField> derivedFields) {
NodeList<Expression> arguments = new NodeList<>();
int counter = 0;
for (DerivedField derivedField : derivedFields) {
String nestedVariableName = String.format("transformationDictionaryDerivedField_%s", counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getDerivedFieldVariableDeclaration(nestedVariableName, derivedField);
toAdd.getStatements().forEach(body::addStatement);
counter++;
}
return getArraysAsListInvocation(arguments);
}
use of com.github.javaparser.ast.NodeList 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.NodeList in project drools by kiegroup.
the class CommonCodegenUtils method createArraysAsListFromList.
/**
* Create a populated <b>Arrays.asList(?... a)</b> <code>ExpressionStmt</code>
* @param source
* @return
*/
public static ExpressionStmt createArraysAsListFromList(List<?> source) {
ExpressionStmt toReturn = createArraysAsListExpression();
MethodCallExpr arraysCallExpression = toReturn.getExpression().asMethodCallExpr();
NodeList<Expression> arguments = new NodeList<>();
source.forEach(value -> arguments.add(getExpressionForObject(value)));
arraysCallExpression.setArguments(arguments);
toReturn.setExpression(arraysCallExpression);
return toReturn;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class CommonCodegenUtils method getMethodDeclaration.
/**
* Returns
* <pre>
* empty (<i>methodName</i>)((list of <i>parameterType</i> <i>parameter name</i>)) {
* }
* </pre>
* <p>
* <p>
* a <b>multi-parameters</b> <code>MethodDeclaration</code> whose names are the <b>key</b>s of the given
* <code>Map</code>
* and <b>methodArity</b>, and whose parameters types are the <b>value</b>s
*
* <b>The </b>
* @param methodName
* @param parameterNameTypeMap expecting an <b>ordered</b> map here, since parameters order matter for
* <i>caller</i> code
* @return
*/
public static MethodDeclaration getMethodDeclaration(final String methodName, final Map<String, ClassOrInterfaceType> parameterNameTypeMap) {
MethodDeclaration toReturn = getMethodDeclaration(methodName);
NodeList<Parameter> typeParameters = new NodeList<>();
parameterNameTypeMap.forEach((parameterName, classOrInterfaceType) -> {
Parameter toAdd = new Parameter();
toAdd.setName(parameterName);
toAdd.setType(classOrInterfaceType);
typeParameters.add(toAdd);
});
toReturn.setParameters(typeParameters);
return toReturn;
}
Aggregations