Search in sources :

Example 11 with NameExpr

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

the class ToTypedExpressionTest method pointFreeTest.

@Test
public void pointFreeTest() {
    final PointFreeExpr expression = new PointFreeExpr(null, new NameExpr("name"), NodeList.nodeList(new StringLiteralExpr("[A-Z]")), new SimpleName("matches"), false, null, null, null, null);
    TypedExpressionResult typedExpressionResult = new ExpressionTyper(ruleContext, Person.class, null, true).toTypedExpression(expression);
    final TypedExpression actual = typedExpressionResult.getTypedExpression().get();
    final TypedExpression expected = typedResult("eval(org.drools.model.operators.MatchesOperator.INSTANCE, _this.getName(), \"[A-Z]\")", String.class);
    assertEquals(expected, actual);
}
Also used : SimpleName(org.drools.javaparser.ast.expr.SimpleName) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 12 with NameExpr

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

the class PackageModel method addRulesList.

private void addRulesList(BlockStmt rulesListInitializerBody, String listName) {
    MethodCallExpr add = new MethodCallExpr(new NameExpr("rules"), "addAll");
    add.addArgument(listName);
    rulesListInitializerBody.addStatement(add);
}
Also used : NameExpr(org.drools.javaparser.ast.expr.NameExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 13 with NameExpr

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

the class Consequence method rewriteConsequenceBlock.

private String rewriteConsequenceBlock(String consequence) {
    int modifyPos = StringUtils.indexOfOutOfQuotes(consequence, "modify");
    if (modifyPos < 0) {
        return consequence;
    }
    int lastCopiedEnd = 0;
    StringBuilder sb = new StringBuilder();
    sb.append(consequence.substring(lastCopiedEnd, modifyPos));
    lastCopiedEnd = modifyPos + 1;
    for (; modifyPos >= 0; modifyPos = StringUtils.indexOfOutOfQuotes(consequence, "modify", modifyPos + 6)) {
        int declStart = consequence.indexOf('(', modifyPos + 6);
        int declEnd = consequence.indexOf(')', declStart + 1);
        if (declEnd < 0) {
            continue;
        }
        String decl = consequence.substring(declStart + 1, declEnd).trim();
        if (!context.getDeclarationById(decl).isPresent()) {
            continue;
        }
        int blockStart = consequence.indexOf('{', declEnd + 1);
        int blockEnd = consequence.indexOf('}', blockStart + 1);
        if (blockEnd < 0) {
            continue;
        }
        if (lastCopiedEnd < modifyPos) {
            sb.append(consequence.substring(lastCopiedEnd, modifyPos));
        }
        NameExpr declAsNameExpr = new NameExpr(decl);
        String originalBlock = consequence.substring(blockStart + 1, blockEnd).trim();
        BlockStmt modifyBlock = JavaParser.parseBlock("{" + originalBlock + ";}");
        List<MethodCallExpr> originalMethodCalls = modifyBlock.findAll(MethodCallExpr.class);
        for (MethodCallExpr mc : originalMethodCalls) {
            Expression mcWithScope = org.drools.modelcompiler.builder.generator.DrlxParseUtil.prepend(declAsNameExpr, mc);
            modifyBlock.replace(mc, mcWithScope);
        }
        for (Statement n : modifyBlock.getStatements()) {
            if (!(n instanceof EmptyStmt)) {
                sb.append(n);
            }
        }
        sb.append("update(").append(decl).append(");\n");
        lastCopiedEnd = blockEnd + 1;
    }
    if (lastCopiedEnd < consequence.length()) {
        sb.append(consequence.substring(lastCopiedEnd));
    }
    return sb.toString();
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Statement(org.drools.javaparser.ast.stmt.Statement) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) NameExpr(org.drools.javaparser.ast.expr.NameExpr) EmptyStmt(org.drools.javaparser.ast.stmt.EmptyStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 14 with NameExpr

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

the class DrlxParseUtil method toMethodCallWithClassCheck.

public static TypedExpression toMethodCallWithClassCheck(RuleContext context, Expression expr, String bindingId, Class<?> clazz, TypeResolver typeResolver) {
    final Deque<ParsedMethod> callStackLeftToRight = new LinkedList<>();
    createExpressionCall(expr, callStackLeftToRight);
    Class<?> previousClass = clazz;
    Expression previousScope = null;
    for (ParsedMethod e : callStackLeftToRight) {
        if (e.expression instanceof NameExpr || e.expression instanceof FieldAccessExpr) {
            if (e.fieldToResolve.equals(bindingId)) {
                continue;
            }
            if (previousClass == null) {
                try {
                    previousClass = typeResolver.resolveType(e.fieldToResolve);
                    previousScope = new NameExpr(e.fieldToResolve);
                } catch (ClassNotFoundException e1) {
                // ignore
                }
                if (previousClass == null) {
                    previousClass = context.getDeclarationById(e.fieldToResolve).map(DeclarationSpec::getDeclarationClass).orElseThrow(() -> new RuntimeException("Unknown field: " + e.fieldToResolve));
                    previousScope = e.expression;
                }
            } else {
                TypedExpression te = nameExprToMethodCallExpr(e.fieldToResolve, previousClass, previousScope);
                Class<?> returnType = te.getType();
                previousScope = te.getExpression();
                previousClass = returnType;
            }
        } else if (e.expression instanceof MethodCallExpr) {
            Class<?> returnType = returnTypeOfMethodCallExpr(context, typeResolver, (MethodCallExpr) e.expression, previousClass, null);
            MethodCallExpr cloned = ((MethodCallExpr) e.expression.clone()).setScope(previousScope);
            previousScope = cloned;
            previousClass = returnType;
        }
    }
    return new TypedExpression(previousScope, previousClass);
}
Also used : ExpressionTyper.findLeftLeafOfNameExpr(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper.findLeftLeafOfNameExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) LinkedList(java.util.LinkedList) Expression(org.drools.javaparser.ast.expr.Expression) DrlxExpression(org.drools.javaparser.ast.drlx.expr.DrlxExpression) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 15 with NameExpr

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

the class FlowExpressionBuilder method buildIndexedBy.

private MethodCallExpr buildIndexedBy(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
    if (!hasIndex(drlxParseResult)) {
        return exprDSL;
    }
    TypedExpression left = drlxParseResult.getLeft();
    TypedExpression right = drlxParseResult.getRight();
    Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
    ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
    // not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
    FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), drlxParseResult.getDecodeConstraintType().toString());
    LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
    indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
    boolean leftContainsThis = left.getExpression().toString().contains("_this");
    indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
    MethodCallExpr indexedByDSL = new MethodCallExpr(exprDSL, INDEXED_BY_CALL);
    indexedByDSL.addArgument(indexedBy_indexedClass);
    indexedByDSL.addArgument(indexedBy_constraintType);
    indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
    indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
    Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
    if (isAlphaIndex(usedDeclarations)) {
        indexedByDSL.addArgument(right.getExpression());
    } else {
        LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
        indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
        indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
        indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
    }
    return indexedByDSL;
}
Also used : LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) ExpressionStmt(org.drools.javaparser.ast.stmt.ExpressionStmt) UnknownType(org.drools.javaparser.ast.type.UnknownType) Objects(java.util.Objects) Parameter(org.drools.javaparser.ast.body.Parameter) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

NameExpr (org.drools.javaparser.ast.expr.NameExpr)24 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)20 Expression (org.drools.javaparser.ast.expr.Expression)14 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)10 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)10 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)8 List (java.util.List)6 Optional (java.util.Optional)6 Parameter (org.drools.javaparser.ast.body.Parameter)6 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)6 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)6 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)6 ArrayList (java.util.ArrayList)5 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)5 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)5 ExpressionStmt (org.drools.javaparser.ast.stmt.ExpressionStmt)5 PackageModel (org.drools.modelcompiler.builder.PackageModel)5 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)5 Collection (java.util.Collection)4 UnknownType (org.drools.javaparser.ast.type.UnknownType)4