Пример #1
0
        /// <summary>
        /// Gets the list of Search Results which match the given search term
        /// </summary>
        /// <param name="text">Search Term</param>
        /// <param name="callback">Callback to invoke when the operation completes</param>
        /// <param name="state">State to pass to the callback</param>
        public void Search(String text, PelletSearchServiceCallback callback, Object state)
        {
            String search = this._searchUri + "?search=" + Uri.EscapeDataString(text);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);

            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = "text/json";

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

            String jsonText;
            JArray json;
            request.BeginGetResponse(result =>
            {
                using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                {
#if DEBUG
                    if (Options.HttpDebugging)
                    {
                        Tools.HttpDebugResponse(response);
                    }
#endif
                    jsonText = new StreamReader(response.GetResponseStream()).ReadToEnd();
                    json     = JArray.Parse(jsonText);

                    response.Close();

                    //Parse the Response into Search Results

                    List <SearchServiceResult> results = new List <SearchServiceResult>();

                    foreach (JToken res in json.Children())
                    {
                        JToken hit  = res.SelectToken("hit");
                        String type = (String)hit.SelectToken("type");
                        INode node;
                        if (type.ToLower().Equals("uri"))
                        {
                            node = new UriNode(null, UriFactory.Create((String)hit.SelectToken("value")));
                        }
                        else
                        {
                            node = new BlankNode(null, (String)hit.SelectToken("value"));
                        }
                        double score = (double)res.SelectToken("score");

                        results.Add(new SearchServiceResult(node, score));
                    }

                    callback(results, state);
                }
            }, null);
        }
Пример #2
0
        /// <summary>
        /// Gets the list of Search Results which match the given search term
        /// </summary>
        /// <param name="text">Search Term</param>
        /// <param name="callback">Callback to invoke when the operation completes</param>
        /// <param name="state">State to pass to the callback</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 Search(String text, PelletSearchServiceCallback callback, Object state)
        {
            String search = this._searchUri + "?search=" + HttpUtility.UrlEncode(text);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);

            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = "text/json";

            Tools.HttpDebugRequest(request);

            try
            {
                String jsonText;
                JArray json;
                request.BeginGetResponse(result =>
                {
                    try
                    {
                        using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                        {
                            Tools.HttpDebugResponse(response);
                            jsonText = new StreamReader(response.GetResponseStream()).ReadToEnd();
                            json     = JArray.Parse(jsonText);

                            response.Close();

                            // Parse the Response into Search Results

                            List <SearchServiceResult> results = new List <SearchServiceResult>();

                            foreach (JToken res in json.Children())
                            {
                                JToken hit  = res.SelectToken("hit");
                                String type = (String)hit.SelectToken("type");
                                INode node;
                                if (type.ToLower().Equals("uri"))
                                {
                                    node = new UriNode(null, UriFactory.Create((String)hit.SelectToken("value")));
                                }
                                else
                                {
                                    node = new BlankNode(null, (String)hit.SelectToken("value"));
                                }
                                double score = (double)res.SelectToken("score");

                                results.Add(new SearchServiceResult(node, score));
                            }

                            callback(results, state);
                        }
                    }
                    catch (WebException webEx)
                    {
                        if (webEx.Response != null)
                        {
                            Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                        }
                        callback(null, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state));
                    }
                    catch (Exception ex)
                    {
                        callback(null, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state));
                    }
                }, null);
            }
            catch (WebException webEx)
            {
                if (webEx.Response != null)
                {
                    Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                }
                callback(null, new AsyncError(new RdfReasoningException("A HTTP error occurred while communicating with the Pellet Server, see inner exception for details", webEx), state));
            }
            catch (Exception ex)
            {
                callback(null, new AsyncError(new RdfReasoningException("An unexpected error occurred while communicating with the Pellet Server, see inner exception for details", ex), state));
            }
        }
Пример #3
0
        /// <summary>
        /// Gets the list of Search Results which match the given search term
        /// </summary>
        /// <param name="text">Search Term</param>
        /// <param name="callback">Callback to invoke when the operation completes</param>
        /// <param name="state">State to pass to the callback</param>
        public void Search(String text, PelletSearchServiceCallback callback, Object state)
        {
            String search = this._searchUri + "?search=" + Uri.EscapeDataString(text);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = "text/json";

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

            String jsonText;
            JArray json;
            request.BeginGetResponse(result =>
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                    {
#if DEBUG
                        if (Options.HttpDebugging)
                        {
                            Tools.HttpDebugResponse(response);
                        }
#endif
                        jsonText = new StreamReader(response.GetResponseStream()).ReadToEnd();
                        json = JArray.Parse(jsonText);

                        response.Close();

                        //Parse the Response into Search Results

                        List<SearchServiceResult> results = new List<SearchServiceResult>();

                        foreach (JToken res in json.Children())
                        {
                            JToken hit = res.SelectToken("hit");
                            String type = (String)hit.SelectToken("type");
                            INode node;
                            if (type.ToLower().Equals("uri"))
                            {
                                node = new UriNode(null, new Uri((String)hit.SelectToken("value")));
                            }
                            else
                            {
                                node = new BlankNode(null, (String)hit.SelectToken("value"));
                            }
                            double score = (double)res.SelectToken("score");

                            results.Add(new SearchServiceResult(node, score));
                        }

                        callback(results, state);
                    }
                }, null);
        }