use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class Expressions method genContextType.
public static Expression genContextType(Map<String, Expression> fields) {
final ClassOrInterfaceType sie = parseClassOrInterfaceType(java.util.AbstractMap.SimpleImmutableEntry.class.getCanonicalName());
sie.setTypeArguments(parseClassOrInterfaceType(String.class.getCanonicalName()), parseClassOrInterfaceType(org.kie.dmn.feel.lang.Type.class.getCanonicalName()));
List<Expression> entryParams = fields.entrySet().stream().map(e -> new ObjectCreationExpr(null, sie, new NodeList<>(stringLiteral(e.getKey()), e.getValue()))).collect(Collectors.toList());
MethodCallExpr mOf = new MethodCallExpr(new NameExpr(java.util.stream.Stream.class.getCanonicalName()), "of");
entryParams.forEach(mOf::addArgument);
MethodCallExpr mCollect = new MethodCallExpr(mOf, "collect");
mCollect.addArgument(new MethodCallExpr(new NameExpr(java.util.stream.Collectors.class.getCanonicalName()), "toMap").addArgument(new MethodReferenceExpr(new NameExpr(java.util.Map.Entry.class.getCanonicalName()), new NodeList<>(), "getKey")).addArgument(new MethodReferenceExpr(new NameExpr(java.util.Map.Entry.class.getCanonicalName()), new NodeList<>(), "getValue")));
return new ObjectCreationExpr(null, MapBackedTypeT, new NodeList<>(stringLiteral("[anonymous]"), mCollect));
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class Constants method numeric.
public static FieldDeclaration numeric(String name, String numericValue) {
ObjectCreationExpr initializer = new ObjectCreationExpr();
initializer.setType(BigDecimalT);
String originalText = numericValue;
try {
Long.parseLong(originalText);
// see EvalHelper.getBigDecimalOrNull
initializer.addArgument(originalText.replaceFirst("^0+(?!$)", ""));
} catch (Throwable t) {
initializer.addArgument(new StringLiteralExpr(originalText));
}
initializer.addArgument(DECIMAL_128);
return of(BigDecimalT, name, initializer);
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class DTAnalysisMeta method hrAsExpression.
private static Expression hrAsExpression(Hyperrectangle gap) {
int dimensions = gap.getDimensions();
MethodCallExpr edgesExpression = parseExpression("Arrays.asList()");
for (Interval edge : gap.getEdges()) {
Expression intervalAsExpression = intervalAsExpression(edge);
edgesExpression.addArgument(intervalAsExpression);
}
ObjectCreationExpr newExpression = parseExpression("new Hyperrectangle()");
newExpression.addArgument(new IntegerLiteralExpr(dimensions));
newExpression.addArgument(edgesExpression);
return newExpression;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class DTAnalysisMeta method overlapAsExpression.
private static Expression overlapAsExpression(Overlap overlap) {
MethodCallExpr edgesExpression = parseExpression("Arrays.asList()");
for (Number edge : overlap.getRules()) {
edgesExpression.addArgument(new IntegerLiteralExpr(edge.intValue()));
}
ObjectCreationExpr newExpression = parseExpression("new Overlap()");
newExpression.addArgument(edgesExpression);
newExpression.addArgument(hrAsExpression(overlap.getOverlap()));
return newExpression;
}
use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.
the class KiePMMLDefineFunctionFactory method getDefineFunctionVariableDeclaration.
static BlockStmt getDefineFunctionVariableDeclaration(final DefineFunction defineFunction) {
final MethodDeclaration methodDeclaration = DEFINE_FUNCTION_TEMPLATE.getMethodsByName(GETKIEPMMLDEFINEFUNCTION).get(0).clone();
final BlockStmt defineFunctionBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(defineFunctionBody, DEFINE_FUNCTION).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DEFINE_FUNCTION, defineFunctionBody)));
variableDeclarator.setName(defineFunction.getName());
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> parameterFieldArguments = new NodeList<>();
for (ParameterField parameterField : defineFunction.getParameterFields()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, defineFunction.getName(), counter);
parameterFieldArguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getParameterFieldVariableDeclaration(nestedVariableName, parameterField);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
String kiePMMLExpression = String.format("%s_Expression", defineFunction.getName());
BlockStmt toAdd = getKiePMMLExpressionBlockStmt(kiePMMLExpression, defineFunction.getExpression());
toAdd.getStatements().forEach(toReturn::addStatement);
final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DEFINE_FUNCTION, toReturn))).asObjectCreationExpr();
final StringLiteralExpr nameExpr = new StringLiteralExpr(defineFunction.getName());
objectCreationExpr.getArguments().set(0, nameExpr);
final Expression dataTypeExpression = getExpressionForDataType(defineFunction.getDataType());
final Expression opTypeExpression = getExpressionForOpType(defineFunction.getOpType());
objectCreationExpr.getArguments().set(2, dataTypeExpression);
objectCreationExpr.getArguments().set(3, opTypeExpression);
objectCreationExpr.getArguments().get(4).asMethodCallExpr().setArguments(parameterFieldArguments);
objectCreationExpr.getArguments().set(5, new NameExpr(kiePMMLExpression));
defineFunctionBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
Aggregations