use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLInlineTableFactory method getInlineTableVariableDeclaration.
static BlockStmt getInlineTableVariableDeclaration(final String variableName, final InlineTable inlineTable) {
final MethodDeclaration methodDeclaration = INLINETABLE_TEMPLATE.getMethodsByName(GETKIEPMMLINLINETABLE).get(0).clone();
final BlockStmt inlineTableBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(inlineTableBody, INLINETABLE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, INLINETABLE, inlineTableBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (Row row : inlineTable.getRows()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getRowVariableDeclaration(nestedVariableName, row);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, INLINETABLE, toReturn))).asObjectCreationExpr();
objectCreationExpr.getArguments().set(0, new StringLiteralExpr(variableName));
objectCreationExpr.getArguments().get(2).asMethodCallExpr().setArguments(arguments);
inlineTableBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLNormDiscreteFactory method getNormDiscreteVariableDeclaration.
static BlockStmt getNormDiscreteVariableDeclaration(final String variableName, final NormDiscrete normDiscrete) {
final MethodDeclaration methodDeclaration = NORMDISCRETE_TEMPLATE.getMethodsByName(GETKIEPMMLNORMDISCRETE).get(0).clone();
final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, NORM_DISCRETE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, NORM_DISCRETE, toReturn)));
variableDeclarator.setName(variableName);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, NORM_DISCRETE, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(normDiscrete.getField().getValue());
final Expression mapMissingToExpr = getExpressionForObject(normDiscrete.getMapMissingTo());
objectCreationExpr.getArguments().set(0, nameExpr);
objectCreationExpr.getArguments().set(2, new StringLiteralExpr((String) normDiscrete.getValue()));
objectCreationExpr.getArguments().set(3, mapMissingToExpr);
return toReturn;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class PMMLRuleMappersFactory method getPMMLRuleMappersSource.
public static String getPMMLRuleMappersSource(final String packageName, final List<String> generatedRuleMappers) {
CompilationUnit cloneCU = JavaParserUtils.getKiePMMLModelCompilationUnit(KIE_PMML_RULE_MAPPERS_CLASS_NAME, packageName, KIE_PMML_RULE_MAPPERS_TEMPLATE_JAVA, KIE_PMML_RULE_MAPPERS_CLASS_NAME);
ClassOrInterfaceDeclaration typeDeclaration = (ClassOrInterfaceDeclaration) cloneCU.getTypes().get(0);
FieldDeclaration ruleNameField = typeDeclaration.getFieldByName("pmmlRuleMappers").orElseThrow(() -> new RuntimeException("The template " + KIE_PMML_RULE_MAPPERS_TEMPLATE_JAVA + " has been modified."));
final List<Expression> nodeList = generatedRuleMappers.stream().map(generatedRuleMapper -> {
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(generatedRuleMapper);
return objectCreationExpr;
}).collect(Collectors.toList());
NodeList<Expression> expressions = NodeList.nodeList(nodeList);
MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions);
ruleNameField.getVariables().get(0).setInitializer(methodCallExpr);
return cloneCU.toString();
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr 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.expr.ObjectCreationExpr 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);
}
Aggregations