Search in sources :

Example 46 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class ConstraintParser method toBigDecimalExpression.

// TODO luca this logic should be moved in Constraint compiler?
private Expression toBigDecimalExpression(TypedExpression typedExpression) {
    MethodCallExpr toBigDecimalMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.toBigDecimal");
    Expression arg = typedExpression.getExpression();
    Optional<Class<?>> originalPatternType = typedExpression.getOriginalPatternType();
    ConstraintCompiler constraintCompiler = createConstraintCompiler(context, originalPatternType);
    CompiledExpressionResult compiledBlockResult = constraintCompiler.compileExpression(PrintUtil.printNode(arg));
    arg = compiledBlockResult.getExpression();
    if (arg.isEnclosedExpr()) {
        arg = arg.asEnclosedExpr().getInner();
    }
    if (arg instanceof BigIntegerLiteralExpr) {
        arg = new ObjectCreationExpr(null, toClassOrInterfaceType(BigInteger.class), NodeList.nodeList(new StringLiteralExpr(((BigIntegerLiteralExpr) arg).asBigInteger().toString())));
    } else if (arg instanceof BigDecimalLiteralExpr) {
        arg = new ObjectCreationExpr(null, toClassOrInterfaceType(BigDecimal.class), NodeList.nodeList(new StringLiteralExpr(((BigDecimalLiteralExpr) arg).asBigDecimal().toString())));
    }
    toBigDecimalMethod.addArgument(arg);
    return toBigDecimalMethod;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) CompiledExpressionResult(org.drools.mvelcompiler.CompiledExpressionResult) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) ConstraintCompiler(org.drools.mvelcompiler.ConstraintCompiler) DrlxParseUtil.createConstraintCompiler(org.drools.modelcompiler.builder.generator.DrlxParseUtil.createConstraintCompiler) BigDecimal(java.math.BigDecimal) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr)

Example 47 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class ObjectCreationExpressionT method toJavaExpression.

@Override
public Node toJavaExpression() {
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(type);
    List<Expression> arguments = this.constructorArguments.stream().map(typedExpression -> (Expression) typedExpression.toJavaExpression()).collect(Collectors.toList());
    objectCreationExpr.setArguments(NodeList.nodeList(arguments));
    return objectCreationExpr;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) NodeList(com.github.javaparser.ast.NodeList) List(java.util.List) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) Collectors(java.util.stream.Collectors) Node(com.github.javaparser.ast.Node) Expression(com.github.javaparser.ast.expr.Expression)

Example 48 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class ParserUtil method objectCreationExprToValue.

public static Object objectCreationExprToValue(ObjectCreationExpr objectCreationExpr, RuleContext context) {
    // Only a few classes/constructors are handled. Otherwise, value becomes null so a link would be UNKNOWN. To be enhanced : DROOLS-6711
    ClassOrInterfaceType type = objectCreationExpr.getType();
    Class<?> clazz = getClassFromType(context.getTypeResolver(), type);
    if (clazz.equals(BigDecimal.class)) {
        NodeList<Expression> arguments = objectCreationExpr.getArguments();
        Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigDecimal::new);
        return opt.orElse(null);
    } else if (clazz.equals(BigInteger.class)) {
        NodeList<Expression> arguments = objectCreationExpr.getArguments();
        Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigInteger::new);
        return opt.orElse(null);
    }
    return null;
}
Also used : NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) DrlxParseUtil.getClassFromType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getClassFromType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BigDecimal(java.math.BigDecimal) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Optional(java.util.Optional) BigInteger(java.math.BigInteger) Expression(com.github.javaparser.ast.expr.Expression) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BigInteger(java.math.BigInteger) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) BigDecimal(java.math.BigDecimal)

Example 49 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class FlowDSLQueryGenerator method queryNameClass.

private void queryNameClass(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration query1 = clazz.addMethod(QUERY, Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
    query1.addParameter(STRINGLITERAL, "name");
    BlockStmt stmts = new BlockStmt();
    NodeList<Type> typeArguments = nodeList();
    NodeList<TypeParameter> typeParameters = nodeList();
    NodeList<Expression> arguments = nodeList();
    ClassOrInterfaceType queryCallViewItemImpl = queryDefImpl();
    ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
    objCreationExpr.setDiamondOperator();
    stmts.addStatement(new ReturnStmt(objCreationExpr));
    objCreationExpr.addArgument(new NameExpr(VIEWBUILDER));
    objCreationExpr.addArgument("name");
    rangeArity().forEach(i -> {
        String genericTypeName = stringWithIndex("T", i);
        String type = stringWithIndex("type", i);
        typeArguments.add(parseType(genericTypeName));
        arguments.add(new NameExpr(type));
        String classGenericType = genericType(genericTypeName);
        Type genericType = parseType(classGenericType);
        query1.addParameter(genericType, type);
        typeParameters.add(new TypeParameter(genericTypeName));
    });
    query1.setTypeParameters(typeParameters);
    query1.setBody(stmts);
    ClassOrInterfaceType type = queryDef();
    type.setTypeArguments(typeArguments);
    query1.setType(type);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

Example 50 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project drools by kiegroup.

the class FlowDSLQueryGenerator method queryPkgNameClass.

private void queryPkgNameClass(ClassOrInterfaceDeclaration clazz) {
    MethodDeclaration query1 = clazz.addMethod(QUERY, Modifier.Keyword.PUBLIC, Modifier.Keyword.STATIC);
    query1.addParameter(STRINGLITERAL, "pkg");
    query1.addParameter(STRINGLITERAL, "name");
    BlockStmt stmts = new BlockStmt();
    NodeList<Type> typeArguments = nodeList();
    NodeList<TypeParameter> typeParameters = nodeList();
    NodeList<Expression> arguments = nodeList();
    ClassOrInterfaceType queryCallViewItemImpl = queryDefImpl();
    ObjectCreationExpr objCreationExpr = new ObjectCreationExpr(null, queryCallViewItemImpl, arguments);
    objCreationExpr.setDiamondOperator();
    stmts.addStatement(new ReturnStmt(objCreationExpr));
    objCreationExpr.addArgument(new NameExpr(VIEWBUILDER));
    objCreationExpr.addArgument("pkg");
    objCreationExpr.addArgument("name");
    rangeArity().forEach(i -> {
        String genericTypeName = stringWithIndex("T", i);
        String type = stringWithIndex("type", i);
        typeArguments.add(parseType(genericTypeName));
        arguments.add(new NameExpr(type));
        String classGenericType = genericType(genericTypeName);
        Type genericType = parseType(classGenericType);
        query1.addParameter(genericType, type);
        typeParameters.add(new TypeParameter(genericTypeName));
    });
    query1.setTypeParameters(typeParameters);
    query1.setBody(stmts);
    ClassOrInterfaceType type = queryDef();
    type.setTypeArguments(typeArguments);
    query1.setType(type);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) TypeParameter(com.github.javaparser.ast.type.TypeParameter) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) Expression(com.github.javaparser.ast.expr.Expression) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt)

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