Search in sources :

Example 51 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.

the class InlineFieldReferenceInitHandler method generateInitBody.

private void generateInitBody(BlockStmt initBodyPerPartition, List<NetworkNode> subNodes, int partitionIndex) {
    CompilationUnit partitionedCompilationUnit = partitionedNodeInitialisationClasses.get(partitionIndex);
    PackageDeclaration partitionedClassPackage = (PackageDeclaration) partitionedCompilationUnit.getChildNodes().get(0);
    ClassOrInterfaceDeclaration partitionedClass = (ClassOrInterfaceDeclaration) partitionedCompilationUnit.getChildNodes().get(1);
    for (NetworkNode n : subNodes) {
        if (n instanceof CanInlineInANC) {
            String variableName;
            final ClassOrInterfaceType initMethodReturnType;
            if (n instanceof AlphaNode) {
                variableName = getVariableName((AlphaNode) n);
                initMethodReturnType = StaticJavaParser.parseClassOrInterfaceType(getVariableType((AlphaNode) n).getCanonicalName());
            } else {
                variableName = getVariableName((Sink) n);
                initMethodReturnType = StaticJavaParser.parseClassOrInterfaceType(getVariableType((Sink) n).getCanonicalName());
            }
            String initMethodName = String.format("init%s", variableName);
            // We'll need to partition the lambda creation as with too many the compiler crashes
            // We'll move it to a separated class
            MethodDeclaration initMethod = new MethodDeclaration(nodeList(Modifier.publicModifier(), Modifier.staticModifier()), initMethodName, initMethodReturnType, parametersFromAdditionalFields());
            Expression initExpressionForNode = ((CanInlineInANC) n).toANCInlinedForm();
            partitionedClass.addMember(initMethod);
            BlockStmt initMethodBlock = new BlockStmt();
            initMethodBlock.addStatement(new ReturnStmt(initExpressionForNode));
            initMethod.setBody(initMethodBlock);
            String callInitMethodAndAssignToFieldString = String.format("%s = %s.%s.%s(ctx);", variableName, partitionedClassPackage.getNameAsString(), partitionedClass.getNameAsString(), initMethodName);
            initBodyPerPartition.addStatement(parseStatement(callInitMethodAndAssignToFieldString));
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NetworkNode(org.drools.core.common.NetworkNode) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) AlphaNode(org.drools.core.reteoo.AlphaNode) Sink(org.drools.core.reteoo.Sink) Expression(com.github.javaparser.ast.expr.Expression) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) PackageDeclaration(com.github.javaparser.ast.PackageDeclaration)

Example 52 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType 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 53 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.

the class QueryDefImplGenerator method classDeclaration.

private ClassOrInterfaceDeclaration classDeclaration(CompilationUnit cu) {
    ClassOrInterfaceDeclaration clazz = cu.addClass(className, Modifier.Keyword.PUBLIC);
    clazz.addExtendedType("QueryDefImpl");
    clazz.addImplementedType("ModelComponent");
    ClassOrInterfaceType implement = new ClassOrInterfaceType(null, String.format("Query%dDef", arity));
    NodeList<Type> typeArguments = nodeList();
    rangeArity().forEach(i -> {
        String genericTypeName = genericTypeName(i);
        typeArguments.add(parseType(genericTypeName));
        clazz.addTypeParameter(new TypeParameter(genericTypeName));
        clazz.addField(parseType(genericType("Variable", genericTypeName)), argIndex(i), Modifier.Keyword.PRIVATE, Modifier.Keyword.FINAL);
    });
    implement.setTypeArguments(typeArguments);
    clazz.addImplementedType(implement);
    return clazz;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) StaticJavaParser.parseType(com.github.javaparser.StaticJavaParser.parseType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) TypeParameter(com.github.javaparser.ast.type.TypeParameter) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)

Example 54 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType 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 55 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType 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

ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)76 Expression (com.github.javaparser.ast.expr.Expression)33 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)29 Type (com.github.javaparser.ast.type.Type)24 NameExpr (com.github.javaparser.ast.expr.NameExpr)23 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)21 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)21 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)17 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)17 NodeList (com.github.javaparser.ast.NodeList)14 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)13 TypeParameter (com.github.javaparser.ast.type.TypeParameter)13 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)12 Test (org.junit.Test)11 StaticJavaParser.parseType (com.github.javaparser.StaticJavaParser.parseType)9 List (java.util.List)9 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)8 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 HashMap (java.util.HashMap)7