use of org.drools.javaparser.ast.expr.Expression in project drools by kiegroup.
the class DrlxParseUtil method generateLambdaWithoutParameters.
public static Expression generateLambdaWithoutParameters(Collection<String> usedDeclarations, Expression expr, boolean skipFirstParamAsThis) {
if (skipFirstParamAsThis && usedDeclarations.isEmpty()) {
return expr;
}
LambdaExpr lambdaExpr = new LambdaExpr();
lambdaExpr.setEnclosingParameters(true);
if (!skipFirstParamAsThis) {
lambdaExpr.addParameter(new Parameter(new UnknownType(), "_this"));
}
usedDeclarations.stream().map(s -> new Parameter(new UnknownType(), s)).forEach(lambdaExpr::addParameter);
lambdaExpr.setBody(new ExpressionStmt(expr));
return lambdaExpr;
}
use of org.drools.javaparser.ast.expr.Expression in project drools by kiegroup.
the class DrlxParseUtil method createExpressionCall.
private static Expression createExpressionCall(Expression expr, Deque<ParsedMethod> expressions) {
if (expr instanceof NodeWithSimpleName) {
NodeWithSimpleName fae = (NodeWithSimpleName) expr;
expressions.push(new ParsedMethod(expr, fae.getName().asString()));
}
if (expr instanceof NodeWithOptionalScope) {
final NodeWithOptionalScope<?> exprWithScope = (NodeWithOptionalScope) expr;
exprWithScope.getScope().map((Expression scope) -> createExpressionCall(scope, expressions));
} else if (expr instanceof FieldAccessExpr) {
// Cannot recurse over getScope() as FieldAccessExpr doesn't support the NodeWithOptionalScope,
// it will support a new interface to traverse among scopes called NodeWithTraversableScope so
// we can merge this and the previous branch
createExpressionCall(((FieldAccessExpr) expr).getScope(), expressions);
}
return expr;
}
use of org.drools.javaparser.ast.expr.Expression in project drools by kiegroup.
the class DrlxParseUtil method trasformHalfBinaryToBinary.
public static Expression trasformHalfBinaryToBinary(Expression drlxExpr) {
final Optional<Node> parent = drlxExpr.getParentNode();
if (drlxExpr instanceof HalfBinaryExpr && parent.isPresent()) {
HalfBinaryExpr halfBinaryExpr = (HalfBinaryExpr) drlxExpr;
Expression parentLeft = findLeftLeafOfNameExpr(parent.get());
Operator operator = toBinaryExprOperator(halfBinaryExpr.getOperator());
return new BinaryExpr(parentLeft, halfBinaryExpr.getRight(), operator);
}
return drlxExpr;
}
use of org.drools.javaparser.ast.expr.Expression in project drools by kiegroup.
the class DrlxParseUtil method removeRootNode.
public static RemoveRootNodeResult removeRootNode(Expression expr) {
Optional<Expression> rootNode = findRootNodeViaScope(expr);
if (rootNode.isPresent()) {
Expression root = rootNode.get();
Optional<Node> parent = root.getParentNode();
parent.ifPresent(p -> p.remove(root));
return new RemoveRootNodeResult(rootNode, (Expression) parent.orElse(expr));
}
return new RemoveRootNodeResult(rootNode, expr);
}
use of org.drools.javaparser.ast.expr.Expression in project drools by kiegroup.
the class ModelGenerator method ruleAttributes.
/**
* Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#attribute(org.drools.model.Rule.Attribute, Object)}
* starting from a drools-compiler {@link RuleDescr}.
* The tuple represent the Rule Attribute expressed in JavParser form, and the attribute value expressed in JavaParser form.
*/
private static List<MethodCallExpr> ruleAttributes(RuleContext context, RuleDescr ruleDescr) {
List<MethodCallExpr> ruleAttributes = new ArrayList<>();
for (Entry<String, AttributeDescr> as : ruleDescr.getAttributes().entrySet()) {
// dialect=mvel is not an attribute of DSL expr(), so we check it before.
if (as.getKey().equals("dialect")) {
if (as.getValue().getValue().equals("mvel")) {
context.setRuleDialect(RuleDialect.MVEL);
}
continue;
}
MethodCallExpr attributeCall = new MethodCallExpr(null, ATTRIBUTE_CALL);
attributeCall.addArgument(attributesMap.get(as.getKey()));
switch(as.getKey()) {
case "dialect":
throw new RuntimeException("should not have reached this part of the code");
case "no-loop":
case "salience":
case "enabled":
case "auto-focus":
case "lock-on-active":
attributeCall.addArgument(parseExpression(as.getValue().getValue()));
break;
case "agenda-group":
case "activation-group":
case "ruleflow-group":
case "duration":
case "timer":
attributeCall.addArgument(new StringLiteralExpr(as.getValue().getValue()));
break;
case "calendars":
String value = as.getValue().getValue().trim();
if (value.startsWith("[")) {
value = value.substring(1, value.length() - 1).trim();
}
Expression arrayExpr = parseExpression("new String[] { " + value + " }");
attributeCall.addArgument(arrayExpr);
break;
case "date-effective":
case "date-expires":
attributeCall.addArgument(parseExpression(String.format("GregorianCalendar.from(LocalDate.parse(\"%s\", dateTimeFormatter).atStartOfDay(ZoneId.systemDefault()))", as.getValue().getValue())));
break;
default:
throw new UnsupportedOperationException("Unhandled case for rule attribute: " + as.getKey());
}
ruleAttributes.add(attributeCall);
}
return ruleAttributes;
}
Aggregations