Search in sources :

Example 6 with UnknownType

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

Aggregations

Parameter (org.drools.javaparser.ast.body.Parameter)6 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)6 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)6 NameExpr (org.drools.javaparser.ast.expr.NameExpr)6 UnknownType (org.drools.javaparser.ast.type.UnknownType)6 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 List (java.util.List)4 Objects (java.util.Objects)4 JavaParser (org.drools.javaparser.JavaParser)4 Expression (org.drools.javaparser.ast.expr.Expression)4 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)4 ExpressionStmt (org.drools.javaparser.ast.stmt.ExpressionStmt)4 Optional (java.util.Optional)3 IndexUtil (org.drools.core.util.index.IndexUtil)3 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)3 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)3 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)3 Optional.of (java.util.Optional.of)2