예제 #1
0
        /// <summary>
        /// Determines whether two Result Sets are equal
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        /// <remarks>
        /// Experimental and not yet complete
        /// </remarks>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            if (obj is SparqlResultSet)
            {
                SparqlResultSet results = (SparqlResultSet)obj;

                //Must contain same number of Results to be equal
                if (this.Count != results.Count)
                {
                    return(false);
                }

                //Must have same Boolean result to be equal
                if (this.Result != results.Result)
                {
                    return(false);
                }

                //Must contain the same set of variables
                if (this.Variables.Count() != results.Variables.Count())
                {
                    return(false);
                }
                if (!this.Variables.All(v => results.Variables.Contains(v)))
                {
                    return(false);
                }
                if (results.Variables.Any(v => !this._variables.Contains(v)))
                {
                    return(false);
                }

                //If both have no results then they are equal
                if (this.Count == 0 && results.Count == 0)
                {
                    return(true);
                }

                //All Ground Results from the Result Set must appear in the Other Result Set
                List <SparqlResult> otherResults = results.OrderByDescending(r => r.Variables.Count()).ToList();
                List <SparqlResult> localResults = new List <SparqlResult>();
                int grCount = 0;
                foreach (SparqlResult result in this.Results.OrderByDescending(r => r.Variables.Count()))
                {
                    if (result.IsGroundResult)
                    {
                        //If a Ground Result in this Result Set is not in the other Result Set we're not equal
                        if (!otherResults.Remove(result))
                        {
                            return(false);
                        }
                        grCount++;
                    }
                    else
                    {
                        localResults.Add(result);
                    }
                }

                //If all the Results were ground results and we've emptied all the Results from the other Result Set
                //then we were equal
                if (this.Count == grCount && otherResults.Count == 0)
                {
                    return(true);
                }

                //If the Other Results still contains Ground Results we're not equal
                if (otherResults.Any(r => r.IsGroundResult))
                {
                    return(false);
                }

                //Create Graphs of the two sets of non-Ground Results
                SparqlResultSet local = new SparqlResultSet();
                SparqlResultSet other = new SparqlResultSet();
                foreach (String var in this._variables)
                {
                    local.AddVariable(var);
                    other.AddVariable(var);
                }
                foreach (SparqlResult r in localResults)
                {
                    local.AddResult(r);
                }
                foreach (SparqlResult r in otherResults)
                {
                    other.AddResult(r);
                }

                //Compare the two Graphs for equality
                SparqlRdfWriter writer = new SparqlRdfWriter();
                IGraph          g      = writer.GenerateOutput(local);
                IGraph          h      = writer.GenerateOutput(other);
                return(g.Equals(h));
            }
            else
            {
                return(false);
            }
        }