예제 #1
0
        private SparqlResultSet ParseSparqlResultSetXmlResultFile(XDocument expected)
        {
            XNamespace ns = "http://www.w3.org/2005/sparql-results#";

            var variableNodes = expected
                                .Descendants()
                                .Where(x => x.Name.Namespace == ns && x.Name.LocalName == "head")
                                .Descendants()
                                .Where(x => x.Name.Namespace == ns && x.Name.LocalName == "variable")
                                .Select(x => x.Attribute("name"))
                                .Where(x => x != null)
                                .Select(x => x.Value);

            var resultSet = new SparqlResultSet();
            var handler   = new ResultSetHandler(resultSet);

            handler.StartResults();

            foreach (var variable in variableNodes)
            {
                handler.HandleVariable(variable);
            }

            var resultNodes = expected
                              .Descendants()
                              .Where(x => x.Name.Namespace == ns && x.Name.LocalName == "results")
                              .Descendants()
                              .Where(x => x.Name.Namespace == ns && x.Name.LocalName == "result");

            foreach (var resultNode in resultNodes)
            {
                var bindings = resultNode
                               .Descendants()
                               .Where(x => x.Name.Namespace == ns && x.Name.LocalName == "binding")
                               .Where(x => x.Attribute("name") != null);


                var set = new Set();

                foreach (var binding in bindings)
                {
                    var varNane = binding.Attribute("name").Value;
                    var value   = GetSparqlValue(binding, handler);

                    if (value != null)
                    {
                        set.Add(varNane, value);
                    }
                }

                handler.HandleResult(new SparqlResult(set));
            }

            handler.EndResults(true);

            return(resultSet);
        }
예제 #2
0
        /// <summary>
        /// Runs the task
        /// </summary>
        /// <returns></returns>
        protected override object RunTaskInternal()
        {
            try
            {
                //Firstly try and parse the Query
                this.Query = this._parser.ParseFromString(this.QueryString);
            }
            catch
            {
                this.Information = "Query is not valid SPARQL 1.0/1.1 - will attempt to evaluate it but underlying store may reject the originalQuery...";
            }

            // Successfuly parsed originalQuery
            if (this.Query != null)
            {
                //Then apply it to the Manager using the GenericQueryProcessor
                try
                {
                    //Check that paging can be used if it was enabled
                    if (this._usePaging)
                    {
                        if (this.Query.Limit >= 0 || this.Query.Offset > 0)
                        {
                            throw new RdfQueryException("Cannot apply originalQuery paging when the SPARQL Query already contains an explicit LIMIT and/or OFFSET clause");
                        }
                        else if (this.Query.QueryType == SparqlQueryType.Ask)
                        {
                            throw new RdfQueryException("Cannot apply originalQuery paging to an ASK Query");
                        }
                    }

                    int      offset    = 0;
                    TimeSpan totalTime = TimeSpan.Zero;

                    switch (this.Query.QueryType)
                    {
                    case SparqlQueryType.Ask:
                        SparqlResultSet blnResult = this._processor.ProcessQuery(this.Query) as SparqlResultSet;
                        if (blnResult == null)
                        {
                            throw new RdfQueryException("Store did not return a SPARQL Result Set for the ASK originalQuery as was expected");
                        }
                        return(blnResult);

                    case SparqlQueryType.Construct:
                    case SparqlQueryType.Describe:
                    case SparqlQueryType.DescribeAll:
                        Graph g = new Graph();
                        g.NamespaceMap.Import(this.Query.NamespaceMap);

                        do
                        {
                            if (this._usePaging)
                            {
                                this.Query.Limit  = this._pageSize;
                                this.Query.Offset = offset;
                            }
                            Object result = this._processor.ProcessQuery(this.Query);
                            totalTime += this.Query.QueryExecutionTime.HasValue ? this.Query.QueryExecutionTime.Value : TimeSpan.Zero;

                            if (!(result is IGraph))
                            {
                                throw new RdfQueryException("SPARQL Query did not return a RDF Graph as expected");
                            }
                            IGraph temp = (IGraph)result;

                            //If no further results can halt
                            if (temp.Triples.Count == 0)
                            {
                                break;
                            }
                            offset += this._pageSize;

                            //Merge the partial result into the final result
                            g.Merge(temp);
                        } while (this._usePaging);

                        this.Information = "Query Completed OK (Took " + totalTime.ToString() + ")";
                        return(g);

                    case SparqlQueryType.Select:
                    case SparqlQueryType.SelectAll:
                    case SparqlQueryType.SelectAllDistinct:
                    case SparqlQueryType.SelectAllReduced:
                    case SparqlQueryType.SelectDistinct:
                    case SparqlQueryType.SelectReduced:
                        SparqlResultSet  results = new SparqlResultSet();
                        ResultSetHandler handler = new ResultSetHandler(results);
                        try
                        {
                            handler.StartResults();

                            do
                            {
                                if (this._usePaging)
                                {
                                    this.Query.Limit  = this._pageSize;
                                    this.Query.Offset = offset;
                                }
                                Object result = this._processor.ProcessQuery(this.Query);
                                totalTime += this.Query.QueryExecutionTime.HasValue ? this.Query.QueryExecutionTime.Value : TimeSpan.Zero;

                                if (!(result is SparqlResultSet))
                                {
                                    throw new RdfQueryException("SPARQL Query did not return a SPARQL Result Set as expected");
                                }
                                SparqlResultSet rset = (SparqlResultSet)result;
                                foreach (String var in rset.Variables)
                                {
                                    handler.HandleVariable(var);
                                }

                                //If no further results can halt
                                if (rset.Count == 0)
                                {
                                    break;
                                }
                                offset += this._pageSize;

                                //Merge the partial result into the final result
                                foreach (SparqlResult r in rset)
                                {
                                    handler.HandleResult(r);
                                }
                            } while (this._usePaging);

                            handler.EndResults(true);
                        }
                        catch
                        {
                            handler.EndResults(false);
                            throw;
                        }
                        this.Information = "Query Completed OK (Took " + totalTime.ToString() + ")";
                        return(results);

                    default:
                        throw new RdfQueryException("Cannot evaluate an unknown originalQuery type");
                    }
                }
                catch
                {
                    //Try and show the execution time if possible
                    if (this.Query.QueryExecutionTime.HasValue)
                    {
                        this.Information = "Query Failed (Took " + this.Query.QueryExecutionTime.Value + ")";
                    }
                    else
                    {
                        this.Information = "Query Failed";
                    }
                    throw;
                }
            }

            // Unsuccessfully parsed originalQuery - may be using syntax extensions specific to the target store
            DateTime start = DateTime.Now;

            try
            {
                if (this._usePaging)
                {
                    throw new RdfQueryException("Cannot apply paging to a Query that we cannot parse as a valid SPARQL 1.0/1.1 originalQuery");
                }
                Object results = this._storage.Query(this.QueryString);
                this.Information = "Query Completed OK (Took " + (DateTime.Now - start) + ")";
                return(results);
            }
            catch
            {
                //Try and show the execution time if possible
                try
                {
                    this.Information = "Query Failed (Took " + (DateTime.Now - start) + ")";
                }
                catch
                {
                    this.Information = "Query Failed";
                }
                throw;
            }
        }