use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLMiningFieldFactory method getIntervalsExpressions.
private static NodeList<Expression> getIntervalsExpressions(final DataField dataField) {
final NodeList<Expression> toReturn = new NodeList<>();
if (dataField.hasIntervals()) {
dataField.getIntervals().forEach(interval -> {
BlockStmt intervalStmt = getIntervalVariableDeclaration("name", interval);
Expression toAdd = intervalStmt.getStatement(0).asExpressionStmt().getExpression().asVariableDeclarationExpr().getVariable(0).getInitializer().orElseThrow(() -> new KiePMMLInternalException(String.format("Failed to create initializer " + "for " + "Interval %s", interval)));
toReturn.add(toAdd);
});
}
return toReturn;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLNormContinuousFactory method getNormContinuousVariableDeclaration.
static BlockStmt getNormContinuousVariableDeclaration(final String variableName, final NormContinuous normContinuous) {
final MethodDeclaration methodDeclaration = NORMCONTINUOUS_TEMPLATE.getMethodsByName(GETKIEPMMLNORMCONTINUOUS).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, NORM_CONTINUOUS).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, NORM_CONTINUOUS, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NORM_CONTINUOUS, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(normContinuous.getField().getValue());
final OUTLIER_TREATMENT_METHOD outlierTreatmentMethod = OUTLIER_TREATMENT_METHOD.byName(normContinuous.getOutliers().value());
final NameExpr outlierTreatmentMethodExpr = new NameExpr(OUTLIER_TREATMENT_METHOD.class.getName() + "." + outlierTreatmentMethod.name());
NodeList<Expression> arguments = new NodeList<>();
int counter = 0;
for (LinearNorm linearNorm : normContinuous.getLinearNorms()) {
arguments.add(getNewKiePMMLLinearNormExpression(linearNorm, "LinearNorm-" + counter));
}
final Expression mapMissingToExpr = getExpressionForObject(normContinuous.getMapMissingTo());
objectCreationExpr.getArguments().set(0, nameExpr);
objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
objectCreationExpr.getArguments().set(3, outlierTreatmentMethodExpr);
objectCreationExpr.getArguments().set(4, mapMissingToExpr);
return toReturn;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLNormContinuousFactory method getNewKiePMMLLinearNormExpression.
static Expression getNewKiePMMLLinearNormExpression(LinearNorm linearNorm, String name) {
ObjectCreationExpr toReturn = new ObjectCreationExpr();
toReturn.setType(KiePMMLLinearNorm.class);
NodeList<Expression> arguments = new NodeList<>();
arguments.add(new StringLiteralExpr(name));
arguments.add(new MethodCallExpr(new NameExpr("Collections"), "emptyList"));
arguments.add(getExpressionForObject(linearNorm.getOrig()));
arguments.add(getExpressionForObject(linearNorm.getNorm()));
toReturn.setArguments(arguments);
return toReturn;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLSimpleSetPredicateFactory method getSimpleSetPredicateVariableDeclaration.
static BlockStmt getSimpleSetPredicateVariableDeclaration(final String variableName, final SimpleSetPredicate simpleSetPredicate) {
final MethodDeclaration methodDeclaration = SIMPLESET_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLSIMPLESETPREDICATE).get(0).clone();
final BlockStmt simpleSetPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(simpleSetPredicateBody, SIMPLESET_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, SIMPLESET_PREDICATE, simpleSetPredicateBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
final NodeList<Expression> arguments = new NodeList<>();
List<Object> values = getObjectsFromArray(simpleSetPredicate.getArray());
for (Object value : values) {
arguments.add(getExpressionForObject(value));
}
final ARRAY_TYPE arrayType = ARRAY_TYPE.byName(simpleSetPredicate.getArray().getType().value());
final NameExpr arrayTypeExpr = new NameExpr(ARRAY_TYPE.class.getName() + "." + arrayType.name());
final IN_NOTIN inNotIn = IN_NOTIN.byName(simpleSetPredicate.getBooleanOperator().value());
final NameExpr inNotInExpr = new NameExpr(IN_NOTIN.class.getName() + "." + inNotIn.name());
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, SIMPLESET_PREDICATE, simpleSetPredicateBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(0, new StringLiteralExpr(simpleSetPredicate.getField().getValue()));
builder.setArgument(2, arrayTypeExpr);
builder.setArgument(3, inNotInExpr);
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
simpleSetPredicateBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.NodeList in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method commonPopulateGetCreatedKiePMMLOutputFieldsMethod.
static void commonPopulateGetCreatedKiePMMLOutputFieldsMethod(final MethodDeclaration methodDeclaration, final List<org.dmg.pmml.OutputField> outputFields) {
BlockStmt body = new BlockStmt();
NodeList<Expression> arguments = new NodeList<>();
for (org.dmg.pmml.OutputField outputField : outputFields) {
String outputFieldVariableName = getSanitizedVariableName(outputField.getName().getValue()).toLowerCase();
BlockStmt toAdd = getOutputFieldVariableDeclaration(outputFieldVariableName, outputField);
toAdd.getStatements().forEach(body::addStatement);
arguments.add(new NameExpr(outputFieldVariableName));
}
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);
}
Aggregations