Search in sources :

Example 16 with NameExpr

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

the class FlowExpressionBuilder method buildBinding.

public MethodCallExpr buildBinding(DrlxParseSuccess drlxParseResult) {
    MethodCallExpr bindDSL = new MethodCallExpr(null, BIND_CALL);
    if (drlxParseResult.hasUnificationVariable()) {
        bindDSL.addArgument(new NameExpr(toVar(drlxParseResult.getUnificationVariable())));
    } else {
        bindDSL.addArgument(new NameExpr(toVar(drlxParseResult.getExprBinding())));
    }
    MethodCallExpr bindAsDSL = new MethodCallExpr(bindDSL, BIND_AS_CALL);
    bindAsDSL.addArgument(new NameExpr(toVar(drlxParseResult.getPatternBinding())));
    final Expression constraintExpression = drlxParseResult.getExpr() instanceof EnclosedExpr ? buildConstraintExpression(drlxParseResult, ((EnclosedExpr) drlxParseResult.getExpr()).getInner()) : buildConstraintExpression(drlxParseResult, drlxParseResult.getUsedDeclarationsOnLeft(), DrlxParseUtil.findLeftLeafOfMethodCall(drlxParseResult.getLeft().getExpression()));
    bindAsDSL.addArgument(constraintExpression);
    return buildReactOn(drlxParseResult, bindAsDSL);
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) NameExpr(org.drools.javaparser.ast.expr.NameExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 17 with NameExpr

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

the class PatternExpressionBuilder method buildBinding.

@Override
public MethodCallExpr buildBinding(DrlxParseSuccess drlxParseResult) {
    MethodCallExpr bindDSL = new MethodCallExpr(null, BIND_CALL);
    if (drlxParseResult.hasUnificationVariable()) {
        bindDSL.addArgument(new NameExpr(toVar(drlxParseResult.getUnificationVariable())));
    } else {
        bindDSL.addArgument(new NameExpr(toVar(drlxParseResult.getExprBinding())));
    }
    final Expression constraintExpression = drlxParseResult.getExpr() instanceof EnclosedExpr ? buildConstraintExpression(drlxParseResult, ((EnclosedExpr) drlxParseResult.getExpr()).getInner()) : buildConstraintExpression(drlxParseResult, drlxParseResult.getUsedDeclarationsOnLeft(), DrlxParseUtil.findLeftLeafOfMethodCall(drlxParseResult.getLeft().getExpression()));
    bindDSL.addArgument(constraintExpression);
    final Optional<MethodCallExpr> methodCallExpr = buildReactOn(drlxParseResult);
    methodCallExpr.ifPresent(bindDSL::addArgument);
    return bindDSL;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) NameExpr(org.drools.javaparser.ast.expr.NameExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 18 with NameExpr

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

the class PatternExpressionBuilder method buildIndexedBy.

private Optional<MethodCallExpr> buildIndexedBy(DrlxParseSuccess drlxParseResult) {
    if (!hasIndex(drlxParseResult)) {
        return Optional.empty();
    }
    IndexUtil.ConstraintType decodeConstraintType = drlxParseResult.getDecodeConstraintType();
    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"), decodeConstraintType.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(null, drlxParseResult.isBetaNode() ? BETA_INDEXED_BY_CALL : ALPHA_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 if (usedDeclarations.size() == 1) {
        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 Optional.of(indexedByDSL);
}
Also used : IndexUtil(org.drools.core.util.index.IndexUtil) 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)

Example 19 with NameExpr

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

the class ExpressionTyper method processFirstNode.

private Optional<TypedExpressionCursor> processFirstNode(Expression drlxExpr, List<Node> childNodes, Node firstNode, boolean isInLineCast, Class<?> originalTypeCursor) {
    final Optional<TypedExpressionCursor> result;
    if (firstNode instanceof ThisExpr || (firstNode instanceof NameExpr && firstNode.toString().equals(bindingId))) {
        result = of(thisExpr(drlxExpr, childNodes, isInLineCast, originalTypeCursor));
    } else if (firstNode instanceof NameExpr) {
        result = nameExpr(drlxExpr, (NameExpr) firstNode, isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof FieldAccessExpr && ((FieldAccessExpr) firstNode).getScope() instanceof ThisExpr) {
        result = of(fieldAccessExpr((FieldAccessExpr) firstNode, originalTypeCursor));
    } else if (firstNode instanceof MethodCallExpr) {
        result = of(methodCallExpr((MethodCallExpr) firstNode, originalTypeCursor, new NameExpr("_this")));
    } else if (firstNode instanceof StringLiteralExpr) {
        result = of(stringLiteralExpr((StringLiteralExpr) firstNode));
    } else if (firstNode instanceof EnclosedExpr) {
        result = processFirstNode(drlxExpr, childNodes, ((EnclosedExpr) firstNode).getInner(), isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof CastExpr) {
        result = castExpr((CastExpr) firstNode, drlxExpr, childNodes, isInLineCast, originalTypeCursor);
    } else if (firstNode instanceof ArrayCreationExpr) {
        result = of(arrayCreationExpr(((ArrayCreationExpr) firstNode)));
    } else if (firstNode instanceof BinaryExpr) {
        result = of(binaryExpr((BinaryExpr) firstNode));
    } else {
        result = of(new TypedExpressionCursor((Expression) firstNode, getExpressionType(ruleContext, ruleContext.getTypeResolver(), (Expression) firstNode, context.getUsedDeclarations())));
    }
    result.ifPresent(te -> {
        if (drlxExpr instanceof NullSafeFieldAccessExpr) {
            extractPrefixExpressions((NullSafeFieldAccessExpr) drlxExpr, te.expressionCursor);
        }
    });
    return result.map(te -> {
        if (isInLineCast) {
            Expression exprWithInlineCast = addCastToExpression(te.typeCursor, te.expressionCursor, isInLineCast);
            return new TypedExpressionCursor(exprWithInlineCast, te.typeCursor);
        } else {
            return te;
        }
    });
}
Also used : BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) CastExpr(org.drools.javaparser.ast.expr.CastExpr) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) EnclosedExpr(org.drools.javaparser.ast.expr.EnclosedExpr) ArrayCreationExpr(org.drools.javaparser.ast.expr.ArrayCreationExpr) ThisExpr(org.drools.javaparser.ast.expr.ThisExpr) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 20 with NameExpr

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

the class ExpressionTyper method fieldAccessExpr.

private TypedExpressionCursor fieldAccessExpr(FieldAccessExpr firstNode, Class<?> originalTypeCursor) {
    TypedExpressionCursor teCursor;
    final Class<?> tc4 = originalTypeCursor;
    String firstName = firstNode.getName().getIdentifier();
    Method firstAccessor = ClassUtils.getAccessor(tc4, firstName);
    if (firstAccessor != null) {
        context.addReactOnProperties(firstName);
        teCursor = new TypedExpressionCursor(new MethodCallExpr(new NameExpr("_this"), firstAccessor.getName()), firstAccessor.getReturnType());
    } else {
        throw new UnsupportedOperationException("firstNode I don't know about");
    // TODO would it be fine to assume is a global if it's not in the declarations and not the first accesssor in a chain?
    }
    return teCursor;
}
Also used : NameExpr(org.drools.javaparser.ast.expr.NameExpr) Method(java.lang.reflect.Method) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) 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