Search in sources :

Example 6 with AnnotationDescr

use of org.drools.compiler.lang.descr.AnnotationDescr in project drools by kiegroup.

the class DRL5Expressions method conditionalOrExpression.

// $ANTLR end "annotationArray"
// $ANTLR start "conditionalOrExpression"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:214:1: conditionalOrExpression returns [BaseDescr result] : left= conditionalAndExpression ( DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression )* ;
public final BaseDescr conditionalOrExpression() throws RecognitionException {
    BaseDescr result = null;
    BaseDescr left = null;
    AnnotationDescr args = null;
    BaseDescr right = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:215:3: (left= conditionalAndExpression ( DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression )* )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:215:5: left= conditionalAndExpression ( DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression )*
        {
            pushFollow(FOLLOW_conditionalAndExpression_in_conditionalOrExpression1110);
            left = conditionalAndExpression();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:216:3: ( DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression )*
            loop25: while (true) {
                int alt25 = 2;
                int LA25_0 = input.LA(1);
                if ((LA25_0 == DOUBLE_PIPE)) {
                    alt25 = 1;
                }
                switch(alt25) {
                    case 1:
                        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:216:5: DOUBLE_PIPE (args= fullAnnotation[null] )? right= conditionalAndExpression
                        {
                            match(input, DOUBLE_PIPE, FOLLOW_DOUBLE_PIPE_in_conditionalOrExpression1119);
                            if (state.failed)
                                return result;
                            if (state.backtracking == 0) {
                                if (isNotEOF())
                                    helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_OPERATOR);
                            }
                            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:218:13: (args= fullAnnotation[null] )?
                            int alt24 = 2;
                            int LA24_0 = input.LA(1);
                            if ((LA24_0 == AT)) {
                                alt24 = 1;
                            }
                            switch(alt24) {
                                case 1:
                                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:218:13: args= fullAnnotation[null]
                                    {
                                        pushFollow(FOLLOW_fullAnnotation_in_conditionalOrExpression1141);
                                        args = fullAnnotation(null);
                                        state._fsp--;
                                        if (state.failed)
                                            return result;
                                    }
                                    break;
                            }
                            pushFollow(FOLLOW_conditionalAndExpression_in_conditionalOrExpression1147);
                            right = conditionalAndExpression();
                            state._fsp--;
                            if (state.failed)
                                return result;
                            if (state.backtracking == 0) {
                                if (buildDescr) {
                                    ConstraintConnectiveDescr descr = ConstraintConnectiveDescr.newOr();
                                    descr.addOrMerge(result);
                                    descr.addOrMerge(right);
                                    if (args != null) {
                                        descr.addAnnotation(args);
                                    }
                                    result = descr;
                                }
                            }
                        }
                        break;
                    default:
                        break loop25;
                }
            }
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 7 with AnnotationDescr

use of org.drools.compiler.lang.descr.AnnotationDescr in project drools by kiegroup.

the class DRL5Expressions method fullAnnotation.

// $ANTLR end "ternaryExpression"
// $ANTLR start "fullAnnotation"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:180:1: fullAnnotation[AnnotatedDescrBuilder inDescrBuilder] returns [AnnotationDescr result] : AT name= ID ( DOT x= ID )* annotationArgs[result] ;
public final AnnotationDescr fullAnnotation(AnnotatedDescrBuilder inDescrBuilder) throws RecognitionException {
    AnnotationDescr result = null;
    Token name = null;
    Token x = null;
    String n = "";
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:182:3: ( AT name= ID ( DOT x= ID )* annotationArgs[result] )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:182:5: AT name= ID ( DOT x= ID )* annotationArgs[result]
        {
            match(input, AT, FOLLOW_AT_in_fullAnnotation894);
            if (state.failed)
                return result;
            name = (Token) match(input, ID, FOLLOW_ID_in_fullAnnotation898);
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                n = (name != null ? name.getText() : null);
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:182:36: ( DOT x= ID )*
            loop18: while (true) {
                int alt18 = 2;
                int LA18_0 = input.LA(1);
                if ((LA18_0 == DOT)) {
                    alt18 = 1;
                }
                switch(alt18) {
                    case 1:
                        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:182:38: DOT x= ID
                        {
                            match(input, DOT, FOLLOW_DOT_in_fullAnnotation904);
                            if (state.failed)
                                return result;
                            x = (Token) match(input, ID, FOLLOW_ID_in_fullAnnotation908);
                            if (state.failed)
                                return result;
                            if (state.backtracking == 0) {
                                n += "." + (x != null ? x.getText() : null);
                            }
                        }
                        break;
                    default:
                        break loop18;
                }
            }
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = inDescrBuilder != null ? (AnnotationDescr) inDescrBuilder.newAnnotation(n).getDescr() : new AnnotationDescr(n);
                }
            }
            pushFollow(FOLLOW_annotationArgs_in_fullAnnotation929);
            annotationArgs(result);
            state._fsp--;
            if (state.failed)
                return result;
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 8 with AnnotationDescr

use of org.drools.compiler.lang.descr.AnnotationDescr in project drools by kiegroup.

the class RuleParserTest method testMultiValueAnnotationsBackwardCompatibility.

@Test
public void testMultiValueAnnotationsBackwardCompatibility() throws Exception {
    // multiple values with no keys are parsed as a single value
    final String text = "rule X @ann1( val1, val2 ) @ann2( \"val1\", \"val2\" ) when then end";
    RuleDescr rule = (RuleDescr) parse("rule", text);
    AnnotationDescr ann = rule.getAnnotation("ann1");
    assertNotNull(ann);
    assertEquals("val1, val2", ann.getValue());
    ann = rule.getAnnotation("ann2");
    assertNotNull(ann);
    assertEquals("\"val1\", \"val2\"", ann.getValue());
}
Also used : RuleDescr(org.drools.compiler.lang.descr.RuleDescr) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Test(org.junit.Test)

Example 9 with AnnotationDescr

use of org.drools.compiler.lang.descr.AnnotationDescr in project drools by kiegroup.

the class PatternBuilder method processMetadataAnnotations.

protected void processMetadataAnnotations(PatternDescr patternDescr, Pattern pattern, TypeResolver typeResolver) {
    for (AnnotationDescr ann : patternDescr.getAnnotations()) {
        String annFQN = ann.getFullyQualifiedName();
        if (!Watch.class.getCanonicalName().equals(annFQN)) {
            AnnotationDefinition def = buildAnnotationDef(ann, typeResolver);
            pattern.getAnnotations().put(annFQN, def);
        }
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr)

Example 10 with AnnotationDescr

use of org.drools.compiler.lang.descr.AnnotationDescr in project drools by kiegroup.

the class QueryBuilder method build.

public Pattern build(final RuleBuildContext context, final QueryDescr queryDescr) {
    ObjectType queryObjectType = ClassObjectType.DroolsQuery_ObjectType;
    final Pattern pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
    0, queryObjectType, null);
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, queryDescr, pattern, "name", null, true);
    final QueryNameConstraint constraint = new QueryNameConstraint(extractor, queryDescr.getName());
    PatternBuilder.registerReadAccessor(context, queryObjectType, "name", constraint);
    // adds appropriate constraint to the pattern
    pattern.addConstraint(constraint);
    ObjectType argsObjectType = ClassObjectType.DroolsQuery_ObjectType;
    InternalReadAccessor arrayExtractor = PatternBuilder.getFieldReadAccessor(context, queryDescr, null, argsObjectType, "elements", null, true);
    QueryImpl query = ((QueryImpl) context.getRule());
    String[] params;
    String[] types;
    int numParams = queryDescr.getParameters().length;
    if (query.isAbductive()) {
        params = Arrays.copyOf(queryDescr.getParameters(), queryDescr.getParameters().length + 1);
        types = Arrays.copyOf(queryDescr.getParameterTypes(), queryDescr.getParameterTypes().length + 1);
    } else {
        params = queryDescr.getParameters();
        types = queryDescr.getParameterTypes();
    }
    Declaration[] declarations = new Declaration[params.length];
    Class<?> abductionReturnKlass = null;
    if (query.isAbductive()) {
        Abductive abductive = queryDescr.getTypedAnnotation(Abductive.class);
        abductionReturnKlass = abductive.target();
        params[numParams] = "";
        types[numParams] = abductionReturnKlass.getName();
    }
    int i = 0;
    try {
        for (i = 0; i < params.length; i++) {
            Declaration declr = pattern.addDeclaration(params[i]);
            // this bit is different, notice its the ArrayElementReader that we wire up to, not the declaration.
            ArrayElementReader reader = new ArrayElementReader(arrayExtractor, i, context.getDialect().getTypeResolver().resolveType(types[i]));
            PatternBuilder.registerReadAccessor(context, argsObjectType, "elements", reader);
            declr.setReadAccessor(reader);
            declarations[i] = declr;
        }
        query.setParameters(declarations);
    } catch (ClassNotFoundException e) {
        context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, "Unable to resolve type '" + types[i] + " for parameter" + params[i]));
    }
    context.setPrefixPattern(pattern);
    if (query.isAbductive()) {
        String returnName = "";
        try {
            AnnotationDescr ann = queryDescr.getAnnotation(Abductive.class);
            Object[] argsVal = ((Object[]) ann.getValue("args"));
            String[] args = argsVal != null ? Arrays.copyOf(argsVal, argsVal.length, String[].class) : null;
            returnName = types[numParams];
            ObjectType objectType = new ClassObjectType(abductionReturnKlass, false);
            objectType = context.getPkg().getClassFieldAccessorStore().wireObjectType(objectType, (AbductiveQuery) query);
            ((AbductiveQuery) query).setReturnType(objectType, params, args, declarations);
        } catch (NoSuchMethodException e) {
            context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, "Unable to resolve abducible constructor for type : " + returnName + " with types " + Arrays.toString(types)));
        } catch (IllegalArgumentException e) {
            context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, e.getMessage()));
        }
    }
    return pattern;
}
Also used : Pattern(org.drools.core.rule.Pattern) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ClassObjectType(org.drools.core.base.ClassObjectType) Abductive(org.drools.core.beliefsystem.abductive.Abductive) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) QueryImpl(org.drools.core.rule.QueryImpl) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) AbductiveQuery(org.drools.core.rule.AbductiveQuery)

Aggregations

AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)27 AnnotatedBaseDescr (org.drools.compiler.lang.descr.AnnotatedBaseDescr)8 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)8 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)8 HashMap (java.util.HashMap)5 AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)5 ArrayList (java.util.ArrayList)4 TypeDeclarationDescr (org.drools.compiler.lang.descr.TypeDeclarationDescr)4 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)4 Map (java.util.Map)3 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)3 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)3 List (java.util.List)2 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)2 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)2 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)2 FieldDefinition (org.drools.core.factmodel.FieldDefinition)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 StringReader (java.io.StringReader)1