public void StorageSesameSaveLoad() { try { Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/SesameTest"); SesameHttpProtocolConnector sesame = new SesameHttpProtocolConnector("http://nottm-virtual.ecs.soton.ac.uk:8080/openrdf-sesame/", "unit-test"); sesame.SaveGraph(g); //Options.HttpDebugging = true; //Options.HttpFullDebugging = true; Graph h = new Graph(); sesame.LoadGraph(h, "http://example.org/SesameTest"); Assert.IsFalse(h.IsEmpty, "Graph should not be empty after loading"); Assert.AreEqual(g, h, "Graphs should have been equal"); } catch (Exception ex) { TestTools.ReportError("Error", ex, true); } finally { //Options.HttpFullDebugging = false; //Options.HttpDebugging = true; } }
public void StorageSesameDeleteTriples3() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/sesame/chinese"); FileLoader.Load(g, @"..\\resources\chinese.ttl"); sesame.SaveGraph(g); String ask = "ASK WHERE { GRAPH <http://example.org/sesame/chinese> { ?s ?p '例子' } }"; Object results = sesame.Query(ask); Assert.IsAssignableFrom <SparqlResultSet>(results); if (results is SparqlResultSet) { TestTools.ShowResults(results); Assert.True(((SparqlResultSet)results).Result); } // Now delete the triple in question sesame.UpdateGraph(g.BaseUri, null, g.Triples); // Re-issue ASK to check deletion results = sesame.Query(ask); Assert.IsAssignableFrom <SparqlResultSet>(results); if (results is SparqlResultSet) { TestTools.ShowResults(results); Assert.False(((SparqlResultSet)results).Result); } }
private void queryButton_Click(object sender, EventArgs e) { DisableInput(); using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { try { SparqlResultSet queryResult = connector.Query(queryTextBox.Text) as SparqlResultSet; if (queryResult != null) { resultTextBox.Text = queryResult.Results.SerializeObject(); } else { resultTextBox.Text = queryResult.SerializeObject(); } } catch (Exception ex) { resultTextBox.Text = ex.Message; } finally { EnableInput(); } } } }
public void StorageSesameDeleteGraph2() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); Uri graphUri = null; sesame.DeleteGraph(graphUri); Graph g = new Graph(); FileLoader.Load(g, "resources\\InferenceTest.ttl"); g.BaseUri = graphUri; sesame.SaveGraph(g); Graph h = new Graph(); sesame.LoadGraph(h, graphUri); Assert.False(h.IsEmpty, "Graph should not be empty after loading"); Assert.Equal(g, h); sesame.DeleteGraph(graphUri); h = new Graph(); sesame.LoadGraph(h, graphUri); Assert.True(h.IsEmpty, "Graph should be equal after deletion"); Assert.NotEqual(g, h); }
public void StorageSesameDeleteTriples2() { Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/SesameTest/Delete2"); g.NamespaceMap.AddNamespace("ex", new Uri("http://example.org/ns#")); g.Assert(g.CreateUriNode("ex:subj"), g.CreateUriNode("ex:pred"), g.CreateUriNode("ex:obj")); SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); sesame.SaveGraph(g); Console.WriteLine("Graph before deletion"); TestTools.ShowGraph(g); //Delete the single triple sesame.UpdateGraph(g.BaseUri, null, g.Triples); Object results = sesame.Query("ASK WHERE { GRAPH <http://example.org/SesameTest/Delete2> { <http://example.org/ns#subj> ?p ?o } }"); if (results is SparqlResultSet) { Assert.IsFalse(((SparqlResultSet)results).Result, "There should no longer be any triples present in the graph"); } Graph h = new Graph(); sesame.LoadGraph(h, g.BaseUri); Console.WriteLine("Graph after deletion"); TestTools.ShowGraph(h); Assert.IsTrue(h.IsEmpty, "Graph should not be completely empty"); Assert.IsFalse(g.Equals(h), "Graph retrieved should not be equal to original Graph"); }
public void StorageSesameSparqlUpdate1() { Skip.IfNot(TestConfigManager.GetSettingAsBoolean(TestConfigManager.UseRemoteParsing), "Test Config marks Remote Parsing as unavailable, test cannot be run"); SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); sesame.Update(@"DROP GRAPH <http://example.org/sparqlUpdateLoad>; LOAD <http://dbpedia.org/resource/Ilkeston> INTO GRAPH <http://example.org/sparqlUpdateLoad>; DELETE WHERE { GRAPH <http://example.org/sparqlUpdateLoad> { ?s <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?long ; <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat } }"); Graph orig = new Graph(); orig.LoadFromUri(new Uri("http://dbpedia.org/resource/Ilkeston")); orig.Retract(orig.GetTriplesWithPredicate(new Uri("http://www.w3.org/2003/01/geo/wgs84_pos#long")).ToList()); orig.Retract(orig.GetTriplesWithPredicate(new Uri("http://www.w3.org/2003/01/geo/wgs84_pos#lat")).ToList()); Graph actual = new Graph(); sesame.LoadGraph(actual, "http://example.org/sparqlUpdateLoad"); GraphDiffReport diff = orig.Difference(actual); if (!diff.AreEqual) { TestTools.ShowDifferences(diff); } Assert.Equal(orig, actual); }
public void StorageSesameSparqlUpdate1() { try { Options.HttpDebugging = true; SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); sesame.Update("DROP GRAPH <http://example.org/sparqlUpdateLoad>; LOAD <http://dbpedia.org/resource/Ilkeston> INTO GRAPH <http://example.org/sparqlUpdateLoad>"); Graph orig = new Graph(); orig.LoadFromUri(new Uri("http://dbpedia.org/resource/Ilkeston")); Graph actual = new Graph(); sesame.LoadGraph(actual, "http://example.org/sparqlUpdateLoad"); GraphDiffReport diff = orig.Difference(actual); if (!diff.AreEqual) { TestTools.ShowDifferences(diff); } Assert.AreEqual(orig, actual, "Graphs should be equal"); } finally { Options.HttpDebugging = false; } }
public void StorageSesameSaveLoad() { try { Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/SesameTest"); SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); sesame.SaveGraph(g); //Options.HttpDebugging = true; //Options.HttpFullDebugging = true; Graph h = new Graph(); sesame.LoadGraph(h, "http://example.org/SesameTest"); Assert.IsFalse(h.IsEmpty, "Graph should not be empty after loading"); Assert.AreEqual(g, h, "Graphs should have been equal"); } finally { //Options.HttpFullDebugging = false; //Options.HttpDebugging = true; } }
public void StorageSesameDeleteTriples1() { Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/SesameTest"); SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); sesame.SaveGraph(g); Console.WriteLine("Graph before deletion"); TestTools.ShowGraph(g); //Delete all Triples about the Ford Fiesta sesame.UpdateGraph(g.BaseUri, null, g.GetTriplesWithSubject(new Uri("http://example.org/vehicles/FordFiesta"))); Object results = sesame.Query("ASK WHERE { GRAPH <http://example.org/SesameTest> { <http://example.org/vehicles/FordFiesta> ?p ?o } }"); if (results is SparqlResultSet) { Assert.IsFalse(((SparqlResultSet)results).Result, "There should no longer be any triples about the Ford Fiesta present"); } Graph h = new Graph(); sesame.LoadGraph(h, g.BaseUri); Console.WriteLine("Graph after deletion"); TestTools.ShowGraph(h); Assert.IsFalse(h.IsEmpty, "Graph should not be completely empty"); Assert.IsTrue(g.HasSubGraph(h), "Graph retrieved with missing Triples should be a sub-graph of the original Graph"); Assert.IsFalse(g.Equals(h), "Graph retrieved should not be equal to original Graph"); }
public void StorageSesameSparqlUpdate4() { // Test case adapted from CORE-374 sample update SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); // Insert the Data StringBuilder updates = new StringBuilder(); using (StreamReader reader = File.OpenText(@"..\\resources\core-374.ru")) { updates.Append(reader.ReadToEnd()); reader.Close(); } sesame.Update(updates.ToString()); String ask = "ASK WHERE { GRAPH <http://example.org/sesame/core-374> { ?s ?p 'République du Niger'@fr } }"; // Issue query to validate data was added Object results = sesame.Query(ask); Assert.IsAssignableFrom <SparqlResultSet>(results); if (results is SparqlResultSet) { TestTools.ShowResults(results); Assert.True(((SparqlResultSet)results).Result); } }
public void StorageSesameDeleteGraph1() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); Uri graphUri = new Uri("http://example.org/Sesame/delete"); Graph g = new Graph(); FileLoader.Load(g, "resources\\InferenceTest.ttl"); g.BaseUri = graphUri; sesame.SaveGraph(g); Graph h = new Graph(); sesame.LoadGraph(h, graphUri); Assert.IsFalse(h.IsEmpty, "Graph should not be empty after loading"); Assert.AreEqual(g, h, "Graphs should have been equal"); sesame.DeleteGraph(graphUri); h = new Graph(); sesame.LoadGraph(h, graphUri); Assert.IsTrue(h.IsEmpty, "Graph should be equal after deletion"); Assert.AreNotEqual(g, h, "Graphs should not be equal after deletion"); }
/// <summary> /// Get all statistical regions in Slovenia /// Gorenjska, Primorska ... /// </summary> /// <returns></returns> public IList<String> GetAllSlovenianRegions() { IList<String> result = new List<String>(); using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?label /// WHERE { /// SERVICE <http://dbpedia.org/sparql> { /// ?region dbpedia-owl:type dbpedia:Statistical_regions_of_Slovenia . /// ?region rdfs:label ?label . /// FILTER(langMatches(lang(?label ), "NL")) /// } /// } string query = "SELECT ?label " + "WHERE {{ " + "SERVICE <http://dbpedia.org/sparql> {{ " + "?region dbpedia-owl:type dbpedia:Statistical_regions_of_Slovenia . " + "?region rdfs:label ?label . " + "FILTER(langMatches(lang(?label ), \"NL\")) " + "}} " + "}}"; SparqlResultSet queryResult = connector.QueryFormat(query); if (queryResult == null) { this._logger.FatalFormat("RepositoryService, GetAllSlovenianRegions, Query result is null - QUERY: {0}", query); } else if (!queryResult.Results.IsEmpty()) { LiteralNode literalNode; foreach (SparqlResult res in queryResult.Results) { literalNode = res.Value("label") as LiteralNode; if (literalNode != null) { result.Add(literalNode.Value); } } } return result; } else { this._logger.FatalFormat("RepositoryService, GetAllSlovenianRegions, SesameHttpProtocolConnector is not ready"); } } return null; }
public void StorageSesameAsk() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); String ask = "ASK WHERE { ?s ?p ?o }"; Object results = sesame.Query(ask); Assert.IsAssignableFrom <SparqlResultSet>(results); if (results is SparqlResultSet) { TestTools.ShowResults(results); } }
/// <summary> /// Clear repository - delete graph /// </summary> public void ClearRepository() { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { connector.DeleteGraph(RepositoryHelper.BaseUrl.ToUri()); } else { this._logger.FatalFormat("RepositoryService, Clear, SesameHttpProtocolConnector is not ready"); } } }
public void StorageSesameDescribe() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); String describe = "DESCRIBE <http://example.org/vehicles/FordFiesta>"; Object results = sesame.Query(describe); Assert.IsAssignableFrom <IGraph>(results); if (results is IGraph) { TestTools.ShowGraph((IGraph)results); } }
public void StorageSesameSaveEmptyGraph1() { Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/Sesame/empty"); SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); sesame.SaveGraph(g); Graph h = new Graph(); sesame.LoadGraph(h, "http://example.org/Sesame/empty"); Assert.True(h.IsEmpty, "Graph should be empty after loading"); Assert.Equal(g, h); }
public void StorageSesameDescribe() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); String describe = "DESCRIBE <http://example.org/vehicles/FordFiesta>"; Object results = sesame.Query(describe); if (results is IGraph) { TestTools.ShowGraph((IGraph)results); } else { Assert.Fail("Failed to get a Graph as expected"); } }
public void StorageSesameAsk() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); String ask = "ASK WHERE { ?s ?p ?o }"; Object results = sesame.Query(ask); if (results is SparqlResultSet) { TestTools.ShowResults(results); } else { Assert.Fail("Failed to get a Result Set as expected"); } }
public void StorageSesameSparqlUpdate2() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); Graph g = new Graph(); g.LoadFromEmbeddedResource("VDS.RDF.Configuration.configuration.ttl"); g.BaseUri = new Uri("http://example.org/sparqlUpdateDeleteWhere"); sesame.SaveGraph(g); sesame.Update("WITH <http://example.org/sparqlUpdateDeleteWhere> DELETE { ?s a ?type } WHERE { ?s a ?type }"); Graph h = new Graph(); sesame.LoadGraph(h, "http://example.org/sparqlUpdateDeleteWhere"); INode rdfType = h.CreateUriNode("rdf:type"); Assert.False(h.GetTriplesWithPredicate(rdfType).Any(), "Should not be any rdf:type triples after SPARQL Update operation"); }
public void StorageSesameSparqlUpdate3() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); // Ensure required graph is present Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/sesame/chinese"); FileLoader.Load(g, @"resources\chinese.ttl"); sesame.SaveGraph(g); String ask = "ASK WHERE { GRAPH <http://example.org/sesame/chinese> { ?s ?p '例子' } }"; // Issue query to validate data was added Object results = sesame.Query(ask); if (results is SparqlResultSet) { TestTools.ShowResults(results); Assert.IsTrue(((SparqlResultSet)results).Result); } else { Assert.Fail("Failed to get a Result Set as expected"); } // Issue a DELETE for the Chinese literal String update = "DELETE WHERE { GRAPH <http://example.org/sesame/chinese> { ?s ?p '例子' } }"; sesame.Update(update); // Re-issue query to validate triple was deleted results = sesame.Query(ask); if (results is SparqlResultSet) { TestTools.ShowResults(results); Assert.IsFalse(((SparqlResultSet)results).Result); } else { Assert.Fail("Failed to get a Result Set as expected"); } }
public void StorageSesameChinese() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/sesame/chinese"); FileLoader.Load(g, @"..\\resources\chinese.ttl"); sesame.SaveGraph(g); String ask = "ASK WHERE { GRAPH <http://example.org/sesame/chinese> { ?s ?p '例子' } }"; Object results = sesame.Query(ask); Assert.IsAssignableFrom <SparqlResultSet>(results); if (results is SparqlResultSet) { TestTools.ShowResults(results); Assert.True(((SparqlResultSet)results).Result); } }
public void StorageSesameCyrillic() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/sesame/cyrillic"); FileLoader.Load(g, "cyrillic.rdf"); sesame.SaveGraph(g); String ask = "ASK WHERE {?s ?p 'литерал'}"; Object results = sesame.Query(ask); if (results is SparqlResultSet) { TestTools.ShowResults(results); } else { Assert.Fail("Failed to get a Result Set as expected"); } }
public void StorageSesameDeleteTriples() { try { Graph g = new Graph(); FileLoader.Load(g, "InferenceTest.ttl"); g.BaseUri = new Uri("http://example.org/SesameTest"); SesameHttpProtocolConnector sesame = new SesameHttpProtocolConnector("http://nottm-virtual.ecs.soton.ac.uk:8080/openrdf-sesame/", "unit-test"); sesame.SaveGraph(g); Console.WriteLine("Graph before deletion"); TestTools.ShowGraph(g); //Delete all Triples about the Ford Fiesta sesame.UpdateGraph(g.BaseUri, null, g.GetTriplesWithSubject(new Uri("http://example.org/vehicles/FordFiesta"))); Object results = sesame.Query("ASK WHERE { <http://example.org/vehicles/FordFiesta> ?p ?o }"); if (results is SparqlResultSet) { Assert.IsFalse(((SparqlResultSet)results).Result, "There should no longer be any triples about the Ford Fiesta present"); } Graph h = new Graph(); sesame.LoadGraph(h, g.BaseUri); Console.WriteLine("Graph after deletion"); TestTools.ShowGraph(h); Assert.IsFalse(h.IsEmpty, "Graph should not be completely empty"); Assert.IsTrue(g.HasSubGraph(h), "Graph retrieved with missing Triples should be a sub-graph of the original Graph"); Assert.IsFalse(g.Equals(h), "Graph retrieved should not be equal to original Graph"); } catch (Exception ex) { TestTools.ReportError("Error", ex, true); } }
public void StorageSesameSaveEmptyGraph3() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); Uri graphUri = null; Console.WriteLine("Deleting any existing graph"); sesame.DeleteGraph(graphUri); Console.WriteLine("Existing graph deleted"); // First create a non-empty graph Graph g = new Graph(); g.BaseUri = graphUri; g.Assert(g.CreateBlankNode(), g.CreateUriNode("rdf:type"), g.CreateUriNode(new Uri("http://example.org/BNode"))); Console.WriteLine("Saving non-empty graph"); sesame.SaveGraph(g); Console.WriteLine("Non-empty graph saved"); Graph h = new Graph(); sesame.LoadGraph(h, graphUri); Assert.False(h.IsEmpty, "Graph should not be empty after loading"); Assert.Equal(g, h); // Now attempt to overwrite with an empty graph g = new Graph(); g.BaseUri = graphUri; Console.WriteLine("Attempting to save empty graph with same name"); sesame.SaveGraph(g); Console.WriteLine("Empty graph saved"); h = new Graph(); sesame.LoadGraph(h, graphUri); // Since saving to default graph does not overwrite the graph we've just retrieved must contain the empty graph as a sub-graph Assert.True(h.HasSubGraph(g)); }
public void StorageSesameSaveEmptyGraph2() { SesameHttpProtocolConnector sesame = SesameTests.GetConnection(); Uri graphUri = new Uri("http://example.org/Sesame/empty2"); Console.WriteLine("Deleting any existing graph"); sesame.DeleteGraph(graphUri); Console.WriteLine("Existing graph deleted"); // First create a non-empty graph Graph g = new Graph(); g.BaseUri = graphUri; g.Assert(g.CreateBlankNode(), g.CreateUriNode("rdf:type"), g.CreateUriNode(new Uri("http://example.org/BNode"))); Console.WriteLine("Saving non-empty graph"); sesame.SaveGraph(g); Console.WriteLine("Non-empty graph saved"); Graph h = new Graph(); sesame.LoadGraph(h, graphUri); Assert.False(h.IsEmpty, "Graph should not be empty after loading"); Assert.Equal(g, h); // Now attempt to save an empty graph as well g = new Graph(); g.BaseUri = graphUri; Console.WriteLine("Attempting to save empty graph with same name"); sesame.SaveGraph(g); Console.WriteLine("Empty graph saved"); h = new Graph(); sesame.LoadGraph(h, graphUri); Assert.True(h.IsEmpty, "Graph should be empty after loading"); Assert.Equal(g, h); }
public void StorageSesameAsk() { try { SesameHttpProtocolConnector sesame = new SesameHttpProtocolConnector("http://nottm-virtual.ecs.soton.ac.uk:8080/openrdf-sesame/", "unit-test"); String ask = "ASK WHERE { ?s ?p ?o }"; Object results = sesame.Query(ask); if (results is SparqlResultSet) { TestTools.ShowResults(results); } else { Assert.Fail("Failed to get a Result Set as expected"); } } catch (Exception ex) { TestTools.ReportError("Error", ex, true); } }
/// <summary> /// Save new user /// </summary> /// <param name="user"></param> /// <param name="update"></param> /// <returns>User guid url</returns> public string SaveUser(User user, bool update = false) { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?guidUrl /// WHERE { /// ?guidUrl rdf:type sioc:UserAccount /// ; news:ID "id" /// } string query = string.Format("SELECT ?guidUrl WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" }}", Predicate.RdfType, Predicate.SiocUserAccount, Predicate.NewsId, user.Id); if (update) { /// SELECT ?guidUrl ?predicate ?object /// WHERE { /// ?guidUrl rdf:type sioc:UserAccount /// ; news:ID "id" /// ; ?predicate ?object /// } query = string.Format("SELECT ?guidUrl ?predicate ?object WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" ; ?predicate ?object }}", Predicate.RdfType, Predicate.SiocUserAccount, Predicate.NewsId, user.Id); } SparqlResultSet queryResult = connector.QueryFormat(query); string guidUrl = null; UriNode guidUrlNode = null; if (queryResult != null && !queryResult.Results.IsEmpty()) { /// user already exist guidUrlNode = queryResult.Results.First().Value("guidUrl") as UriNode; guidUrl = guidUrlNode.Uri.AbsoluteUri; if (!update) { return guidUrl; } } /// save new or update using (IGraph g = new Graph()) { g.BaseUri = RepositoryHelper.BaseUrl.ToUri(); IList<Triple> newTriples = new List<Triple>(); IList<Triple> removeTriples = new List<Triple>(); if (string.IsNullOrEmpty(guidUrl)) { update = false; guidUrl = string.Format(RepositoryHelper.UserUrlPattern, Guid.NewGuid().ToString()); } INode subject = guidUrlNode != null ? guidUrlNode.CopyNode(g) : guidUrl.ToUriNode(g); user.RepositoryGuidUrl = guidUrl; #region User if (!update) { /// initialize newTriples.Add(new Triple(subject, Predicate.RdfType.ToUriNode(g), Predicate.SiocUserAccount.ToUriNode(g))); /// user function if (user.Function == UserFunctionEnum.Reader) { newTriples.Add(new Triple(subject, Predicate.SiocHasFunction.ToUriNode(g), RepositoryHelper.ReaderRoleUrl.ToUriNode(g))); } else if (user.Function == UserFunctionEnum.Journalist) { newTriples.Add(new Triple(subject, Predicate.SiocHasFunction.ToUriNode(g), RepositoryHelper.JournalistRoleUrl.ToUriNode(g))); } } /// accessed date newTriples.Add(new Triple(subject, Predicate.NewsAccessed.ToUriNode(g), user.AccessedDate.ToString(RepositoryHelper.DateTimeFormat).ToLiteralNode(g, dataType: RepositoryHelper.DateTimeDataType))); /// ID newTriples.Add(new Triple(subject, Predicate.NewsId.ToUriNode(g), user.Id.ToLiteralNode(g))); /// Url newTriples.Add(new Triple(subject, Predicate.RdfsSeeAlso.ToUriNode(g), user.Url.ToUriNode(g))); /// gender if (user.Gender == UserGenderEnum.Male) { newTriples.Add(new Triple(subject, Predicate.MmcHasGender.ToUriNode(g), RepositoryHelper.GenderMaleUrl.ToUriNode(g))); } else if (user.Gender == UserGenderEnum.Female) { newTriples.Add(new Triple(subject, Predicate.MmcHasGender.ToUriNode(g), RepositoryHelper.GenderFemaleUrl.ToUriNode(g))); } /// registered date if (user.DateCreated.HasValue) { newTriples.Add(new Triple(subject, Predicate.DctCreated.ToUriNode(g), user.DateCreated.Value.ToString(RepositoryHelper.DateFormat).ToLiteralNode(g))); } /// nickname if (!string.IsNullOrEmpty(user.Name)) { newTriples.Add(new Triple(subject, Predicate.NewsNickname.ToUriNode(g), user.Name.ToLiteralNode(g))); } /// email if (!string.IsNullOrEmpty(user.Email)) { newTriples.Add(new Triple(subject, Predicate.MmcEmail.ToUriNode(g), user.Email.ToLiteralNode(g))); } /// birthdate if (user.Birthdate.HasValue) { newTriples.Add(new Triple(subject, Predicate.MmcBirthDate.ToUriNode(g), user.Birthdate.Value.ToString(RepositoryHelper.DateFormat).ToLiteralNode(g, dataType: RepositoryHelper.DateDataType))); } /// about if (!string.IsNullOrEmpty(user.Description)) { newTriples.Add(new Triple(subject, Predicate.MmcAbout.ToUriNode(g), user.Description.ToLiteralNode(g))); } /// remove old triples if (update) { this.RemoveTriples(removeTriples, queryResult, g, subject, new string[] { Predicate.NewsAccessed, Predicate.NewsId, Predicate.RdfsSeeAlso, Predicate.MmcHasGender, Predicate.DctCreated, Predicate.NewsNickname, Predicate.MmcEmail, Predicate.MmcBirthDate, Predicate.MmcAbout }); } #endregion User #region UserStatistics string statsGuid = Guid.NewGuid().ToString(); string statsGuidUrl = string.Format(RepositoryHelper.UserStatisticsUrlPattern, statsGuid); UriNode statsGuidNode = null; /// read existing statsGuidUrl if (update) { statsGuidNode = queryResult.Results .First(x => x.Value("predicate").ToSafeString() == Predicate.MmcUserStatistics.ToFullNamespaceUrl()) .Value("object") as UriNode; statsGuidUrl = statsGuidNode.Uri.AbsoluteUri; /// SELECT ?predicate ?object /// WHERE { /// <guid> rdf:type mmc:Statistics /// ; ?predicate ?object /// } query = string.Format("SELECT ?predicate ?object WHERE {{ <{0}> {1} {2} ; ?predicate ?object }}", statsGuidUrl, Predicate.RdfType, Predicate.MmcStatistics); queryResult = connector.QueryFormat(query); if (queryResult == null || queryResult.Results.IsEmpty()) { this._logger.FatalFormat("RepositoryService, SaveUser, Update statistics ERROR - QUERY: {0}", query); } } INode statsSubject = statsGuidNode != null ? statsGuidNode.CopyNode(g) : statsGuidUrl.ToUriNode(g); if(!update) { /// initialize newTriples.Add(new Triple(statsSubject, Predicate.RdfType.ToUriNode(g), Predicate.MmcStatistics.ToUriNode(g))); newTriples.Add(new Triple(subject, Predicate.MmcUserStatistics.ToUriNode(g), statsSubject)); } /// rating if (user.Rating > -1) { newTriples.Add(new Triple(statsSubject, Predicate.NewsAvgRating.ToUriNode(g), user.Rating.ToString().ToLiteralNode(g, dataType: RepositoryHelper.DecimalDataType))); } /// num of ratings if (user.NumOfRatings > -1) { newTriples.Add(new Triple(statsSubject, Predicate.NewsNRatings.ToUriNode(g), user.NumOfRatings.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// num of forum posts if (user.ForumPosts > -1) { newTriples.Add(new Triple(statsSubject, Predicate.MmcNumOfForumPosts.ToUriNode(g), user.ForumPosts.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// num of blog posts if (user.BlogPosts > -1) { newTriples.Add(new Triple(statsSubject, Predicate.MmcNumOfBlogPosts.ToUriNode(g), user.BlogPosts.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// num of published pictures if (user.ForumPosts > -1) { newTriples.Add(new Triple(statsSubject, Predicate.MmcNumOfPublishedPictures.ToUriNode(g), user.PublishedPictures.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// num of published comments if (user.PublishedComments > -1) { newTriples.Add(new Triple(statsSubject, Predicate.MmcNumOfPublishedComments.ToUriNode(g), user.PublishedComments.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// num of published videos if (user.PublishedVideos > -1) { newTriples.Add(new Triple(statsSubject, Predicate.MmcNumOfPublishedVideos.ToUriNode(g), user.PublishedVideos.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// remove old triples if (update) { this.RemoveTriples(removeTriples, queryResult, g, statsSubject, new string[] { Predicate.NewsAvgRating, Predicate.NewsNRatings, Predicate.MmcNumOfForumPosts, Predicate.MmcNumOfBlogPosts, Predicate.MmcNumOfPublishedPictures, Predicate.MmcNumOfPublishedComments, Predicate.MmcNumOfPublishedVideos }); } #endregion UserStatistics connector.UpdateGraph(g.BaseUri, newTriples, removeTriples); return guidUrl; } } else { this._logger.FatalFormat("RepositoryService, SaveUser, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Search user by id (id is part of url) /// </summary> /// <param name="id"></param> /// <param name="update"></param> /// <returns>Guid url</returns> public string SearchUserById(string id, bool update = false) { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?guidUrl /// WHERE { /// ?guidUrl rdf:type sioc:UserAccount /// ; news:ID "id" /// } string query = string.Format("SELECT ?guidUrl WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" }}", Predicate.RdfType, Predicate.SiocUserAccount, Predicate.NewsId, id); if (update) { /// filter users accessed older than 2 weeks DateTime olderThanDays = DateTime.Today.AddDays(-RtvSloConfig.Step3UpdateOlderThanDays); /// SELECT ?guidUrl /// WHERE { /// ?guidUrl rdf:type sioc:UserAccount /// ; news:ID "id" /// ; news:accessed ?date /// FILTER ( ?date < "2013-10-01") /// } query = string.Format( "SELECT ?guidUrl " + "WHERE {{ " + "?guidUrl {0} {1} " + "; {2} \"{3}\" " + "; {4} ?date " + "FILTER (?date < \"{5}\") " + "}}", Predicate.RdfType, Predicate.SiocUserAccount, Predicate.NewsId, id, Predicate.NewsAccessed, olderThanDays.ToString(RepositoryHelper.DateTimeFormat)); } SparqlResultSet queryResult = connector.QueryFormat(query); if (queryResult == null) { this._logger.FatalFormat("RepositoryService, SearchUserById, Query result is null - QUERY: {0}", query); return null; ; } else if (queryResult.Results.IsEmpty()) { return null; } UriNode guidUrlNode = queryResult.Results.First().Value("guidUrl") as UriNode; return guidUrlNode.Uri.AbsoluteUri; } else { this._logger.FatalFormat("RepositoryService, SearchUserById, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Search user by name (for posts author) /// </summary> /// <param name="name"></param> /// <returns>Guid url</returns> public string SearchUserByName(string name) { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?guidUrl /// WHERE { /// ?guidUrl rdf:type sioc:UserAccount /// ; news:nickname "name" /// } string query = string.Format("SELECT ?guidUrl WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" }}", Predicate.RdfType, Predicate.SiocUserAccount, Predicate.NewsNickname, name); SparqlResultSet queryResult = connector.QueryFormat(query); if (queryResult == null) { this._logger.FatalFormat("RepositoryService, SearchUserByName, Query result is null - QUERY: {0}", query); return null; ; } else if (queryResult.Results.IsEmpty()) { return null; } UriNode guidUrlNode = queryResult.Results.First().Value("guidUrl") as UriNode; return guidUrlNode.Uri.AbsoluteUri; } else { this._logger.FatalFormat("RepositoryService, SearchUserByName, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Check if categories already exist in repository and save them if not /// </summary> /// <param name="category"></param> /// <returns>Child category repository url</returns> private string CheckAndSaveCategories(Category category) { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { Category tempCategory = category; if (category.HasChild) { tempCategory = category.LastChild; } /// SELECT ?url /// WHERE { /// ?url rdf:type sioc:Category . /// ?url rdfs:seeAlso <cat_url> . /// } string queryPattern = "SELECT ?url WHERE {{ ?url {0} {1} . ?url {2} <{3}> . }}"; SparqlResultSet result = connector.QueryFormat(queryPattern, Predicate.RdfType, Predicate.SiocCategory, Predicate.RdfsSeeAlso, tempCategory.Url); if (result == null) { return null; } else if (!result.Results.IsEmpty()) { /// child category already exists return result.Results.First().Value("url").ToSafeString(); } string categoryRepositoryUrl = null; /// save category using (IGraph g = new Graph()) { g.BaseUri = RepositoryHelper.BaseUrl.ToUri(); IList<Triple> newTriples = new List<Triple>(); tempCategory = category; bool topCategory = true; do { /// fetch and check if category exists result = connector.QueryFormat(queryPattern, Predicate.RdfType, Predicate.SiocCategory, Predicate.RdfsSeeAlso, tempCategory.Url); if (result == null) { return null; } else if (result.Results.IsEmpty()) { categoryRepositoryUrl = string.Format(RepositoryHelper.CategoryUrlPattern, tempCategory.Label); INode subject = categoryRepositoryUrl.ToUriNode(g); newTriples.Add(new Triple(subject, Predicate.RdfType.ToUriNode(g), Predicate.SiocCategory.ToUriNode(g))); newTriples.Add(new Triple(subject, Predicate.RdfsSeeAlso.ToUriNode(g), tempCategory.Url.ToUriNode(g))); newTriples.Add(new Triple(subject, Predicate.RdfsLabel.ToUriNode(g), tempCategory.Label.ToLiteralNode(g))); if (!topCategory) { INode parentObject = string.Format(RepositoryHelper.CategoryUrlPattern, tempCategory.Parent.Label).ToUriNode(g); newTriples.Add(new Triple(subject, Predicate.NewsSubCategoryOf.ToUriNode(g), parentObject)); } } topCategory = false; tempCategory = tempCategory.NextChild; } while (tempCategory != null); /// save new category connector.UpdateGraph(g.BaseUri, newTriples, new List<Triple>()); } return categoryRepositoryUrl; } else { this._logger.FatalFormat("RepositoryService, CheckAndSaveCategories, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Get next post which hasn't been scraped and saved /// </summary> /// <returns>Post (url, id)</returns> public Post GetNextUnsavedPost() { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?url ?id /// WHERE { /// ?post rdf:type sioc:Post . /// ?post rdfs:seeAlso ?url . /// ?post news:ID ?id /// MINUS { ?post dct:created ?date . } /// } /// LIMIT 1 SparqlResultSet queryResult = connector.QueryFormat("SELECT ?url ?id WHERE {{ ?post {4} {0} . ?post {1} ?url . ?post {3} ?id MINUS {{ ?post {2} ?date . }} }} LIMIT 1", Predicate.SiocPost, Predicate.RdfsSeeAlso, Predicate.DctCreated, Predicate.NewsId, Predicate.RdfType); if (queryResult != null && !queryResult.Results.IsEmpty()) { string url = queryResult.Results.First().Value("url").ToSafeString(); string idString = queryResult.Results.First().Value("id").ToSafeString(); this._logger.InfoFormat("RepositoryService, GetNextUnsavedPost - URL: {0}", url); int id; if (!int.TryParse(idString, out id)) { this._logger.FatalFormat("RepositoryService, GetNextUnsavedPost, can't parse id - ID: {0}", idString); return null; } return new Post() { Id = id, Url = url }; } } else { this._logger.FatalFormat("RepositoryService, GetNextUnsavedPost, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Get posts from selected region in Slovenia /// </summary> /// <param name="region"></param> /// <param name="fromDate"></param> /// <param name="toDate"></param> /// <returns></returns> public IList<Post> GetPostsFromRegion(string region, DateTime? fromDate = null, DateTime? toDate = null) { region.NullCheck(); IList<Post> result = new List<Post>(); using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT DISTINCT ?post ?locationName ?seeAlso /// WHERE { /// ?post a sioc:Post . /// ?post news:location ?locationName . /// ?post rdfs:seeAlso ?seeAlso . /// ?post dct:created ?date . /// /// SERVICE <http://dbpedia.org/sparql> { /// ?region dbpedia-owl:type dbpedia:Statistical_regions_of_Slovenia . /// ?region rdfs:label "Gorenjska"@NL . /// /// { ?city ?x ?region } UNION { ?region ?z ?city } /// ?city ?y ?locationName /// } /// FILTER(langMatches(lang(?locationName ), "EN") && ?date >= "fromDate" && ?date <= "toDate") /// } StringBuilder query = new StringBuilder(); query.AppendFormat( "SELECT DISTINCT ?post ?locationName ?seeAlso " + "WHERE {{ " + "?post rdf:type sioc:Post . " + "?post news:location ?locationName . " + "?post rdfs:seeAlso ?seeAlso . "); if (fromDate.HasValue && toDate.HasValue) { query.AppendFormat( "?post {0} ?date . ", Predicate.DctCreated); } query.AppendFormat( "SERVICE <http://dbpedia.org/sparql> {{ " + "?region dbpedia-owl:type dbpedia:Statistical_regions_of_Slovenia . " + "?region rdfs:label \"{0}\"@NL . " + "{{ ?city ?x ?region }} UNION {{ ?region ?z ?city }} " + "?city ?y ?locationName " + "}} ", region); query.AppendFormat( "FILTER(langMatches(lang(?locationName ), \"EN\")"); if (fromDate.HasValue && toDate.HasValue) { query.AppendFormat( " && ?date >= \"{0}\" && ?date <= \"{1}\"", fromDate.Value.ToString(RepositoryHelper.DateTimeFormat), toDate.Value.ToString(RepositoryHelper.DateTimeFormat)); } query.AppendFormat( ") }}"); SparqlResultSet queryResult = connector.QueryFormat(query.ToString()); if (queryResult == null) { this._logger.FatalFormat("RepositoryService, GetAllSlovenianRegions, Query result is null - QUERY: {0}", query); } else if (!queryResult.Results.IsEmpty()) { LiteralNode literalNode; UriNode uriNode; foreach (SparqlResult res in queryResult.Results) { Post p = new Post(); uriNode = res.Value("post") as UriNode; if (uriNode != null) { p.RepositoryGuidUrl = uriNode.Uri.AbsoluteUri; } uriNode = res.Value("seeAlso") as UriNode; if (uriNode != null) { p.Url = uriNode.Uri.AbsoluteUri; } literalNode = res.Value("locationName") as LiteralNode; if (literalNode != null) { p.Location = literalNode.Value; } result.Add(p); } } return result; } else { this._logger.FatalFormat("RepositoryService, GetAllSlovenianRegions, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Get name of top categories by posts and number of posts in these categories /// </summary> /// <param name="fromDate">From date filtering</param> /// <param name="toDate">To date filtering</param> /// <param name="limit">Number of categories returned</param> /// <returns></returns> public IList<CategoryPostCount> GetTopCategoriesPostCount(DateTime? fromDate = null, DateTime? toDate = null, int limit = 10) { IList<CategoryPostCount> result = new List<CategoryPostCount>(); using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?category (COUNT(?post) as ?count) /// WHERE { /// ?post a sioc:Post . /// ?post sioc:topic ?categoryGuid . /// ?categoryGuid rdfs:label ?category . /// ?post dct:created ?date . /// FILTER(?date >= "fromDate" && ?date <= "toDate") /// } /// GROUP BY ?category /// ORDER BY DESC(?count) /// LIMIT 10 StringBuilder query = new StringBuilder(); query.AppendFormat( "SELECT ?category (COUNT(?post) as ?count) " + "WHERE {{ " + "?post rdf:type sioc:Post . " + "?post sioc:topic ?categoryGuid . " + "?categoryGuid rdfs:label ?category . "); if (fromDate.HasValue && toDate.HasValue) { query.AppendFormat( "?post {0} ?date " + "FILTER(?date >= \"{1}\" && ?date <= \"{2}\") ", Predicate.DctCreated, fromDate.Value.ToString(RepositoryHelper.DateTimeFormat), toDate.Value.ToString(RepositoryHelper.DateTimeFormat)); } query.AppendFormat( "}} " + "GROUP BY ?category " + "ORDER BY DESC(?count) " + "LIMIT {0}", limit); SparqlResultSet queryResult = connector.QueryFormat(query.ToString()); if (queryResult == null) { this._logger.FatalFormat("RepositoryService, GetTopCategoriesPostCount, Query result is null - QUERY: {0}", query); return null; } else if (queryResult.Results.IsEmpty()) { return result; } foreach (SparqlResult sparqlResult in queryResult.Results) { CategoryPostCount model = new CategoryPostCount() { Category = sparqlResult.Value("category").ToSafeString() }; LiteralNode ln = sparqlResult.Value("count") as LiteralNode; if (ln != null) { model.PostCount = int.Parse(ln.Value); } result.Add(model); } return result; } else { this._logger.FatalFormat("RepositoryService, GetTopCategoriesPostCount, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Get number of users by gender /// </summary> /// <param name="fromDate"></param> /// <param name="toDate"></param> /// <returns></returns> public IList<UsersGenderCount> GetUsersGenderCount(DateTime? fromDate = null, DateTime? toDate = null) { IList<UsersGenderCount> result = new List<UsersGenderCount>(); using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?gender (COUNT(?user) AS ?count) /// WHERE { /// ?user rdf:type sioc:UserAccount . /// ?user sioc:has_function mmc:roles/readerAtRtvslo . /// ?user mmc:has_gender ?gender . /// ?user dct:created ?date . /// FILTER(?date >= "fromDate" && ?date <= "toDate") /// } /// GROUP BY ?gender StringBuilder query = new StringBuilder(); query.AppendFormat( "SELECT ?gender (COUNT(?user) AS ?count) " + "WHERE {{ " + "?user rdf:type sioc:UserAccount . " + "?user sioc:has_function <{0}> . " + "?user mmc:has_gender ?gender . ", RepositoryHelper.ReaderRoleUrl.ToFullNamespaceUrl()); if (fromDate.HasValue && toDate.HasValue) { query.AppendFormat( "?user {0} ?date . " + "FILTER(?date >= \"{1}\" && ?date <= \"{2}\") ", Predicate.DctCreated, fromDate.Value.ToString(RepositoryHelper.DateTimeFormat), toDate.Value.ToString(RepositoryHelper.DateTimeFormat)); } query.AppendFormat( "}} " + "GROUP BY ?gender"); SparqlResultSet queryResult = connector.QueryFormat(query.ToString()); if (queryResult == null) { this._logger.FatalFormat("RepositoryService, GetUsersGenderCount, Query result is null - QUERY: {0}", query); } else if (!queryResult.Results.IsEmpty()) { LiteralNode liCount; UriNode liGender; foreach (SparqlResult res in queryResult.Results) { liGender = res.Value("gender") as UriNode; liCount = res.Value("count") as LiteralNode; if (liGender != null && liCount != null) { result.Add(new UsersGenderCount() { Gender = liGender.Uri.ToSafeString(), Count = int.Parse(liCount.Value) }); } } } /// SELECT (COUNT(?user) AS ?count) /// WHERE { /// ?user rdf:type sioc:UserAccount . /// ?user sioc:has_function mmc:roles/readerAtRtvslo /// MINUS { ?user mmc:has_gender ?gender } /// /// ?user dct:created ?date . /// FILTER(?date >= "fromDate" && ?date <= "toDate") /// } query = new StringBuilder(); query.AppendFormat( "SELECT (COUNT(?user) AS ?count) " + "WHERE {{ " + "?user rdf:type sioc:UserAccount . " + "?user sioc:has_function <{0}> . " + "MINUS {{ ?user mmc:has_gender ?gender }} ", RepositoryHelper.ReaderRoleUrl.ToFullNamespaceUrl()); if (fromDate.HasValue && toDate.HasValue) { query.AppendFormat( "?user {0} ?date . " + "FILTER(?date >= \"{1}\" && ?date <= \"{2}\") ", Predicate.DctCreated, fromDate.Value.ToString(RepositoryHelper.DateTimeFormat), toDate.Value.ToString(RepositoryHelper.DateTimeFormat)); } query.AppendFormat( "}} "); queryResult = connector.QueryFormat(query.ToString()); if (queryResult == null) { this._logger.FatalFormat("RepositoryService, GetUsersGenderCount, Query result is null - QUERY: {0}", query); } else if (!queryResult.Results.IsEmpty()) { LiteralNode liCount; SparqlResult res = queryResult.Results.First(); liCount = res.Value("count") as LiteralNode; if (liCount != null) { result.Add(new UsersGenderCount() { Gender = "no_gender", Count = int.Parse(liCount.Value) }); } } return result; } else { this._logger.FatalFormat("RepositoryService, GetUsersGenderCount, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Get top locations by post counts /// From DBpedia get abstract and coordinates /// </summary> /// <param name="fromDate">From date filtering</param> /// <param name="toDate">To date filtering</param> /// <param name="limit"></param> /// <param name="includeDBpedia"></param> /// <returns></returns> public IList<LocationInfo> GetTopLocations(DateTime? fromDate = null, DateTime? toDate = null, int limit = 10, bool includeDBpedia = true) { IList<LocationInfo> result = new List<LocationInfo>(); using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?location (COUNT(?post) AS ?count) /// WHERE { /// ?post a sioc:Post . /// ?post news:location ?location . /// ?post dct:created ?date . /// FILTER(?date >= "fromDate" && ?date <= "toDate") /// } /// GROUP BY ?location /// ORDER BY DESC(?count) /// LIMIT 10 StringBuilder query = new StringBuilder(); query.AppendFormat( "SELECT ?location (COUNT(?post) as ?count) " + "WHERE {{ " + "?post rdf:type sioc:Post . " + "?post news:location ?location . "); if (fromDate.HasValue && toDate.HasValue) { query.AppendFormat( "?post {0} ?date " + "FILTER(?date >= \"{1}\" && ?date <= \"{2}\") ", Predicate.DctCreated, fromDate.Value.ToString(RepositoryHelper.DateTimeFormat), toDate.Value.ToString(RepositoryHelper.DateTimeFormat)); } query.AppendFormat( "}} " + "GROUP BY ?location " + "ORDER BY DESC(?count) " + "LIMIT {0}", limit); SparqlResultSet queryResult = connector.QueryFormat(query.ToString()); if (queryResult == null) { this._logger.FatalFormat("RepositoryService, GetTopLocations, Query result is null - QUERY: {0}", query); return null; } else if (queryResult.Results.IsEmpty()) { return result; } foreach (SparqlResult sparqlResult in queryResult.Results) { LiteralNode ln; LocationInfo model = new LocationInfo(); ln = sparqlResult.Value("location") as LiteralNode; if (ln != null) { model.Name = ln.Value; } ln = sparqlResult.Value("count") as LiteralNode; if (ln != null) { model.PostCount = int.Parse(ln.Value); } /// DBpedia if (includeDBpedia) { /// SELECT ?desc ?lat ?long /// WHERE { /// SERVICE <http://dbpedia.org/sparql> { /// ?city rdf:type dbpedia-owl:Place . /// ?city foaf:name "Celje"@en . /// ?city dbpedia-owl:abstract ?desc . /// ?city geo:lat ?lat . /// ?city geo:long ?long /// FILTER(langMatches(lang(?desc ), "EN")) /// } /// } query = new StringBuilder(); query.Append( @"SELECT ?desc ?lat ?long " + "WHERE { " + "SERVICE <http://dbpedia.org/sparql> { " + "?city rdf:type dbpedia-owl:Place . " + "?city foaf:name \"" + model.Name + "\"@EN . " + "?city dbpedia-owl:abstract ?desc . " + "?city geo:lat ?lat . " + "?city geo:long ?long " + "FILTER(langMatches(lang(?desc ), \"EN\")) " + "} " + "}"); SparqlResultSet queryResultDBpedia = connector.QueryFormat(query.ToString()); if (queryResultDBpedia == null) { this._logger.FatalFormat("RepositoryService, GetTopLocations, DBpedia Query result is null - QUERY: {0}", query); } else if (!queryResultDBpedia.Results.IsEmpty()) { SparqlResult dbpediaResult = queryResultDBpedia.Results.First(); ln = dbpediaResult.Value("desc") as LiteralNode; if (ln != null) { model.Description = ln.Value.ToSafeString(); } ln = dbpediaResult.Value("lat") as LiteralNode; if (ln != null) { model.Latitude = float.Parse(ln.Value, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat); } ln = dbpediaResult.Value("long") as LiteralNode; if (ln != null) { model.Longitude = float.Parse(ln.Value, NumberStyles.Float, CultureInfo.InvariantCulture.NumberFormat); } } } result.Add(model); } return result; } else { this._logger.FatalFormat("RepositoryService, GetTopLocations, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Initialize repository /// Save site triples, role triples, new schema definition /// </summary> public void Initialize() { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT * /// WHERE { /// ?s ?p sioc:Site /// } SparqlResultSet queryResult = connector.QueryFormat("SELECT * WHERE {{ ?s ?p {0} }}", Predicate.SiocSite); //TODO optimize query syntax if (queryResult == null) { return; } else if (!queryResult.Results.IsEmpty()) { /// sioc:Site is already in repository /// we assumed that repository is initialized return; } /// clear repository this.ClearRepository(); /// initialize repository using (Graph g = new Graph()) { g.BaseUri = RepositoryHelper.BaseUrl.ToUri(); /// Namespaces //foreach (Namespace value in RepositoryHelper.NamespaceDictionary.Values // .Where(x => x.Status == NamespaceStatusEnum.Internal)) //{ // Uri uri = UriFactory.Create(string.Format("{0}", value.FullPath)); // g.NamespaceMap.AddNamespace(value.Prefix, UriFactory.Create(value.FullPath)); //} /// Site g.Assert(RepositoryHelper.SiteUrl.ToUriNode(g), Predicate.RdfType.ToUriNode(g), Predicate.SiocSite.ToUriNode(g)); g.Assert(RepositoryHelper.SiteUrl.ToUriNode(g), Predicate.RdfsLabel.ToUriNode(g), RtvSloConfig.RtvSloName.ToLiteralNode(g)); /// Role g.Assert(RepositoryHelper.JournalistRoleUrl.ToUriNode(g), Predicate.RdfType.ToUriNode(g), Predicate.SiocRole.ToUriNode(g)); g.Assert(RepositoryHelper.JournalistRoleUrl.ToUriNode(g), Predicate.NewsHasScope.ToUriNode(g), RepositoryHelper.SiteUrl.ToUriNode(g)); g.Assert(RepositoryHelper.ReaderRoleUrl.ToUriNode(g), Predicate.RdfType.ToUriNode(g), Predicate.SiocRole.ToUriNode(g)); g.Assert(RepositoryHelper.ReaderRoleUrl.ToUriNode(g), Predicate.NewsHasScope.ToUriNode(g), RepositoryHelper.SiteUrl.ToUriNode(g)); /// Gender g.Assert(RepositoryHelper.CreateTypeTriple(g, Predicate.MmcGender, Predicate.RdfsClass)); g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.NewsHasScope, Predicate.MmcGender)); g.Assert(RepositoryHelper.CreateTypeTriple(g, RepositoryHelper.GenderMaleUrl, Predicate.MmcGender)); g.Assert(RepositoryHelper.GenderMaleUrl.ToUriNode(g), Predicate.NewsHasScope.ToUriNode(g), RepositoryHelper.SiteUrl.ToUriNode(g)); g.Assert(RepositoryHelper.CreateTypeTriple(g, RepositoryHelper.GenderFemaleUrl, Predicate.MmcGender)); g.Assert(RepositoryHelper.GenderFemaleUrl.ToUriNode(g), Predicate.NewsHasScope.ToUriNode(g), RepositoryHelper.SiteUrl.ToUriNode(g)); /// Category g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.RdfsSeeAlso, Predicate.SiocCategory)); /// Post g.Assert(RepositoryHelper.CreateTypeDomainTriples(g, Predicate.MmcSubtitle, Predicate.OwlDatatypeProperty, Predicate.SiocPost)); g.Assert(Predicate.MmcSubtitle.ToUriNode(g), Predicate.RdfsSubPropertyOf.ToUriNode(g), Predicate.DctTitle.ToUriNode(g)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcLastUpdated, Predicate.OwlDatatypeProperty, Predicate.SiocPost, Predicate.XsdDateTime)); /// Comment g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcRating, Predicate.OwlDatatypeProperty, Predicate.NewsComment, Predicate.XsdInteger)); g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.NewsId, Predicate.NewsComment)); g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.RdfsSeeAlso, Predicate.NewsComment)); g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.NewsAccessed, Predicate.NewsComment)); /// User g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.NewsId, Predicate.SiocUserAccount)); g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.RdfsSeeAlso, Predicate.SiocUserAccount)); g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.NewsAccessed, Predicate.SiocUserAccount)); g.Assert(RepositoryHelper.CreateTypeDomainTriples(g, Predicate.MmcEmail, Predicate.OwlDatatypeProperty, Predicate.SiocUserAccount)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcBirthDate, Predicate.OwlDatatypeProperty, Predicate.SiocUserAccount, Predicate.XsdDate)); g.Assert(RepositoryHelper.CreateTypeDomainTriples(g, Predicate.MmcAbout, Predicate.OwlDatatypeProperty, Predicate.SiocUserAccount)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcUserStatistics, Predicate.OwlObjectProperty, Predicate.SiocUserAccount, Predicate.MmcStatistics)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcHasGender, Predicate.OwlObjectProperty, Predicate.SiocUserAccount, Predicate.MmcGender)); /// Mmc Statistics g.Assert(RepositoryHelper.CreateTypeTriple(g, Predicate.MmcStatistics, Predicate.RdfsClass)); g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.NewsAvgRating, Predicate.MmcStatistics)); g.Assert(RepositoryHelper.CreateDomainTriple(g, Predicate.NewsNRatings, Predicate.MmcStatistics)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcNumOfForumPosts, Predicate.OwlDatatypeProperty, Predicate.MmcStatistics, Predicate.XsdInteger)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcNumOfBlogPosts, Predicate.OwlDatatypeProperty, Predicate.MmcStatistics, Predicate.XsdInteger)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcNumOfPublishedPictures, Predicate.OwlDatatypeProperty, Predicate.MmcStatistics, Predicate.XsdInteger)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcNumOfPublishedComments, Predicate.OwlDatatypeProperty, Predicate.MmcStatistics, Predicate.XsdInteger)); g.Assert(RepositoryHelper.CreateTypeDomainRangeTriples(g, Predicate.MmcNumOfPublishedVideos, Predicate.OwlDatatypeProperty, Predicate.MmcStatistics, Predicate.XsdInteger)); /// Save graph connector.SaveGraph(g); } } else { this._logger.FatalFormat("RepositoryService, Initialize, SesameHttpProtocolConnector is not ready"); } } }
/// <summary> /// Save post author to repository /// </summary> /// <param name="user"></param> /// <param name="update"></param> /// <returns></returns> public string SaveAuthor(User user, bool update = false) { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?guidUrl /// WHERE { /// ?guidUrl rdf:type sioc:UserAccount /// ; news:nickname "name" /// } string query = string.Format("SELECT ?guidUrl WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" }}", Predicate.RdfType, Predicate.SiocUserAccount, Predicate.NewsNickname, user.Name); if (update) { /// SELECT ?guidUrl ?predicate ?object /// WHERE { /// ?guidUrl rdf:type sioc:UserAccount /// ; news:nickname "name" /// ; ?predicate ?object /// } query = string.Format("SELECT ?guidUrl ?predicate ?object WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" ; ?predicate ?object }}", Predicate.RdfType, Predicate.SiocUserAccount, Predicate.NewsNickname, user.Name); } SparqlResultSet queryResult = connector.QueryFormat(query); string guidUrl = null; INode guidUrlNode = null; if (queryResult != null && !queryResult.Results.IsEmpty()) { /// user already exist guidUrlNode = queryResult.Results.First().Value("guidUrl"); guidUrl = ((UriNode)guidUrlNode).Uri.AbsoluteUri; if (!update) { return guidUrl; } } /// save new or update using (IGraph g = new Graph()) { g.BaseUri = RepositoryHelper.BaseUrl.ToUri(); IList<Triple> newTriples = new List<Triple>(); IList<Triple> removeTriples = new List<Triple>(); if (string.IsNullOrEmpty(guidUrl)) { update = false; guidUrl = string.Format(RepositoryHelper.UserUrlPattern, Guid.NewGuid().ToString()); } INode subject = guidUrlNode != null ? guidUrlNode.CopyNode(g) : guidUrl.ToUriNode(g); user.RepositoryGuidUrl = guidUrl; if (!update) { /// initialize newTriples.Add(new Triple(subject, Predicate.RdfType.ToUriNode(g), Predicate.SiocUserAccount.ToUriNode(g))); /// user function if (user.Function == UserFunctionEnum.Reader) { newTriples.Add(new Triple(subject, Predicate.SiocHasFunction.ToUriNode(g), RepositoryHelper.ReaderRoleUrl.ToUriNode(g))); } else if (user.Function == UserFunctionEnum.Journalist) { newTriples.Add(new Triple(subject, Predicate.SiocHasFunction.ToUriNode(g), RepositoryHelper.JournalistRoleUrl.ToUriNode(g))); } } /// accessed date newTriples.Add(new Triple(subject, Predicate.NewsAccessed.ToUriNode(g), user.AccessedDate.ToString(RepositoryHelper.DateTimeFormat).ToLiteralNode(g, dataType: RepositoryHelper.DateTimeDataType))); /// nickname newTriples.Add(new Triple(subject, Predicate.NewsNickname.ToUriNode(g), user.Name.ToLiteralNode(g))); /// remove old triples if (update) { this.RemoveTriples(removeTriples, queryResult, g, subject, new string[] { Predicate.NewsAccessed, Predicate.NewsNickname }); } connector.UpdateGraph(g.BaseUri, newTriples, removeTriples); return guidUrl; } } else { this._logger.FatalFormat("RepositoryService, SaveAuthor, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Save new comment in RDF format /// </summary> /// <param name="comment"></param> /// <param name="update"></param> /// <returns>Guid url</returns> public string SaveComment(Comment comment, bool update = false) { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?guidUrl /// WHERE { /// ?guidUrl rdf:type news:Comment /// ; news:ID "id" /// } string query = string.Format("SELECT ?guidUrl WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" }}", Predicate.RdfType, Predicate.NewsComment, Predicate.NewsId, comment.Id.ToSafeString()); if (update) { /// SELECT ?guidUrl ?predicate ?object /// WHERE { /// ?guidUrl rdf:type news:Comment /// ; news:ID "id" /// ; ?predicate ?object /// } query = string.Format("SELECT ?guidUrl ?predicate ?object WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" ; ?predicate ?object }}", Predicate.RdfType, Predicate.NewsComment, Predicate.NewsId, comment.Id.ToSafeString()); } SparqlResultSet queryResult = connector.QueryFormat(query); string guidUrl = null; INode guidUrlNode = null; if (queryResult != null && !queryResult.Results.IsEmpty()) { /// comment already exist guidUrlNode = queryResult.Results.First().Value("guidUrl"); guidUrl = ((UriNode)guidUrlNode).Uri.AbsoluteUri; if (!update) { return guidUrl; } } /// save new or update using (IGraph g = new Graph()) { g.BaseUri = RepositoryHelper.BaseUrl.ToUri(); IList<Triple> newTriples = new List<Triple>(); IList<Triple> removeTriples = new List<Triple>(); if (string.IsNullOrEmpty(guidUrl)) { update = false; guidUrl = string.Format(RepositoryHelper.CommentUrlPattern, Guid.NewGuid().ToString()); } INode subject = guidUrlNode != null ? guidUrlNode.CopyNode(g) : guidUrl.ToUriNode(g); comment.RepositoryGuidUrl = guidUrl; /// save new if (!update) { /// initialize newTriples.Add(new Triple(subject, Predicate.RdfType.ToUriNode(g), Predicate.NewsComment.ToUriNode(g))); /// ID newTriples.Add(new Triple(subject, Predicate.NewsId.ToUriNode(g), comment.Id.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); /// see also newTriples.Add(new Triple(subject, Predicate.RdfsSeeAlso.ToUriNode(g), comment.Url.ToUriNode(g))); /// link with post if (!string.IsNullOrEmpty(comment.PostGuidUrl)) { newTriples.Add(new Triple(comment.PostGuidUrl.ToUriNode(g), Predicate.SiocHasReply.ToUriNode(g), subject)); } else { this._logger.FatalFormat("RepositoryService, SaveComment, PostGuidUrl IS NULL - COMMENT: {0}", comment.Url); } /// has creator if (!string.IsNullOrEmpty(comment.UserGuidUrl)) { newTriples.Add(new Triple(subject, Predicate.SiocHasCreator.ToUriNode(g), comment.UserGuidUrl.ToUriNode(g))); } else { this._logger.FatalFormat("RepositoryService, SaveComment, UserGuidUrl IS NULL - COMMENT: {0}", comment.Url); } } /// accessed date newTriples.Add(new Triple(subject, Predicate.NewsAccessed.ToUriNode(g), comment.AccessedDate.ToString(RepositoryHelper.DateTimeFormat).ToLiteralNode(g, dataType: RepositoryHelper.DateTimeDataType))); /// content newTriples.Add(new Triple(subject, Predicate.SiocContent.ToUriNode(g), comment.Content.ToLiteralNode(g))); /// date created newTriples.Add(new Triple(subject, Predicate.DctCreated.ToUriNode(g), comment.DateCreated.Value.ToString(RepositoryHelper.DateTimeFormat).ToLiteralNode(g, dataType: RepositoryHelper.DateTimeDataType))); /// rating newTriples.Add(new Triple(subject, Predicate.MmcRating.ToUriNode(g), comment.Rating.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); /// remove old triples if (update) { this.RemoveTriples(removeTriples, queryResult, g, subject, new string[] { Predicate.NewsAccessed, Predicate.SiocContent, Predicate.DctCreated, Predicate.MmcRating }); } /// save connector.UpdateGraph(g.BaseUri, newTriples, removeTriples); return guidUrl; } } else { this._logger.FatalFormat("RepositoryService, SaveComment, SesameHttpProtocolConnector is not ready"); } } return null; }
public void StorageSesameDescribe() { try { SesameHttpProtocolConnector sesame = new SesameHttpProtocolConnector("http://nottm-virtual.ecs.soton.ac.uk:8080/openrdf-sesame/", "unit-test"); String describe = "DESCRIBE <http://example.org/vehicles/FordFiesta>"; Object results = sesame.Query(describe); if (results is IGraph) { TestTools.ShowGraph((IGraph)results); } else { Assert.Fail("Failed to get a Graph as expected"); } } catch (Exception ex) { TestTools.ReportError("Error", ex, true); } }
/// <summary> /// Get post which needs to be updated /// </summary> /// <returns></returns> public Post GetNextPostToUpdate() { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// filter post older than 2 weeks, sort filtered DateTime olderThanDays = DateTime.Today.AddDays(-RtvSloConfig.Step3UpdateOlderThanDays); /// SELECT ?url ?id ?date /// WHERE { /// ?post rdf:type sioc:Post . /// ?post rdfs:seeAlso ?url . /// ?post news:ID ?id . /// ?post news:accessed ?date . /// FILTER ( ?date <= "2013-10-01 00:00:00Z") /// } /// ORDER BY ?date /// LIMIT 1 string query = string.Format( "SELECT ?url ?id ?date " + "WHERE {{ ?post {4} {0} . " + "?post {1} ?url . " + "?post {3} ?id . " + "?post {5} ?date " + "FILTER (?date <= \"{6}\") " + "}} " + "ORDER BY ?date " + "LIMIT 1", Predicate.SiocPost, Predicate.RdfsSeeAlso, Predicate.DctCreated, Predicate.NewsId, Predicate.RdfType, Predicate.NewsAccessed, olderThanDays.ToString(RepositoryHelper.DateTimeFormat), Predicate.XsdDateTime); SparqlResultSet queryResult = connector.QueryFormat(query); if (queryResult != null && !queryResult.Results.IsEmpty()) { string url = queryResult.Results.First().Value("url").ToSafeString(); string idString = queryResult.Results.First().Value("id").ToSafeString(); this._logger.InfoFormat("RepositoryService, GetNextPostToUpdate - URL: {0}", url); int id; if (!int.TryParse(idString, out id)) { this._logger.FatalFormat("RepositoryService, GetNextPostToUpdate, can't parse id - ID: {0}", idString); return null; } return new Post() { Id = id, Url = url }; } } else { this._logger.FatalFormat("RepositoryService, GetNextPostToUpdate, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Save only post data scraped from archive page in RDF format if don't already exist /// ID, Title, Url, Category /// </summary> /// <param name="post"></param> /// <returns>Guid url</returns> public string SaveOrUpdatePostOverview(Post post) { /// save category string categoryUrl = this.CheckAndSaveCategories(post.Category); using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?guidUrl ?predicate ?object /// WHERE { /// ?guidUrl rdf:type sioc:Post /// ; news:ID "id" /// ; ?predicate ?object /// } SparqlResultSet queryResult = connector.QueryFormat("SELECT ?guidUrl ?predicate ?object WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" ; ?predicate ?object }}", Predicate.RdfType, Predicate.SiocPost, Predicate.NewsId, post.Id.ToString(), Predicate.SiocTopic); /// update existing if (queryResult != null && !queryResult.Results.IsEmpty()) { using (IGraph g = new Graph()) { g.BaseUri = RepositoryHelper.BaseUrl.ToUri(); IList<Triple> newTriples = new List<Triple>(); INode postGuid = queryResult.Results.First().Value("guidUrl").CopyNode(g); post.RepositoryGuidUrl = ((UriNode)postGuid).Uri.AbsoluteUri; /// select categories IEnumerable<SparqlResult> categories = queryResult.Results .Where(x => x.Value("predicate").ToSafeString() == Predicate.SiocTopic.ToFullNamespaceUrl()); SparqlResult categoryResult = categories.FirstOrDefault(x => x.Value("object").ToSafeString() == categoryUrl); if (categoryResult == null) { newTriples.Add(new Triple(postGuid, Predicate.SiocTopic.ToUriNode(g), categoryUrl.ToUriNode(g))); } /// select url IEnumerable<SparqlResult> postUrls = queryResult.Results .Where(x => x.Value("predicate").ToSafeString() == Predicate.RdfsSeeAlso.ToFullNamespaceUrl()); SparqlResult postUrlResult = postUrls.FirstOrDefault(x => x.Value("object").ToSafeString() == post.Url); if (postUrlResult == null) { newTriples.Add(new Triple(postGuid, Predicate.RdfsSeeAlso.ToUriNode(g), post.Url.ToUriNode(g))); } connector.UpdateGraph(g.BaseUri, newTriples, new List<Triple>()); } } /// save new else { using (IGraph g = new Graph()) { g.BaseUri = RepositoryHelper.BaseUrl.ToUri(); IList<Triple> newTriples = new List<Triple>(); string guidUrl = string.Format(RepositoryHelper.PostUrlPattern, Guid.NewGuid().ToString()); INode guidNode = guidUrl.ToUriNode(g); post.RepositoryGuidUrl = guidUrl; /// define post newTriples.Add(new Triple(guidNode, Predicate.RdfType.ToUriNode(g), Predicate.SiocPost.ToUriNode(g))); /// ID newTriples.Add(new Triple(guidNode, Predicate.NewsId.ToUriNode(g), post.Id.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); /// title newTriples.Add(new Triple(guidNode, Predicate.DctTitle.ToUriNode(g), post.Title.ToLiteralNode(g))); /// post url newTriples.Add(new Triple(guidNode, Predicate.RdfsSeeAlso.ToUriNode(g), post.Url.ToUriNode(g))); /// category if (!string.IsNullOrEmpty(categoryUrl)) { newTriples.Add(new Triple(guidNode, Predicate.SiocTopic.ToUriNode(g), categoryUrl.ToUriNode(g))); } connector.UpdateGraph(g.BaseUri, newTriples, new List<Triple>()); } } return post.RepositoryGuidUrl; } else { this._logger.FatalFormat("RepositoryService, SavePostOverview, SesameHttpProtocolConnector is not ready"); } } return null; }
/// <summary> /// Save post details page in RDF format /// Updates post title /// </summary> /// <param name="post"></param> /// <param name="update"></param> /// <returns>Guid url</returns> public string SavePostDetails(Post post, bool update = false) { using (SesameHttpProtocolConnector connector = new SesameHttpProtocolConnector(RtvSloConfig.RepositoryUrl, RtvSloConfig.RepositoryName)) { if (connector.IsReady) { /// SELECT ?guidUrl ?predicate ?object /// WHERE { /// ?guidUrl rdf:type sioc:Post /// ; news:ID "id" /// ; ?predicate ?object /// } string query = string.Format("SELECT ?guidUrl ?predicate ?object WHERE {{ ?guidUrl {0} {1} ; {2} \"{3}\" ; ?predicate ?object }}", Predicate.RdfType, Predicate.SiocPost, Predicate.NewsId, post.Id.ToString(), Predicate.SiocTopic); SparqlResultSet queryResult = connector.QueryFormat(query); if (queryResult == null || queryResult.Results.IsEmpty()) { this._logger.FatalFormat("RepositoryService, SavePostDetails, Query result has no results - QUERY: {0}", query); return null; ; } /// save using (IGraph g = new Graph()) { g.BaseUri = RepositoryHelper.BaseUrl.ToUri(); IList<Triple> newTriples = new List<Triple>(); IList<Triple> removeTriples = new List<Triple>(); INode postGuid = queryResult.Results.First().Value("guidUrl").CopyNode(g); post.RepositoryGuidUrl = ((UriNode)postGuid).Uri.AbsoluteUri; #region Post Content /// remove old title this.RemoveTriples(removeTriples, queryResult, g, postGuid, new string[] { Predicate.DctTitle }); if (!update) { /// published at newTriples.Add(new Triple(postGuid, Predicate.NewsPublishedAt.ToUriNode(g), RepositoryHelper.SiteUrl.ToUriNode(g))); } /// date created newTriples.Add(new Triple(postGuid, Predicate.DctCreated.ToUriNode(g), post.DateCreated.Value.ToString(RepositoryHelper.DateTimeFormat).ToLiteralNode(g, dataType: RepositoryHelper.DateTimeDataType))); /// accessed date newTriples.Add(new Triple(postGuid, Predicate.NewsAccessed.ToUriNode(g), post.AccessedDate.ToString(RepositoryHelper.DateTimeFormat).ToLiteralNode(g, dataType: RepositoryHelper.DateTimeDataType))); /// title newTriples.Add(new Triple(postGuid, Predicate.DctTitle.ToUriNode(g), post.Title.ToLiteralNode(g))); /// subtitle if (!string.IsNullOrEmpty(post.Subtitle)) { newTriples.Add(new Triple(postGuid, Predicate.MmcSubtitle.ToUriNode(g), post.Subtitle.ToLiteralNode(g))); } /// abstract if (!string.IsNullOrEmpty(post.Abstract)) { newTriples.Add(new Triple(postGuid, Predicate.DctAbstract.ToUriNode(g), post.Abstract.ToLiteralNode(g))); } /// last updated newTriples.Add(new Triple(postGuid, Predicate.MmcLastUpdated.ToUriNode(g), post.LastUpdated.ToString(RepositoryHelper.DateTimeFormat).ToLiteralNode(g, dataType: RepositoryHelper.DateTimeDataType))); /// location if (!string.IsNullOrEmpty(post.Location)) { newTriples.Add(new Triple(postGuid, Predicate.NewsLocation.ToUriNode(g), post.Location.ToLiteralNode(g, language: RepositoryHelper.LanguageEnglish))); /// hack to get posts from region } /// content if (!string.IsNullOrEmpty(post.Content)) { newTriples.Add(new Triple(postGuid, Predicate.SiocContent.ToUriNode(g), post.Content.ToLiteralNode(g))); } /// authors if (!post.Authors.IsEmpty()) { foreach (User author in post.Authors) { if (!string.IsNullOrEmpty(author.RepositoryGuidUrl)) { newTriples.Add(new Triple(postGuid, Predicate.SiocHasCreator.ToUriNode(g), author.RepositoryGuidUrl.ToUriNode(g))); } } } if (update) { /// remove old triples this.RemoveTriples(removeTriples, queryResult, g, postGuid, new string[]{ Predicate.DctCreated, Predicate.NewsAccessed, Predicate.MmcSubtitle, Predicate.DctAbstract, Predicate.MmcLastUpdated, Predicate.NewsLocation, Predicate.SiocContent, Predicate.SiocHasCreator }); } #endregion Post Content #region Statistics string statsGuid = Guid.NewGuid().ToString(); string statsGuidUrl = string.Format(RepositoryHelper.StatisticsUrlPattern, statsGuid); UriNode statsGuidNode = null; /// read existing statsGuidUrl if (update) { statsGuidNode = queryResult.Results .First(x => x.Value("predicate").ToSafeString() == Predicate.NewsStatistics.ToFullNamespaceUrl()) .Value("object") as UriNode; statsGuidUrl = statsGuidNode.Uri.AbsoluteUri; /// SELECT ?predicate ?object /// WHERE { /// <guid> rdf:type news:Stat /// ; ?predicate ?object /// } query = string.Format("SELECT ?predicate ?object WHERE {{ <{0}> {1} {2} ; ?predicate ?object }}", statsGuidUrl, Predicate.RdfType, Predicate.NewsStat); queryResult = connector.QueryFormat(query); if (queryResult == null || queryResult.Results.IsEmpty()) { this._logger.FatalFormat("RepositoryService, SavePostDetails, Update statistics ERROR - QUERY: {0}", query); } } INode statsSubject = statsGuidNode != null ? statsGuidNode.CopyNode(g) : statsGuidUrl.ToUriNode(g); if (!update) { /// initialize newTriples.Add(new Triple(statsSubject, Predicate.RdfType.ToUriNode(g), Predicate.NewsStat.ToUriNode(g))); newTriples.Add(new Triple(postGuid, Predicate.NewsStatistics.ToUriNode(g), statsSubject)); } /// number of comments if (post.NumOfComments > -1) { newTriples.Add(new Triple(statsSubject, Predicate.NewsNComments.ToUriNode(g), post.NumOfComments.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// avgerage rating if (post.AvgRating > -1) { newTriples.Add(new Triple(statsSubject, Predicate.NewsAvgRating.ToUriNode(g), post.AvgRating.ToString().ToLiteralNode(g, dataType: RepositoryHelper.DecimalDataType))); } /// number of ratings if (post.NumOfRatings > -1) { newTriples.Add(new Triple(statsSubject, Predicate.NewsNRatings.ToUriNode(g), post.NumOfRatings.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// number of FB likes if (post.NumOfFbLikes > -1) { newTriples.Add(new Triple(statsSubject, Predicate.NewsNFBLikes.ToUriNode(g), post.NumOfFbLikes.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } /// number of tweets if (post.NumOfTweets > -1) { newTriples.Add(new Triple(statsSubject, Predicate.NewsNTweets.ToUriNode(g), post.NumOfTweets.ToString().ToLiteralNode(g, dataType: RepositoryHelper.IntegerDataType))); } if (update) { /// remove old triples this.RemoveTriples(removeTriples, queryResult, g, statsSubject, new string[] { Predicate.NewsNComments, Predicate.NewsAvgRating, Predicate.NewsNRatings, Predicate.NewsNFBLikes, Predicate.NewsNTweets }); } #endregion Statistics connector.UpdateGraph(g.BaseUri, newTriples, removeTriples); return post.RepositoryGuidUrl; } } else { this._logger.FatalFormat("RepositoryService, SavePostDetails, SesameHttpProtocolConnector is not ready"); } } return null; }
public void StorageSesameCyrillic() { try { SesameHttpProtocolConnector sesame = new SesameHttpProtocolConnector("http://nottm-virtual.ecs.soton.ac.uk:8080/openrdf-sesame/", "unit-test"); Graph g = new Graph(); g.BaseUri = new Uri("http://example.org/sesame/cyrillic"); FileLoader.Load(g, "cyrillic.rdf"); sesame.SaveGraph(g); String ask = "ASK WHERE {?s ?p 'литерал'}"; Object results = sesame.Query(ask); if (results is SparqlResultSet) { TestTools.ShowResults(results); } else { Assert.Fail("Failed to get a Result Set as expected"); } } catch (Exception ex) { TestTools.ReportError("Error", ex, true); } }
/// <summary> /// Tries to load a Generic IO Manager 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) { IGenericIOManager manager = null; obj = null; String server, user, pwd, store; bool async; Object temp; INode storeObj; //Create the URI Nodes we're going to use to search for things INode propServer = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyServer), propDb = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyDatabase), propStore = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyStore), propAsync = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyAsync); switch (targetType.FullName) { case AllegroGraph: //Get the Server, Catalog and Store server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) return false; String catalog = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyCatalog)); store = ConfigurationLoader.GetConfigurationString(g, objNode, propStore); if (store == null) return false; //Get User Credentials ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd); if (user != null && pwd != null) { manager = new AllegroGraphConnector(server, catalog, store, user, pwd); } else { manager = new AllegroGraphConnector(server, catalog, store); } break; case DatasetFile: //Get the Filename and whether the loading should be done asynchronously String file = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromFile)); if (file == null) return false; file = ConfigurationLoader.ResolvePath(file); async = ConfigurationLoader.GetConfigurationBoolean(g, objNode, propAsync, false); manager = new DatasetFileManager(file, async); break; case Dydra: //Get the Account Name and Store String account = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyCatalog)); if (account == null) return false; store = ConfigurationLoader.GetConfigurationString(g, objNode, propStore); if (store == null) return false; //Get User Credentials ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd); if (user != null) { manager = new DydraConnector(account, store, user); } else { manager = new DydraConnector(account, store); } break; case FourStore: //Get the Server and whether Updates are enabled server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) return false; bool enableUpdates = ConfigurationLoader.GetConfigurationBoolean(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyEnableUpdates), true); manager = new FourStoreConnector(server, enableUpdates); break; case Fuseki: //Get the Server URI server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) return false; manager = new FusekiConnector(server); break; case InMemory: //Get the Dataset/Store INode datasetObj = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyUsingDataset)); if (datasetObj != null) { temp = ConfigurationLoader.LoadObject(g, datasetObj); if (temp is ISparqlDataset) { manager = new InMemoryManager((ISparqlDataset)temp); } else { throw new DotNetRdfConfigurationException("Unable to load the In-Memory Manager identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:usingDataset property points to an Object that cannot be loaded as an object which implements the ISparqlDataset interface"); } } else { //If no dnr:usingDataset try dnr:usingStore instead storeObj = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyUsingStore)); if (storeObj != null) { temp = ConfigurationLoader.LoadObject(g, storeObj); if (temp is IInMemoryQueryableStore) { manager = new InMemoryManager((IInMemoryQueryableStore)temp); } else { throw new DotNetRdfConfigurationException("Unable to load the In-Memory Manager identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:usingStore property points to an Object that cannot be loaded as an object which implements the IInMemoryQueryableStore interface"); } } else { //If no dnr:usingStore either then create a new empty store manager = new InMemoryManager(); } } break; case Joseki: //Get the Query and Update URIs server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) return false; String queryService = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyQueryPath)); if (queryService == null) return false; String updateService = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyUpdatePath)); if (updateService == null) { manager = new JosekiConnector(server, queryService); } else { manager = new JosekiConnector(server, queryService, updateService); } break; case ReadOnly: //Get the actual Manager we are wrapping storeObj = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyGenericManager)); temp = ConfigurationLoader.LoadObject(g, storeObj); if (temp is IGenericIOManager) { manager = new ReadOnlyConnector((IGenericIOManager)temp); } else { throw new DotNetRdfConfigurationException("Unable to load the Read-Only Connector identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:genericManager property points to an Object which cannot be loaded as an object which implements the required IGenericIOManager interface"); } break; case ReadOnlyQueryable: //Get the actual Manager we are wrapping storeObj = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyGenericManager)); temp = ConfigurationLoader.LoadObject(g, storeObj); if (temp is IQueryableGenericIOManager) { manager = new QueryableReadOnlyConnector((IQueryableGenericIOManager)temp); } else { throw new DotNetRdfConfigurationException("Unable to load the Queryable Read-Only Connector identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:genericManager property points to an Object which cannot be loaded as an object which implements the required IQueryableGenericIOManager interface"); } break; case Sesame: //Get the Server and Store ID server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) return false; store = ConfigurationLoader.GetConfigurationString(g, objNode, propStore); if (store == null) return false; ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd); if (user != null && pwd != null) { manager = new SesameHttpProtocolConnector(server, store, user, pwd); } else { manager = new SesameHttpProtocolConnector(server, store); } break; case Sparql: //Get the Endpoint URI or the Endpoint server = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyEndpointUri)); //What's the load mode? String loadModeRaw = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyLoadMode)); SparqlConnectorLoadMethod loadMode = SparqlConnectorLoadMethod.Construct; if (loadModeRaw != null) { try { #if SILVERLIGHT loadMode = (SparqlConnectorLoadMethod)Enum.Parse(typeof(SparqlConnectorLoadMethod), loadModeRaw, false); #else loadMode = (SparqlConnectorLoadMethod)Enum.Parse(typeof(SparqlConnectorLoadMethod), loadModeRaw); #endif } catch { throw new DotNetRdfConfigurationException("Unable to load the SparqlConnector identified by the Node '" + objNode.ToString() + "' as the value given for the property dnr:loadMode is not valid"); } } if (server == null) { INode endpointObj = ConfigurationLoader.GetConfigurationNode(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyEndpoint)); if (endpointObj == null) return false; temp = ConfigurationLoader.LoadObject(g, endpointObj); if (temp is SparqlRemoteEndpoint) { manager = new SparqlConnector((SparqlRemoteEndpoint)temp, loadMode); } else { throw new DotNetRdfConfigurationException("Unable to load the SparqlConnector identified by the Node '" + objNode.ToString() + "' as the value given for the property dnr:endpoint points to an Object which cannot be loaded as an object which is of the type SparqlRemoteEndpoint"); } } else { //Are there any Named/Default Graph URIs IEnumerable<Uri> defGraphs = from def in ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyDefaultGraphUri)) where def.NodeType == NodeType.Uri select ((IUriNode)def).Uri; IEnumerable<Uri> namedGraphs = from named in ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyNamedGraphUri)) where named.NodeType == NodeType.Uri select ((IUriNode)named).Uri; if (defGraphs.Any() || namedGraphs.Any()) { manager = new SparqlConnector(new SparqlRemoteEndpoint(new Uri(server), defGraphs, namedGraphs), loadMode); } else { manager = new SparqlConnector(new Uri(server), loadMode); } } break; case SparqlHttpProtocol: //Get the Service URI server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) return false; manager = new SparqlHttpProtocolConnector(new Uri(server)); break; case Stardog: //Get the Server and Store server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) return false; store = ConfigurationLoader.GetConfigurationString(g, objNode, propStore); if (store == null) return false; //Get User Credentials ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd); if (user != null && pwd != null) { manager = new StardogConnector(server, store, user, pwd); } else { manager = new StardogConnector(server, store); } break; case Talis: //Get the Store Name and User credentials store = ConfigurationLoader.GetConfigurationString(g, objNode, propStore); if (store == null) return false; ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd); if (user != null && pwd != null) { manager = new TalisPlatformConnector(store, user, pwd); } else { manager = new TalisPlatformConnector(store); } break; #if !NO_DATA case Virtuoso: //Get Server settings server = ConfigurationLoader.GetConfigurationString(g, objNode, propServer); if (server == null) return false; String port = ConfigurationLoader.GetConfigurationString(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyPort)); store = ConfigurationLoader.GetConfigurationString(g, objNode, propStore); if (store == null) store = VirtuosoManager.DefaultDB; //Get User Credentials ConfigurationLoader.GetUsernameAndPassword(g, objNode, true, out user, out pwd); if (user == null || pwd == null) return false; if (port != null) { int p = -1; if (Int32.TryParse(port, out p)) { manager = new VirtuosoManager(server, p, store, user, pwd); } else { return false; } } else { manager = new VirtuosoManager(server, VirtuosoManager.DefaultPort, store, user, pwd); } break; #endif } obj = manager; return (manager != null); }