Exemplo n.º 1
0
        private List <Space.Tuple> QueryAll(TupleHandleOption option, int cur, List <object> res, object[] pattern)
        {
            #region LocalFunctions
            // supports recursive querying
            List <Space.Tuple> RecursiveQueryAll(object element)
            {
                res.Add(element);
                var allTuples = this[element].QueryAll(option, cur + 1, res, pattern);

                res.RemoveAt(res.Count - 1);
                return(allTuples);
            }

            #endregion

            // return result if done
            if (cur >= pattern.Length)
            {
                var result = Enumerable.Repeat(new dotSpace.Objects.Space.Tuple(res.ToArray()), Count).ToList();
                if (option == TupleHandleOption.REMOVE)
                {
                    Count = 0;
                }
                return(result);
            }

            var resList         = new List <Space.Tuple>();
            var matchedKeysList = new List <object>();
            if (pattern[cur] is Type)
            {
                // recursively search all children matching the specified type
                foreach (var key in
                         lookupTable.Select(item => item.Key)
                         .Where(key => MatchedType((Type)pattern[cur], key)))
                {
                    var subResult = RecursiveQueryAll(key);
                    if (subResult.Count != 0)
                    {
                        matchedKeysList.Add(key);
                        resList.AddRange(subResult);
                    }
                }
            }
            else if (lookupTable.ContainsKey(pattern[cur]))
            {
                resList = RecursiveQueryAll(pattern[cur]);
                matchedKeysList.Add(pattern[cur]);
            }

            if (option == TupleHandleOption.REMOVE)
            {
                foreach (var key in matchedKeysList)
                {
                    RemoveEmptyChild(key);
                }
            }
            return(resList);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Recursively query for tuple of the specify pattern
        /// </summary>
        /// <param name="option">Whether to keep the tuple or not</param>
        /// <param name="cur">current index in the pattern</param>
        /// <param name="res">current result tuple</param>
        /// <param name="pattern">the pattern we need to match</param>
        /// <returns>The tuple matching the pattern</returns>
        private ITuple Query(TupleHandleOption option, int cur, List <object> res, object[] pattern)
        {
            #region LocalFunctions
            // supports recursive querying
            ITuple RecursiveQuery(object element)
            {
                res.Add(element);
                var resultTuple = this[element].Query(option, cur + 1, res, pattern);

                res.RemoveAt(res.Count - 1);
                return(resultTuple);
            }

            #endregion

            // return result if done
            if (cur >= pattern.Length)
            {
                if (Count <= 0)
                {
                    return(null);
                }
                if (option == TupleHandleOption.REMOVE)
                {
                    Count--;
                }
                return(new Space.Tuple(res.ToArray()));
            }

            ITuple tuple = null;

            if (pattern[cur] is Type)
            {
                // recursively search all children matching the specified type
                tuple = lookupTable.Select(item => item.Key)
                        .Where(key => MatchedType((Type)pattern[cur], key))
                        .Select(RecursiveQuery)
                        .Where(t => t != null)
                        .FirstOrDefault();
            }
            else if (lookupTable.ContainsKey(pattern[cur]))
            {
                tuple = RecursiveQuery(pattern[cur]);
            }

            if (option == TupleHandleOption.REMOVE && tuple != null)
            {
                RemoveEmptyChild(tuple[cur]);
            }
            return(tuple);
        }