Search in sources :

Example 1 with Operator

use of org.drools.model.functions.Operator 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

Expression (org.drools.javaparser.ast.expr.Expression)1 LiteralExpr (org.drools.javaparser.ast.expr.LiteralExpr)1 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)1 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)1 UnaryExpr (org.drools.javaparser.ast.expr.UnaryExpr)1 Operator (org.drools.model.functions.Operator)1 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)1