Search in sources :

Example 1 with NullLiteralExpr

use of org.drools.javaparser.ast.expr.NullLiteralExpr 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}.
 */
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(new StringLiteralExpr(metaAttr));
        AnnotationDescr ad = ruleDescr.getAnnotation(metaAttr);
        String adFqn = ad.getFullyQualifiedName();
        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(new StringLiteralExpr(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.getValues().size() == 1) {
                    metaAttributeCall.addArgument(objectAsJPExpression(resolveValue(ad.getSingleValueAsString())));
                } else {
                    metaAttributeCall.addArgument(objectAsJPExpression(ad.getValueMap()));
                }
            } else {
                metaAttributeCall.addArgument(new NullLiteralExpr());
            }
        }
        ruleMetaAttributes.add(metaAttributeCall);
    }
    return ruleMetaAttributes;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with NullLiteralExpr

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

the class ExpressionTyper method extractPrefixExpressions.

private void extractPrefixExpressions(NullSafeFieldAccessExpr drlxExpr, Expression previous) {
    final BinaryExpr prefixExpression = new BinaryExpr(previous, new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS);
    prefixExpressions.add(prefixExpression);
    final Expression scope = drlxExpr.getScope();
    if (scope != null) {
        final Optional<TypedExpression> typedExpression1 = toTypedExpressionRec(scope);
        typedExpression1.ifPresent(te -> {
            final Expression expression = te.getExpression();
            final BinaryExpr notNullScope = new BinaryExpr(expression, new NullLiteralExpr(), BinaryExpr.Operator.NOT_EQUALS);
            prefixExpressions.add(0, notNullScope);
        });
    }
}
Also used : NullLiteralExpr(org.drools.javaparser.ast.expr.NullLiteralExpr) Expression(org.drools.javaparser.ast.expr.Expression) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) HalfBinaryExpr(org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr) TypedExpression(org.drools.modelcompiler.builder.generator.TypedExpression)

Aggregations

NullLiteralExpr (org.drools.javaparser.ast.expr.NullLiteralExpr)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1 AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)1 HalfBinaryExpr (org.drools.javaparser.ast.drlx.expr.HalfBinaryExpr)1 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)1 Expression (org.drools.javaparser.ast.expr.Expression)1 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)1 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)1 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)1