Search in sources :

Example 81 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class ExpressionTyperTest method transformMethodExpressionToMethodCallWithInlineCast.

@Test
public void transformMethodExpressionToMethodCallWithInlineCast() {
    typeResolver.addImport("org.drools.modelcompiler.domain.InternationalAddress");
    final DrlxExpression expr = DrlxParseUtil.parseExpression("address#InternationalAddress.state");
    final MethodCallExpr expected = StaticJavaParser.parseExpression("((org.drools.modelcompiler.domain.InternationalAddress)_this.getAddress()).getState()");
    assertEquals(PrintUtil.printNode(expected), toTypedExpression(PrintUtil.printNode(expr.getExpr()), Person.class).getExpression().toString());
}
Also used : DrlxExpression(org.drools.mvel.parser.ast.expr.DrlxExpression) Person(org.drools.modelcompiler.domain.Person) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 82 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class FlattenScopeTest method flattenMethodCall.

@Test
public void flattenMethodCall() {
    List<Node> actual = flattenScope(MockTypeResolver.INSTANCE, expr("name.startsWith(\"M\")"));
    MethodCallExpr methodCallExpr = new MethodCallExpr(new NameExpr("name"), "startsWith", nodeList(new StringLiteralExpr("M")));
    methodCallExpr.setTypeArguments(NodeList.nodeList());
    List<Node> expected = asList(new NameExpr("name"), methodCallExpr);
    compareArrays(actual, expected);
}
Also used : Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 83 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class ModifyCompiler method compile.

public CompiledBlockResult compile(String mvelBlock) {
    BlockStmt mvelExpression = MvelParser.parseBlock(mvelBlock);
    preprocessPhase.removeEmptyStmt(mvelExpression);
    Set<String> usedBindings = new HashSet<>();
    mvelExpression.findAll(ModifyStatement.class).forEach(s -> {
        Optional<Node> parentNode = s.getParentNode();
        PreprocessPhase.PreprocessPhaseResult invoke = preprocessPhase.invoke(s);
        usedBindings.addAll(invoke.getUsedBindings());
        parentNode.ifPresent(p -> {
            BlockStmt parentBlock = (BlockStmt) p;
            for (String modifiedFact : invoke.getUsedBindings()) {
                parentBlock.addStatement(new MethodCallExpr(null, "update", nodeList(new NameExpr(modifiedFact))));
            }
        });
        s.remove();
    });
    return new CompiledBlockResult(mvelExpression.getStatements()).setUsedBindings(usedBindings);
}
Also used : ModifyStatement(org.drools.mvel.parser.ast.expr.ModifyStatement) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Node(com.github.javaparser.ast.Node) NameExpr(com.github.javaparser.ast.expr.NameExpr) HashSet(java.util.HashSet) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 84 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class RhsParser method parse.

public void parse(RuleDescr ruleDescr, RuleContext context, Rule rule) {
    BlockStmt ruleVariablesBlock = new BlockStmt();
    MethodCallExpr consequenceExpr = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
    consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> m.getScope().map(s -> s.toString().equals("drools")).orElse(false)).map(m -> processStatement(context, consequenceExpr, m, ruleVariablesBlock)).filter(Objects::nonNull).forEach(a -> rule.getRhs().addAction(a));
}
Also used : ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ParserUtil.literalToValue(org.drools.impact.analysis.parser.impl.ParserUtil.literalToValue) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ParserUtil.isLiteral(org.drools.impact.analysis.parser.impl.ParserUtil.isLiteral) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) Rule(org.drools.impact.analysis.model.Rule) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ParserUtil.getLiteralValue(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralValue) ParserUtil.literalType(org.drools.impact.analysis.parser.impl.ParserUtil.literalType) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) ParserUtil.objectCreationExprToValue(org.drools.impact.analysis.parser.impl.ParserUtil.objectCreationExprToValue) Method(java.lang.reflect.Method) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ParserUtil.stripEnclosedAndCast(org.drools.impact.analysis.parser.impl.ParserUtil.stripEnclosedAndCast) ClassUtils(org.drools.core.util.ClassUtils) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Collectors(java.util.stream.Collectors) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Objects(java.util.Objects) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) InsertAction(org.drools.impact.analysis.model.right.InsertAction) Consequence(org.drools.modelcompiler.builder.generator.Consequence) Optional(java.util.Optional) ConsequenceAction(org.drools.impact.analysis.model.right.ConsequenceAction) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) Consequence(org.drools.modelcompiler.builder.generator.Consequence) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 85 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr in project drools by kiegroup.

the class RhsParser method processModify.

private ModifyAction processModify(RuleContext context, MethodCallExpr consequenceExpr, MethodCallExpr statement, BlockStmt ruleVariablesBlock) {
    String modifiedId = statement.getArgument(0).toString();
    Class<?> modifiedClass = context.getDeclarationById(modifiedId).orElseThrow(() -> new RuntimeException("Unknown declaration: " + modifiedId)).getDeclarationClass();
    ModifyAction action = new ModifyAction(modifiedClass);
    if (statement.getArguments().size() > 1) {
        String maskId = statement.getArgument(1).toString();
        AssignExpr maskAssignExpr = ruleVariablesBlock.findAll(AssignExpr.class).stream().filter(assign -> ((VariableDeclarationExpr) assign.getTarget()).getVariable(0).toString().equals(maskId)).findFirst().orElseThrow(() -> new RuntimeException("Unknown mask: " + maskId));
        MethodCallExpr maskMethod = ((MethodCallExpr) maskAssignExpr.getValue());
        List<MethodCallExpr> modifyingExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> m.getScope().map(s -> s.toString().equals(modifiedId) || s.toString().equals("(" + modifiedId + ")")).orElse(false)).collect(Collectors.toList());
        for (int i = 1; i < maskMethod.getArguments().size(); i++) {
            String property = maskMethod.getArgument(i).asStringLiteralExpr().asString();
            String setter = "set" + ucFirst(property);
            MethodCallExpr setterExpr = modifyingExprs.stream().filter(m -> m.getNameAsString().equals(setter)).filter(m -> m.getArguments().size() == 1).findFirst().orElse(null);
            Object value = null;
            if (setterExpr != null) {
                Expression arg = setterExpr.getArgument(0);
                arg = stripEnclosedAndCast(arg);
                if (arg.isLiteralExpr()) {
                    value = literalToValue(arg.asLiteralExpr());
                } else if (arg.isNameExpr()) {
                    value = ((ImpactAnalysisRuleContext) context).getBindVariableLiteralMap().get(arg.asNameExpr().getName().asString());
                } else if (arg.isObjectCreationExpr()) {
                    value = objectCreationExprToValue((ObjectCreationExpr) arg, context);
                }
            }
            Method accessor = ClassUtils.getAccessor(modifiedClass, property);
            if (accessor != null && Map.class.isAssignableFrom(accessor.getReturnType())) {
                String mapName = property;
                List<MethodCallExpr> mapPutExprs = consequenceExpr.findAll(MethodCallExpr.class).stream().filter(m -> isMapPutExpr(m, modifiedId, accessor.getName())).collect(Collectors.toList());
                mapPutExprs.stream().forEach(expr -> {
                    String mapKey = getLiteralString(context, expr.getArgument(0));
                    Object mapValue = getLiteralValue(context, expr.getArgument(1));
                    action.addModifiedProperty(new ModifiedMapProperty(mapName, mapKey, mapValue));
                });
            } else {
                action.addModifiedProperty(new ModifiedProperty(property, value));
            }
        }
    }
    return action;
}
Also used : ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ParserUtil.literalToValue(org.drools.impact.analysis.parser.impl.ParserUtil.literalToValue) DeclarationSpec(org.drools.modelcompiler.builder.generator.DeclarationSpec) InsertedProperty(org.drools.impact.analysis.model.right.InsertedProperty) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) ParserUtil.isLiteral(org.drools.impact.analysis.parser.impl.ParserUtil.isLiteral) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) Rule(org.drools.impact.analysis.model.Rule) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ParserUtil.getLiteralValue(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralValue) ParserUtil.literalType(org.drools.impact.analysis.parser.impl.ParserUtil.literalType) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) ParserUtil.objectCreationExprToValue(org.drools.impact.analysis.parser.impl.ParserUtil.objectCreationExprToValue) Method(java.lang.reflect.Method) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) ParserUtil.stripEnclosedAndCast(org.drools.impact.analysis.parser.impl.ParserUtil.stripEnclosedAndCast) ClassUtils(org.drools.core.util.ClassUtils) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) StringUtils.ucFirst(org.drools.core.util.StringUtils.ucFirst) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Collectors(java.util.stream.Collectors) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Objects(java.util.Objects) RuleContext(org.drools.modelcompiler.builder.generator.RuleContext) List(java.util.List) InsertAction(org.drools.impact.analysis.model.right.InsertAction) Consequence(org.drools.modelcompiler.builder.generator.Consequence) Optional(java.util.Optional) ConsequenceAction(org.drools.impact.analysis.model.right.ConsequenceAction) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ModifyAction(org.drools.impact.analysis.model.right.ModifyAction) ParserUtil.getLiteralString(org.drools.impact.analysis.parser.impl.ParserUtil.getLiteralString) Method(java.lang.reflect.Method) ModifiedProperty(org.drools.impact.analysis.model.right.ModifiedProperty) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) Expression(com.github.javaparser.ast.expr.Expression) ModifiedMapProperty(org.drools.impact.analysis.model.right.ModifiedMapProperty) Map(java.util.Map) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)299 Expression (com.github.javaparser.ast.expr.Expression)142 NameExpr (com.github.javaparser.ast.expr.NameExpr)106 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)101 Test (org.junit.Test)89 CompilationUnit (com.github.javaparser.ast.CompilationUnit)78 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)77 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)57 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)52 NodeList (com.github.javaparser.ast.NodeList)47 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)47 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)46 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)39 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)37 List (java.util.List)34 MethodUsage (com.github.javaparser.resolution.MethodUsage)33 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)32 ArrayList (java.util.ArrayList)31 TypeSolver (com.github.javaparser.symbolsolver.model.resolution.TypeSolver)30 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)29