Search in sources :

Example 36 with MethodCallExpr

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

the class Consequence method executeCall.

private MethodCallExpr executeCall(BlockStmt ruleVariablesBlock, BlockStmt ruleConsequence, Collection<String> verifiedDeclUsedInRHS, MethodCallExpr onCall) {
    boolean requireDrools = rewriteRHS(ruleVariablesBlock, ruleConsequence);
    MethodCallExpr executeCall = new MethodCallExpr(onCall, EXECUTE_CALL);
    LambdaExpr executeLambda = new LambdaExpr();
    executeCall.addArgument(executeLambda);
    executeLambda.setEnclosingParameters(true);
    if (requireDrools) {
        executeLambda.addParameter(new Parameter(new UnknownType(), "drools"));
    }
    verifiedDeclUsedInRHS.stream().map(x -> new Parameter(new UnknownType(), x)).forEach(executeLambda::addParameter);
    executeLambda.setBody(ruleConsequence);
    return executeCall;
}
Also used : UnknownType(org.drools.javaparser.ast.type.UnknownType) 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) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) Parameter(org.drools.javaparser.ast.body.Parameter) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 37 with MethodCallExpr

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

the class DrlxParseUtilTest method transformMethodExpressionToMethodCallExpressionTypeSafe.

@Test
public void transformMethodExpressionToMethodCallExpressionTypeSafe() {
    final Expression expr = JavaParser.parseExpression("address.city.startsWith(\"M\")");
    final Expression expr1 = JavaParser.parseExpression("getAddress().city.startsWith(\"M\")");
    final Expression expr2 = JavaParser.parseExpression("address.getCity().startsWith(\"M\")");
    final MethodCallExpr expected = JavaParser.parseExpression("getAddress().getCity().startsWith(\"M\")");
    assertEquals(expected.toString(), DrlxParseUtil.toMethodCallWithClassCheck(null, expr, null, Person.class, typeResolver).getExpression().toString());
    assertEquals(expected.toString(), DrlxParseUtil.toMethodCallWithClassCheck(null, expr1, null, Person.class, typeResolver).getExpression().toString());
    assertEquals(expected.toString(), DrlxParseUtil.toMethodCallWithClassCheck(null, expr2, null, Person.class, typeResolver).getExpression().toString());
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) Person(org.drools.modelcompiler.domain.Person) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) Test(org.junit.Test)

Example 38 with MethodCallExpr

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

Example 39 with MethodCallExpr

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

the class PackageModel method addInitStatement.

private void addInitStatement(BlockStmt rulesListInitializerBody, Expression expr, String fieldName) {
    NameExpr rulesFieldName = new NameExpr(fieldName);
    MethodCallExpr add = new MethodCallExpr(rulesFieldName, "add");
    add.addArgument(expr);
    rulesListInitializerBody.addStatement(add);
}
Also used : NameExpr(org.drools.javaparser.ast.expr.NameExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 40 with MethodCallExpr

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

the class Consequence method onCall.

private static MethodCallExpr onCall(Collection<String> usedArguments) {
    MethodCallExpr onCall = null;
    if (!usedArguments.isEmpty()) {
        onCall = new MethodCallExpr(null, ON_CALL);
        usedArguments.stream().map(DrlxParseUtil::toVar).forEach(onCall::addArgument);
    }
    return onCall;
}
Also used : MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)75 NameExpr (org.drools.javaparser.ast.expr.NameExpr)36 Expression (org.drools.javaparser.ast.expr.Expression)32 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)26 ArrayList (java.util.ArrayList)16 List (java.util.List)15 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)15 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)14 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)13 PackageModel (org.drools.modelcompiler.builder.PackageModel)13 Parameter (org.drools.javaparser.ast.body.Parameter)11 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)11 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)11 Collection (java.util.Collection)10 Optional (java.util.Optional)10 Type (org.drools.javaparser.ast.type.Type)10 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)10 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)9 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)9 JavaParser (org.drools.javaparser.JavaParser)8