Search in sources :

Example 6 with VariableDeclarationExpr

use of org.drools.javaparser.ast.expr.VariableDeclarationExpr 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)

Aggregations

AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)6 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)6 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)6 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)5 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)4 BlockStmt (org.drools.javaparser.ast.stmt.BlockStmt)4 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)4 Type (org.drools.javaparser.ast.type.Type)4 MethodDeclaration (org.drools.javaparser.ast.body.MethodDeclaration)3 NameExpr (org.drools.javaparser.ast.expr.NameExpr)3 ReturnStmt (org.drools.javaparser.ast.stmt.ReturnStmt)3 ModelGeneratorVisitor (org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 JavaParser (org.drools.javaparser.JavaParser)2 Modifier (org.drools.javaparser.ast.Modifier)2 Parameter (org.drools.javaparser.ast.body.Parameter)2 Expression (org.drools.javaparser.ast.expr.Expression)2