예제 #1
0
        /// <summary>
        /// Processes a SPARQL Query asynchronously invoking the relevant callback when the query completes.
        /// </summary>
        /// <param name="query">SPARQL QUery.</param>
        /// <param name="rdfCallback">Callback for queries that return a Graph.</param>
        /// <param name="resultsCallback">Callback for queries that return a Result Set.</param>
        /// <param name="state">State to pass to the callback.</param>
        /// <remarks>
        /// In the event of a success the appropriate callback will be invoked, if there is an error both callbacks will be invoked and passed an instance of <see cref="AsyncError"/> which contains details of the error and the original state information passed in.
        /// </remarks>
        public void ProcessQuery(SparqlQuery query, GraphCallback rdfCallback, SparqlResultsCallback resultsCallback, object state)
        {
            var resultGraph = new Graph();
            var resultSet   = new SparqlResultSet();

            Task.Factory.StartNew(() =>
                                  ProcessQuery(new GraphHandler(resultGraph), new ResultSetHandler(resultSet), query)).ContinueWith(
                antecedent =>
            {
                if (antecedent.Exception != null)
                {
                    var innerException = antecedent.Exception.InnerExceptions[0];
                    var queryException = innerException as RdfQueryException ??
                                         new RdfQueryException(
                        "Unexpected error while making an asynchronous query, see inner exception for details",
                        innerException);
                    rdfCallback?.Invoke(null, new AsyncError(queryException, state));
                    resultsCallback?.Invoke(null, new AsyncError(queryException, state));
                }
                else if (resultSet.ResultsType != SparqlResultsType.Unknown)
                {
                    resultsCallback?.Invoke(resultSet, state);
                }
                else
                {
                    rdfCallback?.Invoke(resultGraph, state);
                }
            });
        }
예제 #2
0
        /// <summary>
        /// Makes a SPARQL Query against the Knowledge Base
        /// </summary>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <param name="graphCallback">Callback to invoke for queries that return a Graph</param>
        /// <param name="resultsCallback">Callback to invoke for queries that return a Result Set</param>
        /// <param name="state">State to pass to whichever callback function is invoked</param>
        /// <remarks>
        /// If the operation succeeds the callback will be invoked normally, if there is an error the callback will be invoked with a instance of <see cref="AsyncError"/> passed as the state which provides access to the error message and the original state passed in.
        /// </remarks>
        public void Query(String sparqlQuery, GraphCallback graphCallback, SparqlResultsCallback resultsCallback, Object state)
        {
            Graph           g       = new Graph();
            SparqlResultSet results = new SparqlResultSet();

            this.Query(new GraphHandler(g), new ResultSetHandler(results), sparqlQuery, (gh, rh, s) =>
            {
                if (s is AsyncError)
                {
                    if (graphCallback != null)
                    {
                        graphCallback(null, s);
                    }
                    if (resultsCallback != null)
                    {
                        resultsCallback(null, s);
                    }
                }
                else
                {
                    if (results.ResultsType != SparqlResultsType.Unknown)
                    {
                        resultsCallback(results, state);
                    }
                    else
                    {
                        graphCallback(g, state);
                    }
                }
            }, state);
        }
예제 #3
0
        /// <summary>
        /// Processes a SPARQL Query asynchronously invoking the relevant callback when the query completes
        /// </summary>
        /// <param name="query">SPARQL QUery</param>
        /// <param name="rdfCallback">Callback for queries that return a Graph</param>
        /// <param name="resultsCallback">Callback for queries that return a Result Set</param>
        /// <param name="state">State to pass to the callback</param>
        /// <remarks>
        /// In the event of a success the appropriate callback will be invoked, if there is an error both callbacks will be invoked and passed an instance of <see cref="AsyncError"/> which contains details of the error and the original state information passed in.
        /// </remarks>
        public void ProcessQuery(SparqlQuery query, GraphCallback rdfCallback, SparqlResultsCallback resultsCallback, 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:
                    _endpoint.QueryWithResultSet(_formatter.Format(query), resultsCallback, state);
                    break;

                case SparqlQueryType.Construct:
                case SparqlQueryType.Describe:
                case SparqlQueryType.DescribeAll:
                    _endpoint.QueryWithResultGraph(_formatter.Format(query), rdfCallback, 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;
            }
        }
예제 #4
0
        /// <summary>
        /// Makes a Query asynchronously where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries
        /// </summary>
        /// <param name="query">SPARQL Query String</param>
        /// <param name="callback">Callback to invoke when the query completes</param>
        /// <param name="state">State to pass to the callback</param>
        public void QueryWithResultSet(String query, SparqlResultsCallback callback, Object state)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Uri);

            request.Method      = "POST";
            request.ContentType = MimeTypesHelper.WWWFormURLEncoded;
            request.Accept      = MimeTypesHelper.HttpSparqlAcceptHeader;

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

            request.BeginGetRequestStream(result =>
            {
                Stream stream = request.EndGetRequestStream(result);
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.Write("query=");
                    writer.Write(HttpUtility.UrlEncode(query));

                    foreach (String u in this.DefaultGraphs)
                    {
                        writer.Write("&default-graph-uri=");
                        writer.Write(Uri.EscapeDataString(u));
                    }
                    foreach (String u in this.NamedGraphs)
                    {
                        writer.Write("&named-graph-uri=");
                        writer.Write(Uri.EscapeDataString(u));
                    }

                    writer.Close();
                }

                request.BeginGetResponse(innerResult =>
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(innerResult))
                    {
#if DEBUG
                        if (Options.HttpDebugging)
                        {
                            Tools.HttpDebugResponse(response);
                        }
#endif
                        ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType, false);
                        SparqlResultSet rset        = new SparqlResultSet();
                        parser.Load(rset, new StreamReader(response.GetResponseStream()));

                        response.Close();
                        callback(rset, state);
                    }
                }, null);
            }, null);
        }
예제 #5
0
        /// <summary>
        /// Processes a SPARQL Query asynchronously invoking the relevant callback when the query completes
        /// </summary>
        /// <param name="query">SPARQL QUery</param>
        /// <param name="rdfCallback">Callback for queries that return a Graph</param>
        /// <param name="resultsCallback">Callback for queries that return a Result Set</param>
        /// <param name="state">State to pass to the callback</param>
        public void ProcessQuery(SparqlQuery query, GraphCallback rdfCallback, SparqlResultsCallback resultsCallback, Object state)
        {
            query.QueryExecutionTime = null;
            DateTime start = DateTime.Now;

            try
            {
                this._svc.Query(query.ToString(), rdfCallback, resultsCallback, state);
            }
            finally
            {
                TimeSpan elapsed = (DateTime.Now - start);
                query.QueryExecutionTime = (DateTime.Now - start);
            }
        }
예제 #6
0
        /// <summary>
        /// Processes a SPARQL Query asynchronously invoking the relevant callback when the query completes
        /// </summary>
        /// <param name="query">SPARQL QUery</param>
        /// <param name="rdfCallback">Callback for queries that return a Graph</param>
        /// <param name="resultsCallback">Callback for queries that return a Result Set</param>
        /// <param name="state">State to pass to the callback</param>
        /// <remarks>
        /// In the event of a success the appropriate callback will be invoked, if there is an error both callbacks will be invoked and passed an instance of <see cref="AsyncError"/> which contains details of the error and the original state information passed in.
        /// </remarks>
        public void ProcessQuery(SparqlQuery query, GraphCallback rdfCallback, SparqlResultsCallback resultsCallback, Object state)
        {
            Graph             g    = new Graph();
            SparqlResultSet   rset = new SparqlResultSet();
            ProcessQueryAsync d    = new ProcessQueryAsync(this.ProcessQuery);

            d.BeginInvoke(new GraphHandler(g), new ResultSetHandler(rset), query, r =>
            {
                try
                {
                    d.EndInvoke(r);
                    if (rset.ResultsType != SparqlResultsType.Unknown)
                    {
                        resultsCallback(rset, state);
                    }
                    else
                    {
                        rdfCallback(g, state);
                    }
                }
                catch (RdfQueryException queryEx)
                {
                    if (rdfCallback != null)
                    {
                        rdfCallback(null, new AsyncError(queryEx, state));
                    }
                    if (resultsCallback != null)
                    {
                        resultsCallback(null, new AsyncError(queryEx, state));
                    }
                }
                catch (Exception ex)
                {
                    RdfQueryException queryEx = new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex);
                    if (rdfCallback != null)
                    {
                        rdfCallback(null, new AsyncError(queryEx, state));
                    }
                    if (resultsCallback != null)
                    {
                        resultsCallback(null, new AsyncError(queryEx, state));
                    }
                }
            }, state);
        }
예제 #7
0
        /// <summary>
        /// Processes a SPARQL Query asynchronously invoking the relevant callback when the query completes
        /// </summary>
        /// <param name="query">SPARQL QUery</param>
        /// <param name="rdfCallback">Callback for queries that return a Graph</param>
        /// <param name="resultsCallback">Callback for queries that return a Result Set</param>
        /// <param name="state">State to pass to the callback</param>
        public void ProcessQuery(SparqlQuery query, GraphCallback rdfCallback, SparqlResultsCallback resultsCallback, Object state)
        {
            Graph             g    = new Graph();
            SparqlResultSet   rset = new SparqlResultSet();
            ProcessQueryAsync d    = new ProcessQueryAsync(this.ProcessQuery);

            d.BeginInvoke(new GraphHandler(g), new ResultSetHandler(rset), query, r =>
            {
                d.EndInvoke(r);
                if (rset.ResultsType != SparqlResultsType.Unknown)
                {
                    resultsCallback(rset, state);
                }
                else
                {
                    rdfCallback(g, state);
                }
            }, state);
        }
예제 #8
0
 /// <summary>
 /// Processes a SPARQL Query asynchronously invoking the relevant callback when the query completes
 /// </summary>
 /// <param name="query">SPARQL QUery</param>
 /// <param name="rdfCallback">Callback for queries that return a Graph</param>
 /// <param name="resultsCallback">Callback for queries that return a Result Set</param>
 /// <param name="state">State to pass to the callback</param>
 public void ProcessQuery(SparqlQuery query, GraphCallback rdfCallback, SparqlResultsCallback resultsCallback, Object state)
 {
     query.QueryExecutionTime = null;
     DateTime start = DateTime.Now;
     try
     {
         this._svc.Query(query.ToString(), rdfCallback, resultsCallback, state);
     }
     finally
     {
         TimeSpan elapsed = (DateTime.Now - start);
         query.QueryExecutionTime = (DateTime.Now - start);
     }
 }
예제 #9
0
        /// <summary>
        /// Makes a Query asynchronously where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries.
        /// </summary>
        /// <param name="query">SPARQL Query String.</param>
        /// <param name="callback">Callback to invoke when the query completes.</param>
        /// <param name="state">State to pass to the callback.</param>
        public void QueryWithResultSet(String query, SparqlResultsCallback callback, Object state)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri);

            request.Method      = "POST";
            request.ContentType = MimeTypesHelper.Utf8WWWFormURLEncoded;
            request.Accept      = ResultsAcceptHeader;
            ApplyRequestOptions(request);
            Tools.HttpDebugRequest(request);

            try
            {
                request.BeginGetRequestStream(result =>
                {
                    try
                    {
                        Stream stream = request.EndGetRequestStream(result);
                        using (StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(Options.UseBomForUtf8)))
                        {
                            writer.Write("query=");
                            writer.Write(HttpUtility.UrlEncode(query));

                            foreach (String u in DefaultGraphs)
                            {
                                writer.Write("&default-graph-uri=");
                                writer.Write(HttpUtility.UrlEncode(u));
                            }
                            foreach (String u in NamedGraphs)
                            {
                                writer.Write("&named-graph-uri=");
                                writer.Write(HttpUtility.UrlEncode(u));
                            }

                            writer.Close();
                        }

                        request.BeginGetResponse(innerResult =>
                        {
                            try
                            {
                                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(innerResult))
                                {
                                    Tools.HttpDebugResponse(response);

                                    ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType, false);
                                    SparqlResultSet rset        = new SparqlResultSet();
                                    parser.Load(rset, new StreamReader(response.GetResponseStream()));

                                    response.Close();
                                    callback(rset, state);
                                }
                            }
                            catch (SecurityException secEx)
                            {
                                callback(null, new AsyncError(new RdfQueryException("Calling code does not have permission to access the specified remote endpoint, see inner exception for details", secEx), state));
                            }
                            catch (WebException webEx)
                            {
                                if (webEx.Response != null)
                                {
                                    Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                                }
                                callback(null, new AsyncError(new RdfQueryException("A HTTP error occurred while making an asynchronous query, see inner exception for details", webEx), state));
                            }
                            catch (Exception ex)
                            {
                                callback(null, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state));
                            }
                        }, null);
                    }
                    catch (SecurityException secEx)
                    {
                        callback(null, new AsyncError(new RdfQueryException("Calling code does not have permission to access the specified remote endpoint, see inner exception for details", secEx), state));
                    }
                    catch (WebException webEx)
                    {
                        if (webEx.Response != null)
                        {
                            Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                        }
                        callback(null, new AsyncError(new RdfQueryException("A HTTP error occurred while making an asynchronous query, see inner exception for details", webEx), state));
                    }
                    catch (Exception ex)
                    {
                        callback(null, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state));
                    }
                }, null);
            }
            catch (Exception ex)
            {
                callback(null, new AsyncError(new RdfQueryException("Unexpected error while making an asynchronous query, see inner exception for details", ex), state));
            }
        }
예제 #10
0
        /// <summary>
        /// Makes a SPARQL Query against the Knowledge Base
        /// </summary>
        /// <param name="sparqlQuery">SPARQL Query</param>
        /// <param name="graphCallback">Callback to invoke for queries that return a Graph</param>
        /// <param name="resultsCallback">Callback to invoke for queries that return a Result Set</param>
        /// <param name="state">State to pass to whichever callback function is invoked</param>
        /// <returns></returns>
        public void Query(String sparqlQuery, GraphCallback graphCallback, SparqlResultsCallback resultsCallback, Object state)
        {
            Graph g = new Graph();
            SparqlResultSet results = new SparqlResultSet();

            this.Query(new GraphHandler(g), new ResultSetHandler(results), sparqlQuery, (gh, rh, _) =>
                {
                    if (results.ResultsType != SparqlResultsType.Unknown)
                    {
                        resultsCallback(results, state);
                    }
                    else
                    {
                        graphCallback(g, state);
                    }
                }, state);
        }
예제 #11
0
 /// <summary>
 /// Processes a SPARQL Query asynchronously invoking the relevant callback when the query completes
 /// </summary>
 /// <param name="query">SPARQL QUery</param>
 /// <param name="rdfCallback">Callback for queries that return a Graph</param>
 /// <param name="resultsCallback">Callback for queries that return a Result Set</param>
 /// <param name="state">State to pass to the callback</param>
 public void ProcessQuery(SparqlQuery query, GraphCallback rdfCallback, SparqlResultsCallback resultsCallback, Object state)
 {
     Graph g = new Graph();
     SparqlResultSet rset = new SparqlResultSet();
     ProcessQueryAsync d = new ProcessQueryAsync(this.ProcessQuery);
     d.BeginInvoke(new GraphHandler(g), new ResultSetHandler(rset), query, r =>
         {
             d.EndInvoke(r);
             if (rset.ResultsType != SparqlResultsType.Unknown)
             {
                 resultsCallback(rset, state);
             }
             else
             {
                 rdfCallback(g, state);
             }
         }, state);
 }
예제 #12
0
 /// <summary>
 /// Processes a SPARQL Query asynchronously invoking the relevant callback when the query completes
 /// </summary>
 /// <param name="query">SPARQL QUery</param>
 /// <param name="rdfCallback">Callback for queries that return a Graph</param>
 /// <param name="resultsCallback">Callback for queries that return a Result Set</param>
 /// <param name="state">State to pass to the callback</param>
 public void ProcessQuery(SparqlQuery query, GraphCallback rdfCallback, SparqlResultsCallback resultsCallback, 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(this._formatter.Format(query), resultsCallback, state);
                 break;
             case SparqlQueryType.Construct:
             case SparqlQueryType.Describe:
             case SparqlQueryType.DescribeAll:
                 this._endpoint.QueryWithResultGraph(this._formatter.Format(query), rdfCallback, 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;
     }
 }
예제 #13
0
        /// <summary>
        /// Makes a Query asynchronously where the expected Result is a <see cref="SparqlResultSet">SparqlResultSet</see> i.e. SELECT and ASK Queries
        /// </summary>
        /// <param name="query">SPARQL Query String</param>
        /// <param name="callback">Callback to invoke when the query completes</param>
        /// <param name="state">State to pass to the callback</param>
        public void QueryWithResultSet(String query, SparqlResultsCallback callback, Object state)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.Uri);
            request.Method = "POST";
            request.ContentType = MimeTypesHelper.WWWFormURLEncoded;
            request.Accept = MimeTypesHelper.HttpSparqlAcceptHeader;

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

            request.BeginGetRequestStream(result =>
            {
                Stream stream = request.EndGetRequestStream(result);
                using (StreamWriter writer = new StreamWriter(stream))
                {
                    writer.Write("query=");
                    writer.Write(HttpUtility.UrlEncode(query));

                    foreach (String u in this.DefaultGraphs)
                    {
                        writer.Write("&default-graph-uri=");
                        writer.Write(Uri.EscapeDataString(u));
                    }
                    foreach (String u in this.NamedGraphs)
                    {
                        writer.Write("&named-graph-uri=");
                        writer.Write(Uri.EscapeDataString(u));
                    }

                    writer.Close();
                }

                request.BeginGetResponse(innerResult =>
                    {
                        using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(innerResult))
                        {
            #if DEBUG
                            if (Options.HttpDebugging)
                            {
                                Tools.HttpDebugResponse(response);
                            }
            #endif
                            ISparqlResultsReader parser = MimeTypesHelper.GetSparqlParser(response.ContentType, false);
                            SparqlResultSet rset = new SparqlResultSet();
                            parser.Load(rset, new StreamReader(response.GetResponseStream()));

                            response.Close();
                            callback(rset, state);
                        }
                    }, null);
            }, null);
        }