Ejemplo n.º 1
0
        /// <summary>
        /// Applies the Filter over the results of evaluating the inner pattern.
        /// </summary>
        /// <param name="context">Evaluation Context.</param>
        /// <returns></returns>
        public sealed override BaseMultiset Evaluate(SparqlEvaluationContext context)
        {
            INode term = _term.Evaluate(null, 0);

            // First take appropriate pre-filtering actions
            if (context.InputMultiset is IdentityMultiset)
            {
                // If the Input is Identity switch the input to be a Multiset containing a single Set
                // where the variable is bound to the term
                context.InputMultiset = new Multiset();
                Set s = new Set();
                s.Add(RestrictionVariable, term);
                context.InputMultiset.Add(s);
            }
            else if (context.InputMultiset is NullMultiset)
            {
                // If Input is Null then output is Null
                context.OutputMultiset = context.InputMultiset;
                return(context.OutputMultiset);
            }
            else
            {
                if (context.InputMultiset.ContainsVariable(RestrictionVariable))
                {
                    // If the Input Multiset contains the variable then pre-filter
                    foreach (int id in context.InputMultiset.SetIDs.ToList())
                    {
                        ISet x = context.InputMultiset[id];
                        try
                        {
                            if (x.ContainsVariable(RestrictionVariable))
                            {
                                // If does exist check it has appropriate value and if not remove it
                                if (!term.Equals(x[RestrictionVariable]))
                                {
                                    context.InputMultiset.Remove(id);
                                }
                            }
                            else
                            {
                                // If doesn't exist for this set then bind it to the term
                                x.Add(RestrictionVariable, term);
                            }
                        }
                        catch (RdfQueryException)
                        {
                            context.InputMultiset.Remove(id);
                        }
                    }
                }
                else
                {
                    // If it doesn't contain the variable then bind for each existing set
                    foreach (ISet x in context.InputMultiset.Sets)
                    {
                        x.Add(RestrictionVariable, term);
                    }
                }
            }

            // Then evaluate the inner algebra
            BaseMultiset results = context.Evaluate(InnerAlgebra);

            if (results is NullMultiset || results is IdentityMultiset)
            {
                return(results);
            }

            // Filter the results to ensure that the variable is indeed bound to the term
            foreach (int id in results.SetIDs.ToList())
            {
                ISet x = results[id];
                try
                {
                    if (!term.Equals(x[RestrictionVariable]))
                    {
                        results.Remove(id);
                    }
                }
                catch (RdfQueryException)
                {
                    results.Remove(id);
                }
            }

            if (results.Count > 0)
            {
                context.OutputMultiset = results;
            }
            else
            {
                context.OutputMultiset = new NullMultiset();
            }
            return(context.OutputMultiset);
        }