Search in sources :

Example 61 with ClassOrInterfaceType

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

the class ModelGenerator method addVariable.

private static void addVariable(BlockStmt ruleBlock, DeclarationSpec declaration, RuleContext context, boolean domainClass) {
    if (declaration.getDeclarationClass() == null) {
        context.addCompilationError(new UnknownDeclarationError(declaration.getBindingId()));
        return;
    }
    Type declType = classToReferenceType(declaration);
    ClassOrInterfaceType varType = toClassOrInterfaceType(Variable.class);
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(declaration.getBindingId()), Modifier.finalModifier());
    MethodCallExpr declarationOfCall = createDslTopLevelMethod(DECLARATION_OF_CALL);
    declarationOfCall.addArgument(new ClassExpr(declaration.getBoxedType()));
    if (domainClass) {
        String domainClassSourceName = asJavaSourceName(declaration.getDeclarationClass());
        declarationOfCall.addArgument(DOMAIN_CLASSESS_METADATA_FILE_NAME + context.getPackageModel().getPackageUUID() + "." + domainClassSourceName + DOMAIN_CLASS_METADATA_INSTANCE);
    }
    declarationOfCall.addArgument(toStringLiteral(declaration.getVariableName().orElse(declaration.getBindingId())));
    declaration.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
    declaration.getEntryPoint().ifPresent(ep -> {
        MethodCallExpr entryPointCall = createDslTopLevelMethod(ENTRY_POINT_CALL);
        entryPointCall.addArgument(toStringLiteral(ep));
        declarationOfCall.addArgument(entryPointCall);
    });
    for (BehaviorDescr behaviorDescr : declaration.getBehaviors()) {
        MethodCallExpr windowCall = createDslTopLevelMethod(WINDOW_CALL);
        if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("org.drools.model.Window.Type.TIME");
            windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
        }
        if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("org.drools.model.Window.Type.LENGTH");
            windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
        }
        declarationOfCall.addArgument(windowCall);
    }
    AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
    if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock, var_assign)) {
        ruleBlock.addStatement(var_assign);
    }
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) UnknownDeclarationError(org.drools.modelcompiler.builder.errors.UnknownDeclarationError) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Example 62 with ClassOrInterfaceType

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

the class QueryGenerator method processQueryDef.

public static void processQueryDef(PackageModel packageModel, RuleContext context) {
    QueryDescr queryDescr = ((QueryDescr) context.getRuleDescr());
    packageModel.registerQueryName(queryDescr.getName());
    String queryName = queryDescr.getName();
    final String queryDefVariableName = toQueryDef(queryName);
    context.setQueryName(Optional.of(queryDefVariableName));
    parseQueryParameters(context, packageModel, queryDescr);
    ClassOrInterfaceType queryDefType = getQueryType(context.getQueryParameters());
    MethodCallExpr queryCall = createDslTopLevelMethod(QUERY_CALL);
    if (!queryDescr.getNamespace().isEmpty()) {
        queryCall.addArgument(toStringLiteral(queryDescr.getNamespace()));
    }
    queryCall.addArgument(toStringLiteral(queryName));
    for (QueryParameter qp : context.getQueryParameters()) {
        queryCall.addArgument(new ClassExpr(toClassOrInterfaceType(qp.getType())));
        queryCall.addArgument(toStringLiteral(qp.getName()));
    }
    packageModel.getQueryDefWithType().put(queryDefVariableName, new QueryDefWithType(queryDefType, queryCall, context));
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 63 with ClassOrInterfaceType

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

the class QueryGenerator method getQueryType.

private static ClassOrInterfaceType getQueryType(List<QueryParameter> queryParameters) {
    ClassOrInterfaceType queryType = toClassOrInterfaceType(QueryDef.getQueryClassByArity(queryParameters.size()));
    Type[] genericType = queryParameters.stream().map(e -> e.getType()).map(DrlxParseUtil::classToReferenceType).toArray(Type[]::new);
    if (genericType.length > 0) {
        queryType.setTypeArguments(genericType);
    }
    return queryType;
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) 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)

Example 64 with ClassOrInterfaceType

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

the class CommonCodegenUtilsTest method getTypedClassOrInterfaceType.

@Test
public void getTypedClassOrInterfaceType() {
    String className = "CLASS_NAME";
    List<String> typesName = Arrays.asList("TypeA", "TypeB");
    ClassOrInterfaceType retrieved = CommonCodegenUtils.getTypedClassOrInterfaceTypeByTypeNames(className, typesName);
    assertNotNull(retrieved);
    String expected = String.format("%1$s<%2$s,%3$s>", className, typesName.get(0), typesName.get(1));
    assertEquals(expected, retrieved.asString());
}
Also used : StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Test(org.junit.Test)

Example 65 with ClassOrInterfaceType

use of com.github.javaparser.ast.type.ClassOrInterfaceType 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);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

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