private void MasterWorker_DoWork(object sender, DoWorkEventArgs e) { List <ServiceCallStatus> rows = e.Argument as List <ServiceCallStatus>; int i = 0; while (i < rows.Count) { if (NumWorkers < MAX_WORKERS) { Interlocked.Increment(ref NumWorkers); SADIHelper.debug("ServiceInvocation", "queueing worker " + NumWorkers, rows[i].Data); ThreadPool.QueueUserWorkItem(InvokeServicesWorker_DoWork, rows[i++]); } else { SADIHelper.debug("ServiceInvocation", "waiting for some service calls to finish before spawning more than " + MAX_WORKERS, null); Thread.Sleep(30000); } } while (NumWorkers > 0) { SADIHelper.debug("ServiceInvocation", "waiting for " + NumWorkers + " service calls to finish", null); Thread.Sleep(1000); } }
public void addPropertyRestrictions(SADIService service) { if (service.properties.Count > 0) { SADIHelper.debug("SADIRegistry", "addPropertyRestrictions called twice on same service", service); return; } String query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \r\n" + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \r\n" + "PREFIX owl: <http://www.w3.org/2002/07/owl#> \r\n" + "PREFIX sadi: <http://sadiframework.org/ontologies/sadi.owl#> \r\n" + "PREFIX mygrid: <http://www.mygrid.org.uk/mygrid-moby-service#> \r\n" + "SELECT DISTINCT ?onPropertyURI ?onPropertyLabel ?valuesFromURI ?valuesFromLabel \r\n" + "WHERE {\r\n" + " <"+ service.uri + "> sadi:decoratesWith ?decoration . \r\n" + " ?decoration owl:onProperty ?onPropertyURI . \r\n"+ " OPTIONAL { ?onPropertyURI rdfs:label ?onPropertyLabel } . \r\n"+ " OPTIONAL { \r\n"+ " ?decoration owl:someValuesFrom ?valuesFromURI . \r\n"+ " OPTIONAL { ?valuesFromURI rdfs:label ?valuesFromLabel } \r\n"+ " } . \r\n"+ "}"; foreach (JsonData binding in executeQuery(query)) { service.addProperty( getSPARQLBindingAsString(binding, "onPropertyURI"), getSPARQLBindingAsString(binding, "onPropertyLabel"), getSPARQLBindingAsString(binding, "valuesFromURI"), getSPARQLBindingAsString(binding, "valuesFromLabel")); } }
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 bool checkForInputInstances(SADIService service, IEnumerable <IResource> selectedNodes) { if (service.inputInstanceQuery != null) { string query = SADIHelper.convertConstructQuery(service.inputInstanceQuery); if (query != null) { foreach (ISPARQLResult result in KE.Graph.Query(query).Results) { foreach (IResource node in selectedNodes) { if (node.Equals(result["input"])) { return(true); } } } } } return(false); }
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); } }
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); }