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;
}
Aggregations