Search in sources :

Example 1 with ObjectCreationExpr

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));
}
Also used : StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MapBackedType(org.kie.dmn.feel.lang.impl.MapBackedType) GenFnType(org.kie.dmn.feel.lang.types.GenFnType) UnaryTestNode(org.kie.dmn.feel.lang.ast.UnaryTestNode) Parameter(com.github.javaparser.ast.body.Parameter) QuantifiedExpressionNode(org.kie.dmn.feel.lang.ast.QuantifiedExpressionNode) CastExpr(com.github.javaparser.ast.expr.CastExpr) Type(com.github.javaparser.ast.type.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) NamedParameter(org.kie.dmn.feel.lang.impl.NamedParameter) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) UnknownType(com.github.javaparser.ast.type.UnknownType) BuiltInTypeT(org.kie.dmn.feel.codegen.feel11.Constants.BuiltInTypeT) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) BigDecimalT(org.kie.dmn.feel.codegen.feel11.Constants.BigDecimalT) RangeNode(org.kie.dmn.feel.lang.ast.RangeNode) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) NameExpr(com.github.javaparser.ast.expr.NameExpr) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) List(java.util.List) InfixOpNode(org.kie.dmn.feel.lang.ast.InfixOpNode) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) GenListType(org.kie.dmn.feel.lang.types.GenListType) BaseFEELFunction(org.kie.dmn.feel.runtime.functions.BaseFEELFunction) EvalHelper(org.kie.dmn.feel.util.EvalHelper) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Collectors(java.util.stream.Collectors) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) MethodReferenceExpr(com.github.javaparser.ast.expr.MethodReferenceExpr) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Map(java.util.Map) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 2 with ObjectCreationExpr

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);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr)

Example 3 with ObjectCreationExpr

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;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Expression(com.github.javaparser.ast.expr.Expression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Interval(org.kie.dmn.validation.dtanalysis.model.Interval)

Example 4 with ObjectCreationExpr

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;
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 5 with ObjectCreationExpr

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;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) ParameterField(org.dmg.pmml.ParameterField)

Aggregations

ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)83 Expression (com.github.javaparser.ast.expr.Expression)59 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)44 NameExpr (com.github.javaparser.ast.expr.NameExpr)41 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)38 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)31 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)30 NodeList (com.github.javaparser.ast.NodeList)24 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)24 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)21 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)19 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14 List (java.util.List)13 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)12 Type (com.github.javaparser.ast.type.Type)12 Test (org.junit.Test)12 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)11 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)10