Search in sources :

Example 6 with ClassOrInterfaceType

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

the class ModelGenerator method addVariable.

private static void addVariable(KnowledgeBuilderImpl kbuilder, BlockStmt ruleBlock, DeclarationSpec decl) {
    if (decl.getDeclarationClass() == null) {
        kbuilder.addBuilderResult(new UnknownDeclarationError(decl.getBindingId()));
        return;
    }
    Type declType = classToReferenceType(decl.getDeclarationClass());
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(Variable.class.getCanonicalName());
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(decl.getBindingId()), Modifier.FINAL);
    MethodCallExpr declarationOfCall = new MethodCallExpr(null, DECLARATION_OF_CALL);
    declarationOfCall.addArgument(new ClassExpr(decl.getType()));
    declarationOfCall.addArgument(new StringLiteralExpr(decl.getVariableName().orElse(decl.getBindingId())));
    decl.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
    decl.getEntryPoint().ifPresent(ep -> {
        MethodCallExpr entryPointCall = new MethodCallExpr(null, "entryPoint");
        entryPointCall.addArgument(new StringLiteralExpr(ep));
        declarationOfCall.addArgument(entryPointCall);
    });
    for (BehaviorDescr behaviorDescr : decl.getBehaviors()) {
        MethodCallExpr windowCall = new MethodCallExpr(null, "window");
        if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("Window.Type.TIME");
            windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
        }
        if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("Window.Type.LENGTH");
            windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
        }
        declarationOfCall.addArgument(windowCall);
    }
    AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
    ruleBlock.addStatement(var_assign);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) Variable(org.drools.model.Variable) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) UnknownDeclarationError(org.drools.modelcompiler.builder.errors.UnknownDeclarationError) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr)

Example 7 with ClassOrInterfaceType

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

the class QueryGenerator method getQueryType.

private static ClassOrInterfaceType getQueryType(List<QueryParameter> queryParameters) {
    Class<?> res = QueryDef.getQueryClassByArity(queryParameters.size());
    ClassOrInterfaceType queryType = JavaParser.parseClassOrInterfaceType(res.getCanonicalName());
    Type[] genericType = queryParameters.stream().map(e -> e.type).map(DrlxParseUtil::classToReferenceType).toArray(Type[]::new);
    if (genericType.length > 0) {
        queryType.setTypeArguments(genericType);
    }
    return queryType;
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType)

Example 8 with ClassOrInterfaceType

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

the class QueryGenerator method processQueryDef.

public static void processQueryDef(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, QueryDescr queryDescr, boolean isPattern) {
    RuleContext context = new RuleContext(kbuilder, packageModel, queryDescr, typeResolver, isPattern);
    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 = new MethodCallExpr(null, QUERY_CALL);
    if (!queryDescr.getNamespace().isEmpty()) {
        queryCall.addArgument(new StringLiteralExpr(queryDescr.getNamespace()));
    }
    queryCall.addArgument(new StringLiteralExpr(queryName));
    for (QueryParameter qp : context.getQueryParameters()) {
        queryCall.addArgument(new ClassExpr(JavaParser.parseType(qp.type.getCanonicalName())));
        queryCall.addArgument(new StringLiteralExpr(qp.name));
    }
    packageModel.getQueryDefWithType().put(queryDefVariableName, new QueryDefWithType(queryDefType, queryCall, context));
}
Also used : StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)8 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)6 Type (org.drools.javaparser.ast.type.Type)6 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)5 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)4 FieldDeclaration (org.drools.javaparser.ast.body.FieldDeclaration)2 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)2 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)2 DrlxParseUtil.classToReferenceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType)2 BehaviorDescr (org.drools.compiler.lang.descr.BehaviorDescr)1 JavaParser.parseStatement (org.drools.javaparser.JavaParser.parseStatement)1 VariableDeclarator (org.drools.javaparser.ast.body.VariableDeclarator)1 ArrayAccessExpr (org.drools.javaparser.ast.expr.ArrayAccessExpr)1 ArrayCreationExpr (org.drools.javaparser.ast.expr.ArrayCreationExpr)1 BooleanLiteralExpr (org.drools.javaparser.ast.expr.BooleanLiteralExpr)1 CharLiteralExpr (org.drools.javaparser.ast.expr.CharLiteralExpr)1 DoubleLiteralExpr (org.drools.javaparser.ast.expr.DoubleLiteralExpr)1 IntegerLiteralExpr (org.drools.javaparser.ast.expr.IntegerLiteralExpr)1 LiteralExpr (org.drools.javaparser.ast.expr.LiteralExpr)1 LongLiteralExpr (org.drools.javaparser.ast.expr.LongLiteralExpr)1