use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLClusteringModelFactory method clusterCreationExprFrom.
private static ObjectCreationExpr clusterCreationExprFrom(Cluster cluster) {
NodeList<Expression> arguments = new NodeList<>();
arguments.add(literalExprFrom(cluster.getId()));
arguments.add(literalExprFrom(cluster.getName()));
if (cluster.getArray() != null && cluster.getArray().getType() == Array.Type.REAL) {
String arrayStringValue = (String) cluster.getArray().getValue();
String[] arrayStringChunks = arrayStringValue.split(" ");
try {
Arrays.stream(arrayStringChunks).map(Double::parseDouble).map(DoubleLiteralExpr::new).forEach(arguments::add);
} catch (NumberFormatException e) {
logger.error("Can't parse \"real\" cluster with value \"" + arrayStringValue + "\"", e);
}
}
return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLCluster.class.getCanonicalName()), arguments);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLClusteringModelFactory method clusteringFieldCreationExprFrom.
private static ObjectCreationExpr clusteringFieldCreationExprFrom(ClusteringField clusteringField) {
double fieldWeight = clusteringField.getFieldWeight() == null ? 1.0 : clusteringField.getFieldWeight().doubleValue();
boolean isCenterField = clusteringField.getCenterField() == null || clusteringField.getCenterField() == ClusteringField.CenterField.TRUE;
NodeList<Expression> arguments = new NodeList<>();
arguments.add(literalExprFrom(clusteringField.getField().getValue()));
arguments.add(new DoubleLiteralExpr(fieldWeight));
arguments.add(new BooleanLiteralExpr(isCenterField));
arguments.add(clusteringField.getCompareFunction() == null ? new NullLiteralExpr() : literalExprFrom(compareFunctionFrom(clusteringField.getCompareFunction())));
arguments.add(new NullLiteralExpr());
return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLClusteringField.class.getCanonicalName()), arguments);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method createIntervalsExpression.
static Expression createIntervalsExpression(List<Interval> intervals) {
ExpressionStmt arraysAsListStmt = CommonCodegenUtils.createArraysAsListExpression();
MethodCallExpr arraysCallExpression = arraysAsListStmt.getExpression().asMethodCallExpr();
NodeList<Expression> arguments = new NodeList<>();
intervals.forEach(value -> arguments.add(getObjectCreationExprFromInterval(value)));
arraysCallExpression.setArguments(arguments);
arraysAsListStmt.setExpression(arraysCallExpression);
return arraysAsListStmt.getExpression();
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method commonPopulateGetCreatedKiePMMLMiningFieldsMethod.
/**
* Populate the <code>getCreatedKiePMMLMiningFields</code> method
* @param methodDeclaration
* @param miningFields
* @param fields
*/
static void commonPopulateGetCreatedKiePMMLMiningFieldsMethod(final MethodDeclaration methodDeclaration, final List<org.dmg.pmml.MiningField> miningFields, final List<org.dmg.pmml.Field<?>> fields) {
BlockStmt body = new BlockStmt();
NodeList<Expression> arguments = new NodeList<>();
for (org.dmg.pmml.MiningField miningField : miningFields) {
String miningFieldVariableName = getSanitizedVariableName(miningField.getName().getValue()).toLowerCase();
BlockStmt toAdd = getMiningFieldVariableDeclaration(miningFieldVariableName, miningField, fields);
toAdd.getStatements().forEach(body::addStatement);
arguments.add(new NameExpr(miningFieldVariableName));
}
MethodCallExpr methodCallExpr = new MethodCallExpr();
methodCallExpr.setScope(new NameExpr(Arrays.class.getSimpleName()));
methodCallExpr.setName("asList");
methodCallExpr.setArguments(arguments);
ReturnStmt returnStmt = new ReturnStmt();
returnStmt.setExpression(methodCallExpr);
body.addStatement(returnStmt);
methodDeclaration.setBody(body);
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLApplyFactory method getApplyVariableDeclaration.
static BlockStmt getApplyVariableDeclaration(final String variableName, final Apply apply) {
final MethodDeclaration methodDeclaration = APPLY_TEMPLATE.getMethodsByName(GETKIEPMMLAPPLY).get(0).clone();
final BlockStmt applyBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(applyBody, APPLY).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, APPLY, applyBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (org.dmg.pmml.Expression expression : apply.getExpressions()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, expression);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, APPLY, toReturn))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
final StringLiteralExpr nameExpr = new StringLiteralExpr(variableName);
final StringLiteralExpr functionExpr = new StringLiteralExpr(apply.getFunction());
builder.setArgument(0, nameExpr);
builder.setArgument(2, functionExpr);
getChainedMethodCallExprFrom("withDefaultValue", initializer).setArgument(0, getExpressionForObject(apply.getDefaultValue()));
getChainedMethodCallExprFrom("withMapMissingTo", initializer).setArgument(0, getExpressionForObject(apply.getMapMissingTo()));
final Expression invalidTreatmentExpr = apply.getInvalidValueTreatment() != null ? new StringLiteralExpr(apply.getInvalidValueTreatment().value()) : new NullLiteralExpr();
getChainedMethodCallExprFrom("withInvalidValueTreatmentMethod", initializer).setArgument(0, invalidTreatmentExpr);
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
applyBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
Aggregations