Ejemplo n.º 1
0
        /// <summary>
        /// Creates a new Parser Context
        /// </summary>
        /// <param name="reader">XML Reader</param>
        /// <param name="handler">Results Handler</param>
        public SparqlXmlParserContext(XmlReader reader, ISparqlResultsHandler handler)
            : base(handler)
        {
            if (reader == null) throw new ArgumentNullException("reader");

            this._reader = reader;
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Executes a SPARQL Query on the Graph handling the results with the given handlers
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">SPARQL Results Handler</param>
 /// <param name="sparqlQuery">SPARQL Query</param>
 public void ExecuteQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery)
 {
     if (this._store == null)
     {
         this._store = new TripleStore();
         this._store.Add(this);
     }
     this._store.ExecuteQuery(rdfHandler, resultsHandler, sparqlQuery);
 }
Ejemplo n.º 3
0
 public void ExecuteSparql(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, string sparqlQuery, IStore store)
 {
     try
     {
         var query = ParseSparql(sparqlQuery);
         var queryProcessor = new BrightstarQueryProcessor(store, new StoreSparqlDataset(store));
         queryProcessor.ProcessQuery(rdfHandler, resultsHandler, query);
     }
     catch (Exception ex)
     {
         Logging.LogError(BrightstarEventId.SparqlExecutionError,
                          "Error Executing Sparql {0}. Cause: {1}",
                          sparqlQuery, ex);
         throw;
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Loads a Result Set from a File using a Results Handler
 /// </summary>
 /// <param name="handler">Results Handler to use</param>
 /// <param name="filename">Filename to load from</param>
 public void Load(ISparqlResultsHandler handler, string filename)
 {
     if (filename == null) throw new RdfParseException("Cannot parse SPARQL Results from a null file");
     this.Load(handler, new StreamReader(filename));
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Executes a SPARQL Query on the underlying Store processing the results with an appropriate handler from those provided
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="sparqlQuery">SPARQL Query</param>
 /// <returns></returns>
 public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery)
 {
     this._queryManager.Query(rdfHandler, resultsHandler, sparqlQuery);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Processes a SPARQL Query passing the results to the RDF or Results handler as appropriate
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="query">SPARQL Query</param>
 public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query)
 {
     #if !SILVERLIGHT
     query.QueryExecutionTime = null;
     DateTime start = DateTime.Now;
     try
     {
         this._svc.Query(rdfHandler, resultsHandler, query.ToString());
     }
     finally
     {
         TimeSpan elapsed = (DateTime.Now - start);
         query.QueryExecutionTime = (DateTime.Now - start);
     }
     #else
     throw new NotSupportedException("Synchronous remote query is not supported under Silverlight/WP7 - please use one of the alternative overload of this methods which takes a callback");
     #endif
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Executes a SPARQL Query on the Triple Store processing the results with an appropriate handler from those provided
        /// </summary>
        /// <param name="rdfHandler">RDF Handler</param>
        /// <param name="resultsHandler">Results Handler</param>
        /// <param name="query">SPARQL Query as unparsed String</param>
        public virtual void ExecuteQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String query)
        {
            //Parse the Query
            SparqlQueryParser sparqlparser = new SparqlQueryParser();
            SparqlQuery q = sparqlparser.ParseFromString(query);

            //Invoke other execute method
            this.ExecuteQuery(rdfHandler, resultsHandler, q);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Makes a SPARQL Query against the Store processing the results with the appropriate processor from those given
        /// </summary>
        /// <param name="rdfHandler">RDF Handler</param>
        /// <param name="resultsHandler">Results Handler</param>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <returns></returns>
        public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery)
        {
            if (this._queryParser == null) this._queryParser = new SparqlQueryParser();
            SparqlQuery q = this._queryParser.ParseFromString(sparqlQuery);

            if (this._queryProcessor == null) this._queryProcessor = new LeviathanQueryProcessor(this._dataset);
            this._queryProcessor.ProcessQuery(rdfHandler, resultsHandler, q);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Parser method which parses the Stream as Json
 /// </summary>
 /// <param name="input">Input Stream</param>
 /// <param name="handler">Results Handler</param>
 private void Parse(TextReader input, ISparqlResultsHandler handler)
 {
     this.ParseResultSetObject(new SparqlJsonParserContext(new CommentIgnoringJsonTextReader(input), handler));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Processes a SPARQL Query asynchronously passing the results to the relevant handler and invoking the callback when the query completes
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="query">SPARQL Query</param>
 /// <param name="callback">Callback</param>
 /// <param name="state">State to pass to the callback</param>
 public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query, QueryCallback callback, Object state)
 {
     query.QueryExecutionTime = null;
     DateTime start = DateTime.Now;
     try
     {
         switch (query.QueryType)
         {
             case SparqlQueryType.Ask:
             case SparqlQueryType.Select:
             case SparqlQueryType.SelectAll:
             case SparqlQueryType.SelectAllDistinct:
             case SparqlQueryType.SelectAllReduced:
             case SparqlQueryType.SelectDistinct:
             case SparqlQueryType.SelectReduced:
                 this._endpoint.QueryWithResultSet(resultsHandler, this._formatter.Format(query), callback, state);
                 break;
             case SparqlQueryType.Construct:
             case SparqlQueryType.Describe:
             case SparqlQueryType.DescribeAll:
                 this._endpoint.QueryWithResultGraph(rdfHandler, this._formatter.Format(query), callback, state);
                 break;
             default:
                 throw new RdfQueryException("Unable to execute an unknown query type against a Remote Endpoint");
         }
     }
     finally
     {
         TimeSpan elapsed = (DateTime.Now - start);
         query.QueryExecutionTime = elapsed;
     }
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Processes a SPARQL Query passing the results to the RDF or Results handler as appropriate
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="query">SPARQL Query</param>
 public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query)
 {
     #if !SILVERLIGHT
     query.QueryExecutionTime = null;
     DateTime start = DateTime.Now;
     try
     {
         switch (query.QueryType)
         {
             case SparqlQueryType.Ask:
             case SparqlQueryType.Select:
             case SparqlQueryType.SelectAll:
             case SparqlQueryType.SelectAllDistinct:
             case SparqlQueryType.SelectAllReduced:
             case SparqlQueryType.SelectDistinct:
             case SparqlQueryType.SelectReduced:
                 this._endpoint.QueryWithResultSet(resultsHandler, this._formatter.Format(query));
                 break;
             case SparqlQueryType.Construct:
             case SparqlQueryType.Describe:
             case SparqlQueryType.DescribeAll:
                 this._endpoint.QueryWithResultGraph(rdfHandler, this._formatter.Format(query));
                 break;
             default:
                 throw new RdfQueryException("Unable to execute an unknown query type against a Remote Endpoint");
         }
     }
     finally
     {
         TimeSpan elapsed = (DateTime.Now - start);
         query.QueryExecutionTime = elapsed;
     }
     #else
     throw new NotSupportedException("Synchronous remote query is not supported under Silverlight/WP7 - please use one of the alternative overload of this methods which takes a callback");
     #endif
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Creates a new Results Parser Context
 /// </summary>
 /// <param name="handler">Results Handler</param>
 public BaseResultsParserContext(ISparqlResultsHandler handler)
     : this(handler, false) { }
Ejemplo n.º 13
0
 /// <summary>
 /// Creates a new Parser Context
 /// </summary>
 /// <param name="handler">Results Handler</param>
 /// <param name="traceParsing">Whether to trace parsing</param>
 public BaseResultsParserContext(ISparqlResultsHandler handler, bool traceParsing)
 {
     if (handler == null) throw new ArgumentNullException("handler");
     this._handler = handler;
     this._traceParsing = traceParsing;
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Creates a new Tokenising Parser Context with custom settings
 /// </summary>
 /// <param name="handler">Results Handler</param>
 /// <param name="tokeniser">Tokeniser to use</param>
 /// <param name="queueMode">Tokeniser Queue Mode</param>
 /// <param name="traceParsing">Whether to trace parsing</param>
 /// <param name="traceTokeniser">Whether to trace tokenisation</param>
 public TokenisingResultParserContext(ISparqlResultsHandler handler, ITokeniser tokeniser, TokenQueueMode queueMode, bool traceParsing, bool traceTokeniser)
     : base(handler, traceParsing)
 {
     switch (queueMode)
     {
         case TokenQueueMode.AsynchronousBufferDuringParsing:
             this._queue = new AsynchronousBufferedTokenQueue(tokeniser);
             break;
         case TokenQueueMode.SynchronousBufferDuringParsing:
             this._queue = new BufferedTokenQueue(tokeniser);
             break;
         case TokenQueueMode.QueueAllBeforeParsing:
         default:
             this._queue = new TokenQueue(tokeniser);
             break;
     }
     this._traceTokeniser = traceTokeniser;
     this._queue.Tracing = this._traceTokeniser;
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Creates a new Tokenising Parser Context with custom settings
 /// </summary>
 /// <param name="handler">Results Handler</param>
 /// <param name="tokeniser">Tokeniser to use</param>
 /// <param name="traceParsing">Whether to trace parsing</param>
 /// <param name="traceTokeniser">Whether to trace tokenisation</param>
 public TokenisingResultParserContext(ISparqlResultsHandler handler, ITokeniser tokeniser, bool traceParsing, bool traceTokeniser)
     : this(handler, tokeniser)
 {
     this._traceParsing = traceParsing;
     this._traceTokeniser = traceTokeniser;
     this._queue.Tracing = this._traceTokeniser;
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Processes a SPARQL Query passing the results to the RDF or Results handler as appropriate
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="query">SPARQL Query</param>
 public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query)
 {
     query.QueryTime = -1;
     query.QueryTimeTicks = -1;
     query.QueryExecutionTime = null;
     DateTime start = DateTime.Now;
     try
     {
         this._svc.Query(rdfHandler, resultsHandler, query.ToString());
     }
     finally
     {
         TimeSpan elapsed = (DateTime.Now - start);
         query.QueryExecutionTime = (DateTime.Now - start);
         query.QueryTime = elapsed.Milliseconds;
         query.QueryTimeTicks = elapsed.Ticks;
     }
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Loads a Result Set from an Input Stream using a Results Handler
 /// </summary>
 /// <param name="handler">Results Handler to use</param>
 /// <param name="input">Input Stream to read from</param>
 public void Load(ISparqlResultsHandler handler, StreamReader input)
 {
     this.Load(handler, (TextReader)input);
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Processes a SPARQL Query passing the results to the RDF or Results handler as appropriate
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="query">SPARQL Query</param>
 public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query)
 {
     query.QueryExecutionTime = null;
     DateTime start = DateTime.Now;
     try
     {
         this._store.ExecuteQuery(rdfHandler, resultsHandler, this._formatter.Format(query));
     }
     finally
     {
         TimeSpan elapsed = (DateTime.Now - start);
         query.QueryExecutionTime = elapsed;
     }
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Loads a Result Set from an Input using a Results Handler
        /// </summary>
        /// <param name="handler">Results Handler to use</param>
        /// <param name="input">Input to read from</param>
        public void Load(ISparqlResultsHandler handler, TextReader input)
        {
            if (handler == null) throw new RdfParseException("Cannot read SPARQL Results using a null Results Handler");
            if (input == null) throw new RdfParseException("Cannot read SPARQL Results from a null Input");

            try
            {
                this.Parse(input, handler);
            }
            catch
            {
                throw;
            }
            finally
            {
                try
                {
                    input.Close();
                }
                catch
                {
                    //No catch actions - just trying to cleanup
                }
            }
        }
Ejemplo n.º 20
0
 /// <summary>
 /// Processes a SPARQL Query asynchronously passing the results to the relevant handler and invoking the callback when the query completes
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="query">SPARQL Query</param>
 /// <param name="callback">Callback</param>
 /// <param name="state">State to pass to the callback</param>
 public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query, QueryCallback callback, Object state)
 {
     ProcessQueryAsync d = new ProcessQueryAsync(this.ProcessQuery);
     d.BeginInvoke(rdfHandler, resultsHandler, query, r =>
     {
         d.EndInvoke(r);
         callback(rdfHandler, resultsHandler, state);
     }, state);
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Makes a query against the in-memory copy of the Stores data processing the results with one of the given handlers
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="sparqlQuery">SPARQL Query</param>
 public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery)
 {
     this._store.ExecuteQuery(rdfHandler, resultsHandler, sparqlQuery);
 }
Ejemplo n.º 22
0
 public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query)
 {
     this._underlyingProcessor.ProcessQuery(rdfHandler, resultsHandler, query);
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Executes a SPARQL Query on the Triple Store processing the results with an appropriate handler from those provided
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="query">SPARQL Query as unparsed String</param>
 public virtual void ExecuteQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query)
 {
     query.Evaluate(rdfHandler, resultsHandler, this);
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Provides the required SPARQL query interface for the <see cref="BrightstarIOManager"/> used for SPARQL update support
 /// </summary>
 /// <param name="rdfHandler"></param>
 /// <param name="resultsHandler"></param>
 /// <param name="sparqlQuery"></param>
 /// <param name="store"></param>
 public void ExecuteSparql(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, string sparqlQuery, IStore store)
 {
     try
     {
         var query = ParseSparql(sparqlQuery);
         var dataset = MakeDataset(store);
         if (_defaultGraphUris != null)
         {
             dataset.SetDefaultGraph(_defaultGraphUris);
         }
         var queryProcessor = new BrightstarQueryProcessor(store, dataset);
         queryProcessor.ProcessQuery(rdfHandler, resultsHandler, query);
     }
     catch (Exception ex)
     {
         Logging.LogError(BrightstarEventId.SparqlExecutionError,
                          "Error Executing Sparql {0}. Cause: {1}",
                          sparqlQuery, ex);
         throw;
     }
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Processes a SPARQL Query asynchronously passing the results to the relevant handler and invoking the callback when the query completes
 /// </summary>
 /// <param name="rdfHandler">RDF Handler</param>
 /// <param name="resultsHandler">Results Handler</param>
 /// <param name="query">SPARQL Query</param>
 /// <param name="callback">Callback</param>
 /// <param name="state">State to pass to the callback</param>
 public void ProcessQuery(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, SparqlQuery query, QueryCallback callback, Object state)
 {
     query.QueryExecutionTime = null;
     DateTime start = DateTime.Now;
     try
     {
         this._svc.Query(rdfHandler, resultsHandler, query.ToString(), callback, state);
     }
     finally
     {
         TimeSpan elapsed = (DateTime.Now - start);
         query.QueryExecutionTime = (DateTime.Now - start);
     }
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Makes a SPARQL Query against the underlying Store processing the results with an appropriate handler from those provided
        /// </summary>
        /// <param name="rdfHandler">RDF Handler</param>
        /// <param name="resultsHandler">Results Handler</param>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <returns></returns>
        public virtual void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery)
        {
            try
            {
                //Pre-parse the query to determine what the Query Type is
                bool isAsk = false;
                SparqlQuery q = null;
                try
                {
                    q = this._parser.ParseFromString(sparqlQuery);
                    isAsk = q.QueryType == SparqlQueryType.Ask;
                }
                catch
                {
                    //If parsing error fallback to naive detection
                    isAsk = Regex.IsMatch(sparqlQuery, "ASK", RegexOptions.IgnoreCase);
                }

                //Select Accept Header
                String accept;
                if (q != null)
                {
                    accept = (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask ? MimeTypesHelper.HttpSparqlAcceptHeader : MimeTypesHelper.HttpAcceptHeader);
                }
                else
                {
                    accept = MimeTypesHelper.HttpRdfOrSparqlAcceptHeader;
                }

                HttpWebRequest request;

                //Create the Request
                Dictionary<String, String> queryParams = new Dictionary<string, string>();
                if (sparqlQuery.Length < 2048 && !this._postAllQueries)
                {
                    queryParams.Add("query", EscapeQuery(sparqlQuery));

                    request = this.CreateRequest(this._repositoriesPrefix + this._store + this._queryPath, accept, "GET", queryParams);
                }
                else
                {
                    request = this.CreateRequest(this._repositoriesPrefix + this._store + this._queryPath, accept, "POST", queryParams);

                    //Build the Post Data and add to the Request Body
                    request.ContentType = MimeTypesHelper.WWWFormURLEncoded;
                    StringBuilder postData = new StringBuilder();
                    postData.Append("query=");
                    postData.Append(Uri.EscapeDataString(EscapeQuery(sparqlQuery)));
                    StreamWriter writer = new StreamWriter(request.GetRequestStream());
                    writer.Write(postData);
                    writer.Close();
                }

            #if DEBUG
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugRequest(request);
                }
            #endif

                //Get the Response and process based on the Content Type
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
            #if DEBUG
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse(response);
                    }
            #endif
                    StreamReader data = new StreamReader(response.GetResponseStream());
                    String ctype = response.ContentType;
                    try
                    {
                        //Is the Content Type referring to a Sparql Result Set format?
                        ISparqlResultsReader resreader = MimeTypesHelper.GetSparqlParser(ctype, isAsk);
                        resreader.Load(resultsHandler, data);
                        response.Close();
                    }
                    catch (RdfParserSelectionException)
                    {
                        //If we get a Parser Selection exception then the Content Type isn't valid for a Sparql Result Set

                        //Is the Content Type referring to a RDF format?
                        IRdfReader rdfreader = MimeTypesHelper.GetParser(ctype);
                        if (q != null && (SparqlSpecsHelper.IsSelectQuery(q.QueryType) || q.QueryType == SparqlQueryType.Ask))
                        {
                            SparqlRdfParser resreader = new SparqlRdfParser(rdfreader);
                            resreader.Load(resultsHandler, data);
                        }
                        else
                        {
                            rdfreader.Load(rdfHandler, data);
                        }
                        response.Close();
                    }
                }
            }
            catch (WebException webEx)
            {
                if (webEx.Response != null)
                {
            #if DEBUG
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                    }
            #endif
                    if (webEx.Response.ContentLength > 0)
                    {
                        try
                        {
                            String responseText = new StreamReader(webEx.Response.GetResponseStream()).ReadToEnd();
                            throw new RdfQueryException("A HTTP error occured while querying the Store.  Store returned the following error message: " + responseText, webEx);
                        }
                        catch
                        {
                            throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                        }
                    }
                    else
                    {
                        throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                    }
                }
                else
                {
                    throw new RdfQueryException("A HTTP error occurred while querying the Store", webEx);
                }
            }
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Loads a Result Set from an Input Stream using a Results Handler
        /// </summary>
        /// <param name="handler">Results Handler to use</param>
        /// <param name="input">Input Stream to read from</param>
        public void Load(ISparqlResultsHandler handler, StreamReader input)
        {
            if (input == null) throw new RdfParseException("Cannot parser SPARQL Results from a null input stream");

            //Check Encoding
            if (input.CurrentEncoding != Encoding.UTF8)
            {
            #if !SILVERLIGHT
                this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + input.CurrentEncoding.EncodingName + " - Please be aware that parsing errors may occur as a result");
            #else
                this.RaiseWarning("Expected Input Stream to be encoded as UTF-8 but got a Stream encoded as " + input.CurrentEncoding.GetType().Name + " - Please be aware that parsing errors may occur as a result");
            #endif
            }

            this.Load(handler, (TextReader)input);
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Processes a SPARQL Query against the Knowledge Base passing the results to the RDF or Results handler as appropriate
        /// </summary>
        /// <param name="rdfHandler">RDF Handler</param>
        /// <param name="resultsHandler">Results Handler</param>
        /// <param name="sparqlQuery">SPARQL Query</param>
        public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery)
        {
            SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(UriFactory.Create(this._sparqlUri));

            using (HttpWebResponse response = endpoint.QueryRaw(sparqlQuery))
            {
                try
                {
                    ISparqlResultsReader sparqlParser = MimeTypesHelper.GetSparqlParser(response.ContentType);
                    sparqlParser.Load(resultsHandler, new StreamReader(response.GetResponseStream()));
                }
                catch (RdfParserSelectionException)
                {
                    IRdfReader parser = MimeTypesHelper.GetParser(response.ContentType);
                    parser.Load(rdfHandler, new StreamReader(response.GetResponseStream()));
                }
                response.Close();
            }
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Loads a Result Set from an Input using a Results Handler
        /// </summary>
        /// <param name="handler">Results Handler to use</param>
        /// <param name="input">Input to read from</param>
        public void Load(ISparqlResultsHandler handler, TextReader input)
        {
            if (handler == null) throw new RdfParseException("Cannot parse SPARQL Results into a null Result Handler");
            if (input == null) throw new RdfParseException("Cannot parse SPARQL Results from a null input stream");

            try
            {
                TokenisingResultParserContext context = new TokenisingResultParserContext(handler, new CsvTokeniser(BlockingTextReader.Create(input)));
                this.TryParseResults(context);
                input.Close();
            }
            catch
            {
                try
                {
                    input.Close();
                }
                catch
                {
                    //No catch actions just trying to clean up
                }
                throw;
            }
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Processes a SPARQL Query against the Knowledge Base passing the results to the RDF or Results handler as appropriate
        /// </summary>
        /// <param name="rdfHandler">RDF Handler</param>
        /// <param name="resultsHandler">Results Handler</param>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <param name="callback">Callback to invoke once handling of results has completed</param>
        /// <param name="state">State to pass to the callback</param>
        public void Query(IRdfHandler rdfHandler, ISparqlResultsHandler resultsHandler, String sparqlQuery, QueryCallback callback, Object state)
        {
            SparqlQueryParser parser = new SparqlQueryParser();
            SparqlQuery q = parser.ParseFromString(sparqlQuery);

            SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(UriFactory.Create(this._sparqlUri));
            switch (q.QueryType)
            {
                case SparqlQueryType.Ask:
                case SparqlQueryType.Select:
                case SparqlQueryType.SelectAll:
                case SparqlQueryType.SelectAllDistinct:
                case SparqlQueryType.SelectAllReduced:
                case SparqlQueryType.SelectDistinct:
                case SparqlQueryType.SelectReduced:
                    endpoint.QueryWithResultSet(sparqlQuery, (rs, _) =>
                        {
                            resultsHandler.Apply(rs);
                            callback(rdfHandler, resultsHandler, state);
                        }, state);
                    break;
                case SparqlQueryType.Construct:
                case SparqlQueryType.Describe:
                case SparqlQueryType.DescribeAll:
                    endpoint.QueryWithResultGraph(sparqlQuery, (g, _) =>
                        {
                            rdfHandler.Apply(g);
                            callback(rdfHandler, resultsHandler, state);
                        }, state);
                    break;

                default:
                    throw new RdfQueryException("Cannot execute unknown query types against Pellet Server");
            }
        }