Search in sources :

Example 1 with SimpleName

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;
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) SimpleName(org.drools.javaparser.ast.expr.SimpleName) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) FieldDeclaration(org.drools.javaparser.ast.body.FieldDeclaration) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) VariableDeclarator(org.drools.javaparser.ast.body.VariableDeclarator)

Example 2 with SimpleName

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;
}
Also used : SimpleName(org.drools.javaparser.ast.expr.SimpleName) NameExpr(org.drools.javaparser.ast.expr.NameExpr)

Example 3 with SimpleName

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);
}
Also used : SimpleName(org.drools.javaparser.ast.expr.SimpleName) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 4 with SimpleName

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();
}
Also used : BitMask(org.drools.model.BitMask) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseUtil.isNameExprWithName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isNameExprWithName) DroolsImpl(org.drools.modelcompiler.consequence.DroolsImpl) JavaParser(org.drools.javaparser.JavaParser) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) BREAKING_CALL(org.drools.modelcompiler.builder.generator.visitor.NamedConsequenceVisitor.BREAKING_CALL) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Statement(org.drools.javaparser.ast.stmt.Statement) Collectors.toSet(java.util.stream.Collectors.toSet) PackageModel(org.drools.modelcompiler.builder.PackageModel) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) DrlxParseUtil.hasScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.hasScope) Parameter(org.drools.javaparser.ast.body.Parameter) ClassUtils(org.drools.core.util.ClassUtils) Collection(java.util.Collection) DrlxParseUtil.parseBlock(org.drools.modelcompiler.builder.generator.DrlxParseUtil.parseBlock) Set(java.util.Set) EmptyStmt(org.drools.javaparser.ast.stmt.EmptyStmt) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Objects(java.util.Objects) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) DrlxParseUtil.findAllChildrenRecursive(org.drools.modelcompiler.builder.generator.DrlxParseUtil.findAllChildrenRecursive) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Modifier(org.drools.javaparser.ast.Modifier) SimpleName(org.drools.javaparser.ast.expr.SimpleName) StringUtils(org.drools.core.util.StringUtils) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) SimpleName(org.drools.javaparser.ast.expr.SimpleName) ArrayList(java.util.ArrayList) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Objects(java.util.Objects) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 5 with SimpleName

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);
}
Also used : Node(org.drools.javaparser.ast.Node) SimpleName(org.drools.javaparser.ast.expr.SimpleName) InlineCastExpr(org.drools.javaparser.ast.drlx.expr.InlineCastExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) NullSafeFieldAccessExpr(org.drools.javaparser.ast.drlx.expr.NullSafeFieldAccessExpr) FieldAccessExpr(org.drools.javaparser.ast.expr.FieldAccessExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) DrlxParseUtil.nameExprToMethodCallExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.nameExprToMethodCallExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

SimpleName (org.drools.javaparser.ast.expr.SimpleName)5 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)3 NameExpr (org.drools.javaparser.ast.expr.NameExpr)3 Expression (org.drools.javaparser.ast.expr.Expression)2 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)2 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)1 ClassUtils (org.drools.core.util.ClassUtils)1 StringUtils (org.drools.core.util.StringUtils)1 JavaParser (org.drools.javaparser.JavaParser)1 JavaParser.parseExpression (org.drools.javaparser.JavaParser.parseExpression)1