PageRenderTime 24ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/webbox-chrome/web/webbox/webbox-kb.js

https://github.com/electronicmax/webbox
JavaScript | 181 lines | 167 code | 6 blank | 8 comment | 12 complexity | 7562000d4191ff1840430070d6aa8197 MD5 | raw file
  1. define(['/web/lib/require.js','/web/webbox/webbox-ns.js', '/web/webbox/webbox-config.js','/web/webbox/util.js'],
  2. function(require, ns, config, util) {
  3. var ModelSeq = Backbone.Collection.extend({ seq:true });
  4. var make_seq = function(arr) {
  5. var s = new ModelSeq();
  6. if (arr !== undefined) { s.reset(arr); }
  7. return s;
  8. };
  9. config = config.config;
  10. var is_model = function(v) { return typeof(v) == 'object' && v instanceof Model; };
  11. var make_kb = function() {
  12. return $.rdf.databank([], { base: ns.me, namespaces:ns.ns });
  13. };
  14. var ping = function(url, fourstore) {
  15. console.log('ping at url ', url);
  16. if (url === undefined) { url = config.SPARQL_URL; }
  17. var this_ = this;
  18. var query = "SELECT ?s ?p ?o WHERE { ?s ?p ?o . } LIMIT 1";
  19. if (this.get !== undefined) {
  20. this.get.abort();
  21. delete this.get;
  22. }
  23. // only works for webboxes :
  24. this.get = $.ajax({ type:"GET", url:( url !== undefined ? url + (fourstore ? "/sparql" : "/webbox") : config.SPARQL_URL ), data:{query:query}});
  25. var D = new $.Deferred();
  26. this.get.success(function() { delete this_.get; D.resolve.apply(D,arguments); }).error(D.reject);
  27. return D.promise();
  28. };
  29. // @Deprecated, should not be used
  30. var make_spo_query = function(query, cont) {
  31. var get = $.ajax({ type:"GET", url:config.SPARQL_URL, data:{query:query}});
  32. var kb = make_kb();
  33. get.then(function(doc){
  34. var results = [];
  35. kb.load(doc, {});
  36. $.rdf({databank:kb}).where('?s ?p ?o').each(
  37. function() {
  38. results.push(
  39. {s:this.s ? this.s.value : undefined,
  40. p:this.p ? this.p.value : undefined,
  41. o:this.o ? this.o.value : undefined });
  42. });
  43. if(cont) { cont(results); }
  44. });
  45. };
  46. var get_objects_of_type = function(type_uri) {
  47. var d = new $.Deferred();
  48. if (util.is_resource(type_uri)) { type_uri = type_uri.value.toString(); }
  49. if (is_model(type_uri)) { type_uri = type_uri.uri; }
  50. if (typeof(type_uri) !== 'string') { type_uri = type_uri.toString(); }
  51. type_uri = ns.expand(type_uri);
  52. var query = _("SELECT ?s WHERE { GRAPH ?s { ?s a \<<%= type_uri %>\> . }} LIMIT 100000").template({type_uri:type_uri});
  53. var get = $.ajax({ type:"GET", url:config.SPARQL_URL, data:{query:query}}).then(
  54. function(doc) {
  55. var resources = $(doc, "results").find('uri').map(
  56. function(x) {
  57. return $(this).text();
  58. }).get();
  59. console.log("resources : ", resources);
  60. d.resolve(resources);
  61. });
  62. return d.promise();
  63. };
  64. var get_sp_object = function(subject, predicate, graph) {
  65. // subject should be a uri,
  66. subject = ns.expand(subject.toString());
  67. predicate = ns.expand(predicate.toString());
  68. var d = new $.Deferred();
  69. var query = graph ?
  70. _("SELECT ?o WHERE { GRAPH \<<%= g %>\> { \<<%=s%>\> \<<%=p%>\> ?o . }} LIMIT 100000").template({s:subject,p:predicate, g:graph}) :
  71. _("SELECT ?o WHERE { \<<%=s%>\> \<<%=p%>\> ?o . } LIMIT 100000").template({s:subject,p:predicate});
  72. console.log("get_value query ", query, config.SPARQL_URL);
  73. var get = $.ajax({ type:"GET", url:config.SPARQL_URL, data:{query:query}}).then(
  74. function(doc) {
  75. var lits = $(doc, "results").find('literal');
  76. if (lits.length > 0) {
  77. d.resolve(lits.map(function() { return $(this).text(); }));
  78. return;
  79. }
  80. var uris = $(doc, "results").find('uri');
  81. if (uris.length > 0) {
  82. d.resolve(uris.map(function() { return $(this).text(); }));
  83. }
  84. });
  85. return d.promise();
  86. };
  87. var get_graphs = function() {
  88. // gets list of distinct ORM entities in the KB:
  89. // TODO: make this more efficient so that we don't have to parse all the triples
  90. var query = "CONSTRUCT { ?g a <http://webbox.ecs.soton.ac.uk/webbox/Object> } WHERE { GRAPH ?g { ?s ?p ?o. } } LIMIT 100000";
  91. console.log("get_graphs() calling ", config, config.SPARQL_URL);
  92. var get = $.ajax({ type:"GET", url:config.SPARQL_URL, data:{query:query}});
  93. var kb = make_kb();
  94. var gs = [];
  95. var d = new $.Deferred();
  96. get.then(function(doc){
  97. kb.load(doc, {});
  98. $.rdf({databank:kb}).where('?s ?p ?o').each(
  99. function() {
  100. var graph = this.s.value.toString();
  101. gs.push(graph);
  102. });
  103. d.resolve(gs);
  104. }).fail(d.reject);
  105. return d.promise();
  106. };
  107. var string = function(s) {
  108. return $.rdf.literal(s,{datatype:ns.expand("xsd:string")});
  109. };
  110. var integer = function(d) {
  111. return $.rdf.literal(d,{datatype:ns.expand("xsd:integer")});
  112. };
  113. var dateTime = function(d) {
  114. console.assert(d instanceof Date, "d must be a date");
  115. return $.rdf.literal(d.toISOString(),{datatype:ns.expand("xsd:dateTime")});
  116. };
  117. var xmlliteral = function(d) {
  118. return $.rdf.literal(d,{datatype:ns.expand("rdf:XMLLiteral")});
  119. };
  120. var resource = function(s) {
  121. s = ns.expand(s);
  122. return $.rdf.resource("<"+s+">");
  123. };
  124. var get_updated_messages = function(since_time) {
  125. // var query_template = 'SELECT ?msg ?time ?entity WHERE { GRAPH <http://webbox.ecs.soton.ac.uk/ns#ReceivedSIOCGraph> { ?msg a <http://rdfs.org/sioc/ns#Post> . ?msg <http://purl.org/dc/terms/created> ?time . ?msg <http://xmlns.com/foaf/0.1/primaryTopic> ?entity. } FILTER (?time > "<%= since_time.toISOString() %>") }';
  126. var d = new $.Deferred();
  127. require(
  128. ['/web/webbox/webbox-model.js'],
  129. function(models) {
  130. var query_template = 'SELECT ?msg ?whom ?when ?what WHERE { GRAPH <http://webbox.ecs.soton.ac.uk/ns#ReceivedSIOCGraph> { ?msg <http://rdfs.org/sioc/ns#addressed_to> ?whom . ?msg <http://purl.org/dc/terms/created> ?when . ?msg <http://xmlns.com/foaf/0.1/primaryTopic> ?what . }}';
  131. var query = _(query_template).template({since_time:since_time || new Date(0)});
  132. var updated_thing_loads = [];
  133. var get = $.ajax({ type:"GET", url:config.SPARQL_URL, data:{query:query}}).success(
  134. function(doc) {
  135. var lits = $(doc, "results").find('result').map(
  136. function() {
  137. // turn em into little models for us
  138. var msg_uri = $(this).find('binding[name=msg]').find('uri').text();
  139. var whom = $(this).find('binding[name=whom]').find('uri').text();
  140. var what = $(this).find('binding[name=what]').find('uri').text();
  141. var date_literal = $(this).find('binding[name=when]').find('literal').text();
  142. var dl = new Date(date_literal);
  143. var m = models.get_resource(msg_uri);
  144. var what_r = models.get_resource(what);
  145. m.set2('sioc:addressed_to',models.get_resource(whom));
  146. m.set2('foaf:primaryTopic', what_r);
  147. m.set2('dc:created',dl);
  148. m.set2('rdf:type',models.get_resource('webbox:WebboxMessage'));
  149. // prepare a deferred for the target's retrieval
  150. var _d = new $.Deferred();
  151. updated_thing_loads.push(_d);
  152. what_r.fetch().then(_d.resolve);
  153. return m;
  154. }).get();
  155. $.when.apply($,updated_thing_loads).then(function() { d.resolve(lits); });
  156. }).error(d.fail);
  157. });
  158. return d.promise();
  159. };
  160. return {
  161. get_updated_messages:get_updated_messages,
  162. ping:ping,
  163. make_kb:make_kb,
  164. get_graphs:get_graphs,
  165. get_sp_object:get_sp_object,
  166. get_objects_of_type:get_objects_of_type,
  167. string:string,
  168. xmlliteral:xmlliteral,
  169. integer:integer,
  170. dateTime:dateTime,
  171. resource:resource,
  172. seq:make_seq,
  173. ModelSeq:ModelSeq
  174. };
  175. });