use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ExecModelLambdaPostProcessor method convertIndexedByCall.
private void convertIndexedByCall(MethodCallExpr methodCallExpr) {
NodeList<Expression> arguments = methodCallExpr.getArguments();
Expression firstArgument = arguments.get(0);
if (!firstArgument.isClassExpr()) {
logger.warn("argument is not ClassExpr. argument : {}, methodCallExpr : {}", firstArgument, methodCallExpr);
return;
}
Optional<String> exprId = methodCallExpr.findFirst(StringLiteralExpr.class).map(LiteralStringValueExpr::getValue);
Expression leftOperandExtractorLambda = arguments.get(3);
if (leftOperandExtractorLambda.isLambdaExpr()) {
replaceLambda(leftOperandExtractorLambda.asLambdaExpr(), id -> new MaterializedLambdaExtractor(packageName, ruleClassName, getType(firstArgument).clone()), exprId);
}
for (int i = 4; i < arguments.size(); i++) {
Expression expr = arguments.get(i);
if (expr.isLambdaExpr()) {
replaceLambda(expr.asLambdaExpr(), id -> new MaterializedLambdaExtractor(packageName, ruleClassName, toClassOrInterfaceType(Object.class)), exprId);
}
}
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class Query method buildPattern.
@Override
public void buildPattern() {
NameExpr queryCall = new NameExpr(toQueryDef(pattern.getObjectType()));
MethodCallExpr callCall = new MethodCallExpr(queryCall, QUERY_INVOCATION_CALL);
callCall.addArgument("" + !pattern.isQuery());
if (!constraintDescrs.isEmpty()) {
List<QueryParameter> queryParams = packageModel.queryVariables(queryName);
if (queryParams.size() != constraintDescrs.size()) {
context.addCompilationError(new InvalidExpressionErrorResult("Wrong number of argument invoking query '" + queryName + "'"));
return;
}
Expression[] queryArgs = new Expression[queryParams.size()];
for (int i = 0; i < constraintDescrs.size(); i++) {
BaseDescr baseDescr = constraintDescrs.get(i);
String itemText = baseDescr.getText();
boolean isPositional = baseDescr instanceof ExprConstraintDescr && ((ExprConstraintDescr) baseDescr).getType() == ExprConstraintDescr.Type.POSITIONAL;
boolean isBinding = baseDescr instanceof BindingDescr || itemText.contains(":");
if ((!isPositional) && (!isBinding)) {
// error, can't have non binding slots.
context.addCompilationError(new InvalidExpressionErrorResult("Query's must use positional or bindings, not field constraints: " + itemText));
} else if (isPositional && isBinding) {
// error, can't have positional binding slots.
context.addCompilationError(new InvalidExpressionErrorResult("Query's can't use positional bindings: " + itemText));
}
int colonPos = itemText.indexOf(':');
if (colonPos > 0) {
String bindingId = itemText.substring(0, colonPos).trim();
String paramName = itemText.substring(colonPos + 1).trim();
for (int j = 0; j < queryParams.size(); j++) {
if (queryParams.get(j).getName().equals(paramName)) {
addQueryArg(queryParams, queryArgs, bindingId, j);
break;
} else if (queryParams.get(j).getName().equals(bindingId)) {
addQueryArg(queryParams, queryArgs, paramName, j);
break;
}
}
} else {
addQueryArg(queryParams, queryArgs, itemText, i);
}
}
for (Expression queryArg : queryArgs) {
callCall.addArgument(queryArg);
}
}
context.addExpression(callCall);
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class Query method addQueryArg.
private void addQueryArg(List<QueryParameter> queryParams, Expression[] queryArgs, String itemText, int i) {
if (isLiteral(itemText)) {
MethodCallExpr valueOfMethod = createDslTopLevelMethod(VALUE_OF_CALL);
valueOfMethod.addArgument(new NameExpr(itemText));
queryArgs[i] = valueOfMethod;
} else {
Expression expr = parseExpression(itemText);
if (expr.isNameExpr()) {
context.addDeclaration(itemText, queryParams.get(i).getType());
queryArgs[i] = context.getVarExpr(itemText);
} else {
String variableName = context.getExprId(queryParams.get(i).getType(), itemText);
context.addDeclaration(variableName, queryParams.get(i).getType(), createFromExpr(variableName, expr));
queryArgs[i] = context.getVarExpr(variableName);
}
}
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class DrlxParseUtilTest method prependTestWithCast.
@Test
public void prependTestWithCast() {
final Expression expr = StaticJavaParser.parseExpression("((InternationalAddress) getAddress()).getState()");
final NameExpr nameExpr = new NameExpr(THIS_PLACEHOLDER);
final Expression concatenated = DrlxParseUtil.prepend(nameExpr, expr);
assertEquals("((InternationalAddress) _this.getAddress()).getState()", concatenated.toString());
}
use of com.github.javaparser.ast.expr.Expression in project drools by kiegroup.
the class DrlxParseUtilTest method prependTest.
@Test
public void prependTest() {
final Expression expr = StaticJavaParser.parseExpression("getAddressName().startsWith(\"M\")");
final NameExpr nameExpr = new NameExpr(THIS_PLACEHOLDER);
final Expression concatenated = DrlxParseUtil.prepend(nameExpr, expr);
assertEquals(THIS_PLACEHOLDER + ".getAddressName().startsWith(\"M\")", concatenated.toString());
}
Aggregations