Search in sources :

Example 96 with MethodCallExpr

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

the class WindowReferenceGenerator method addField.

private void addField(KnowledgeBuilderImpl kbuilder, PackageModel packageModel, WindowDeclarationDescr descr) {
    final String windowName = toVar(descr.getName());
    final MethodCallExpr initializer = createDslTopLevelMethod(WINDOW_CALL);
    final PatternDescr pattern = descr.getPattern();
    ParsedBehavior behavior = pattern.getBehaviors().stream().map(this::parseTypeFromBehavior).findFirst().orElseThrow(RuntimeException::new);
    final WindowDefinition.Type windowType = behavior.windowType;
    initializer.addArgument(new NameExpr(windowType.getDeclaringClass().getCanonicalName() + "." + windowType.toString()));
    initializer.addArgument(new IntegerLiteralExpr(behavior.duration.getValue()));
    final TimeUnit timeUnit = behavior.duration.getTimeUnit();
    initializer.addArgument(new NameExpr(timeUnit.getDeclaringClass().getCanonicalName() + "." + timeUnit.name()));
    final Class<?> initClass = DrlxParseUtil.getClassFromContext(typeResolver, pattern.getObjectType());
    initializer.addArgument(new ClassExpr(toJavaParserType(initClass)));
    if (pattern.getSource() != null) {
        String epName = ((EntryPointDescr) pattern.getSource()).getEntryId();
        MethodCallExpr entryPointCall = createDslTopLevelMethod(ENTRY_POINT_CALL);
        entryPointCall.addArgument(toStringLiteral(epName));
        initializer.addArgument(entryPointCall);
    }
    parseConditions(kbuilder, packageModel, pattern, initClass).forEach(initializer::addArgument);
    packageModel.addAllWindowReferences(windowName, initializer);
}
Also used : IntegerLiteralExpr(com.github.javaparser.ast.expr.IntegerLiteralExpr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) NameExpr(com.github.javaparser.ast.expr.NameExpr) EntryPointDescr(org.drools.drl.ast.descr.EntryPointDescr) TimeUnit(java.util.concurrent.TimeUnit) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) WindowDefinition(org.drools.model.WindowDefinition) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 97 with MethodCallExpr

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

the class ConstraintUtil method isProperty.

private static boolean isProperty(Expression expr) {
    if (expr instanceof CastExpr) {
        expr = ((CastExpr) expr).getExpression();
    }
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr mcExpr = (MethodCallExpr) expr;
        if (mcExpr.getName().asString().equals(TO_BIG_DECIMAL) && mcExpr.getArgument(0) instanceof MethodCallExpr) {
            mcExpr = (MethodCallExpr) mcExpr.getArgument(0);
        }
        Optional<Expression> thisScope = getRootScope(mcExpr).filter(scope -> scope.equals(new NameExpr(THIS_PLACEHOLDER)));
        if (thisScope.isPresent()) {
            return true;
        }
    }
    return false;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) CastExpr(com.github.javaparser.ast.expr.CastExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 98 with MethodCallExpr

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

the class ConstraintUtil method processExpression.

private static void processExpression(Expression expr) {
    if (expr instanceof MethodCallExpr) {
        MethodCallExpr mcExpr = (MethodCallExpr) expr;
        if (canInverse(mcExpr)) {
            inverseMethodCallExpr(mcExpr);
        }
    } else if (expr instanceof BinaryExpr) {
        BinaryExpr bExpr = (BinaryExpr) expr;
        if (bExpr.getOperator() == BinaryExpr.Operator.AND || bExpr.getOperator() == BinaryExpr.Operator.OR) {
            Expression left = bExpr.getLeft();
            processExpression(left);
            Expression right = bExpr.getRight();
            processExpression(right);
        }
    } else if (expr instanceof UnaryExpr) {
        Expression expression = ((UnaryExpr) expr).getExpression();
        processExpression(expression);
    } else if (expr instanceof EnclosedExpr) {
        Expression inner = ((EnclosedExpr) expr).getInner();
        processExpression(inner);
    }
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) BinaryExpr(com.github.javaparser.ast.expr.BinaryExpr) EnclosedExpr(com.github.javaparser.ast.expr.EnclosedExpr) DrlxParseUtil.stripEnclosedExpr(org.drools.modelcompiler.builder.generator.DrlxParseUtil.stripEnclosedExpr) UnaryExpr(com.github.javaparser.ast.expr.UnaryExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 99 with MethodCallExpr

use of com.github.javaparser.ast.expr.MethodCallExpr 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 100 with MethodCallExpr

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

the class ModelGenerator method ruleMetaAttributes.

/**
 * Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#metadata(String, Object)}
 * starting from a drools-compiler {@link RuleDescr}.<br/>
 * Based on {@link org.drools.modelcompiler.KiePackagesBuilder#setRuleMetaAttributes(Rule, RuleImpl)} the reserved annotation keywords are:
 * Propagation, All, Direct.
 */
private static List<MethodCallExpr> ruleMetaAttributes(RuleContext context, RuleDescr ruleDescr) {
    List<MethodCallExpr> ruleMetaAttributes = new ArrayList<>();
    for (String metaAttr : ruleDescr.getAnnotationNames()) {
        MethodCallExpr metaAttributeCall = new MethodCallExpr(METADATA_CALL);
        metaAttributeCall.addArgument(toStringLiteral(metaAttr));
        AnnotationDescr ad = ruleDescr.getAnnotation(metaAttr);
        String adFqn = ad.getFullyQualifiedName();
        if ("Propagation".equals(metaAttr)) {
            // legacy case, as explained in the javadoc annotation above, ref. DROOLS-5685
            metaAttributeCall.addArgument(parseExpression(org.kie.api.definition.rule.Propagation.Type.class.getCanonicalName() + "." + ad.getSingleValueAsString()));
        } else if (adFqn != null) {
            AnnotationDefinition annotationDefinition;
            try {
                annotationDefinition = AnnotationDefinition.build(context.getTypeResolver().resolveType(adFqn), ad.getValueMap(), context.getTypeResolver());
            } catch (NoSuchMethodException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            if (annotationDefinition.getValues().size() == 1 && annotationDefinition.getValues().containsKey(AnnotationDescr.VALUE)) {
                Object annValue = annotationDefinition.getPropertyValue(AnnotationDescr.VALUE);
                metaAttributeCall.addArgument(toStringLiteral(annValue.toString()));
            } else {
                Map<String, Object> map = new HashMap<>(annotationDefinition.getValues().size());
                for (String key : annotationDefinition.getValues().keySet()) {
                    map.put(key, annotationDefinition.getPropertyValue(key));
                }
                metaAttributeCall.addArgument(objectAsJPExpression(map));
            }
        } else {
            if (ad.hasValue()) {
                if (ad.getValueMap().size() == 1) {
                    metaAttributeCall.addArgument(annotationSingleValueExpression(ad));
                } else {
                    metaAttributeCall.addArgument(objectAsJPExpression(ad.getValueMap()));
                }
            } else {
                metaAttributeCall.addArgument(new NullLiteralExpr());
            }
        }
        ruleMetaAttributes.add(metaAttributeCall);
    }
    return ruleMetaAttributes;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) ArrayList(java.util.ArrayList) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Map(java.util.Map) HashMap(java.util.HashMap) 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