Search in sources :

Example 6 with StringLiteralExpr

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

the class ConstraintParser method getEqualityExpression.

private static Expression getEqualityExpression(TypedExpression left, TypedExpression right, BinaryExpr.Operator operator) {
    if (isAnyOperandBigDecimal(left, right)) {
        return compareBigDecimal(operator, left, right);
    }
    if (isPrimitiveExpression(right.getExpression())) {
        if (left.getType() != String.class) {
            return new BinaryExpr(left.getExpression(), right.getExpression(), operator == BinaryExpr.Operator.EQUALS ? BinaryExpr.Operator.EQUALS : BinaryExpr.Operator.NOT_EQUALS);
        } else if (right.getExpression() instanceof LiteralExpr) {
            right.setExpression(new StringLiteralExpr(right.getExpression().toString()));
        }
    }
    if (left.getType() == String.class && right.getType() == Object.class) {
        right.setExpression(new MethodCallExpr(right.getExpression(), "toString"));
    }
    MethodCallExpr methodCallExpr = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals");
    methodCallExpr.addArgument(left.getExpression());
    // don't create NodeList with static method because missing "parent for child" would null and NPE
    methodCallExpr.addArgument(right.getExpression());
    return operator == BinaryExpr.Operator.EQUALS ? methodCallExpr : new UnaryExpr(methodCallExpr, UnaryExpr.Operator.LOGICAL_COMPLEMENT);
}
Also used : BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) LiteralExpr(org.drools.javaparser.ast.expr.LiteralExpr) IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) UnaryExpr(org.drools.javaparser.ast.expr.UnaryExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 7 with StringLiteralExpr

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

the class FlowExpressionBuilder method buildExpressionWithIndexing.

public Expression buildExpressionWithIndexing(DrlxParseSuccess drlxParseResult) {
    String exprId = drlxParseResult.getExprId();
    MethodCallExpr exprDSL = new MethodCallExpr(null, EXPR_CALL);
    if (exprId != null && !"".equals(exprId)) {
        exprDSL.addArgument(new StringLiteralExpr(exprId));
    }
    exprDSL = buildExpression(drlxParseResult, exprDSL);
    exprDSL = buildIndexedBy(drlxParseResult, exprDSL);
    exprDSL = buildReactOn(drlxParseResult, exprDSL);
    return exprDSL;
}
Also used : StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 8 with StringLiteralExpr

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

the class PatternExpressionBuilder method buildExpressionWithIndexing.

@Override
public Expression buildExpressionWithIndexing(DrlxParseSuccess drlxParseResult) {
    String exprId = drlxParseResult.getExprId();
    MethodCallExpr exprDSL = new MethodCallExpr(null, EXPR_CALL);
    if (exprId != null && !"".equals(exprId)) {
        exprDSL.addArgument(new StringLiteralExpr(exprId));
    }
    exprDSL = buildExpression(drlxParseResult, exprDSL);
    MethodCallExpr finalExprDSL = exprDSL;
    Optional<MethodCallExpr> indexedByExpr = buildIndexedBy(drlxParseResult);
    indexedByExpr.ifPresent(finalExprDSL::addArgument);
    final Optional<MethodCallExpr> reactOnDSL = buildReactOn(drlxParseResult);
    reactOnDSL.ifPresent(finalExprDSL::addArgument);
    return exprDSL;
}
Also used : StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 9 with StringLiteralExpr

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

the class PackageModel method addGlobalField.

private static void addGlobalField(ClassOrInterfaceDeclaration classDeclaration, String packageName, String globalName, Class<?> globalClass) {
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(Global.class.getCanonicalName());
    varType.setTypeArguments(DrlxParseUtil.classToReferenceType(globalClass));
    Type declType = DrlxParseUtil.classToReferenceType(globalClass);
    MethodCallExpr declarationOfCall = new MethodCallExpr(null, "globalOf");
    declarationOfCall.addArgument(new ClassExpr(declType));
    declarationOfCall.addArgument(new StringLiteralExpr(packageName));
    declarationOfCall.addArgument(new StringLiteralExpr(globalName));
    FieldDeclaration field = classDeclaration.addField(varType, toVar(globalName), Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL);
    field.getVariables().get(0).setInitializer(declarationOfCall);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Global(org.drools.model.Global) FieldDeclaration(org.drools.javaparser.ast.body.FieldDeclaration) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 10 with StringLiteralExpr

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

the class Consequence method executeScriptCall.

private MethodCallExpr executeScriptCall(RuleDescr ruleDescr, MethodCallExpr onCall) {
    MethodCallExpr executeCall = new MethodCallExpr(onCall, EXECUTESCRIPT_CALL);
    executeCall.addArgument(new StringLiteralExpr("mvel"));
    ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
    objectCreationExpr.setType(StringBuilder.class.getCanonicalName());
    Expression mvelSB = objectCreationExpr;
    for (String i : packageModel.getImports()) {
        if (i.equals(packageModel.getName() + ".*")) {
            // skip same-package star import.
            continue;
        }
        MethodCallExpr appendCall = new MethodCallExpr(mvelSB, "append");
        StringLiteralExpr importAsStringLiteral = new StringLiteralExpr();
        // use the setter method in order for the string literal be properly escaped.
        importAsStringLiteral.setString("import " + i + ";\n");
        appendCall.addArgument(importAsStringLiteral);
        mvelSB = appendCall;
    }
    StringLiteralExpr mvelScriptBodyStringLiteral = new StringLiteralExpr();
    // use the setter method in order for the string literal be properly escaped.
    mvelScriptBodyStringLiteral.setString(ruleDescr.getConsequence().toString());
    MethodCallExpr appendCall = new MethodCallExpr(mvelSB, "append");
    appendCall.addArgument(mvelScriptBodyStringLiteral);
    executeCall.addArgument(new MethodCallExpr(appendCall, "toString"));
    return executeCall;
}
Also used : ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) 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