public void SparqlRemoteEndpointSyncVsAsyncTimeDBPedia() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing)) { throw new SkipTestException("Test Config marks Remote Parsing as unavailable, test cannot be run"); } String query; using (StreamReader reader = File.OpenText("resources\\dbpedia-query-time.rq")) { query = reader.ReadToEnd(); reader.Close(); } SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); Stopwatch timer = new Stopwatch(); timer.Start(); SparqlResultSet syncGetResults = endpoint.QueryWithResultSet(query) as SparqlResultSet; timer.Stop(); Console.WriteLine("Sync Query (GET): " + timer.Elapsed); TestTools.ShowResults(syncGetResults); Console.WriteLine(); timer.Reset(); timer.Start(); endpoint.HttpMode = "POST"; SparqlResultSet syncPostResults = endpoint.QueryWithResultSet(query) as SparqlResultSet; timer.Stop(); Console.WriteLine("Sync Query (POST): " + timer.Elapsed); TestTools.ShowResults(syncPostResults); Console.WriteLine(); timer.Reset(); ManualResetEvent signal = new ManualResetEvent(false); SparqlResultSet asyncResults = null; //DateTime start = DateTime.Now; //DateTime end = start; timer.Start(); endpoint.QueryWithResultSet(query, (r, s) => { //end = DateTime.Now; timer.Stop(); asyncResults = r; signal.Set(); signal.Close(); }, null); Thread.Sleep(AsyncTimeout); Assert.True(signal.SafeWaitHandle.IsClosed, "Wait Handle should be closed"); Console.WriteLine("Async Query: " + timer.Elapsed);//(end - start)); TestTools.ShowResults(asyncResults); Assert.Equal(syncGetResults, asyncResults); }
/// <summary> /// Search by country name - Country /// </summary> /// <param name="countryName"></param> public IActionResult Get(string countryName) { Dictionary<string, Countries.CountryDiseases> countrydiseases = new Dictionary<string, Countries.CountryDiseases>(); string query = ""; try { database db = new database(database.maindb); MySqlDataReader rd; rd = db.ExecuteReader("SELECT code,sum(deaths) as nrDeaths FROM `diseasestatistics` where lower(country)=lower('" + countryName + "') group by code order by sum(deaths) desc limit 10"); while (rd.Read()) { countrydiseases.Add(rd.GetString("code"), new Countries.CountryDiseases(rd.GetInt32("nrDeaths"), "", "","")); } SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); foreach (KeyValuePair<string, Countries.CountryDiseases> disease in countrydiseases) { query = @"SELECT * WHERE { ?url <http://dbpedia.org/ontology/icd10> ?ID. ?url rdfs:label ?name. ?url <http://dbpedia.org/ontology/abstract> ?description. filter regex(str(lcase(?ID)), concat(lcase('" + disease.Key[0] + "'), '[" + disease.Key[1] + "][" + disease.Key[2] + "][.]?[0-9]?') )" + "filter(langMatches(lang(?name), 'EN'))" + "filter(langMatches(lang(?description), 'EN'))" + "} limit 1"; SparqlResultSet results = endpoint.QueryWithResultSet(query); if (results.Count > 0) { disease.Value.Description = results[0]["description"].ToString().Remove(results[0]["description"].ToString().Length - 3); if (disease.Value.Description.Length > 300) disease.Value.Description = disease.Value.Description.Remove(300) + " ..."; disease.Value.Disease = results[0]["name"].ToString().Remove(results[0]["name"].ToString().Length - 3); disease.Value.Url = disease.Value.Disease.Replace(" ", "_"); } else { query = @"SELECT * WHERE { ?url <http://dbpedia.org/ontology/icd10> ?ID. ?url rdfs:label ?name. ?url <http://dbpedia.org/ontology/abstract> ?description. filter regex(str(lcase(?ID)), concat(lcase('" + disease.Key[0] + "'), '[" + disease.Key[1] + "][0-9][.]?[0-9]?') )" + "filter(langMatches(lang(?name), 'EN'))" + "filter(langMatches(lang(?description), 'EN'))" + "} limit 1"; results = endpoint.QueryWithResultSet(query); if (results.Count > 0) { disease.Value.Description = results[0]["description"].ToString().Remove(results[0]["description"].ToString().Length - 3); if (disease.Value.Description.Length > 300) disease.Value.Description = disease.Value.Description.Remove(300) + " ..."; disease.Value.Disease = results[0]["name"].ToString().Remove(results[0]["name"].ToString().Length - 3); } } } db.Close(); } catch (Exception e) { return new ObjectResult(countrydiseases); } return new ObjectResult(countrydiseases); }
public ActionResult Index() { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:3030/ds/sparql")); SparqlResultSet results = endpoint.QueryWithResultSet("PREFIX movie: <http://data.linkedmdb.org/resource/movie/>"+ "SELECT (COUNT(DISTINCT ?film) AS ?brFilm)WHERE {?film a movie:film}"); long brFilm = 0; INode outValue; foreach (var result in results) { result.TryGetValue("brFilm", out outValue); brFilm = outValue.AsValuedNode().AsInteger(); } long brActor = 0; results = endpoint.QueryWithResultSet("PREFIX movie: <http://data.linkedmdb.org/resource/movie/>"+ "SELECT (COUNT(DISTINCT ?actor) AS ?brActor)WHERE {?actor a movie:actor}"); foreach (var result in results) { result.TryGetValue("brActor", out outValue); brActor = outValue.AsValuedNode().AsInteger(); } long brTriples = 0; results = endpoint.QueryWithResultSet("SELECT (COUNT(*) AS ?brTriples)"+ "WHERE { ?s ?p ?o }"); foreach (var result in results) { result.TryGetValue("brTriples", out outValue); brTriples = outValue.AsValuedNode().AsInteger(); } ViewBag.MessageMovies = "Number of movies in LinkedMDB: " + brFilm; ViewBag.MessageActors = "Number of actors in LinkedMDB: " + brActor; ViewBag.MessageTriples = "Total number of triples in LinkedMDB: " + brTriples; return View(); }
public void SparqlRemoteEndpointSyncVsAsyncTimeFactforge() { String query; using (StreamReader reader = File.OpenText("resources\\dbpedia-query-time.rq")) { query = reader.ReadToEnd(); reader.Close(); } SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://factforge.net/sparql")); endpoint.Timeout = AsyncTimeout; Stopwatch timer = new Stopwatch(); timer.Start(); SparqlResultSet syncGetResults = endpoint.QueryWithResultSet(query) as SparqlResultSet; timer.Stop(); Console.WriteLine("Sync Query (GET): " + timer.Elapsed); TestTools.ShowResults(syncGetResults); Console.WriteLine(); timer.Reset(); timer.Start(); endpoint.HttpMode = "POST"; SparqlResultSet syncPostResults = endpoint.QueryWithResultSet(query) as SparqlResultSet; timer.Stop(); Console.WriteLine("Sync Query (POST): " + timer.Elapsed); TestTools.ShowResults(syncPostResults); Console.WriteLine(); timer.Reset(); ManualResetEvent signal = new ManualResetEvent(false); SparqlResultSet asyncResults = null; //DateTime start = DateTime.Now; //DateTime end = start; timer.Start(); endpoint.QueryWithResultSet(query, (r, s) => { //end = DateTime.Now; timer.Stop(); asyncResults = r; signal.Set(); signal.Close(); }, null); Thread.Sleep(AsyncTimeout); Assert.True(signal.SafeWaitHandle.IsClosed, "Wait Handle should be closed"); Console.WriteLine("Async Query: " + timer.Elapsed);//(end - start)); TestTools.ShowResults(asyncResults); Assert.Equal(syncGetResults, asyncResults); }
public void SparqlDBPedia() { try { Options.HttpDebugging = true; String query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE {?s a rdfs:Class } LIMIT 50"; SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); SparqlResultSet results = endpoint.QueryWithResultSet(query); TestTools.ShowResults(results); using (HttpWebResponse response = endpoint.QueryRaw(query)) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { while (!reader.EndOfStream) { Console.WriteLine(reader.ReadLine()); } reader.Close(); } response.Close(); } } finally { Options.HttpDebugging = false; } }
public void SparqlDBPedia() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing)) { throw new SkipTestException("Test Config marks Remote Parsing as unavailable, test cannot be run"); } try { Options.HttpDebugging = true; String query = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT * WHERE {?s a rdfs:Class } LIMIT 50"; SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); SparqlResultSet results = endpoint.QueryWithResultSet(query); TestTools.ShowResults(results); using (HttpWebResponse response = endpoint.QueryRaw(query)) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { while (!reader.EndOfStream) { Console.WriteLine(reader.ReadLine()); } reader.Close(); } response.Close(); } } finally { Options.HttpDebugging = false; } }
public void SparqlWikidata() { Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing), "Test Config marks Remote Parsing as unavailable, test cannot be run"); try { Options.HttpDebugging = true; String query = "SELECT * WHERE {?s ?p ?o } LIMIT 1"; SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("https://query.wikidata.org/sparql"), "https://www.wikidata.org"); endpoint.UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"; SparqlResultSet results = endpoint.QueryWithResultSet(query); TestTools.ShowResults(results); using (HttpWebResponse response = endpoint.QueryRaw(query)) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { while (!reader.EndOfStream) { Console.WriteLine(reader.ReadLine()); } reader.Close(); } response.Close(); } } finally { Options.HttpDebugging = false; } }
/// <summary> /// Gets the count of persons already linked to Libris /// </summary> /// <returns>count of already made links</returns> public int GetNrOfLinkedPersons() { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "prefix owl: <http://www.w3.org/2002/07/owl#>" + "prefix ksamsok: <http://kulturarvsdata.se/ksamsok#>" + "select (COUNT(DISTINCT ?a) AS ?count)where{" + "?a foaf:fullName ?name." + "?f ksamsok:architect ?a." + "?a owl:sameAs ?link." + "FILTER regex(str(?link), '^http://libris.kb.se/resource/auth/') . " + "MINUS{ ?a foaf:fullName 'Okänd'}" + "}"); int count = 0; foreach (SparqlResult result in results) { if (result.Value("count") != null) { string s = result.Value("count").ToString(); String[] s1 = s.Split('^'); count = int.Parse(s1[0].Trim()); } } return count; }
public void SparqlRemoteEndpointLongQuery() { try { Options.HttpDebugging = true; StringBuilder input = new StringBuilder(); input.AppendLine("SELECT * WHERE {?s ?p ?o}"); input.AppendLine(new String('#', 2048)); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(TestQueryUri)); Object results = endpoint.QueryWithResultSet(input.ToString()); if (results is SparqlResultSet) { TestTools.ShowResults(results); } else { Assert.Fail("Should have returned a SPARQL Result Set"); } } finally { Options.HttpDebugging = false; } }
public void SparqlRemoteEndpointMemoryLeak2() { //Do a GC before attempting the test GC.GetTotalMemory(true); //First off make sure to load some data into the some SparqlRemoteUpdateEndpoint updateEndpoint = RemoteEndpoints.GetUpdateEndpoint(); updateEndpoint.Update("DROP ALL"); int totalRuns = 10000; int subjects = 1000; int predicates = 10; //Loop over making queries to try and reproduce the memory leak for (int i = 1; i <= totalRuns; i++) { //Add new data each time around updateEndpoint.Update("INSERT DATA { <http://subject/" + (i % subjects) + "> <http://predicate/" + (i % predicates) + "> <http://object/" + i + "> . }"); SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint(); SparqlParameterizedString queryString = new SparqlParameterizedString(); queryString.CommandText = "SELECT * WHERE { <http://subject/" + (i % 1000) + "> ?p ?o }"; ResultCountHandler handler = new ResultCountHandler(); endpoint.QueryWithResultSet(handler, queryString.ToString()); Assert.True(handler.Count >= 1 && handler.Count <= subjects, "Result Count " + handler.Count + " is not in expected range 1 <= x < " + (i % 1000)); if (i % 500 == 0) { Debug.WriteLine("Memory Usage after " + i + " Iterations: " + Process.GetCurrentProcess().PrivateMemorySize64); } } Debug.WriteLine("Memory Usage after " + totalRuns + " Iterations: " + Process.GetCurrentProcess().PrivateMemorySize64); }
public void SparqlRemoteEndpointLongQuery() { try { Options.HttpDebugging = true; StringBuilder input = new StringBuilder(); input.AppendLine("SELECT * WHERE {?s ?p ?o}"); input.AppendLine(new String('#', 2048)); SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint(); Object results = endpoint.QueryWithResultSet(input.ToString()); if (results is SparqlResultSet) { TestTools.ShowResults(results); } else { Assert.Fail("Should have returned a SPARQL Result Set"); } } finally { Options.HttpDebugging = false; } }
public void SparqlDbPediaDotIssue() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing)) { throw new SkipTestException("Test Config marks Remote Parsing as unavailable, test cannot be run"); } try { Options.HttpDebugging = true; String query = @"PREFIX dbpediaO: <http://dbpedia.org/ontology/> select distinct ?entity ?redirectedEntity where { ?entity rdfs:label 'Apple Computer'@en . ?entity dbpediaO:wikiPageRedirects ?redirectedEntity . }"; SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); Console.WriteLine("Results obtained with QueryWithResultSet()"); SparqlResultSet results = endpoint.QueryWithResultSet(query); TestTools.ShowResults(results); Console.WriteLine(); Console.WriteLine("Results obtained with QueryRaw()"); using (HttpWebResponse response = endpoint.QueryRaw(query)) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { while (!reader.EndOfStream) { Console.WriteLine(reader.ReadLine()); } reader.Close(); } response.Close(); } Console.WriteLine(); Console.WriteLine("Results obtained with QueryRaw() requesting JSON"); using (HttpWebResponse response = endpoint.QueryRaw(query, new String[] { "application/sparql-results+json" })) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { while (!reader.EndOfStream) { Console.WriteLine(reader.ReadLine()); } reader.Close(); } response.Close(); } Console.WriteLine(); } finally { Options.HttpDebugging = false; } }
public void connectMarkLogic() { string query = "SELECT * WHERE {?s ?p ?o} LIMIT 50"; var endpoint = new VDS.RDF.Query.SparqlRemoteEndpoint(new Uri("http://*****:*****@123"); SparqlResultSet results = endpoint.QueryWithResultSet(query); }
public void connectOntotext() { string query = "SELECT * WHERE {?s ?p ?o} LIMIT 50"; var endpoint = new VDS.RDF.Query.SparqlRemoteEndpoint(new Uri("http://localhost:7200/repositories/786")); VDS.RDF.Options.ForceHttpBasicAuth = true; endpoint.SetCredentials("admin", "admin"); SparqlResultSet results = endpoint.QueryWithResultSet(query); }
public void SparqlRemoteEndpointResultCountHandler() { SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint(); ResultCountHandler handler = new ResultCountHandler(); endpoint.QueryWithResultSet(handler, "SELECT * WHERE { { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }"); Console.WriteLine("Result Count: " + handler.Count); Assert.NotEqual(0, handler.Count); }
public SparqlResultSet doQuery() { string query = "select * where {<http://dbpedia.org/ontology/purpose> ?x ?y}"; SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql")); SparqlResultSet set = endpoint.QueryWithResultSet(query); List<SparqlResultSet> listset = new List<SparqlResultSet>(); listset.Add(set); //SparqlResultSet set2 = endpoint.QueryWithResultSet("select * where {<http://dbpedia.org/ontology/supplementalDraftRound> ?x ?y}"); //listset.Add(set2); return listset[0]; }
private static String getAbstract(String SubjectURI) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://weetit:8890/sparql")); String query = "select * where {<" + SubjectURI + "><http://dbpedia.org/ontology/abstract> ?obj}"; SparqlResultSet results = endpoint.QueryWithResultSet(query); if (results != null) { SparqlResult result = results[0]; return ((LiteralNode)result.Value("obj")).Value; } else return null; }
public ActionResult ActorDetails(string name) { Actor actor = new Actor(); actor.name = name; SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:3030/ds/sparql")); SparqlResultSet results = endpoint.QueryWithResultSet("PREFIX movie: <http://data.linkedmdb.org/resource/movie/>"+ "PREFIX dbp: <http://dbpedia.org/property/> PREFIX owl: <http://www.w3.org/2002/07/owl#>"+ "PREFIX dc: <http://purl.org/dc/terms/> PREFIX dcd: <http://purl.org/dc/elements/1.1/>"+ "SELECT ?uri ?birthDate ?placeOfBirth ?residence ?nickName ?description ?occupation"+ "WHERE{ ?uri movie:actor_name \""+ name +"\" ."+ "SERVICE <http://dbpedia.org/sparql> { ?Actor dbp:name ?imeDBP ."+ "OPTIONAL { ?Actor dbp:occupation ?occupation } OPTIONAL { ?Actor dbp:nickname ?nickName }"+ "OPTIONAL { ?Actor dbp:birthDate ?birthDate } OPTIONAL { ?Actor dbp:placeOfBirth ?placeOfBirth }"+ "OPTIONAL { ?Actor dbp:residence ?residence } OPTIONAL { ?Actor dcd:description ?description }"+ "FILTER(STR(?imeDBP) = \""+ name +"\") FILTER regex(STR(?occupation), 'actor', 'i')}}"); foreach(var result in results) { string birthDate, placeOfBirth, residence, nickName, description, occupation; INode outValue; result.TryGetValue("birthDate", out outValue); if (outValue == null) birthDate = "Unknown"; else birthDate = outValue.AsValuedNode().AsString(); actor.birthDate = birthDate; result.TryGetValue("placeOfBirth", out outValue); if (outValue == null) placeOfBirth = "Unknown"; else placeOfBirth = outValue.AsValuedNode().AsString(); actor.birthPlace = placeOfBirth; result.TryGetValue("residence", out outValue); if (outValue == null) residence = "Unknown"; else residence = outValue.AsValuedNode().AsString(); actor.residence = residence; result.TryGetValue("nickName", out outValue); if (outValue == null) nickName = "Unknown"; else nickName = outValue.AsValuedNode().AsString(); actor.nickname = nickName; result.TryGetValue("description", out outValue); if (outValue == null) description = "Unknown"; else description = outValue.AsValuedNode().AsString(); actor.description = description; result.TryGetValue("occupation", out outValue); if (outValue == null) occupation = "Unknown"; else occupation = outValue.AsValuedNode().AsString(); actor.occupation = occupation; } return View(actor); }
public static string getLabel(String URI) { //at least best one for now URI = Uri.EscapeUriString(URI); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); string query = "select * where {<" + URI + "> <http://www.w3.org/2000/01/rdf-schema#label> ?obj}"; SparqlResultSet results = endpoint.QueryWithResultSet(query); //if there's no results from the first query, we will try to get the name if (results.Count < 1) { string name_query = "select * where {<" + URI + "> <http://xmlns.com/foaf/0.1/name> ?obj}"; results = endpoint.QueryWithResultSet(name_query); //if there's no result from the second query //get the name after the / if (results.Count < 1) { string toreturn = new string(URI.ToCharArray().Reverse().ToArray());//URI.Reverse().ToString(); toreturn = toreturn.Remove(toreturn.IndexOf("/")); toreturn = new string(toreturn.ToCharArray().Reverse().ToArray()); toreturn = toreturn.Replace("_", " "); //TODO : get back the encoding toreturn = toreturn.Trim(); return toreturn; } else { //returning return ((LiteralNode)results[0].Value("obj")).Value; } } else { //returning it return ((LiteralNode)results[0].Value("obj")).Value; } }
/// <summary> /// Processes a SPARQL Query /// </summary> /// <param name="query">SPARQL Query</param> /// <returns></returns> public object ProcessQuery(SparqlQuery query) { query.QueryExecutionTime = null; DateTime start = DateTime.Now; Object temp; 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: temp = _endpoint.QueryWithResultSet(_formatter.Format(query)); break; case SparqlQueryType.Construct: case SparqlQueryType.Describe: case SparqlQueryType.DescribeAll: temp = _endpoint.QueryWithResultGraph(_formatter.Format(query)); break; default: throw new RdfQueryException("Unable to execute an unknown query type against a Remote Endpoint"); } return(temp); } finally { TimeSpan elapsed = (DateTime.Now - start); query.QueryExecutionTime = elapsed; } }
public void SparqlRemoteEndpointAsyncApiQueryWithResultSet() { SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint(); ManualResetEvent signal = new ManualResetEvent(false); endpoint.QueryWithResultSet("SELECT * WHERE { ?s ?p ?o }", (r, s) => { TestTools.ShowResults(r); signal.Set(); signal.Close(); }, null); Thread.Sleep(AsyncTimeout); Assert.True(signal.SafeWaitHandle.IsClosed, "WaitHandle should be closed"); }
public void SparqlRemoteEndpointMemoryLeak1() { /* * Dim endpoint = New SparqlRemoteEndpoint(New Uri("http://localhost:8080/sesame/repositories/my_repo")) * Dim queryString As SparqlParameterizedString = New SparqlParameterizedString() * queryString.Namespaces.AddNamespace("annot", New Uri(oAppSettingsReader.GetValue("BaseUriSite", GetType(System.String)) & "/annotations.owl#")) * queryString.CommandText = "SELECT DISTINCT ?text WHERE {?annotation annot:onContent <" & _uriDocument & "> ; annot:onContentPart """ & ContentPart & """ ; annot:text ?text ; annot:isValid ""false""^^xsd:boolean . }" * Dim results As SparqlResultSet = endpoint.QueryWithResultSet(queryString.ToString) * For Each result As SparqlResult In results * Console.WriteLine(DirectCast(result.Value("text"), LiteralNode).Value) * Next * results.Dispose() */ //Do a GC before attempting the test GC.GetTotalMemory(true); //First off make sure to load some data into the some SparqlRemoteUpdateEndpoint updateEndpoint = RemoteEndpoints.GetUpdateEndpoint(); updateEndpoint.Update("DROP ALL; INSERT DATA { <http://subject> <http://predicate> <http://object> . }"); int totalRuns = 10000; //Loop over making queries to try and reproduce the memory leak for (int i = 1; i <= totalRuns; i++) { SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint(); SparqlParameterizedString queryString = new SparqlParameterizedString(); queryString.CommandText = "SELECT * WHERE { ?s ?p ?o }"; SparqlResultSet results = endpoint.QueryWithResultSet(queryString.ToString()); Assert.Equal(1, results.Count); foreach (SparqlResult result in results) { //We're just iterating to make sure we touch the whole of the results } results.Dispose(); if (i % 500 == 0) { Debug.WriteLine("Memory Usage after " + i + " Iterations: " + Process.GetCurrentProcess().PrivateMemorySize64); } } Debug.WriteLine("Memory Usage after " + totalRuns + " Iterations: " + Process.GetCurrentProcess().PrivateMemorySize64); }
public static SparqlResultSet RequestWithHTTP(string request) { SparqlResultSet toreturn = new SparqlResultSet(); try { StreamReader sr = new StreamReader("endpointURI.txt"); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(sr.ReadLine())); sr.Close(); endpoint.Timeout = 999999; toreturn = endpoint.QueryWithResultSet(request); } catch (Exception e) { util.log(request + e.Message + "==>" + e.Data); } return toreturn; }
/// <summary> /// Gets the total nr of architects from Riksantikvarieämbetet /// </summary> /// <returns>The total count of architects</returns> public int GetNrOfPersons() { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( "PREFIX foaf: <http://xmlns.com/foaf/0.1/> prefix ksamsok: <http://kulturarvsdata.se/ksamsok#> prefix type: <http://kulturarvsdata.se/resurser/EntityType#> select (COUNT(DISTINCT ?a) AS ?count) where{ ?a foaf:fullName ?name. ?a ?s type:person. ?f ksamsok:architect ?a. MINUS{ ?a foaf:fullName 'Okänd'}}"); int count = 0; foreach (SparqlResult result in results) { if (result.Value("count") != null) { string s = result.Value("count").ToString(); String[] s1 = s.Split('^'); count = int.Parse(s1[0].Trim()); } } return count; }
/// <summary> /// do a HTTP request using a sparql query from a sparql endpoint stated in a file /// </summary> /// <param name="request">sparql Query to be executed</param> /// <returns>set of results resulted from executing the query</returns> public static SparqlResultSet executeSparqlQuery(string request) { SparqlResultSet toreturn = new SparqlResultSet(); try { string path = HttpRuntime.BinDirectory + "endpointURI.txt"; StreamReader sr = new StreamReader(path); string endpointURI = sr.ReadLine(); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri (endpointURI)); sr.Close(); endpoint.Timeout = 999999; toreturn = endpoint.QueryWithResultSet(request); } catch (Exception e) { util.log(request + e.Message + "==>" + e.Data); } return toreturn; }
protected void Button1_Click(object sender, EventArgs e) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); //Make a SELECT query against the Endpoinre SparqlResultSet results = endpoint.QueryWithResultSet(TextBox1.Text); resultTable.InnerHtml = "<table>"; foreach (SparqlResult result in results) { int x=result.Count; resultTable.InnerHtml += "<tr>"; for (int i = 0; i < x; i++) { resultTable.InnerHtml += "<td>" + result[i].ToString() + "</td>"; } resultTable.InnerHtml += "</tr>"; } resultTable.InnerHtml += "</table>"; }
/// <summary> /// Gets all information avalible for the person with the given URI /// </summary> /// <param name="librisId">Unique URI from Libris</param> /// <returns>a Person object filld with data from Libris</returns> public Person GetPerson(String librisId) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( " PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + " PREFIX date: <http://dbpedia.org/property/> " + " PREFIX links: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> " + " select ?name ?birth ?death ?links where{<" + librisId + "> foaf:name ?name." + "OPTIONAL {<" + librisId + "> date:birthYear ?birth." + "}OPTIONAL {<" + librisId + "> date:deathYear ?death." + " }}"); Person person = new Person(); foreach (SparqlResult result in results) { if (result.Value("name") != null) { person.Name = result.Value("name").ToString(); } if (result.Value("birth") != null) { person.BirthYear = result.Value("birth").ToString(); } if (result.Value("death") != null) { person.DeathYear = result.Value("death").ToString(); } } person.URI = librisId; person.AddListOfExternalEntitie(GetListOfBooksAbout(librisId)); person.AddListOfExternalEntitie(GetListOfBooksBy(librisId)); return person; }
/// <summary> /// Gets a list of person objects where the name matches the param /// </summary> /// <param name="fullName">Name of the person</param> /// <returns>A list with all persons with the given name</returns> public List<Person> GetPersons(String fullName) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( "PREFIX date: <http://dbpedia.org/property/> " + "PREFIX foaf: <http://xmlns.com/foaf/0.1/> " + "select ?a ?birth ?death where{" + "?a foaf:name" + "'" + fullName + "'" + ". " + "OPTIONAL {" + "?a date:birthYear ?birth." + "}OPTIONAL {" + "?a date:deathYear ?death." + "}}"); List<Person> listOfPersons = new List<Person>(); foreach (SparqlResult result in results) { Person person = new Person { Name = fullName }; if (result.Value("a") != null) { person.URI = result.Value("a").ToString(); } if (result.Value("birth") != null) { person.BirthYear = result.Value("birth").ToString(); } if (result.Value("death") != null) { person.DeathYear = result.Value("death").ToString(); } person.AddListOfExternalEntitie(GetListOfBooksAbout(person.URI)); person.AddListOfExternalEntitie(GetListOfBooksBy(person.URI)); listOfPersons.Add(person); } return listOfPersons; }
private String findURIs(List<String> subjectsNames) { SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://*****:*****@en }"; result = remoteEndPoint.QueryWithResultSet(query); //QueryProcessor.closeConnection(); if (result.Count == 0) { //a panic mode to be added to generate a more generic query with more results uris.Add(""); continue; } else if (result.Count == 1) { if ((result[0].Value("redirects") == null)) uris.Add(result[0].Value("subject").ToString()); else uris.Add(result[0].Value("redirects").ToString()); continue; } else { int new_value; int min_value = 1000; int max_index = 0; for (int j = 0; j < result.Count; j++) { new_value = (computeLevenshteinDistance(subjectsNames[i], result[j].Value("literal").ToString())); scores.Add(new_value); if (new_value < min_value) { max_index = j; min_value = new_value; } else if (new_value == min_value) { if (result[j].Value("redirects") == null) { max_index = j; min_value = new_value; } else { min_value = new_value; } } } if ((result[max_index].Value("redirects") == null)) uris.Add(result[max_index].Value("subject").ToString()); else uris.Add(result[max_index].Value("redirects").ToString()); min_value = 0; } } comma_sep_uris = string.Join(",", uris.ToArray()); return comma_sep_uris; }
/// <summary> /// get predicates is a method in lexicon class that get all predicates objects that match some words in the Question /// </summary> /// <param name="question">question to get matched predicates of it </param> /// <param name="topN">the number of top matching results to be returned, default = 10</param> /// <param name="Limit">the limit of the number of returned results in the query, default = 20</param> /// <returns>list of top matching LexiconPredicates</returns> public List<LexiconPredicate> getPredicates(string question, int topN = 20, int Limit = 30) { DateTime dt = DateTime.Now; // capturing time for testing List<LexiconPredicate> __predicateList = new List<LexiconPredicate>(); //getting all permutation of words formed from the question string List<string> permutationList = getPermutations(question); //removing permutations that most propbably wont return results and will take time in querying permutationList = trimPermutations(permutationList); //Get the stemmed version of the question words Dictionary<string, List<string>> stemmedWords = GetStemmedWords(question); // to check if the predicates are filled before - so returning the matching predicates only - or not if (predicateFilled) { foreach (LexiconPredicate predicate in predicateList) { if (permutationList.Contains(predicate.QuestionMatch)) { __predicateList.Add(predicate); } } return __predicateList; } else { string bifContainsValue = ""; // iterating over each permutation of Question left and Query them from virtuoso and return predicate list and add them foreach (string questionleft in permutationList) { //Get all forms of questionLeft by replacing words with its stemmed version bifContainsValue = ""; //empty string bifContainsValue +="\'" + questionleft + "\'"; //add the original questionleft //Replace words in questionleft with its stem and add it to the bifContainsValue foreach (string word in stemmedWords.Keys) { if (questionleft.Contains(word)) { foreach (string stem in stemmedWords[word]) //This is created because a wordcan has many stems (rare case) { bifContainsValue += "or\'" + questionleft.Replace(word, stem) + "\'"; } } } string Query = "SELECT * WHERE { { " + "?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label ." + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#DatatypeProperty>." + "?label bif:contains \"" + bifContainsValue + "\" } " + "union {" + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> ." + "?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label ." + "?label bif:contains \"" + bifContainsValue + "\" } " + "union {" + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> ." + "?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label ." + "?label bif:contains \"" + bifContainsValue + "\" } " + "} limit " + Limit; //another Query to Get predicates untill deciding which of them is the best using statistics string Query2 = "SELECT ?predicate ?label WHERE { " + "{ ?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label . " + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?propertyType. " + "?label bif:contains \"" + bifContainsValue + "\" } " + "filter ( ?propertyType = <http://www.w3.org/2002/07/owl#DatatypeProperty> || " + "?propertyType = <http://www.w3.org/2002/07/owl#ObjectProperty> || " + "?propertyType = <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> ) " + "} limit " + Limit; SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); try { //executing the Query and finding results SparqlResultSet resultSet = remoteEndPoint.QueryWithResultSet(Query); //iterating over matched predicates in the resultset foreach (SparqlResult result in resultSet) { INode predicateURI = result.Value("predicate"); INode predicateLabel = result.Value("label"); LexiconPredicate tmplexiconpredicate = new LexiconPredicate(); // check that the property is used .. not a non-used property bool hasResuts = false; string checkQuery = "select distinct * where { ?x <" + predicateURI + "> ?y } limit 1 "; QueryHandler.startConnection(); SparqlResultSet checkResults = QueryHandler.ExecuteQueryWithString(checkQuery); QueryHandler.closeConnection(); if (checkResults.Count != 0) { hasResuts = true; } // check that the predicate doesn't exists in the predicateslist before bool exists = false; foreach (LexiconPredicate x in __predicateList) { // we added Questionmatch == question left bec new predicates may be added with better score that the old ones so this should be considered if (x.URI == predicateURI.ToString() && x.QuestionMatch == questionleft ) { exists = true; break; } } // adding the new predicate to the __predicatelist if (!exists && hasResuts) { tmplexiconpredicate.URI = predicateURI.ToString(); tmplexiconpredicate.QuestionMatch = questionleft; tmplexiconpredicate.label = predicateLabel.ToString(); __predicateList.Add(tmplexiconpredicate); } } } // skipping results that raised timeout exceptions catch { util.log("skipped : " + questionleft + " ---- due to time out "); } } util.log(" finished getting " + __predicateList.Count + " predicates " + " Time taken : " + DateTime.Now.Subtract(dt).TotalMilliseconds + " msec"); // now done of collecting predicates scoring them down and get the best n ones this.predicateList = scorePredicates(__predicateList, topN); this.predicateList = addDomainAndRange(this.predicateList); util.log("total time taken :" + DateTime.Now.Subtract(dt).TotalMilliseconds.ToString() + " mSecs"); predicateFilled = true; return this.predicateList; } }
public static SparqlResultSet RequestWithHTTP(string request) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(new StreamReader("endpointURI.txt").ReadLine())); endpoint.Timeout = 999999; return endpoint.QueryWithResultSet(request); }
/// <summary> /// Gets information about a person given the URI /// </summary> /// <param name="uri">The Unique URI for a person</param> /// <returns>a person object with data added</returns> public Person GetPersonByURI(String uri) { Person person = new Person(); //Endast för test Gunnar Asplund person.URI = "http://kulturarvsdata.se/raa/bbrp/21600000003542"; SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( "prefix ksamsok: <http://kulturarvsdata.se/ksamsok#>" + "prefix wiki: <http://kulturarvsdata.se/ugc#>" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "select ?name ?wikiLink ?image ?born ?death" + "where{" + "<" + uri + "> foaf:fullName ?name." + "optional{<" + uri + "> wiki:sameAsWikipedia ?wikiLink}" + "optional{<" + uri + "> ksamsok:isVisualizedBy ?image}" + "}"); person.URI = uri; foreach (SparqlResult result in results) { if (result.Value("name") != null) { person.Name = result.Value("name").ToString(); } if (result.Value("wikiLink") != null) { person.WikipediaLink = result.Value("wikiLink").ToString(); } if (result.Value("image") != null) { person.SetImageUrl(result.Value("image").ToString()); } person = GetBirthAndDeathYear(person); } person.AddListOfExternalEntitie(GetListOfHouses(person.URI)); return person; }
/// <summary> /// get predicates is a method in lexicon class that get all predicates objects that match some words in the Question /// </summary> /// <param name="question">question to get matched predicates of it </param> /// <param name="topN">the number of top matching results to be returned, default = 10</param> /// <param name="Limit">the limit of the number of returned results in the query, default = 20</param> /// <returns>list of top matching LexiconPredicates</returns> public List<LexiconPredicate> getPredicates(string question , int topN = 10 , int Limit = 20 ) { List<LexiconPredicate> __predicateList = new List<LexiconPredicate>(); //getting all permutation of words formed from the question string List<string> permutationList = getPermutations(question); //removing permutations that most propbably wont return results and will take time in querying permutationList = trimPermutations(permutationList); DateTime dt = DateTime.Now; // capturing time for testing // iterating over each permutation of Question left and Query them from virtuoso and return predicate list and add them foreach (string questionleft in permutationList) { string Query = "SELECT * WHERE { {" + "?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label ." + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> ." + "?label bif:contains '\"" + questionleft + "\"'}"+ "union { "+ "?predicate <http://www.w3.org/2000/01/rdf-schema#label> ?label ." + "?predicate <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2002/07/owl#ObjectProperty> ." + "?label bif:contains '\"" + questionleft + "\"'}" + "} LIMIT "+Limit+" "; //SparqlResultSet resultset = QueryHandler.ExecuteQueryWithString(Query); SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); try { //executing the Query and finding results SparqlResultSet resultSet = remoteEndPoint.QueryWithResultSet(Query); //iterating over matched predicates in the resultset foreach (SparqlResult result in resultSet) { INode predicateURI = result.Value("predicate"); INode predicateLabel = result.Value("label"); LexiconPredicate tmplexiconpredicate = new LexiconPredicate(); // check that the property is used .. not a non-used property bool hasResuts = false; string checkQuery = "select distinct * where { ?x <" + predicateURI + "> ?y } limit 1 "; QueryHandler.startConnection(); SparqlResultSet checkResults = QueryHandler.ExecuteQueryWithString(checkQuery); QueryHandler.closeConnection(); if (checkResults.Count != 0) { hasResuts = true; } // check that the predicate doesn't exists in the predicateslist before bool exists = false; foreach (LexiconPredicate x in __predicateList) { if (x.URI == predicateURI.ToString()) { exists = true; break; } } // adding the new predicate to the __predicatelist if (!exists && hasResuts) { tmplexiconpredicate.URI = predicateURI.ToString(); tmplexiconpredicate.QuestionMatch = questionleft; tmplexiconpredicate.label = predicateLabel.ToString(); __predicateList.Add(tmplexiconpredicate); } } } // skipping results that raised timeout exceptions catch { util.log("skipped : " + questionleft + " ---- due to time out "); } } util.log(" finished getting " + __predicateList.Count + " predicates " + " Time taken : " + DateTime.Now.Subtract(dt).TotalMilliseconds + " msec"); // now done of collecting predicates scoring them down and get the best n ones List<LexiconPredicate> predicateList = scorepredicates(__predicateList, topN); //now interating over the final predicate list and fill the rest of it's details <Domain , Range> foreach (LexiconPredicate x in predicateList) { string Query = "Select distinct ?domain ?range where { " + "<" + x.URI + ">" + "<http://www.w3.org/2000/01/rdf-schema#domain> ?domain." + "<" + x.URI + ">" + " <http://www.w3.org/2000/01/rdf-schema#range> ?range ." + "}"; QueryHandler.startConnection(); SparqlResultSet resulSet = QueryHandler.ExecuteQueryWithString(Query); if (resulSet.Count() != 0) { foreach (SparqlResult result in resulSet) { if (!x.domains.Contains(result.Value("domain").ToString())) { x.domains.Add(result.Value("domain").ToString()); } if (!x.ranges.Contains(result.Value("range").ToString())) { x.ranges.Add(result.Value("range").ToString()); } } QueryHandler.closeConnection(); } } return predicateList; }
public ActionResult SearchMovie(string name) { List<Movie> movieList = new List<Movie>(); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:3030/ds/sparql")); SparqlResultSet results = endpoint.QueryWithResultSet("PREFIX movie: <http://data.linkedmdb.org/resource/movie/>" + " PREFIX dc: <http://purl.org/dc/terms/> SELECT ?uri ?movieName WHERE { ?uri dc:title ?movieName" + " FILTER(regex( str(?movieName) , \"" + name + "\", \"i\"))} ORDER BY ?movieName"); foreach (var result in results) { Movie mov = new Movie(); INode outValue; result.TryGetValue("movieName", out outValue); string movieName = outValue.AsValuedNode().AsString(); result.TryGetValue("uri", out outValue); string uri = outValue.AsValuedNode().AsString(); mov.title = movieName; mov.uri = uri; movieList.Add(mov); } return View(movieList); }
public ActionResult SearchActor(string name) { List<Actor> actorList = new List<Actor>(); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:3030/ds/sparql")); SparqlResultSet results = endpoint.QueryWithResultSet("PREFIX movie: <http://data.linkedmdb.org/resource/movie/>" + "SELECT ?uri ?actorName WHERE { ?uri movie:actor_name ?actorName FILTER (regex( str(?actorName) ,\"" + name + "\", \"i\"))} ORDER BY ?actorName"); foreach (var result in results) { Actor act = new Actor(); INode outValue; result.TryGetValue("actorName", out outValue); string actorName = outValue.AsValuedNode().AsString(); act.name = actorName; result.TryGetValue("uri", out outValue); string uri = outValue.AsValuedNode().AsString(); act.uri = uri; actorList.Add(act); } return View(actorList); }
public void connectBlazegraph() { string query = "SELECT * WHERE {?s ?p ?o} LIMIT 50"; var endpoint = new VDS.RDF.Query.SparqlRemoteEndpoint(new Uri("http://10.109.219.5:9999/blazegraph/sparql")); SparqlResultSet results = endpoint.QueryWithResultSet(query); }
/// <summary> /// Gets a random URI based of the offset parameter. /// </summary> /// <param name="offset">should be a random nr between 0 and total count</param> /// <returns>URI for an architect</returns> public String GetRandomPersonId(int offset) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( "prefix type: <http://kulturarvsdata.se/resurser/EntityType#> prefix ksamsok: <http://kulturarvsdata.se/ksamsok#> select distinct ?uri where{?f ksamsok:architect ?uri. ?uri ?s type:person. FILTER(?uri != <http://kulturarvsdata.se/raa/bbrp/-1>)} limit 1 offset " + offset); String uri = ""; foreach (SparqlResult result in results) { if (result.Value("uri") != null) { uri = result.Value("uri").ToString(); } } return uri; }
/// <summary> /// Adds birth and death year for a person if it's available /// </summary> /// <param name="inPerson">the person object</param> /// <returns>same person object as sent in with added information</returns> private Person GetBirthAndDeathYear(Person inPerson) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( "PREFIX ksamsok: <http://kulturarvsdata.se/ksamsok#>" + "select ?year where{" + "<" + inPerson.URI + "> ksamsok:context ?b." + "?b ksamsok:toTime ?year." + "}limit 2"); if (results.Count == 2) { string year1 = results[0].Value("year").ToString(); string year2 = results[1].Value("year").ToString(); if (int.Parse(year1) > int.Parse(year2)) { inPerson.BirthYear = year2; inPerson.DeathYear = year1; } else { inPerson.BirthYear = year1; inPerson.DeathYear = year2; } } return inPerson; }
/// <summary> /// get predicates is a method in lexicon class that get all LexiconLiterals objects that match some words in the Question /// </summary> /// <param name="question">question to get matched predicates of it </param> /// <param name="topN">the number of top matching results to be returned, default = 10</param> /// <param name="Limit">the limit of the number of returned results in the query, default = 20</param> /// <returns>list of top matching LexiconLiterals with it's type of owner and predicate </returns> public List<LexiconLiteral> getLiterals(string question, int topN = 10, int Limit = 20) { DateTime dt = DateTime.Now; // capturing time for testing List<LexiconLiteral> __literalList = new List<LexiconLiteral>(); //getting all permutation of words formed from the question string List<string> permutationList = getPermutations(question); //removing permutations that most propbably wont return results and will take time in querying permutationList = trimPermutations(permutationList); // iterating over each permutation of Question left and Query them from virtuoso and return predicate list and add them foreach (string questionleft in permutationList) { // Query 1 is suitable for Keywords like : United states which match the most popular resource string Query1 = "SELECT distinct ?subject ?literal ?typeOfOwner " + "where { " + " ?subject <http://www.w3.org/2000/01/rdf-schema#label> ?literal . " + " optional { ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?typeOfOwner ." + " ?literal bif:contains '\"" + questionleft + "\"' OPTION (score ?sc) . " + "FILTER (" + "!(?typeOfOwner = <http://www.w3.org/2002/07/owl#Thing> " + "|| ?typeOfOwner = <http://www.w3.org/2004/02/skos/core#Concept> " + "|| ?typeOfOwner = <http://www.w3.org/2002/07/owl#ObjectProperty> " + "|| ?typeOfOwner = <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> " + "|| ?typeOfOwner = <http://www.w3.org/2002/07/owl#DatatypeProperty>) " + ")" + "} limit " + Limit; // Query2 is suitable for Keywords like : USA , which match the redirections string Query2 = "SELECT distinct ?subject ?literal ?typeOfOwner " + "WHERE { " + " ?subject2 <http://www.w3.org/2000/01/rdf-schema#label> ?literal . " + " ?subject2 <http://dbpedia.org/ontology/wikiPageRedirects> ?subject ." + " ?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?typeOfOwner ." + " ?literal bif:contains '\"" + questionleft + "\"' OPTION (score ?sc) ." + "FILTER (" + "!(?typeOfOwner = <http://www.w3.org/2002/07/owl#Thing> " + "|| ?typeOfOwner = <http://www.w3.org/2004/02/skos/core#Concept> " + "|| ?typeOfOwner = <http://www.w3.org/2002/07/owl#ObjectProperty> " + "|| ?typeOfOwner = <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> " + "|| ?typeOfOwner = <http://www.w3.org/2002/07/owl#DatatypeProperty>) " + ")" + "} limit " + Limit; SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); SparqlResultSet resultSet1 = new SparqlResultSet(); SparqlResultSet resultSet2 = new SparqlResultSet(); List<SparqlResult> resultSet = new List<SparqlResult>(); try { //executing the Query and finding results resultSet1 = remoteEndPoint.QueryWithResultSet(Query1); } // skipping results that raised timeout exceptions catch { util.log("skipped Query1 : " + questionleft + " ---- due to time out "); } try { resultSet2 = remoteEndPoint.QueryWithResultSet(Query2); } // skipping results that raised timeout exceptions catch { util.log("skipped Query2: " + questionleft + " ---- due to time out "); } resultSet = (resultSet1.Count != 0) ? resultSet1.ToList<SparqlResult>() : resultSet; resultSet = (resultSet2.Count != 0) ? resultSet.Concat<SparqlResult>(resultSet2.ToList<SparqlResult>()).ToList() : resultSet; //iterating over matched Literals in the resultset foreach (SparqlResult result in resultSet) { INode resourceURI = result.Value("subject"); INode literalLabel = result.Value("literal"); INode literalTypeOfOwner = result.Value("typeOfOwner"); // check that the predicate doesn't exists in the predicateslist before bool exists = false; // URI + Label only Exists bool totallyExists = false; // URI + Label + TypeofOwner exists in the literal list foreach (LexiconLiteral x in __literalList) { if (x.URI == resourceURI.ToString() && x.label == literalLabel.ToString()) { exists = true; if (x.typeOfOwner.Contains(literalTypeOfOwner.ToString())) { totallyExists = true; break; } } } // adding the new literals to the literallist . if (!exists) { LexiconLiteral tmpLexiconLiteral = new LexiconLiteral(resourceURI.ToString(), literalLabel.ToString(), questionleft, literalTypeOfOwner.ToString()); __literalList.Add(tmpLexiconLiteral); } if (!totallyExists && exists) { foreach (LexiconLiteral let in __literalList) { if (let.URI == resourceURI.ToString() && let.label == literalLabel.ToString()) { let.typeOfOwner.Add(literalTypeOfOwner.ToString()); } } } } } //scoring literals . trimming duplicates , __literalList = scoreLiterals(__literalList, topN); util.log("total time taken :" + DateTime.Now.Subtract(dt).TotalMilliseconds.ToString() + " msecs "); return __literalList; }
/// <summary> /// overload of Execute query /// </summary> /// <param name="input">the query text as string</param> /// <returns></returns> public static SparqlResultSet ExecuteQueryWithString(string input) { //list to hold the results SparqlResultSet resultSet = new SparqlResultSet(); try { //just in cas someone didn't open the connection if (!isConnectionStarted) startConnection(); //making the query //Object result = manager.Query(input); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); endpoint.Timeout = 99999999; resultSet = endpoint.QueryWithResultSet(input); //Object result = manager.ExecuteQuery(input); //resultSet = (SparqlResultSet)result; } catch { } return resultSet; }
public void SparqlRemoteEndpointSyncVsAsyncTimeLocalVirtuoso() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseVirtuoso)) { Assert.Inconclusive("Test Config marks Virtuoso as unavailable, test cannot be run"); } String query; using (StreamReader reader = new StreamReader("dbpedia-query-time.rq")) { query = reader.ReadToEnd(); reader.Close(); } SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(TestConfigManager.GetSetting(TestConfigManager.VirtuosoEndpoint))); endpoint.Timeout = AsyncTimeout; Stopwatch timer = new Stopwatch(); timer.Start(); SparqlResultSet syncGetResults = endpoint.QueryWithResultSet(query) as SparqlResultSet; timer.Stop(); Console.WriteLine("Sync Query (GET): " + timer.Elapsed); TestTools.ShowResults(syncGetResults); Console.WriteLine(); timer.Reset(); timer.Start(); endpoint.HttpMode = "POST"; SparqlResultSet syncPostResults = endpoint.QueryWithResultSet(query) as SparqlResultSet; timer.Stop(); Console.WriteLine("Sync Query (POST): " + timer.Elapsed); TestTools.ShowResults(syncPostResults); Console.WriteLine(); timer.Reset(); ManualResetEvent signal = new ManualResetEvent(false); SparqlResultSet asyncResults = null; //DateTime start = DateTime.Now; //DateTime end = start; timer.Start(); endpoint.QueryWithResultSet(query, (r, s) => { //end = DateTime.Now; timer.Stop(); asyncResults = r; signal.Set(); signal.Close(); }, null); Thread.Sleep(AsyncTimeout); Assert.IsTrue(signal.SafeWaitHandle.IsClosed, "Wait Handle should be closed"); Console.WriteLine("Async Query: " + timer.Elapsed);//(end - start)); TestTools.ShowResults(asyncResults); Assert.AreEqual(syncGetResults, asyncResults, "Result Sets should be equal"); }
/// <summary> /// get predicates is a method in lexicon class that get all LexiconLiterals objects that match some words in the Question /// </summary> /// <param name="question">question to get matched predicates of it </param> /// <param name="topN">the number of top matching results to be returned, default = 10</param> /// <param name="Limit">the limit of the number of returned results in the query, default = 20</param> /// <returns>list of top matching LexiconLiterals with it's type of owner and predicate </returns> public List<LexiconLiteral> getLiterals(string question, int topN = 30, int Limit = 30) { DateTime dt = DateTime.Now; // capturing time for testing List<LexiconLiteral> __literalList = new List<LexiconLiteral>(); //getting all permutation of words formed from the question string List<string> permutationList = getPermutations(question); //removing permutations that most propbably wont return results and will take time in querying permutationList = trimPermutations(permutationList); // to check if the literals are filled before - so returning the matching Literals only or not if (literalFilled) { foreach (LexiconLiteral literal in this.literalList) { if (permutationList.Contains(literal.QuestionMatch)) { __literalList.Add(literal); } } return __literalList; } else { // iterating over each permutation of Question left and Query them from virtuoso and return predicate list and add them foreach (string questionleft in permutationList) { string Query = "select distinct ?subject ?literal ?redirects ?typeOfOwner ?redirectsTypeOfOwner where{" + "?subject <http://www.w3.org/2000/01/rdf-schema#label> ?literal." + "optional { ?subject <http://dbpedia.org/ontology/wikiPageRedirects> ?redirects . " + "optional {?redirects <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?redirectsTypeOfOwner ." + "}}." + "optional {?subject <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?typeOfOwner}." + "Filter ( !bound(?typeOfOwner) || " + " ( !(?typeOfOwner = <http://www.w3.org/2004/02/skos/core#Concept>)" + " && !(?typeOfOwner = <http://www.w3.org/2002/07/owl#Thing>) " + " && !(?typeOfOwner = <http://www.opengis.net/gml/_Feature>) " + " && !(?typeOfOwner = <http://www.w3.org/2002/07/owl#ObjectProperty>) " + " && !(?typeOfOwner = <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> ) " + " && !(?typeOfOwner = <http://www.w3.org/2002/07/owl#DatatypeProperty> )))." + "?literal bif:contains '\"" + questionleft + "\"'.} limit " + Limit; SparqlRemoteEndpoint remoteEndPoint = new SparqlRemoteEndpoint(new Uri("http://localhost:8890/sparql")); SparqlResultSet resultSet = new SparqlResultSet(); try { resultSet = remoteEndPoint.QueryWithResultSet(Query); } // skipping results that raised timeout exceptions catch { util.log("skipped Query3: " + questionleft + " ---- due to time out "); } //iterating over matched Literals in the resultset foreach (SparqlResult result in resultSet) { string resultTypeOfOwner = ""; string resultURI; string resultLabel = result.Value("literal").ToString(); string resultquestionMatch = questionleft; if (result.Value("redirects") != null) { resultURI = result.Value("redirects").ToString(); if (result.Value("redirectsTypeOfOwner") != null) { resultTypeOfOwner = result.Value("redirectsTypeOfOwner").ToString(); } } else { resultURI = result.Value("subject").ToString(); if (result.Value("typeOfOwner") != null) { resultTypeOfOwner = result.Value("typeOfOwner").ToString(); } } // check that the predicate doesn't exists in the predicateslist before bool exists = false; // URI + Label only Exists bool totallyExists = false; // URI + Label + TypeofOwner exists in the literal list foreach (LexiconLiteral x in __literalList) { if (x.URI == resultURI && x.label == resultLabel && x.QuestionMatch == resultquestionMatch) { exists = true; if (x.typeOfOwner.Contains(resultTypeOfOwner) && resultTypeOfOwner.Length > 0) { totallyExists = true; break; } } } // adding the new literals to the literallist. if (!exists) { LexiconLiteral tmpLexiconLiteral = new LexiconLiteral(resultURI, resultLabel, resultquestionMatch, resultTypeOfOwner); __literalList.Add(tmpLexiconLiteral); } if (!totallyExists && exists) { foreach (LexiconLiteral let in __literalList) { if (let.URI == resultURI && let.label == resultLabel) { let.typeOfOwner.Add(resultTypeOfOwner); } } } } } //scoring literals . trimming duplicates , __literalList = scoreLiterals(__literalList, topN); util.log("total time taken :" + DateTime.Now.Subtract(dt).TotalMilliseconds.ToString() + " msecs "); literalFilled = true; this.literalList = __literalList; return __literalList; } }
public static void Main(string[] args) { StreamWriter output = new StreamWriter("RemoteSPARQLTestSuite.txt",false,Encoding.UTF8); String[] skipTests = { }; Console.SetOut(output); output.WriteLine("### Running Remote SPARQL Endpoint Tests"); output.WriteLine(); output.WriteLine("Accept Header for SPARQL SELECT and ASK: " + MimeTypesHelper.HttpSparqlAcceptHeader); output.WriteLine("Accept Header for SPARQL DESCRIBE and CONSTRUCT: " + MimeTypesHelper.HttpAcceptHeader); output.WriteLine(); //Options.HttpDebugging = true; try { SparqlRemoteEndpoint dbpedia = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); SparqlResultSet results = new SparqlResultSet(); String[] queries = new String[3]; queries[0] = "select distinct ?Concept where {[] a ?Concept} limit 10"; queries[1] = "select distinct ?Concept where {[] a ?Concept} limit 10 offset 5"; queries[2] = "prefix skos: <http://www.w3.org/2004/02/skos/core#> select distinct ?City where {?City skos:subject <http://dbpedia.org/resource/Category:Cities_in_England>}"; foreach (String query in queries) { output.WriteLine("## Making a SPARQL SELECT Query"); output.WriteLine("# Query"); output.WriteLine(); output.WriteLine(query); output.WriteLine(); output.WriteLine("# Results"); results = dbpedia.QueryWithResultSet(query); foreach (SparqlResult result in results) { output.WriteLine(result.ToString()); } output.WriteLine(); } //Options.HttpFullDebugging = true; String gquery = "DESCRIBE <http://dbpedia.org/resource/Southampton>"; output.WriteLine("## Making a SPARQL DESCRIBE Query"); output.WriteLine("# Query"); output.WriteLine(); output.WriteLine(gquery); output.WriteLine(); output.WriteLine("# Results"); IGraph g = dbpedia.QueryWithResultGraph(gquery); foreach (Triple t in g.Triples) { output.WriteLine(t.ToString()); } } catch (XmlException xmlEx) { reportError(output, "XML Exception", xmlEx); } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } //Options.HttpDebugging = false; output.WriteLine(); output.WriteLine("### Running Federated SPARQL Test"); try { SparqlRemoteEndpoint dbpedia = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org"); SparqlRemoteEndpoint bbcProgs = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://www.bbc.co.uk/programmes"); SparqlRemoteEndpoint books = new SparqlRemoteEndpoint(new Uri("http://sparql.org/books")); String fedQuery = "SELECT * WHERE {?s a ?type} LIMIT 10"; FederatedSparqlRemoteEndpoint fedEndpoint = new FederatedSparqlRemoteEndpoint(new SparqlRemoteEndpoint[] { dbpedia, bbcProgs/*, books*/ }); fedEndpoint.MaxSimultaneousRequests = 1; SparqlResultSet results = fedEndpoint.QueryWithResultSet(fedQuery); foreach (SparqlResult result in results) { output.WriteLine(result.ToString()); } output.WriteLine(); } catch (XmlException xmlEx) { reportError(output, "XML Exception", xmlEx); } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } output.WriteLine(); output.WriteLine("### Running Result Set Parser Tests"); try { int testsPassed = 0; int testsFailed = 0; String[] files = Directory.GetFiles("sparql_tests"); bool passed, passDesired; SparqlResultSet results = new SparqlResultSet(); SparqlXmlParser parser = new SparqlXmlParser(); foreach (String file in files) { if (skipTests.Contains(Path.GetFileName(file))) { output.WriteLine("## Skipping Test of File " + Path.GetFileName(file)); output.WriteLine(); continue; } if (Path.GetExtension(file) != ".srx") { continue; } Debug.WriteLine("Testing File " + Path.GetFileName(file)); output.WriteLine("## Testing File " + Path.GetFileName(file)); output.WriteLine("# Test Started at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat)); passed = false; passDesired = true; try { if (Path.GetFileNameWithoutExtension(file).StartsWith("bad")) { passDesired = false; output.WriteLine("# Desired Result = Parsing Failed"); } else { output.WriteLine("# Desired Result = Parses OK"); } results = new SparqlResultSet(); parser.Load(results, file); passed = true; output.WriteLine("Parsed OK"); } catch (XmlException xmlEx) { reportError(output, "XML Exception", xmlEx); } catch (IOException ioEx) { reportError(output, "IO Exception", ioEx); } catch (RdfParseException parseEx) { reportError(output, "Parsing Exception", parseEx); } catch (RdfException rdfEx) { reportError(output, "RDF Exception", rdfEx); } catch (Exception ex) { reportError(output, "Other Exception", ex); } finally { if (passed && passDesired) { //Passed and we wanted to Pass testsPassed++; output.WriteLine("# Result = Test Passed"); } else if (!passed && passDesired) { //Failed when we should have Passed testsFailed++; output.WriteLine("# Result = Test Failed"); } else if (passed && !passDesired) { //Passed when we should have Failed testsFailed++; output.WriteLine("# Result = Test Failed"); } else { //Failed and we wanted to Fail testsPassed++; output.WriteLine("# Result = Test Passed"); } output.WriteLine("# Results Generated = " + results.Count); output.WriteLine("# Query Result was " + results.Result); output.WriteLine("# Test Ended at " + DateTime.Now.ToString(TestSuite.TestSuiteTimeFormat)); } output.WriteLine(); } output.WriteLine(testsPassed + " Tests Passed"); output.WriteLine(testsFailed + " Tests Failed"); } catch (Exception ex) { reportError(output, "Other Exception", ex); } Console.SetOut(Console.Out); Console.WriteLine("Done"); Debug.WriteLine("Finished"); output.Close(); }
/// <summary> /// Get the label and hyperlink for a given house /// </summary> /// <param name="uri">URI for a house</param> /// <returns>house as a Link object</returns> private Link GetHouse(String uri) { Link house = new Link(); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( "prefix ksamsok: <http://kulturarvsdata.se/ksamsok#>" + "select distinct ?name ?url where{ " + "<" + uri + "> ksamsok:itemLabel ?name." + "<" + uri + "> ksamsok:url ?url." + "}limit 1"); foreach (SparqlResult result in results) { if (result.HasValue("url")) { house.Uri = result.Value("url").ToString(); } if (result.HasValue("name")) { house.Title = result.Value("name").ToString(); } return house; } return new Link(); }
/// <summary> /// Gets a list of houses for a given architect /// </summary> /// <param name="raaId">URI for the architect</param> /// <returns>a list of links objects</returns> private List<Link> GetListOfHouses(String raaId) { List<Link> links = new List<Link>(); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(URI)); SparqlResultSet results = endpoint.QueryWithResultSet( "prefix ksamsok: <http://kulturarvsdata.se/ksamsok#>" + "prefix pres: <http://kulturarvsdata.se/presentation#>" + "select distinct ?uri where{" + "?f ksamsok:architect <" + raaId + ">." + "?uri ksamsok:context ?f.}"); foreach (SparqlResult result in results) { if (result.HasValue("uri")) { links.Add(GetHouse(result.Value("uri").ToString())); } } return links; }