use of org.drools.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class PackageModel method buildRulesField.
private MethodCallExpr buildRulesField(ClassOrInterfaceDeclaration rulesClass) {
MethodCallExpr rulesInit = new MethodCallExpr(null, "Arrays.asList");
ClassOrInterfaceType rulesType = new ClassOrInterfaceType(null, new SimpleName("List"), new NodeList<Type>(new ClassOrInterfaceType(null, "Rule")));
VariableDeclarator rulesVar = new VariableDeclarator(rulesType, "rulesList", rulesInit);
rulesClass.addMember(new FieldDeclaration(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), rulesVar));
return rulesInit;
}
use of org.drools.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class ExpressionTyper method thisExpr.
private TypedExpressionCursor thisExpr(Expression drlxExpr, List<Node> childNodes, boolean isInLineCast, Class<?> originalTypeCursor) {
TypedExpressionCursor teCursor;
if (childNodes.size() > 1 && !isInLineCast) {
SimpleName fieldName = null;
if (childNodes.get(1) instanceof NameExpr) {
fieldName = ((NameExpr) childNodes.get(1)).getName();
} else if (childNodes.get(1) instanceof SimpleName) {
fieldName = (SimpleName) childNodes.get(1);
}
if (fieldName != null) {
context.addReactOnProperties(getFieldName(drlxExpr, fieldName));
}
}
teCursor = new TypedExpressionCursor(new NameExpr("_this"), originalTypeCursor);
return teCursor;
}
use of org.drools.javaparser.ast.expr.SimpleName 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.SimpleName in project drools by kiegroup.
the class Consequence method rewriteRHS.
private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) {
AtomicBoolean requireDrools = new AtomicBoolean(false);
List<MethodCallExpr> methodCallExprs = rhs.findAll(MethodCallExpr.class);
List<MethodCallExpr> updateExprs = new ArrayList<>();
for (MethodCallExpr methodCallExpr : methodCallExprs) {
if (isDroolsMethod(methodCallExpr)) {
if (!methodCallExpr.getScope().isPresent()) {
methodCallExpr.setScope(new NameExpr("drools"));
}
if (knowledgeHelperMethods.contains(methodCallExpr.getNameAsString())) {
methodCallExpr.setScope(asKnoledgeHelperExpression);
} else if (methodCallExpr.getNameAsString().equals("update")) {
updateExprs.add(methodCallExpr);
} else if (methodCallExpr.getNameAsString().equals("retract")) {
methodCallExpr.setName(new SimpleName("delete"));
}
requireDrools.set(true);
}
}
for (MethodCallExpr updateExpr : updateExprs) {
Expression argExpr = updateExpr.getArgument(0);
if (argExpr instanceof NameExpr) {
String updatedVar = ((NameExpr) argExpr).getNameAsString();
Class<?> updatedClass = context.getDeclarationById(updatedVar).map(DeclarationSpec::getDeclarationClass).orElseThrow(RuntimeException::new);
MethodCallExpr bitMaskCreation = new MethodCallExpr(new NameExpr(BitMask.class.getCanonicalName()), "getPatternMask");
bitMaskCreation.addArgument(new ClassExpr(JavaParser.parseClassOrInterfaceType(updatedClass.getCanonicalName())));
methodCallExprs.subList(0, methodCallExprs.indexOf(updateExpr)).stream().filter(mce -> mce.getScope().isPresent() && hasScope(mce, updatedVar)).map(mce -> ClassUtils.setter2property(mce.getNameAsString())).filter(Objects::nonNull).distinct().forEach(s -> bitMaskCreation.addArgument(new StringLiteralExpr(s)));
VariableDeclarationExpr bitMaskVar = new VariableDeclarationExpr(BITMASK_TYPE, "mask_" + updatedVar, Modifier.FINAL);
AssignExpr bitMaskAssign = new AssignExpr(bitMaskVar, bitMaskCreation, AssignExpr.Operator.ASSIGN);
ruleBlock.addStatement(bitMaskAssign);
updateExpr.addArgument("mask_" + updatedVar);
}
}
return requireDrools.get();
}
use of org.drools.javaparser.ast.expr.SimpleName in project drools by kiegroup.
the class ExpressionTyper method toTypedExpressionFromMethodCallOrField.
private TypedExpressionResult toTypedExpressionFromMethodCallOrField(Expression drlxExpr) {
final List<Node> childrenNodes = flattenScope(drlxExpr);
final Node firstChild = childrenNodes.get(0);
boolean isInLineCast = firstChild instanceof InlineCastExpr;
final Class<?> originalTypeCursor;
final Node firstNode;
if (isInLineCast) {
InlineCastExpr inlineCast = (InlineCastExpr) firstChild;
originalTypeCursor = originalTypeCursorFromInlineCast(inlineCast);
firstNode = inlineCast.getExpression();
} else {
originalTypeCursor = patternType;
firstNode = firstChild;
}
final Optional<TypedExpressionCursor> teCursor = processFirstNode(drlxExpr, childrenNodes, firstNode, isInLineCast, originalTypeCursor);
Expression previous;
Class<?> typeCursor;
if (!teCursor.isPresent()) {
return new TypedExpressionResult(Optional.empty(), context);
} else {
previous = teCursor.get().expressionCursor;
typeCursor = teCursor.get().typeCursor;
}
List<Node> childrenWithoutFirst = childrenNodes.subList(1, childrenNodes.size());
for (Node part : childrenWithoutFirst) {
if (typeCursor.isEnum()) {
previous = drlxExpr;
} else if (part instanceof SimpleName) {
String field = part.toString();
TypedExpression expression = nameExprToMethodCallExpr(field, typeCursor, previous);
typeCursor = expression.getType();
previous = expression.getExpression();
} else if (part instanceof MethodCallExpr) {
TypedExpressionCursor typedExpr = methodCallExpr((MethodCallExpr) part, typeCursor, previous);
typeCursor = typedExpr.typeCursor;
previous = typedExpr.expressionCursor;
} else if (part instanceof InlineCastExpr && ((InlineCastExpr) part).getExpression() instanceof FieldAccessExpr) {
InlineCastExpr inlineCastExprPart = (InlineCastExpr) part;
final FieldAccessExpr fieldAccessExpr = (FieldAccessExpr) inlineCastExprPart.getExpression();
final TypedExpression toMethodCallExpr = nameExprToMethodCallExpr(fieldAccessExpr.getNameAsString(), typeCursor, previous);
final Class<?> castClass = getClassFromType(ruleContext.getTypeResolver(), inlineCastExprPart.getType());
previous = addCastToExpression(castClass, toMethodCallExpr.getExpression(), false);
} else {
throw new UnsupportedOperationException();
}
}
return new TypedExpressionResult(of(new TypedExpression().setExpression(previous).setType(typeCursor)), context);
}
Aggregations