use of org.drools.javaparser.ast.expr.NameExpr in project drools by kiegroup.
the class ToTypedExpressionTest method pointFreeTest.
@Test
public void pointFreeTest() {
final PointFreeExpr expression = new PointFreeExpr(null, new NameExpr("name"), NodeList.nodeList(new StringLiteralExpr("[A-Z]")), new SimpleName("matches"), false, null, null, null, null);
TypedExpressionResult typedExpressionResult = new ExpressionTyper(ruleContext, Person.class, null, true).toTypedExpression(expression);
final TypedExpression actual = typedExpressionResult.getTypedExpression().get();
final TypedExpression expected = typedResult("eval(org.drools.model.operators.MatchesOperator.INSTANCE, _this.getName(), \"[A-Z]\")", String.class);
assertEquals(expected, actual);
}
use of org.drools.javaparser.ast.expr.NameExpr in project drools by kiegroup.
the class PackageModel method addRulesList.
private void addRulesList(BlockStmt rulesListInitializerBody, String listName) {
MethodCallExpr add = new MethodCallExpr(new NameExpr("rules"), "addAll");
add.addArgument(listName);
rulesListInitializerBody.addStatement(add);
}
use of org.drools.javaparser.ast.expr.NameExpr in project drools by kiegroup.
the class Consequence method rewriteConsequenceBlock.
private String rewriteConsequenceBlock(String consequence) {
int modifyPos = StringUtils.indexOfOutOfQuotes(consequence, "modify");
if (modifyPos < 0) {
return consequence;
}
int lastCopiedEnd = 0;
StringBuilder sb = new StringBuilder();
sb.append(consequence.substring(lastCopiedEnd, modifyPos));
lastCopiedEnd = modifyPos + 1;
for (; modifyPos >= 0; modifyPos = StringUtils.indexOfOutOfQuotes(consequence, "modify", modifyPos + 6)) {
int declStart = consequence.indexOf('(', modifyPos + 6);
int declEnd = consequence.indexOf(')', declStart + 1);
if (declEnd < 0) {
continue;
}
String decl = consequence.substring(declStart + 1, declEnd).trim();
if (!context.getDeclarationById(decl).isPresent()) {
continue;
}
int blockStart = consequence.indexOf('{', declEnd + 1);
int blockEnd = consequence.indexOf('}', blockStart + 1);
if (blockEnd < 0) {
continue;
}
if (lastCopiedEnd < modifyPos) {
sb.append(consequence.substring(lastCopiedEnd, modifyPos));
}
NameExpr declAsNameExpr = new NameExpr(decl);
String originalBlock = consequence.substring(blockStart + 1, blockEnd).trim();
BlockStmt modifyBlock = JavaParser.parseBlock("{" + originalBlock + ";}");
List<MethodCallExpr> originalMethodCalls = modifyBlock.findAll(MethodCallExpr.class);
for (MethodCallExpr mc : originalMethodCalls) {
Expression mcWithScope = org.drools.modelcompiler.builder.generator.DrlxParseUtil.prepend(declAsNameExpr, mc);
modifyBlock.replace(mc, mcWithScope);
}
for (Statement n : modifyBlock.getStatements()) {
if (!(n instanceof EmptyStmt)) {
sb.append(n);
}
}
sb.append("update(").append(decl).append(");\n");
lastCopiedEnd = blockEnd + 1;
}
if (lastCopiedEnd < consequence.length()) {
sb.append(consequence.substring(lastCopiedEnd));
}
return sb.toString();
}
use of org.drools.javaparser.ast.expr.NameExpr in project drools by kiegroup.
the class DrlxParseUtil method toMethodCallWithClassCheck.
public static TypedExpression toMethodCallWithClassCheck(RuleContext context, Expression expr, String bindingId, Class<?> clazz, TypeResolver typeResolver) {
final Deque<ParsedMethod> callStackLeftToRight = new LinkedList<>();
createExpressionCall(expr, callStackLeftToRight);
Class<?> previousClass = clazz;
Expression previousScope = null;
for (ParsedMethod e : callStackLeftToRight) {
if (e.expression instanceof NameExpr || e.expression instanceof FieldAccessExpr) {
if (e.fieldToResolve.equals(bindingId)) {
continue;
}
if (previousClass == null) {
try {
previousClass = typeResolver.resolveType(e.fieldToResolve);
previousScope = new NameExpr(e.fieldToResolve);
} catch (ClassNotFoundException e1) {
// ignore
}
if (previousClass == null) {
previousClass = context.getDeclarationById(e.fieldToResolve).map(DeclarationSpec::getDeclarationClass).orElseThrow(() -> new RuntimeException("Unknown field: " + e.fieldToResolve));
previousScope = e.expression;
}
} else {
TypedExpression te = nameExprToMethodCallExpr(e.fieldToResolve, previousClass, previousScope);
Class<?> returnType = te.getType();
previousScope = te.getExpression();
previousClass = returnType;
}
} else if (e.expression instanceof MethodCallExpr) {
Class<?> returnType = returnTypeOfMethodCallExpr(context, typeResolver, (MethodCallExpr) e.expression, previousClass, null);
MethodCallExpr cloned = ((MethodCallExpr) e.expression.clone()).setScope(previousScope);
previousScope = cloned;
previousClass = returnType;
}
}
return new TypedExpression(previousScope, previousClass);
}
use of org.drools.javaparser.ast.expr.NameExpr in project drools by kiegroup.
the class FlowExpressionBuilder method buildIndexedBy.
private MethodCallExpr buildIndexedBy(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
if (!hasIndex(drlxParseResult)) {
return exprDSL;
}
TypedExpression left = drlxParseResult.getLeft();
TypedExpression right = drlxParseResult.getRight();
Class<?> indexType = Stream.of(left, right).map(TypedExpression::getType).filter(Objects::nonNull).findFirst().get();
ClassExpr indexedBy_indexedClass = new ClassExpr(JavaParser.parseType(indexType.getCanonicalName()));
// not 100% accurate as the type in "nameExpr" is actually parsed if it was JavaParsers as a big chain of FieldAccessExpr
FieldAccessExpr indexedBy_constraintType = new FieldAccessExpr(new NameExpr("org.drools.model.Index.ConstraintType"), drlxParseResult.getDecodeConstraintType().toString());
LambdaExpr indexedBy_leftOperandExtractor = new LambdaExpr();
indexedBy_leftOperandExtractor.addParameter(new Parameter(new UnknownType(), "_this"));
boolean leftContainsThis = left.getExpression().toString().contains("_this");
indexedBy_leftOperandExtractor.setBody(new ExpressionStmt(leftContainsThis ? left.getExpression() : right.getExpression()));
MethodCallExpr indexedByDSL = new MethodCallExpr(exprDSL, INDEXED_BY_CALL);
indexedByDSL.addArgument(indexedBy_indexedClass);
indexedByDSL.addArgument(indexedBy_constraintType);
indexedByDSL.addArgument("" + indexIdGenerator.getFieldId(drlxParseResult.getPatternType(), left.getFieldName()));
indexedByDSL.addArgument(indexedBy_leftOperandExtractor);
Collection<String> usedDeclarations = drlxParseResult.getUsedDeclarations();
if (isAlphaIndex(usedDeclarations)) {
indexedByDSL.addArgument(right.getExpression());
} else {
LambdaExpr indexedBy_rightOperandExtractor = new LambdaExpr();
indexedBy_rightOperandExtractor.addParameter(new Parameter(new UnknownType(), usedDeclarations.iterator().next()));
indexedBy_rightOperandExtractor.setBody(new ExpressionStmt(!leftContainsThis ? left.getExpression() : right.getExpression()));
indexedByDSL.addArgument(indexedBy_rightOperandExtractor);
}
return indexedByDSL;
}
Aggregations