Search in sources :

Example 6 with LiteralExpr

use of com.github.javaparser.ast.expr.LiteralExpr in project drools by kiegroup.

the class PatternExpressionBuilder method buildIndexedBy.

private Optional<MethodCallExpr> buildIndexedBy(SingleDrlxParseSuccess drlxParseResult) {
    if (drlxParseResult.isUnification()) {
        TypedExpression left = drlxParseResult.getLeft();
        TypedExpression right = drlxParseResult.getRight();
        LambdaExpr indexedByLeftOperandExtractor = createIndexedByLambda(drlxParseResult, left, right);
        MethodCallExpr indexedByDSL = indexedByDSL(drlxParseResult, left);
        indexedByDSL.addArgument(org.drools.model.Index.ConstraintType.class.getCanonicalName() + ".EQUAL");
        indexedByDSL.addArgument("-1");
        indexedByDSL.addArgument(indexedByLeftOperandExtractor);
        indexedByDSL.addArgument("" + null);
        return Optional.of(indexedByDSL);
    }
    if (!shouldCreateIndex(drlxParseResult)) {
        return Optional.empty();
    }
    TypedExpression left = drlxParseResult.getLeft();
    TypedExpression right = drlxParseResult.getRight();
    Expression rightExpression = right.getExpression();
    if (!drlxParseResult.isBetaConstraint() && !(rightExpression instanceof LiteralExpr || isStringToDateExpression(rightExpression) || isNumberToStringExpression(rightExpression))) {
        return Optional.empty();
    }
    // 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.class.getCanonicalName()), drlxParseResult.getDecodeConstraintType().toString());
    LambdaExpr indexedByLeftOperandExtractor = createIndexedByLambda(drlxParseResult, left, right);
    MethodCallExpr indexedByDSL = indexedByDSL(drlxParseResult, left);
    indexedByDSL.addArgument(indexedBy_constraintType);
    indexedByDSL.addArgument(getIndexIdArgument(drlxParseResult, left));
    indexedByDSL.addArgument(indexedByLeftOperandExtractor);
    Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
    java.lang.reflect.Type leftType = left.getType();
    if (drlxParseResult.isBetaConstraint()) {
        addIndexedByDeclaration(left, right, left.containThis(), indexedByDSL, usedDeclarations);
    } else {
        indexedByDSL.addArgument(narrowExpressionToType(right, leftType));
    }
    return Optional.of(indexedByDSL);
}
Also used : LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) FieldAccessExpr(com.github.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 7 with LiteralExpr

use of com.github.javaparser.ast.expr.LiteralExpr in project drools by kiegroup.

the class ParserUtil method objectCreationExprToValue.

public static Object objectCreationExprToValue(ObjectCreationExpr objectCreationExpr, RuleContext context) {
    // Only a few classes/constructors are handled. Otherwise, value becomes null so a link would be UNKNOWN. To be enhanced : DROOLS-6711
    ClassOrInterfaceType type = objectCreationExpr.getType();
    Class<?> clazz = getClassFromType(context.getTypeResolver(), type);
    if (clazz.equals(BigDecimal.class)) {
        NodeList<Expression> arguments = objectCreationExpr.getArguments();
        Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigDecimal::new);
        return opt.orElse(null);
    } else if (clazz.equals(BigInteger.class)) {
        NodeList<Expression> arguments = objectCreationExpr.getArguments();
        Optional<Object> opt = arguments.stream().findFirst().filter(StringLiteralExpr.class::isInstance).map(literalExpr -> literalExpr.asStringLiteralExpr().asString()).map(BigInteger::new);
        return opt.orElse(null);
    }
    return null;
}
Also used : NodeList(com.github.javaparser.ast.NodeList) SimpleName(com.github.javaparser.ast.expr.SimpleName) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) DrlxParseUtil.getClassFromType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.getClassFromType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BigDecimal(java.math.BigDecimal) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) BigDecimalLiteralExpr(org.drools.mvel.parser.ast.expr.BigDecimalLiteralExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr) BigIntegerLiteralExpr(org.drools.mvel.parser.ast.expr.BigIntegerLiteralExpr) CharLiteralExpr(com.github.javaparser.ast.expr.CharLiteralExpr) DoubleLiteralExpr(com.github.javaparser.ast.expr.DoubleLiteralExpr) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Optional(java.util.Optional) BigInteger(java.math.BigInteger) Expression(com.github.javaparser.ast.expr.Expression) LongLiteralExpr(com.github.javaparser.ast.expr.LongLiteralExpr) IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) Optional(java.util.Optional) Expression(com.github.javaparser.ast.expr.Expression) NodeList(com.github.javaparser.ast.NodeList) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) BigInteger(java.math.BigInteger) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) BigDecimal(java.math.BigDecimal)

Example 8 with LiteralExpr

use of com.github.javaparser.ast.expr.LiteralExpr in project drools by kiegroup.

the class AccumulateVisitor method parseFirstParameter.

private Optional<NewBinding> parseFirstParameter(PatternDescr basePattern, BaseDescr input, AccumulateDescr.AccumulateFunctionCallDescr function, MethodCallExpr functionDSL, String bindingId) {
    final String accumulateFunctionParameterStr = function.getParams()[0];
    final Expression accumulateFunctionParameter = DrlxParseUtil.parseExpression(accumulateFunctionParameterStr).getExpr();
    if (accumulateFunctionParameter instanceof BinaryExpr) {
        return binaryExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameterStr);
    }
    if (parameterNeedsConvertionToMethodCallExpr(accumulateFunctionParameter)) {
        return methodCallExprParameter(basePattern, input, function, functionDSL, bindingId, accumulateFunctionParameter);
    }
    if (accumulateFunctionParameter instanceof DrlNameExpr) {
        nameExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameter);
    } else if (accumulateFunctionParameter instanceof LiteralExpr) {
        literalExprParameter(basePattern, function, functionDSL, bindingId, accumulateFunctionParameter);
    } else {
        context.addCompilationError(new InvalidExpressionErrorResult("Invalid expression " + accumulateFunctionParameterStr, Optional.of(context.getRuleDescr())));
        throw new AccumulateParsingFailedException();
    }
    return Optional.empty();
}
Also used : InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) Expression(com.github.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlNameExpr(org.drools.mvel.parser.ast.expr.DrlNameExpr) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) LiteralExpr(com.github.javaparser.ast.expr.LiteralExpr)

Aggregations

Expression (com.github.javaparser.ast.expr.Expression)8 LiteralExpr (com.github.javaparser.ast.expr.LiteralExpr)8 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)7 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)6 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)5 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)5 DrlNameExpr (org.drools.mvel.parser.ast.expr.DrlNameExpr)5 NodeList (com.github.javaparser.ast.NodeList)4 BinaryExpr (com.github.javaparser.ast.expr.BinaryExpr)4 DoubleLiteralExpr (com.github.javaparser.ast.expr.DoubleLiteralExpr)4 FieldAccessExpr (com.github.javaparser.ast.expr.FieldAccessExpr)4 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)4 CastExpr (com.github.javaparser.ast.expr.CastExpr)3 CharLiteralExpr (com.github.javaparser.ast.expr.CharLiteralExpr)3 SimpleName (com.github.javaparser.ast.expr.SimpleName)3 UnaryExpr (com.github.javaparser.ast.expr.UnaryExpr)3 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3