Search in sources :

Example 16 with VariableDeclarationExpr

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

the class ModelGenerator method processRule.

private static void processRule(PackageDescr packageDescr, RuleContext context) {
    PackageModel packageModel = context.getPackageModel();
    RuleDescr ruleDescr = context.getRuleDescr();
    context.addGlobalDeclarations();
    context.setDialectFromAttributes(ruleDescr.getAttributes().values());
    for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
        context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
    }
    RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
    BlockStmt ruleVariablesBlock = context.getRuleVariablesBlock();
    new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
    if (context.hasCompilationError()) {
        return;
    }
    final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
    MethodDeclaration ruleMethod = new MethodDeclaration(NodeList.nodeList(Modifier.publicModifier(), Modifier.staticModifier()), toClassOrInterfaceType(Rule.class), ruleMethodName);
    ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
    VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(toClassOrInterfaceType(Rule.class), "rule");
    MethodCallExpr ruleCall = createDslTopLevelMethod(RULE_CALL);
    if (!ruleDescr.getNamespace().isEmpty()) {
        ruleCall.addArgument(toStringLiteral(ruleDescr.getNamespace()));
    }
    ruleCall.addArgument(toStringLiteral(ruleDescr.getName()));
    MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(toClassOrInterfaceType(ruleUnitDescr.getCanonicalName()))) : ruleCall;
    for (MethodCallExpr attributeExpr : ruleAttributes(context, ruleDescr)) {
        attributeExpr.setScope(buildCallScope);
        buildCallScope = attributeExpr;
    }
    for (MethodCallExpr metaAttributeExpr : ruleMetaAttributes(context, ruleDescr)) {
        metaAttributeExpr.setScope(buildCallScope);
        buildCallScope = metaAttributeExpr;
    }
    MethodCallExpr buildCall = new MethodCallExpr(buildCallScope, BUILD_CALL, NodeList.nodeList(context.getExpressions()));
    createVariables(ruleVariablesBlock, packageModel, context);
    ruleMethod.setBody(ruleVariablesBlock);
    MethodCallExpr executeCall = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
    buildCall.addArgument(executeCall);
    ruleVariablesBlock.addStatement(new AssignExpr(ruleVar, buildCall, AssignExpr.Operator.ASSIGN));
    ruleVariablesBlock.addStatement(new ReturnStmt("rule"));
    packageModel.putRuleMethod(ruleUnitDescr != null ? ruleUnitDescr.getSimpleName() : DEFAULT_RULE_UNIT, ruleMethod, context.getRuleIndex());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) PackageModel(org.drools.modelcompiler.builder.PackageModel) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) Rule(org.drools.model.Rule) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 17 with VariableDeclarationExpr

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

the class CommonCodegenUtils method getFilteredKiePMMLNameValueExpression.

/**
 * Returns
 * <pre>
 *  Optional<KiePMMLNameValue> kiePMMLNameValue = (<i>kiePMMLNameValueListParam</i>)
 *      .stream()
 *      .filter((KiePMMLNameValue kpmmlnv) -> Objects.equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName()))
 *      .findFirst();
 * </pre>
 * <p>
 * expression, where <b>kiePMMLNameValueListParam</b> is the name of the
 * <code>List&lt;KiePMMLNameValue&gt;</code> parameter, and
 * <b>fieldNameToRef</b> is the name of the field to find, in the containing method
 * @param kiePMMLNameValueListParam
 * @param fieldNameToRef
 * @param stringLiteralComparison if <code>true</code>, equals comparison is made on the String, e.g Objects
 * .equals("(<i>fieldNameToRef</i>)", kpmmlnv.getName())),
 * otherwise, is done on object reference,  e.g Objects.equals((<i>fieldNameToRef</i>), kpmmlnv.getName())). In
 * this latter case, a <i>fieldNameToRef</i> variable is
 * expected to exists
 * @return
 */
public static ExpressionStmt getFilteredKiePMMLNameValueExpression(final String kiePMMLNameValueListParam, final String fieldNameToRef, boolean stringLiteralComparison) {
    // kpmmlnv.getName()
    MethodCallExpr argumentBodyExpressionArgument2 = new MethodCallExpr("getName");
    argumentBodyExpressionArgument2.setScope(new NameExpr(LAMBDA_PARAMETER_NAME));
    // Objects.equals(fieldNameToRef, kpmmlnv.getName())
    MethodCallExpr argumentBodyExpression = new MethodCallExpr("equals");
    Expression equalsComparisonExpression;
    if (stringLiteralComparison) {
        equalsComparisonExpression = new StringLiteralExpr(fieldNameToRef);
    } else {
        equalsComparisonExpression = new NameExpr(fieldNameToRef);
    }
    argumentBodyExpression.setArguments(NodeList.nodeList(equalsComparisonExpression, argumentBodyExpressionArgument2));
    argumentBodyExpression.setScope(new NameExpr(Objects.class.getName()));
    ExpressionStmt argumentBody = new ExpressionStmt(argumentBodyExpression);
    // (KiePMMLNameValue kpmmlnv) -> Objects.equals(fieldNameToRef, kpmmlnv.getName())
    Parameter argumentParameter = new Parameter(parseClassOrInterfaceType(KiePMMLNameValue.class.getName()), LAMBDA_PARAMETER_NAME);
    LambdaExpr argument = new LambdaExpr();
    // 
    argument.setEnclosingParameters(true).setParameters(NodeList.nodeList(argumentParameter));
    // (KiePMMLNameValue kpmmlnv) ->
    // Objects.equals(fieldNameToRef, kpmmlnv.getName())
    argument.setBody(argumentBody);
    // kiePMMLNameValueListParam.stream()
    MethodCallExpr initializerScopeScope = new MethodCallExpr("stream");
    initializerScopeScope.setScope(new NameExpr(kiePMMLNameValueListParam));
    // kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv)  -> Objects.equals(fieldNameToRef,
    // kpmmlnv.getName()))
    MethodCallExpr initializerScope = new MethodCallExpr("filter");
    initializerScope.setScope(initializerScopeScope);
    initializerScope.setArguments(NodeList.nodeList(argument));
    // kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue kpmmlnv)  -> Objects.equals(fieldNameToRef,
    // kpmmlnv.getName())).findFirst()
    MethodCallExpr initializer = new MethodCallExpr("findFirst");
    initializer.setScope(initializerScope);
    // Optional<KiePMMLNameValue> kiePMMLNameValue
    VariableDeclarator variableDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Optional.class.getName(), Collections.singletonList(KiePMMLNameValue.class.getName())), OPTIONAL_FILTERED_KIEPMMLNAMEVALUE_NAME);
    // Optional<KiePMMLNameValue> kiePMMLNameValue = kiePMMLNameValueListParam.stream().filter((KiePMMLNameValue
    // kpmmlnv)  -> Objects.equals(fieldNameToRef, kpmmlnv.getName())).findFirst()
    variableDeclarator.setInitializer(initializer);
    // 
    VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(NodeList.nodeList(variableDeclarator));
    ExpressionStmt toReturn = new ExpressionStmt();
    toReturn.setExpression(variableDeclarationExpr);
    return toReturn;
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) KiePMMLNameValue(org.kie.pmml.commons.model.tuples.KiePMMLNameValue) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 18 with VariableDeclarationExpr

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

the class CommonCodegenUtilsTest method getVariableDeclarator.

@Test
public void getVariableDeclarator() {
    final String variableName = "variableName";
    final BlockStmt body = new BlockStmt();
    assertFalse(CommonCodegenUtils.getVariableDeclarator(body, variableName).isPresent());
    final VariableDeclarationExpr variableDeclarationExpr = new VariableDeclarationExpr(parseClassOrInterfaceType("String"), variableName);
    body.addStatement(variableDeclarationExpr);
    Optional<VariableDeclarator> retrieved = CommonCodegenUtils.getVariableDeclarator(body, variableName);
    assertTrue(retrieved.isPresent());
    VariableDeclarator variableDeclarator = retrieved.get();
    assertEquals(variableName, variableDeclarator.getName().asString());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 19 with VariableDeclarationExpr

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

the class KiePMMLNodeFactoryTest method populateEvaluateNodeWithNodeFunctions.

@Test
public void populateEvaluateNodeWithNodeFunctions() {
    final BlockStmt toPopulate = new BlockStmt();
    final VariableDeclarator variableDeclarator = new VariableDeclarator();
    variableDeclarator.setType("Object");
    variableDeclarator.setName(NODE_FUNCTIONS);
    toPopulate.addStatement(new VariableDeclarationExpr(variableDeclarator));
    assertFalse(variableDeclarator.getInitializer().isPresent());
    // empty list
    List<String> nestedNodesFullClasses = Collections.emptyList();
    KiePMMLNodeFactory.populateEvaluateNodeWithNodeFunctions(toPopulate, nestedNodesFullClasses);
    commonVerifyEvaluateNodeWithNodeFunctions(variableDeclarator, nestedNodesFullClasses);
    // populated list
    nestedNodesFullClasses = IntStream.range(0, 2).mapToObj(i -> "full.node.NodeClassName" + i).collect(Collectors.toList());
    KiePMMLNodeFactory.populateEvaluateNodeWithNodeFunctions(toPopulate, nestedNodesFullClasses);
    commonVerifyEvaluateNodeWithNodeFunctions(variableDeclarator, nestedNodesFullClasses);
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Example 20 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project checker-framework by typetools.

the class DoubleJavaParserVisitor method visit.

@Override
public void visit(final VariableDeclarationExpr node1, final Node other) {
    VariableDeclarationExpr node2 = (VariableDeclarationExpr) other;
    defaultAction(node1, node2);
    visitLists(node1.getModifiers(), node2.getModifiers());
    visitLists(node1.getVariables(), node2.getVariables());
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr)

Aggregations

VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)38 Test (org.junit.Test)17 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)16 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 Expression (com.github.javaparser.ast.expr.Expression)11 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)8 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)5 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)4 Parameter (com.github.javaparser.ast.body.Parameter)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 Type (com.github.javaparser.ast.type.Type)4 Node (com.github.javaparser.ast.Node)3 CastExpr (com.github.javaparser.ast.expr.CastExpr)3 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)3 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)2