Search in sources :

Example 1 with VariableDeclarationExpr

use of org.drools.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class ModelGenerator method processRule.

private static void processRule(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, PackageDescr packageDescr, RuleDescr ruleDescr, boolean isPattern) {
    RuleContext context = new RuleContext(kbuilder, packageModel, ruleDescr, typeResolver, isPattern);
    for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
        context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
    }
    new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
    final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
    MethodDeclaration ruleMethod = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), RULE_TYPE, ruleMethodName);
    ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
    VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(RULE_TYPE, RULE_CALL);
    MethodCallExpr ruleCall = new MethodCallExpr(null, RULE_CALL);
    if (!ruleDescr.getNamespace().isEmpty()) {
        ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getNamespace()));
    }
    ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getName()));
    RuleUnitDescr ruleUnitDescr = context.getRuleUnitDescr();
    MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(classToReferenceType(ruleUnitDescr.getRuleUnitClass()))) : ruleCall;
    for (MethodCallExpr attributeExpr : ruleAttributes(context, ruleDescr)) {
        attributeExpr.setScope(buildCallScope);
        buildCallScope = attributeExpr;
    }
    for (MethodCallExpr metaAttributeExpr : ruleMetaAttributes(context, ruleDescr)) {
        metaAttributeExpr.setScope(buildCallScope);
        buildCallScope = metaAttributeExpr;
    }
    MethodCallExpr buildCall = new MethodCallExpr(buildCallScope, BUILD_CALL, NodeList.nodeList(context.getExpressions()));
    BlockStmt ruleVariablesBlock = new BlockStmt();
    createUnitData(ruleUnitDescr, ruleVariablesBlock);
    createVariables(kbuilder, ruleVariablesBlock, packageModel, context);
    ruleMethod.setBody(ruleVariablesBlock);
    MethodCallExpr executeCall = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
    buildCall.addArgument(executeCall);
    ruleVariablesBlock.addStatement(new AssignExpr(ruleVar, buildCall, AssignExpr.Operator.ASSIGN));
    ruleVariablesBlock.addStatement(new ReturnStmt(RULE_CALL));
    packageModel.putRuleMethod(ruleMethodName, ruleMethod);
}
Also used : VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with VariableDeclarationExpr

use of org.drools.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class QueryGenerator method processQuery.

public static void processQuery(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, QueryDescr queryDescr) {
    String queryDefVariableName = toQueryDef(queryDescr.getName());
    RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
    new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
    final Type queryType = JavaParser.parseType(Query.class.getCanonicalName());
    MethodDeclaration queryMethod = new MethodDeclaration(EnumSet.of(Modifier.PRIVATE), queryType, "query_" + toId(queryDescr.getName()));
    BlockStmt queryBody = new BlockStmt();
    ModelGenerator.createVariables(kbuilder, queryBody, packageModel, context);
    queryMethod.setBody(queryBody);
    String queryBuildVarName = toId(queryDescr.getName()) + "_build";
    VariableDeclarationExpr queryBuildVar = new VariableDeclarationExpr(queryType, queryBuildVarName);
    MethodCallExpr buildCall = new MethodCallExpr(new NameExpr(queryDefVariableName), BUILD_CALL);
    context.getExpressions().forEach(buildCall::addArgument);
    AssignExpr queryBuildAssign = new AssignExpr(queryBuildVar, buildCall, AssignExpr.Operator.ASSIGN);
    queryBody.addStatement(queryBuildAssign);
    queryBody.addStatement(new ReturnStmt(queryBuildVarName));
    packageModel.putQueryMethod(queryMethod);
}
Also used : VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) Query(org.drools.model.Query) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) NameExpr(org.drools.javaparser.ast.expr.NameExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 3 with VariableDeclarationExpr

use of org.drools.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class AccumulateVisitor method visitAccInlineCustomCode.

/**
 * By design this legacy accumulate (with inline custome code) visitor supports only with 1-and-only binding in the accumulate code/expressions.
 */
protected void visitAccInlineCustomCode(RuleContext context2, AccumulateDescr descr, MethodCallExpr accumulateDSL, PatternDescr basePattern, PatternDescr inputDescr) {
    context.pushExprPointer(accumulateDSL::addArgument);
    final MethodCallExpr functionDSL = new MethodCallExpr(null, "accFunction");
    String code = null;
    try {
        code = new String(IoUtils.readBytesFromInputStream(this.getClass().getResourceAsStream("/AccumulateInlineFunction.java")));
    } catch (IOException e1) {
        e1.printStackTrace();
        throw new RuntimeException("Unable to locate template.");
    }
    String targetClassName = StringUtil.toId(context2.getRuleDescr().getName()) + "Accumulate" + descr.getLine();
    code = code.replaceAll("AccumulateInlineFunction", targetClassName);
    CompilationUnit templateCU = JavaParser.parse(code);
    ClassOrInterfaceDeclaration templateClass = templateCU.getClassByName(targetClassName).orElseThrow(() -> new RuntimeException("Template did not contain expected type definition."));
    ClassOrInterfaceDeclaration templateContextClass = templateClass.getMembers().stream().filter(m -> m instanceof ClassOrInterfaceDeclaration && ((ClassOrInterfaceDeclaration) m).getNameAsString().equals("ContextData")).map(ClassOrInterfaceDeclaration.class::cast).findFirst().orElseThrow(() -> new RuntimeException("Template did not contain expected type definition."));
    List<String> contextFieldNames = new ArrayList<>();
    MethodDeclaration initMethod = templateClass.getMethodsByName("init").get(0);
    BlockStmt initBlock = JavaParser.parseBlock("{" + descr.getInitCode() + "}");
    for (Statement stmt : initBlock.getStatements()) {
        if (stmt instanceof ExpressionStmt && ((ExpressionStmt) stmt).getExpression() instanceof VariableDeclarationExpr) {
            VariableDeclarationExpr vdExpr = (VariableDeclarationExpr) ((ExpressionStmt) stmt).getExpression();
            for (VariableDeclarator vd : vdExpr.getVariables()) {
                contextFieldNames.add(vd.getNameAsString());
                templateContextClass.addField(vd.getType(), vd.getNameAsString(), Modifier.PUBLIC);
                if (vd.getInitializer().isPresent()) {
                    Expression initializer = vd.getInitializer().get();
                    Expression target = new FieldAccessExpr(new NameExpr("data"), vd.getNameAsString());
                    Statement initStmt = new ExpressionStmt(new AssignExpr(target, initializer, AssignExpr.Operator.ASSIGN));
                    initMethod.getBody().get().addStatement(initStmt);
                }
            }
        } else {
            // add as-is.
            initMethod.getBody().get().addStatement(stmt);
        }
    }
    Type singleAccumulateType = JavaParser.parseType("java.lang.Object");
    MethodDeclaration accumulateMethod = templateClass.getMethodsByName("accumulate").get(0);
    BlockStmt actionBlock = JavaParser.parseBlock("{" + descr.getActionCode() + "}");
    Collection<String> allNamesInActionBlock = collectNamesInBlock(context2, actionBlock);
    if (allNamesInActionBlock.size() == 1) {
        String nameExpr = allNamesInActionBlock.iterator().next();
        accumulateMethod.getParameter(1).setName(nameExpr);
        singleAccumulateType = context2.getDeclarationById(nameExpr).get().getType();
    } else {
        new LegacyAccumulate(context, descr, basePattern).build();
        return;
    }
    writeAccumulateMethod(contextFieldNames, singleAccumulateType, accumulateMethod, actionBlock);
    // <result expression>: this is a semantic expression in the selected dialect that is executed after all source objects are iterated.
    MethodDeclaration resultMethod = templateClass.getMethodsByName("getResult").get(0);
    Type returnExpressionType = JavaParser.parseType("java.lang.Object");
    Expression returnExpression = JavaParser.parseExpression(descr.getResultCode());
    if (returnExpression instanceof NameExpr) {
        returnExpression = new EnclosedExpr(returnExpression);
    }
    rescopeNamesToNewScope(new NameExpr("data"), contextFieldNames, returnExpression);
    resultMethod.getBody().get().addStatement(new ReturnStmt(returnExpression));
    MethodDeclaration getResultTypeMethod = templateClass.getMethodsByName("getResultType").get(0);
    getResultTypeMethod.getBody().get().addStatement(new ReturnStmt(new ClassExpr(returnExpressionType)));
    if (descr.getReverseCode() != null) {
        MethodDeclaration supportsReverseMethod = templateClass.getMethodsByName("supportsReverse").get(0);
        supportsReverseMethod.getBody().get().addStatement(JavaParser.parseStatement("return true;"));
        MethodDeclaration reverseMethod = templateClass.getMethodsByName("reverse").get(0);
        BlockStmt reverseBlock = JavaParser.parseBlock("{" + descr.getReverseCode() + "}");
        Collection<String> allNamesInReverseBlock = collectNamesInBlock(context2, reverseBlock);
        if (allNamesInReverseBlock.size() == 1) {
            reverseMethod.getParameter(1).setName(allNamesInReverseBlock.iterator().next());
        } else {
            new LegacyAccumulate(context, descr, basePattern).build();
            return;
        }
        writeAccumulateMethod(contextFieldNames, singleAccumulateType, reverseMethod, reverseBlock);
    } else {
        MethodDeclaration supportsReverseMethod = templateClass.getMethodsByName("supportsReverse").get(0);
        supportsReverseMethod.getBody().get().addStatement(JavaParser.parseStatement("return false;"));
        MethodDeclaration reverseMethod = templateClass.getMethodsByName("reverse").get(0);
        reverseMethod.getBody().get().addStatement(JavaParser.parseStatement("throw new UnsupportedOperationException(\"This function does not support reverse.\");"));
    }
    // add resulting accumulator class into the package model
    this.packageModel.addGeneratedPOJO(templateClass);
    functionDSL.addArgument(new ClassExpr(JavaParser.parseType(targetClassName)));
    functionDSL.addArgument(new NameExpr(toVar(inputDescr.getIdentifier())));
    final String bindingId = basePattern.getIdentifier();
    final MethodCallExpr asDSL = new MethodCallExpr(functionDSL, "as");
    asDSL.addArgument(new NameExpr(toVar(bindingId)));
    accumulateDSL.addArgument(asDSL);
    context.popExprPointer();
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) JavaParser(org.drools.javaparser.JavaParser) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) DrlxParseUtil.toType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toType) AccumulateUtil(org.drools.compiler.rule.builder.util.AccumulateUtil) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) DrlxParseUtil.forceCastForName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.forceCastForName) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) DrlxParseUtil.toVar(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar) ConstraintParser(org.drools.modelcompiler.builder.generator.drlxparse.ConstraintParser) Statement(org.drools.javaparser.ast.stmt.Statement) PackageModel(org.drools.modelcompiler.builder.PackageModel) Parameter(org.drools.javaparser.ast.body.Parameter) Collection(java.util.Collection) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) List(java.util.List) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Optional(java.util.Optional) DrlxParseFail(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseFail) DrlxParseResult(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseResult) AbstractExpressionBuilder(org.drools.modelcompiler.builder.generator.expression.AbstractExpressionBuilder) DrlxParseSuccess(org.drools.modelcompiler.builder.generator.drlxparse.DrlxParseSuccess) DrlxParseUtil.rescopeNamesToNewScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.rescopeNamesToNewScope) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) IoUtils(org.drools.core.util.IoUtils) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) StringUtil(org.drools.modelcompiler.util.StringUtil) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) ClassOrInterfaceDeclaration(org.drools.javaparser.ast.body.ClassOrInterfaceDeclaration) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) AccumulateDescr(org.drools.compiler.lang.descr.AccumulateDescr) VariableDeclarator(org.drools.javaparser.ast.body.VariableDeclarator) IOException(java.io.IOException) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) CompilationUnit(org.drools.javaparser.ast.CompilationUnit) Type(org.drools.javaparser.ast.type.Type) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) Collectors.toList(java.util.stream.Collectors.toList) ParseResultVisitor(org.drools.modelcompiler.builder.generator.drlxparse.ParseResultVisitor) Modifier(org.drools.javaparser.ast.Modifier) ClassOrInterfaceDeclaration(org.drools.javaparser.ast.body.ClassOrInterfaceDeclaration) ArrayList(java.util.ArrayList) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) VariableDeclarator(org.drools.javaparser.ast.body.VariableDeclarator) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) CompilationUnit(org.drools.javaparser.ast.CompilationUnit) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) Statement(org.drools.javaparser.ast.stmt.Statement) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) IOException(java.io.IOException) DrlxParseUtil.toType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toType) UnknownType(org.drools.javaparser.ast.type.UnknownType) Type(org.drools.javaparser.ast.type.Type) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 4 with VariableDeclarationExpr

use of org.drools.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class Consequence method rewriteRHS.

private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) {
    AtomicBoolean requireDrools = new AtomicBoolean(false);
    List<MethodCallExpr> methodCallExprs = rhs.findAll(MethodCallExpr.class);
    List<MethodCallExpr> updateExprs = new ArrayList<>();
    for (MethodCallExpr methodCallExpr : methodCallExprs) {
        if (isDroolsMethod(methodCallExpr)) {
            if (!methodCallExpr.getScope().isPresent()) {
                methodCallExpr.setScope(new NameExpr("drools"));
            }
            if (knowledgeHelperMethods.contains(methodCallExpr.getNameAsString())) {
                methodCallExpr.setScope(asKnoledgeHelperExpression);
            } else if (methodCallExpr.getNameAsString().equals("update")) {
                updateExprs.add(methodCallExpr);
            } else if (methodCallExpr.getNameAsString().equals("retract")) {
                methodCallExpr.setName(new SimpleName("delete"));
            }
            requireDrools.set(true);
        }
    }
    for (MethodCallExpr updateExpr : updateExprs) {
        Expression argExpr = updateExpr.getArgument(0);
        if (argExpr instanceof NameExpr) {
            String updatedVar = ((NameExpr) argExpr).getNameAsString();
            Class<?> updatedClass = context.getDeclarationById(updatedVar).map(DeclarationSpec::getDeclarationClass).orElseThrow(RuntimeException::new);
            MethodCallExpr bitMaskCreation = new MethodCallExpr(new NameExpr(BitMask.class.getCanonicalName()), "getPatternMask");
            bitMaskCreation.addArgument(new ClassExpr(JavaParser.parseClassOrInterfaceType(updatedClass.getCanonicalName())));
            methodCallExprs.subList(0, methodCallExprs.indexOf(updateExpr)).stream().filter(mce -> mce.getScope().isPresent() && hasScope(mce, updatedVar)).map(mce -> ClassUtils.setter2property(mce.getNameAsString())).filter(Objects::nonNull).distinct().forEach(s -> bitMaskCreation.addArgument(new StringLiteralExpr(s)));
            VariableDeclarationExpr bitMaskVar = new VariableDeclarationExpr(BITMASK_TYPE, "mask_" + updatedVar, Modifier.FINAL);
            AssignExpr bitMaskAssign = new AssignExpr(bitMaskVar, bitMaskCreation, AssignExpr.Operator.ASSIGN);
            ruleBlock.addStatement(bitMaskAssign);
            updateExpr.addArgument("mask_" + updatedVar);
        }
    }
    return requireDrools.get();
}
Also used : BitMask(org.drools.model.BitMask) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseUtil.isNameExprWithName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isNameExprWithName) DroolsImpl(org.drools.modelcompiler.consequence.DroolsImpl) JavaParser(org.drools.javaparser.JavaParser) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) BREAKING_CALL(org.drools.modelcompiler.builder.generator.visitor.NamedConsequenceVisitor.BREAKING_CALL) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Statement(org.drools.javaparser.ast.stmt.Statement) Collectors.toSet(java.util.stream.Collectors.toSet) PackageModel(org.drools.modelcompiler.builder.PackageModel) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) DrlxParseUtil.hasScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.hasScope) Parameter(org.drools.javaparser.ast.body.Parameter) ClassUtils(org.drools.core.util.ClassUtils) Collection(java.util.Collection) DrlxParseUtil.parseBlock(org.drools.modelcompiler.builder.generator.DrlxParseUtil.parseBlock) Set(java.util.Set) EmptyStmt(org.drools.javaparser.ast.stmt.EmptyStmt) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Objects(java.util.Objects) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) DrlxParseUtil.findAllChildrenRecursive(org.drools.modelcompiler.builder.generator.DrlxParseUtil.findAllChildrenRecursive) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Modifier(org.drools.javaparser.ast.Modifier) SimpleName(org.drools.javaparser.ast.expr.SimpleName) StringUtils(org.drools.core.util.StringUtils) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) SimpleName(org.drools.javaparser.ast.expr.SimpleName) ArrayList(java.util.ArrayList) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Objects(java.util.Objects) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 5 with VariableDeclarationExpr

use of org.drools.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class ModelGenerator method addUnitData.

private static void addUnitData(String unitVar, Class<?> type, BlockStmt ruleBlock) {
    Type declType = classToReferenceType(type);
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(UnitData.class.getCanonicalName());
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(unitVar), Modifier.FINAL);
    MethodCallExpr unitDataCall = new MethodCallExpr(null, UNIT_DATA_CALL);
    unitDataCall.addArgument(new ClassExpr(declType));
    unitDataCall.addArgument(new StringLiteralExpr(unitVar));
    AssignExpr var_assign = new AssignExpr(var_, unitDataCall, 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) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) UnitData(org.drools.model.UnitData) 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