Search in sources :

Example 16 with StringLiteralExpr

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

the class ModelGenerator method addVariable.

private static void addVariable(KnowledgeBuilderImpl kbuilder, BlockStmt ruleBlock, DeclarationSpec decl) {
    if (decl.getDeclarationClass() == null) {
        kbuilder.addBuilderResult(new UnknownDeclarationError(decl.getBindingId()));
        return;
    }
    Type declType = classToReferenceType(decl.getDeclarationClass());
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(Variable.class.getCanonicalName());
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(decl.getBindingId()), Modifier.FINAL);
    MethodCallExpr declarationOfCall = new MethodCallExpr(null, DECLARATION_OF_CALL);
    declarationOfCall.addArgument(new ClassExpr(decl.getType()));
    declarationOfCall.addArgument(new StringLiteralExpr(decl.getVariableName().orElse(decl.getBindingId())));
    decl.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
    decl.getEntryPoint().ifPresent(ep -> {
        MethodCallExpr entryPointCall = new MethodCallExpr(null, "entryPoint");
        entryPointCall.addArgument(new StringLiteralExpr(ep));
        declarationOfCall.addArgument(entryPointCall);
    });
    for (BehaviorDescr behaviorDescr : decl.getBehaviors()) {
        MethodCallExpr windowCall = new MethodCallExpr(null, "window");
        if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("Window.Type.TIME");
            windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
        }
        if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("Window.Type.LENGTH");
            windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
        }
        declarationOfCall.addArgument(windowCall);
    }
    AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
    ruleBlock.addStatement(var_assign);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) Variable(org.drools.model.Variable) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) UnknownDeclarationError(org.drools.modelcompiler.builder.errors.UnknownDeclarationError) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr)

Example 17 with StringLiteralExpr

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

the class ConstraintParser method handleSpecialComparisonCases.

private static Expression handleSpecialComparisonCases(BinaryExpr.Operator operator, TypedExpression left, TypedExpression right) {
    if (isComparisonOperator(operator)) {
        MethodCallExpr compareMethod = null;
        if (left.getType() == String.class && right.getType() == String.class) {
            compareMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.compareStringsAsNumbers");
        } else if (Comparable.class.isAssignableFrom(left.getType()) && Comparable.class.isAssignableFrom(right.getType())) {
            compareMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.compare");
        }
        if (compareMethod != null) {
            compareMethod.addArgument(left.getExpression());
            compareMethod.addArgument(right.getExpression());
            compareMethod.addArgument(new StringLiteralExpr(operator.asString()));
            return compareMethod;
        }
    }
    if (isAnyOperandBigDecimal(left, right) && (isComparisonOperator(operator))) {
        return compareBigDecimal(operator, left, right);
    }
    return new BinaryExpr(left.getExpression(), right.getExpression(), operator);
}
Also used : BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 18 with StringLiteralExpr

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

the class QueryGenerator method processQueryDef.

public static void processQueryDef(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, QueryDescr queryDescr, boolean isPattern) {
    RuleContext context = new RuleContext(kbuilder, packageModel, queryDescr, typeResolver, isPattern);
    String queryName = queryDescr.getName();
    final String queryDefVariableName = toQueryDef(queryName);
    context.setQueryName(Optional.of(queryDefVariableName));
    parseQueryParameters(context, packageModel, queryDescr);
    ClassOrInterfaceType queryDefType = getQueryType(context.getQueryParameters());
    MethodCallExpr queryCall = new MethodCallExpr(null, QUERY_CALL);
    if (!queryDescr.getNamespace().isEmpty()) {
        queryCall.addArgument(new StringLiteralExpr(queryDescr.getNamespace()));
    }
    queryCall.addArgument(new StringLiteralExpr(queryName));
    for (QueryParameter qp : context.getQueryParameters()) {
        queryCall.addArgument(new ClassExpr(JavaParser.parseType(qp.type.getCanonicalName())));
        queryCall.addArgument(new StringLiteralExpr(qp.name));
    }
    packageModel.getQueryDefWithType().put(queryDefVariableName, new QueryDefWithType(queryDefType, queryCall, context));
}
Also used : StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 19 with StringLiteralExpr

use of org.drools.javaparser.ast.expr.StringLiteralExpr 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 StringLiteralExpr

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

the class CustomOperatorSpec method getExpression.

public Expression getExpression(RuleContext context, PointFreeExpr pointFreeExpr, TypedExpression left) {
    MethodCallExpr methodCallExpr = new MethodCallExpr(null, "eval");
    String opName = pointFreeExpr.getOperator().asString();
    Operator operator = Operator.Register.getOperator(opName);
    try {
        // if the operator has an INSTANCE field avoid the operator lookup at runtime
        operator.getClass().getField("INSTANCE");
        methodCallExpr.addArgument(operator.getClass().getCanonicalName() + ".INSTANCE");
    } catch (NoSuchFieldException e) {
        methodCallExpr.addArgument(new StringLiteralExpr(opName));
    }
    methodCallExpr.addArgument(left.getExpression());
    for (Expression rightExpr : pointFreeExpr.getRight()) {
        if (rightExpr instanceof LiteralExpr) {
            methodCallExpr.addArgument(rightExpr);
        } else {
            TypedExpression typedExpression = DrlxParseUtil.toMethodCallWithClassCheck(context, rightExpr, null, null, context.getTypeResolver());
            methodCallExpr.addArgument(typedExpression.getExpression());
        }
    }
    return pointFreeExpr.isNegated() ? new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT) : methodCallExpr;
}
Also used : Operator(org.drools.model.functions.Operator) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) Expression(org.drools.javaparser.ast.expr.Expression) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)21 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)19 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)6 Expression (org.drools.javaparser.ast.expr.Expression)6 NameExpr (org.drools.javaparser.ast.expr.NameExpr)5 ArrayList (java.util.ArrayList)4 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)4 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)4 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)4 HashMap (java.util.HashMap)3 JavaParser.parseExpression (org.drools.javaparser.JavaParser.parseExpression)3 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)3 List (java.util.List)2 Map (java.util.Map)2 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)2 MethodDeclaration (org.drools.javaparser.ast.body.MethodDeclaration)2 PointFreeExpr (org.drools.javaparser.ast.drlx.expr.PointFreeExpr)2 CastExpr (org.drools.javaparser.ast.expr.CastExpr)2 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)2 IntegerLiteralExpr (org.drools.javaparser.ast.expr.IntegerLiteralExpr)2