Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

DbPediaDataAccess.cs 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Luticate2.Utils.Dbo.Basic;
  5. using Luticate2.Utils.Dbo.PaginatedRequest;
  6. using Luticate2.Utils.Dbo.Result;
  7. using Microsoft.Extensions.Logging;
  8. using VDS.RDF.Nodes;
  9. using VDS.RDF.Query;
  10. using WebApiWebSem.Dbo.Articles;
  11. using WebApiWebSem.Dbo.ArticlesFields;
  12. namespace WebApiWebSem.DataAccess
  13. {
  14. public class DbPediaDataAccess
  15. {
  16. private readonly SparqlRemoteEndpoint _sparqlRemoteEndpoint;
  17. private readonly ILogger<DbPediaDataAccess> _logger;
  18. protected string Prefixes = @"
  19. PREFIX owl: <http://www.w3.org/2002/07/owl#>
  20. PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
  21. PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  22. PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
  23. PREFIX foaf: <http://xmlns.com/foaf/0.1/>
  24. PREFIX dc: <http://purl.org/dc/elements/1.1/>
  25. PREFIX : <http://dbpedia.org/resource/>
  26. PREFIX dbpedia2: <http://dbpedia.org/property/>
  27. PREFIX dbpedia: <http://dbpedia.org/>
  28. PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
  29. PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
  30. ";
  31. public DbPediaDataAccess(SparqlRemoteEndpoint sparqlRemoteEndpoint, ILogger<DbPediaDataAccess> logger)
  32. {
  33. _sparqlRemoteEndpoint = sparqlRemoteEndpoint;
  34. _logger = logger;
  35. }
  36. protected string Escape(string str)//TODO
  37. {
  38. return str.Replace('\'', ' ').Replace('"', ' ');
  39. }
  40. protected LuResult<SparqlResultSet> Query(string query)
  41. {
  42. _logger.LogInformation(query);
  43. try
  44. {
  45. var results = _sparqlRemoteEndpoint.QueryWithResultSet(query);
  46. return LuResult<SparqlResultSet>.Ok(results);
  47. }
  48. catch (Exception e)
  49. {
  50. return LuResult<SparqlResultSet>.Error(LuStatus.BackendError, e);
  51. }
  52. }
  53. protected ArticlesDbo ModelToDbo(SparqlResult result)
  54. {
  55. var dbo = new ArticlesDbo
  56. {
  57. Fields = new List<ArticlesFieldsDbo>()
  58. };
  59. foreach (var variable in result.Variables)
  60. {
  61. string data = null;
  62. var v = result[variable];
  63. if (v != null)
  64. {
  65. data = v.AsValuedNode().AsString();
  66. }
  67. if (variable == "id")
  68. {
  69. dbo.Id = data.Split('/').Last();
  70. }
  71. else if (variable == "abstract")
  72. {
  73. dbo.Text = data;
  74. }
  75. else if (variable == "pictureUrl")
  76. {
  77. dbo.PictureUrl = data;
  78. }
  79. else if (variable == "pictureDesc")
  80. {
  81. dbo.PictureCaption = data;
  82. }
  83. else
  84. {
  85. dbo.Fields.Add(new ArticlesFieldsDbo
  86. {
  87. Property = variable,
  88. Type = "text/plain",
  89. Value = data
  90. });
  91. }
  92. }
  93. return dbo;
  94. }
  95. public LuResult<LuPaginatedDbo<ArticlesDbo>> GetMultiple(LuPaginatedRequestDbo request)
  96. {
  97. var type = ArticlesDataAccess.GetFilterType(request.Filter);
  98. string query = null;
  99. string querySelect = null;
  100. string queryWhereId = null;
  101. string queryWhereVars = null;
  102. if (type == "home")
  103. {
  104. querySelect = "?id ?abstract ?pictureUrl ?pictureDesc";
  105. queryWhereId = @"?id foaf:isPrimaryTopicOf wikipedia-en:World_War_II .";
  106. queryWhereVars = @"
  107. OPTIONAL { ?id dbo:abstract ?abstract FILTER(lang(?abstract) = 'en') }
  108. OPTIONAL { ?id foaf:depiction ?pictureUrl }
  109. OPTIONAL { ?id dbpedia-owl:depictionDescription ?pictureDesc }
  110. BIND ((?abstract) AS ?search)";
  111. }
  112. else if (type == "persons")
  113. {
  114. querySelect = "?id ?abstract ?pictureUrl ?pictureDesc (MIN(?name) AS ?name) (str(?birthDate) AS ?birthDate) (str(?deathDate) AS ?deathDate)";
  115. queryWhereId = @"?id rdf:type dbo:Person ;
  116. ?type dbr:World_War_II .";
  117. queryWhereVars = @"
  118. OPTIONAL { ?id dbo:abstract ?abstract FILTER(lang(?abstract) = 'en') }
  119. OPTIONAL { ?id foaf:depiction ?pictureUrl }
  120. OPTIONAL { ?id dbpedia-owl:depictionDescription ?pictureDesc }
  121. OPTIONAL { ?id dbp:name ?name . FILTER (lang(?name) = 'en') }
  122. OPTIONAL { ?id dbp:birthDate ?birthDate }
  123. OPTIONAL { ?id dbp:deathDate ?deathDate }
  124. BIND (CONCAT(?abstract, ' ', ?id) AS ?search)";
  125. }
  126. var filter = $"FILTER regex(?search, '{request.Filter.Query}', 'i')";
  127. var count = Query(string.Format(@"{0}
  128. SELECT (COUNT(?id) AS ?count) WHERE {{
  129. {1}
  130. {2}
  131. {3}
  132. }}
  133. ", Prefixes, queryWhereId, queryWhereVars, filter));
  134. if (!count)
  135. {
  136. return count.To<LuPaginatedDbo<ArticlesDbo>>();
  137. }
  138. var res = Query(string.Format(@"{0}
  139. SELECT DISTINCT {1} WHERE {{
  140. {2}
  141. {3}
  142. {4}
  143. }}
  144. ORDER BY {5}
  145. LIMIT {6}
  146. OFFSET {7}", Prefixes, querySelect, queryWhereId, queryWhereVars, filter, request.OrderBy.ToDbPediaModel(),
  147. request.PerPage, request.Page * request.PerPage));
  148. if (!res)
  149. {
  150. return res.To<LuPaginatedDbo<ArticlesDbo>>();
  151. }
  152. var results = new LuPaginatedDbo<ArticlesDbo>
  153. {
  154. Count = count.Data.Results.First()["count"].AsValuedNode().AsInteger(),
  155. Data = res.Data.Results.Select(ModelToDbo).Select(dbo =>
  156. {
  157. dbo.Type = type;
  158. return dbo;
  159. }).ToList()
  160. };
  161. return LuResult<LuPaginatedDbo<ArticlesDbo>>.Ok(results);
  162. }
  163. }
  164. }