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; } }
/// <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 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 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; } }
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(); }
/// <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 = 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 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); }
private void btnOpenQueryResults_Click(object sender, RoutedEventArgs e) { try { Uri u = new Uri(this.txtEndpoint.Text); String defGraph = this.txtDefaultGraph.Text; SparqlRemoteEndpoint endpoint; if (defGraph.Equals(String.Empty)) { endpoint = new SparqlRemoteEndpoint(u); } else { endpoint = new SparqlRemoteEndpoint(u, defGraph); } String data; using (HttpWebResponse response = endpoint.QueryRaw(this._editor.DocumentManager.ActiveDocument.Text)) { data = new StreamReader(response.GetResponseStream()).ReadToEnd(); try { this._parser = MimeTypesHelper.GetSparqlParser(response.ContentType); } catch (RdfParserSelectionException) { //Ignore here we'll try other means of getting a parser after this } response.Close(); } this._data = data; if (this._parser == null) { try { this._parser = StringParser.GetResultSetParser(this._data); } catch (RdfParserSelectionException) { this._parser = null; } } this.DialogResult = true; this.Close(); } catch (UriFormatException) { MessageBox.Show("You have failed to enter a valid Endpoint URI", "Invalid URI"); } catch (WebException webEx) { MessageBox.Show("A HTTP error occurred making the Query: " + webEx.Message, "Open Query Results Failed"); } catch (Exception ex) { MessageBox.Show("An error occurred while making the Query: " + ex.Message, "Open Query Results Failed"); } }
public void SparqlRemoteEndpointAsyncApiUpdate() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing)) { Assert.Inconclusive("Test Config marks Remote Parsing as unavailable, test cannot be run"); } SparqlRemoteUpdateEndpoint endpoint = RemoteEndpoints.GetUpdateEndpoint(); ManualResetEvent signal = new ManualResetEvent(false); endpoint.Update("LOAD <http://dbpedia.org/resource/Ilkeston> INTO GRAPH <http://example.org/async/graph>", s => { signal.Set(); signal.Close(); }, null); Thread.Sleep(AsyncTimeout); Assert.IsTrue(signal.SafeWaitHandle.IsClosed, "Wait Handle should be closed"); //Check that the Graph was really loaded SparqlRemoteEndpoint queryEndpoint = RemoteEndpoints.GetQueryEndpoint(); IGraph g = queryEndpoint.QueryWithResultGraph("CONSTRUCT FROM <http://example.org/async/graph> WHERE { ?s ?p ?o }"); Assert.IsFalse(g.IsEmpty, "Graph should not be empty"); }
public void SparqlRemoteEndpointSyncVsAsyncTimeDBPedia() { if (!TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing)) { Assert.Inconclusive("Test Config marks Remote Parsing as unavailable, test cannot be run"); } String query; using (StreamReader reader = new StreamReader("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.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"); }
public void SparqlRemoteEndpointWriteThroughHandler() { SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint(); WriteThroughHandler handler = new WriteThroughHandler(typeof(NTriplesFormatter), Console.Out, false); endpoint.QueryWithResultGraph(handler, "CONSTRUCT WHERE { ?s ?p ?o }"); }
public void SparqlRemoteEndpointSyncVsAsyncTimeLocalVirtuoso() { Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing), "Test Config marks Remote Parsing as unavailable, test cannot be run"); String query; using (StreamReader reader = File.OpenText("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.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 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 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 Comparison(List<string> uris, List<string> PredicatesToExclude = null) { //Initializing the server Uri serverUri = new Uri("http://localhost:8890/sparql"); endpoint = new SparqlRemoteEndpoint(serverUri); endpoint.Timeout = 999999; ComparisonOutput = new List<ResourceInformation>(); getData(uris); }
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 void SparqlRemoteEndpointCountHandler() { SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint(); CountHandler handler = new CountHandler(); endpoint.QueryWithResultGraph(handler, "CONSTRUCT { ?s ?p ?o } WHERE { { ?s ?p ?o } UNION { GRAPH ?g { ?s ?p ?o } } }"); Console.WriteLine("Triple 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 void SparqlRemoteVirtuosoWithSponging() { Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseVirtuoso), "Test Config marks Virtuoso as unavailable, cannot run test"); SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(TestConfigManager.GetSetting(TestConfigManager.VirtuosoEndpoint) + "?should-sponge=soft")); endpoint.HttpMode = "POST"; String query = "CONSTRUCT { ?s ?p ?o } FROM <http://www.dotnetrdf.org/configuration#> WHERE { ?s ?p ?o }"; IGraph g = endpoint.QueryWithResultGraph(query); TestTools.ShowGraph(g); Assert.False(g.IsEmpty, "Graph should not be empty"); }
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 void SparqlRemoteEndpointAsyncApiQueryWithResultGraph() { SparqlRemoteEndpoint endpoint = RemoteEndpoints.GetQueryEndpoint(); ManualResetEvent signal = new ManualResetEvent(false); endpoint.QueryWithResultGraph("CONSTRUCT WHERE { ?s ?p ?o }", (r, s) => { TestTools.ShowResults(r); signal.Set(); signal.Close(); }, null); Thread.Sleep(AsyncTimeout); Assert.True(signal.SafeWaitHandle.IsClosed, "Wait Handle 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 ODataSparqlServiceSettings(string name, HttpServerUtility server) { Name = name; _server = server; var serviceConfiguration = System.Configuration.ConfigurationManager.GetSection("odataSparql") as ODataSparqlServiceConfigurationSection; ConfigureWriterSettings(serviceConfiguration); var endpoint = FindEndpointConfiguration(name, serviceConfiguration); ReadEndpointMetadata(endpoint); MaxPageSize = endpoint.MaxPageSize; DefaultLanguageCode = endpoint.DefaultLanguage; SparqlEndpoint = new SparqlRemoteEndpoint(new Uri(endpoint.Address), endpoint.DefaultGraphUri) { Timeout = 60000, RdfAcceptHeader = "application/rdf+xml" }; }
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; }
public EndToEndTests() { using (var edmxStream = File.OpenRead("dbpedia.metadata")) { IEnumerable<EdmError> errors; Microsoft.Data.Edm.Csdl.EdmxReader.TryParse(new XmlTextReader(edmxStream), out _dbpediaModel, out errors); } _dbpediaMap = new SparqlMap("dbpedia.metadata", "http://dbpedia.org/ontology/", NameMapping.Unchanged, "http://dbpedia.org/property/", NameMapping.LowerCamelCase); _sparqlEndpoint = new SparqlRemoteEndpoint(new Uri("http://dbpedia.org/sparql"), "http://dbpedia.org") { Timeout = 60000, RdfAcceptHeader = "application/rdf+xml" }; _odataBase = "http://example.org/odata/"; }
public void SparqlRemoteEndpointAsyncApiUpdate() { SparqlRemoteUpdateEndpoint endpoint = RemoteEndpoints.GetUpdateEndpoint(); ManualResetEvent signal = new ManualResetEvent(false); endpoint.Update("LOAD <http://dbpedia.org/resource/Ilkeston> INTO GRAPH <http://example.org/async/graph>", s => { signal.Set(); signal.Close(); }, null); Thread.Sleep(AsyncTimeout); Assert.IsTrue(signal.SafeWaitHandle.IsClosed, "Wait Handle should be closed"); //Check that the Graph was really loaded SparqlRemoteEndpoint queryEndpoint = RemoteEndpoints.GetQueryEndpoint(); IGraph g = queryEndpoint.QueryWithResultGraph("CONSTRUCT FROM <http://example.org/async/graph> WHERE { ?s ?p ?o }"); Assert.IsFalse(g.IsEmpty, "Graph should not be empty"); }
/// <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; }
/// <summary> /// Finds the triples, related to a URL (this is called after URL rewriting has been activated). /// </summary> /// <param name="parameter">The parameter from URL rewriting.</param> /// <returns></returns> /// <remarks></remarks> public static string FindRelated(string parameter) { Graph resultGraph; if (Configuration.Configuration.RdfPrefix + "/" + parameter == Configuration.Configuration.OntologyNamespace || string.IsNullOrEmpty(parameter)) { var endpoint = new SparqlRemoteEndpoint(new Uri(TripleStoreManager.EndpointUrl), Configuration.Configuration.OntologyNamespace); resultGraph = endpoint.QueryWithResultGraph("construct {?a ?b ?c} where {?a ?b ?c}") as Graph; } else { var endpoint = new SparqlRemoteEndpoint(new Uri(TripleStoreManager.EndpointUrl), ""); var genericQuery = Configuration.Configuration.RdfPrefix + "/" + parameter == Configuration.Configuration.ResourceNamespace || Configuration.Configuration.RdfPrefix + "/" + parameter == Configuration.Configuration.ResourceNamespace + "/" ; resultGraph = genericQuery ? endpoint.QueryWithResultGraph( "construct {?a ?b ?c} where {graph <" + Configuration.Configuration.ResourceNamespace + "> {?a ?b ?c . filter not exists {?a <" + Configuration.Configuration.OntologyNamespace + "#hasParent> ?p }}}") as Graph : endpoint.QueryWithResultGraph("construct {<" + Configuration.Configuration.RdfPrefix + "/" + parameter + "> ?b ?c} where {graph ?g {<" + Configuration.Configuration.RdfPrefix + "/" + parameter + "> ?b ?c}}") as Graph; } var sw = new StringWriter(); var writer = new PrettyRdfXmlWriter(); writer.Save(resultGraph, sw); var result = sw.ToString().Replace("encoding=\"utf-16\"", "encoding=\"utf-8\""); return result; }
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 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> /// 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; }
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> /// 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; }
/// <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; }
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> /// Creates a new Federated SPARQL Endpoint using a given Endpoint /// </summary> /// <param name="endpoint">Endpoint</param> public FederatedSparqlRemoteEndpoint(SparqlRemoteEndpoint endpoint) { this._endpoints.Add(endpoint); }
/// <summary> /// Removes a given endpoint from this endpoint /// </summary> /// <param name="endpoint">Endpoint</param> public void RemoveEndpoint(SparqlRemoteEndpoint endpoint) { this._endpoints.Remove(endpoint); }
/// <summary> /// Adds a additional endpoint to be used by this endpoint /// </summary> /// <param name="endpoint">Endpoint</param> public void AddEndpoint(SparqlRemoteEndpoint endpoint) { this._endpoints.Add(endpoint); }
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 static SparqlResultSet RequestWithHTTP(string request) { SparqlRemoteEndpoint endpoint = new SparqlRemoteEndpoint(new Uri(new StreamReader("endpointURI.txt").ReadLine())); endpoint.Timeout = 999999; return endpoint.QueryWithResultSet(request); }
/// <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; }
/// <summary> /// Tries to load a SPARQL Endpoint based on information from the Configuration Graph /// </summary> /// <param name="g">Configuration Graph</param> /// <param name="objNode">Object Node</param> /// <param name="targetType">Target Type</param> /// <param name="obj">Output Object</param> /// <returns></returns> public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj) { BaseEndpoint endpoint = null; obj = null; switch (targetType.FullName) { case Endpoint: String endpointUri = ConfigurationLoader.GetConfigurationValue(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyEndpointUri)); if (endpointUri == null) return false; //Get Default/Named Graphs if specified IEnumerable<String> defaultGraphs = from n in ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyDefaultGraphUri)) select n.ToString(); IEnumerable<String> namedGraphs = from n in ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyNamedGraphUri)) select n.ToString(); endpoint = new SparqlRemoteEndpoint(new Uri(endpointUri), defaultGraphs, namedGraphs); break; #if !SILVERLIGHT case FederatedEndpoint: IEnumerable<INode> endpoints = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyEndpoint)); foreach (INode e in endpoints) { Object temp = ConfigurationLoader.LoadObject(g, e); if (temp is SparqlRemoteEndpoint) { if (endpoint == null) { endpoint = new FederatedSparqlRemoteEndpoint((SparqlRemoteEndpoint)temp); } else { ((FederatedSparqlRemoteEndpoint)endpoint).AddEndpoint((SparqlRemoteEndpoint)temp); } } else { throw new DotNetRdfConfigurationException("Unable to load the SPARQL Endpoint identified by the Node '" + e.ToString() + "' as one of the values for the dnr:endpoint property points to an Object which cannot be loaded as an object which is a SparqlRemoteEndpoint"); } } break; #endif } if (endpoint != null) { //Are there any credentials specified? String user, pwd; ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd); if (user != null && pwd != null) { endpoint.SetCredentials(user, pwd); } #if !NO_PROXY //Is there a Proxy Server specified INode proxyNode = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyProxy)); if (proxyNode != null) { Object proxy = ConfigurationLoader.LoadObject(g, proxyNode); if (proxy is WebProxy) { endpoint.Proxy = (WebProxy)proxy; //Are we supposed to use the same credentials for the proxy as for the endpoint? bool useCredentialsForProxy = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyUseCredentialsForProxy), false); if (useCredentialsForProxy) { endpoint.UseCredentialsForProxy = true; } } else { throw new DotNetRdfConfigurationException("Unable to load SPARQL Endpoint identified by the Node '" + objNode.ToString() + "' as the value for the dnr:proxy property points to an Object which cannot be loaded as an object of type WebProxy"); } } #endif } obj = endpoint; return (endpoint != null); }
private UnionGraph GetUnionGraph() { SparqlQuery restaurantsQuery = GetRestaurantsQuery(); SparqlQuery hotelsQuery = GetHotelsQuery(); SparqlQuery museumsQuery = GetGeneralQuery("Museum"); SparqlQuery hospitalsQuery = GetGeneralQuery("Hospital"); SparqlQuery shopsQuery = GetGeneralQuery("ShoppingMall"); Uri uri = new Uri(@"http://dbpedia.org/sparql"); SparqlRemoteEndpoint endPoint = new SparqlRemoteEndpoint(uri); ISparqlQueryProcessor processor = new RemoteQueryProcessor(endPoint); Graph restaurantsGraph = (Graph)processor.ProcessQuery(restaurantsQuery); Graph hotelsGraph = (Graph)processor.ProcessQuery(hotelsQuery); Graph museumsGraph = (Graph)processor.ProcessQuery(museumsQuery); Graph hospitalsGraph = (Graph)processor.ProcessQuery(hospitalsQuery); Graph shopsGraph = (Graph)processor.ProcessQuery(shopsQuery); List<Graph> all = new List<Graph>(); all.Add(restaurantsGraph); all.Add(hotelsGraph); all.Add(museumsGraph); all.Add(hospitalsGraph); all.Add(shopsGraph); return new UnionGraph(new Graph(), all); }
/// <summary> /// Creates a new Remote Query Processor /// </summary> /// <param name="endpoint">SPARQL Endpoint</param> public RemoteQueryProcessor(SparqlRemoteEndpoint endpoint) { _endpoint = endpoint; }
/// <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 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(); }
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); }