Search in sources :

Example 26 with Expression

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

the class DrlxParseUtil method generateLambdaWithoutParameters.

public static Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations, Expression expr, boolean skipFirstParamAsThis) {
    if (skipFirstParamAsThis && usedDeclarations.isEmpty()) {
        return expr;
    }
    LambdaExpr lambdaExpr = new LambdaExpr();
    lambdaExpr.setEnclosingParameters(true);
    if (!skipFirstParamAsThis) {
        lambdaExpr.addParameter(new Parameter(new UnknownType(), "_this"));
    }
    usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
    lambdaExpr.setBody(new ExpressionStmt(expr));
    return lambdaExpr;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) Expression(org.drools.javaparser.ast.expr.Expression) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) Optional.of(java.util.Optional.of) JavaParser(org.drools.javaparser.JavaParser) Operator(org.drools.javaparser.ast.expr.BinaryExpr.Operator) ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ConstraintType(org.drools.core.util.index.IndexUtil.ConstraintType) ClassUtil(org.drools.modelcompiler.util.ClassUtil) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) PrimitiveType(org.drools.javaparser.ast.type.PrimitiveType) Method(java.lang.reflect.Method) LongLiteralExpr(org.drools.javaparser.ast.expr.LongLiteralExpr) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) Parameter(org.drools.javaparser.ast.body.Parameter) TypeResolver(org.kie.soup.project.datamodel.commons.types.TypeResolver) Collection(java.util.Collection) Collectors(java.util.stream.Collectors) NameExpr(org.drools.javaparser.ast.expr.NameExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) IndexUtil(org.drools.core.util.index.IndexUtil) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) List(java.util.List) Stream(java.util.stream.Stream) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) NodeWithSimpleName(org.drools.javaparser.ast.nodeTypes.NodeWithSimpleName) ClassUtil.findMethod(org.drools.modelcompiler.util.ClassUtil.findMethod) StringUtils(org.drools.core.util.StringUtils) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) ArrayAccessExpr(org.drools.javaparser.ast.expr.ArrayAccessExpr) NodeWithTraversableScope(org.drools.javaparser.ast.nodeTypes.NodeWithTraversableScope) CastExpr(org.drools.javaparser.ast.expr.CastExpr) CharLiteralExpr(org.drools.javaparser.ast.expr.CharLiteralExpr) Deque(java.util.Deque) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) DrlxParser(org.drools.drlx.DrlxParser) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) BooleanLiteralExpr(org.drools.javaparser.ast.expr.BooleanLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) LiteralStringValueExpr(org.drools.javaparser.ast.expr.LiteralStringValueExpr) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) LinkedList(java.util.LinkedList) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) ClassUtils(org.drools.core.util.ClassUtils) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Field(java.lang.reflect.Field) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Type(org.drools.javaparser.ast.type.Type) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) DoubleLiteralExpr(org.drools.javaparser.ast.expr.DoubleLiteralExpr) NodeWithOptionalScope(org.drools.javaparser.ast.nodeTypes.NodeWithOptionalScope) Collections(java.util.Collections) Node(org.drools.javaparser.ast.Node) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt)

Example 27 with Expression

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

the class DrlxParseUtil method createExpressionCall.

private static Expression createExpressionCall(Expression expr, Deque<ParsedMethod> expressions) {
    if (expr instanceof NodeWithSimpleName) {
        NodeWithSimpleName fae = (NodeWithSimpleName) expr;
        expressions.push(new ParsedMethod(expr, fae.getName().asString()));
    }
    if (expr instanceof NodeWithOptionalScope) {
        final NodeWithOptionalScope<?> exprWithScope = (NodeWithOptionalScope) expr;
        exprWithScope.getScope().map((Expression scope) -> createExpressionCall(scope, expressions));
    } else if (expr instanceof FieldAccessExpr) {
        // Cannot recurse over getScope() as FieldAccessExpr doesn't support the NodeWithOptionalScope,
        // it will support a new interface to traverse among scopes called NodeWithTraversableScope so
        // we can merge this and the previous branch
        createExpressionCall(((FieldAccessExpr) expr).getScope(), expressions);
    }
    return expr;
}
Also used : NodeWithSimpleName(org.drools.javaparser.ast.nodeTypes.NodeWithSimpleName) Expression(org.drools.javaparser.ast.expr.Expression) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) NodeWithOptionalScope(org.drools.javaparser.ast.nodeTypes.NodeWithOptionalScope) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr)

Example 28 with Expression

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

the class DrlxParseUtil method trasformHalfBinaryToBinary.

public static Expression trasformHalfBinaryToBinary(Expression drlxExpr) {
    final Optional<Node> parent = drlxExpr.getParentNode();
    if (drlxExpr instanceof HalfBinaryExpr && parent.isPresent()) {
        HalfBinaryExpr halfBinaryExpr = (HalfBinaryExpr) drlxExpr;
        Expression parentLeft = findLeftLeafOfNameExpr(parent.get());
        Operator operator = toBinaryExprOperator(halfBinaryExpr.getOperator());
        return new BinaryExpr(parentLeft, halfBinaryExpr.getRight(), operator);
    }
    return drlxExpr;
}
Also used : Operator(org.drools.javaparser.ast.expr.BinaryExpr.Operator) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) Node(org.drools.javaparser.ast.Node) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr)

Example 29 with Expression

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

the class DrlxParseUtil method removeRootNode.

public static RemoveRootNodeResult removeRootNode(Expression expr) {
    Optional<Expression> rootNode = findRootNodeViaScope(expr);
    if (rootNode.isPresent()) {
        Expression root = rootNode.get();
        Optional<Node> parent = root.getParentNode();
        parent.ifPresent(p -> p.remove(root));
        return new RemoveRootNodeResult(rootNode, (Expression) parent.orElse(expr));
    }
    return new RemoveRootNodeResult(rootNode, expr);
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) Node(org.drools.javaparser.ast.Node)

Example 30 with Expression

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

the class ModelGenerator method ruleAttributes.

/**
 * Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#attribute(org.drools.model.Rule.Attribute, Object)}
 * starting from a drools-compiler {@link RuleDescr}.
 * The tuple represent the Rule Attribute expressed in JavParser form, and the attribute value expressed in JavaParser form.
 */
private static List<MethodCallExpr> ruleAttributes(RuleContext context, RuleDescr ruleDescr) {
    List<MethodCallExpr> ruleAttributes = new ArrayList<>();
    for (Entry<String, AttributeDescr> as : ruleDescr.getAttributes().entrySet()) {
        // dialect=mvel is not an attribute of DSL expr(), so we check it before.
        if (as.getKey().equals("dialect")) {
            if (as.getValue().getValue().equals("mvel")) {
                context.setRuleDialect(RuleDialect.MVEL);
            }
            continue;
        }
        MethodCallExpr attributeCall = new MethodCallExpr(null, ATTRIBUTE_CALL);
        attributeCall.addArgument(attributesMap.get(as.getKey()));
        switch(as.getKey()) {
            case "dialect":
                throw new RuntimeException("should not have reached this part of the code");
            case "no-loop":
            case "salience":
            case "enabled":
            case "auto-focus":
            case "lock-on-active":
                attributeCall.addArgument(parseExpression(as.getValue().getValue()));
                break;
            case "agenda-group":
            case "activation-group":
            case "ruleflow-group":
            case "duration":
            case "timer":
                attributeCall.addArgument(new StringLiteralExpr(as.getValue().getValue()));
                break;
            case "calendars":
                String value = as.getValue().getValue().trim();
                if (value.startsWith("[")) {
                    value = value.substring(1, value.length() - 1).trim();
                }
                Expression arrayExpr = parseExpression("new String[] { " + value + " }");
                attributeCall.addArgument(arrayExpr);
                break;
            case "date-effective":
            case "date-expires":
                attributeCall.addArgument(parseExpression(String.format("GregorianCalendar.from(LocalDate.parse(\"%s\", dateTimeFormatter).atStartOfDay(ZoneId.systemDefault()))", as.getValue().getValue())));
                break;
            default:
                throw new UnsupportedOperationException("Unhandled case for rule attribute: " + as.getKey());
        }
        ruleAttributes.add(attributeCall);
    }
    return ruleAttributes;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) ArrayList(java.util.ArrayList) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

Expression (org.drools.javaparser.ast.expr.Expression)48 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)32 NameExpr (org.drools.javaparser.ast.expr.NameExpr)25 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)19 List (java.util.List)14 ArrayList (java.util.ArrayList)13 Optional (java.util.Optional)13 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)13 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)11 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)11 PackageModel (org.drools.modelcompiler.builder.PackageModel)11 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)11 Node (org.drools.javaparser.ast.Node)10 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)10 Collection (java.util.Collection)9 Parameter (org.drools.javaparser.ast.body.Parameter)9 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)9 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)9 DrlxParseUtil.toVar (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toVar)8 JavaParser (org.drools.javaparser.JavaParser)7