private static Pair <Type, T> ResolveFromLocal <T>(
            string soughtName,
            ClassProvidedCompileTimeRegistry locals,
            Type annotationType,
            string objectName,
            Func <T, ISet <string> > namesProvider)
            where T : Attribute
        {
            var foundLocal = new List <Pair <Type, T> >();

            foreach (var entry in locals.Classes)
            {
                EPTypeHelper.TraverseAnnotations <T>(
                    entry.Value.ClassesMayNull,
                    (clazz, annotation) => {
                    var t     = (T)annotation;
                    var names = namesProvider.Invoke(t);
                    foreach (var name in names)
                    {
                        if (soughtName.Equals(name))
                        {
                            foundLocal.Add(new Pair <Type, T>(clazz, t));
                        }
                    }
                });
            }

            if (foundLocal.Count > 1)
            {
                throw GetDuplicateSingleRow(soughtName, objectName);
            }

            if (foundLocal.Count == 1)
            {
                return(foundLocal[0]);
            }

            return(null);
        }
Esempio n. 2
0
        public void Add(IList<Type> classes)
        {
            this.classes.AddAll(classes);

            try {
                EPTypeHelper.TraverseAnnotations<ExtensionSingleRowFunctionAttribute>(
                    classes,
                    (
                        clazz,
                        annotation) => {
                        if (singleRowFunctionExtensions.IsEmpty()) {
                            singleRowFunctionExtensions = new Dictionary<string, Pair<Type, ExtensionSingleRowFunctionAttribute>>();
                        }

                        if (singleRowFunctionExtensions.ContainsKey(annotation.Name)) {
                            throw new EPException("The plug-in single-row function '" + annotation.Name + "' occurs multiple times");
                        }

                        singleRowFunctionExtensions.Put(annotation.Name, new Pair<Type, ExtensionSingleRowFunctionAttribute>(clazz, annotation));
                    });

                EPTypeHelper.TraverseAnnotations<ExtensionAggregationFunctionAttribute>(
                    classes,
                    (
                        clazz,
                        annotation) => {
                        if (aggregationFunctionExtensions.IsEmpty()) {
                            aggregationFunctionExtensions = new Dictionary<string, Pair<Type, ExtensionAggregationFunctionAttribute>>();
                        }

                        if (aggregationFunctionExtensions.ContainsKey(annotation.Name)) {
                            throw new EPException("The plug-in aggregation function '" + annotation.Name + "' occurs multiple times");
                        }

                        aggregationFunctionExtensions.Put(annotation.Name, new Pair<Type, ExtensionAggregationFunctionAttribute>(clazz, annotation));
                    });

                EPTypeHelper.TraverseAnnotations<ExtensionAggregationMultiFunctionAttribute>(
                    classes,
                    (
                        clazz,
                        annotation) => {
                        if (aggregationMultiFunctionExtensions.IsEmpty()) {
                            aggregationMultiFunctionExtensions = new Dictionary<string, Pair<Type, string[]>>();
                        }

                        var names = annotation.Names.Split(',');
                        var namesDeduplicated = new HashSet<string>();
                        foreach (var nameWithSpaces in names) {
                            var name = nameWithSpaces.Trim();
                            namesDeduplicated.Add(name);
                        }

                        var namesArray = namesDeduplicated.ToArray();

                        foreach (var name in namesDeduplicated) {
                            if (aggregationMultiFunctionExtensions.ContainsKey(name)) {
                                throw new EPException("The plug-in aggregation multi-function '" + name + "' occurs multiple times");
                            }

                            aggregationMultiFunctionExtensions.Put(name, new Pair<Type, string[]>(clazz, namesArray));
                        }
                    });
            }
            catch (EPException ex) {
                throw new ExprValidationException(ex.Message, ex);
            }
        }
        private static Pair <Type, T> ResolveFromPath <T>(
            string soughtName,
            PathRegistry <string, ClassProvided> path,
            Type annotationType,
            string objectName,
            ICollection <string> moduleUses,
            ModuleDependenciesCompileTime moduleDependencies,
            Func <T, ISet <string> > namesProvider)
            where T : Attribute
        {
            // TBD: Verify that annotationType is derived from T
            if (!typeof(T).IsAssignableFrom(annotationType))
            {
                throw new ArgumentException("cannot assign annotationType from " + typeof(T).FullName);
            }

            IList <PathFunc <T> > foundPath = new List <PathFunc <T> >();

            path.TraverseWithModule((moduleName, classProvided) => {
                EPTypeHelper.TraverseAnnotations <T>(
                    classProvided.ClassesMayNull,
                    (
                        clazz,
                        annotation) => {
                    var t     = annotation;
                    var names = namesProvider.Invoke(t);
                    foreach (var name in names)
                    {
                        if (soughtName.Equals(name))
                        {
                            foundPath.Add(new PathFunc <T>(moduleName, clazz, t));
                        }
                    }
                });
            });

            PathFunc <T> foundPathFunc;

            if (foundPath.IsEmpty())
            {
                return(null);
            }
            else if (foundPath.Count == 1)
            {
                foundPathFunc = foundPath[0];
            }
            else
            {
                if (moduleUses == null || moduleUses.IsEmpty())
                {
                    throw GetDuplicateSingleRow(soughtName, objectName);
                }

                IList <PathFunc <T> > matchesUses = new List <PathFunc <T> >(2);
                foreach (var func in foundPath)
                {
                    if (moduleUses.Contains(func.OptionalModuleName))
                    {
                        matchesUses.Add(func);
                    }
                }

                if (matchesUses.Count > 1)
                {
                    throw GetDuplicateSingleRow(soughtName, objectName);
                }

                if (matchesUses.IsEmpty())
                {
                    return(null);
                }

                foundPathFunc = matchesUses[0];
            }

            moduleDependencies.AddPathClass(foundPathFunc.Clazz.FullName, foundPathFunc.OptionalModuleName);
            return(new Pair <Type, T>(foundPathFunc.Clazz, foundPathFunc.Annotation));
        }