Beispiel #1
0
        private void resolveAsynchronousData(MemoryStore output, string uri)
        {
            while (uri != null)
            {
                //SADIHelper.debug("SADIService", "fetching asynchronous data", uri);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method            = "GET";
                request.AllowAutoRedirect = false;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.Redirect)
                {
                    String newURL = response.Headers["Location"];
                    if (newURL != null)
                    {
                        uri = newURL;
                    }

                    int    toSleep = 5;
                    String retry   = response.Headers["Retry-After"];
                    try
                    {
                        if (retry != null)
                        {
                            toSleep = Int16.Parse(retry);
                        }
                    }
                    catch (Exception e)
                    {
                        SADIHelper.error("SADIService", "failed to parse Retry-After header", retry, e);
                    }
                    //SADIHelper.debug("SADIService", "sleeping " + toSleep + "s before asynchronous request", null);
                    System.Threading.Thread.Sleep(toSleep * 1000);
                }
                else
                {
                    Stream       stream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream);
                    using (RdfReader rdfReader = new RdfXmlReader(reader))
                    {
                        output.Import(rdfReader);
                    }
                    reader.Close();
                    stream.Close();
                    uri = null;
                }
                response.Close();
            }
        }
        private void InvokeServicesWorker_DoWork(object threadContext)
        {
            ServiceCallStatus call    = threadContext as ServiceCallStatus;
            SADIService       service = call.Data as SADIService;

            try
            {
                call.Status = "Assembling input";
                MasterWorker.ReportProgress(1, call);
                MemoryStore input = assembleInput(SelectedNodes, service);

                call.Status = "Calling service";
                call.Data   = "Assembled input:\r\n" + SemWebHelper.storeToString(input);
                MasterWorker.ReportProgress(33, call);
                Store output = service.invokeService(input);

                call.Status = "Storing output";
                call.Data   = "Received output:\r\n" + SemWebHelper.storeToString(output);
                MasterWorker.ReportProgress(66, call);
                ICollection <IStatement> statements = KE.Import(output);
                showNewStatements(statements);

                call.Status = "Done";
                call.Data   = service;
                MasterWorker.ReportProgress(100, call);
            }
            catch (Exception err)
            {
                SADIHelper.error("ServiceCall", "error calling service", service, err);
                call.Status = "Error";
                call.Data   = "Error:\r\n" + err.Message;
                MasterWorker.ReportProgress(100, call);
            }
            finally
            {
                Interlocked.Decrement(ref NumWorkers);
            }
        }
Beispiel #3
0
        private void FindServicesWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            List <String> types = new List <String>();

            foreach (IResource node in SelectedNodes)
            {
                if (node is IEntity)
                {
                    if (!KE.HasType(node))
                    {
                        Uri uri = (node as IEntity).Uri;
                        if (!ReportProgressIfNotCancelled(0, String.Format("Resolving {0}...", uri)))
                        {
                            return;
                        }
                        try
                        {
                            SADIHelper.debug("ServiceSelection", "resolving URI", node);
                            if (FindServicesWorker.CancellationPending)
                            {
                                return;
                            }
                            KE.Import(SemWebHelper.resolveURI(uri));
                        }
                        catch (Exception err)
                        {
                            SADIHelper.error("ServiceSelection", "error resolving URI", node, err);
                        }
                        try
                        {
                            SADIHelper.debug("ServiceSelection", "resolving against SADI resolver", node);
                            if (FindServicesWorker.CancellationPending)
                            {
                                return;
                            }
                            KE.Import(SADIHelper.resolve(uri));
                        }
                        catch (Exception err)
                        {
                            SADIHelper.error("ServiceSelection", "error resolving against SADI resolver", node, err);
                        }
                    }
                    foreach (IEntity type in KE.GetTypes(node as IEntity))
                    {
                        types.Add(type.Uri.ToString());
                    }
                }
            }

            // find services by exact input class; quick, but misses a lot...
            if (!ReportProgressIfNotCancelled(0, "Finding services by direct type..."))
            {
                return;
            }
            ICollection <SADIService> services = SADIRegistry.Instance().findServicesByInputClass(types);
            int i = 0;
            int n = services.Count;

            foreach (SADIService service in services)
            {
                SADIRegistry.Instance().addPropertyRestrictions(service);
                if (!ReportProgressIfNotCancelled((++i * 100) / n, service))
                {
                    return;
                }
            }

            // reset progress bar
            if (!ReportProgressIfNotCancelled(0, "Finding services by input instance query..."))
            {
                return;
            }

            // find service by input instance SPARQL query; slow, but is complete modulo reasoning...
            i = 0;
            n = SADIRegistry.Instance().getServiceCount();
            do
            {
                if (!ReportProgressIfNotCancelled((i * 100) / n, String.Format("Finding services by input instance query {0}-{1}/{2}",
                                                                               i, Math.Min(i + SERVICES_PER_QUERY, n), n)))
                {
                    return;
                }
                services = SADIRegistry.Instance().getAllServices(i, SERVICES_PER_QUERY);
                foreach (SADIService service in services)
                {
                    //SADIHelper.debug("ServiceDiscoveryDialog", String.Format("checking {0}", service.uri), null);
                    Object rv = null;
                    try
                    {
                        if (checkForInputInstances(service, SelectedNodes))
                        {
                            SADIRegistry.Instance().addPropertyRestrictions(service);
                            rv = service;
                        }
                        else
                        {
                            rv = String.Format("No match to {0}", service.uri);
                        }
                    }
                    catch (Exception ex)
                    {
                        //rv = ex;
                        rv = String.Format("Error executing input instance query for {0}: {1}", service.uri, ex.Message);
                    }
                    if (!ReportProgressIfNotCancelled((++i * 100) / n, rv))
                    {
                        return;
                    }
                }
            } while (services.Count == SERVICES_PER_QUERY);
        }