-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage-jena.js
386 lines (346 loc) · 11.1 KB
/
storage-jena.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright 2014 IBM Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* storage.js implements the abstract ldp-service/storage.js module by
* storing RDF graphs in Apache Jena. Each document representations
* an RDF graph.
*
* This implementation delegates all the database requests to Fuesiki
* using the JenaURL from the env parameter
*
* env configuration parameters are:
* * env.jenaURL - the URL of the Fuseki data source
*/
// The abstract storage services this module instantiates
var storage_services = require('ldp-service').storageService;
module.exports = storage_services;
var ldp = require('./vocab/ldp.js') // LDP vocabulary
var rdflib = require('rdflib')
var request = require('request')
var db
// Some convenient namespaces
var RDF = rdflib.Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")
var RDFS = rdflib.Namespace("http://www.w3.org/2000/01/rdf-schema#")
var LDP = rdflib.Namespace('http://www.w3.org/ns/ldp#')
storage_services.init = function(env, callback) {
db = env.jenaURL
callback()
}
storage_services.reserveURI = function(uri, callback) {
// simply create a document with only a URI. we will
// just update it later on put
// if it fails, we reject the uri
var options = {
uri: db+"data?graph="+uri,
method: "PUT",
headers: {
'Content-Type': 'application/ld+json'
},
body: "{}"
}
request(options, function(err, ires, body) {
if (err) {
console.log(err.stack)
callback(500)
return
}
callback(ires.statusCode)
})
}
storage_services.releaseURI = function(uri) {
var options = {
uri: db+"data?graph="+uri,
method: "DELETE",
}
request(options, function(err, ires, body){
if (err) console.log(`Cannot delete ${uri}, error: ${err}`);
})
}
storage_services.read = function(uri, callback) {
var options = {
uri: db+"data?graph="+uri,
method: "GET",
headers: {
"Accept": "text/turtle"
}
}
request(options, function(err, ires, body) {
if (err) {
console.log(err.stack)
callback(500)
return
} else if (ires.statusCode != 200) {
callback(ires.statusCode)
return
}
// parse the response for the KB
document = new rdflib.IndexedFormula()
rdflib.parse(body, document, uri, 'text/turtle', function(err, document) {
if (err) {
console.log(err.stack)
callback(err)
return
}
// set the interaction model
var interactionModel = null
document.uri = uri // The container URL
var uriSym = document.sym(uri)
if (document.statementsMatching(uriSym, RDF('type'), LDP('BasicContainer')).length !==0) interactionModel = LDP('BasicContainer').value
if (document.statementsMatching(uriSym, RDF('type'), LDP('DirectContainer')).length !==0) interactionModel = LDP('DirectContainer').value
document.interactionModel = interactionModel
if (document.interactionModel === ldp.DirectContainer) {
var statement = document.any(uriSym, LDP("membershipResource"))
if (statement) document.membershipResource = statement.value
statement = document.any(uriSym, LDP("hasMemberRelation"))
if (statement) document.hasMemberRelation = statement.value
}
callback(ires.statusCode, document)
})
})
}
storage_services.update = function(resource, callback) {
rdflib.serialize(resource.sym(resource.uri), resource, "none:", "text/turtle", function(err, content) {
if (err) {
console.log(err.stack)
callback(500)
return
}
var options = {
uri: db+"data?graph="+resource.uri,
method: "PUT",
headers: {
'Content-Type': 'text/turtle'
},
body: content
}
request(options, function(err, ires, body) {
if (err) {
console.log(err.stack)
callback(500)
return
}
callback(ires.statusCode);
})
})
}
/*
* Insert data (an IndexedFormula) into an existing resource.
* Could be useful to implement HTTP PATCH.
*
* @param {IndexedFormula} data - the triples to insert
* @param {URI} uri - URI of the resource to insert the triples into
* @param {callback} callback(status)
*/
storage_services.insertData = function insertData(data, uri, callback) {
let content = '';
let statements = data.statementsMatching(undefined, undefined, undefined);
for (let s of statements) {
content = content + `<${s.subject.uri}> <${s.predicate.uri}> <${s.object.uri}>. `
}
content = 'INSERT DATA {GRAPH <'+uri+'> {'+content+'}}';
var options = {
uri: db+"update",
method: "POST",
headers: {
'Content-Type': 'application/sparql-update'
},
body: content
}
request(options, function(err, ires, body) {
if (err) {
console.log(`insertData error: ${err}, body: ${body}`)
callback(500)
return
}
callback(ires.statusCode);
})
}
storage_services.remove = function(uri, callback) {
var options = {
uri: db+"data?graph="+uri,
method: "DELETE",
}
request(options, function(err, ires, body) {
if (err) {
console.log(err.stack)
callback(500)
return
}
callback(ires.statusCode);
})
}
/* Get the membership triples for a DirectContainer */
storage_services.getMembershipTriples = function(container, callback) {
var options = {
uri: db+"sparql",
method: "POST",
headers: {
"Content-Type": "application/sparql-query",
"Accept": "application/sparql-results+json"
},
body: "SELECT ?member FROM <"+container.membershipResource+"> WHERE {<"+container.membershipResource+"> <"+container.hasMemberRelation+"> ?member .}"
}
request(options, function(err, ires, body) {
if (err) {
console.error(`Error geting membership triples: ${err}`);
callback(500);
return
} else if (ires.statusCode !== 200) {
callback(ires.statusCode)
return
}
callback(ires.statusCode, JSON.parse(body).results.bindings)
})
}
storage_services.drop = function(callback) {
}
storage_services.query = function(ast, base, callback) {
// convert OSLC query to SPARQL and execute
var node = ast
var stack = new Array()
var var_stack = new Array()
if (node.right.val === "*") {
var uri = "SELECT ?g WHERE {GRAPH ?g { ?s ?p ?o } }"
var options = {
uri: db+"query?query="+uri,
method: "GET",
headers: {
"Accept": "application/sparql-results+json"
}
}
request(options, function(err, ires, body) {
console.log("REQUEST " + options.uri)
callback(err, ires);
})
} else {
var sparql_query_select = "SELECT ?g ";
var sparql_query_where = "WHERE { GRAPH ?g { ";
var sparql_query_prefix = "";
var sparql_query_orderBy = "";
var found = {
"oslc.select": false,
"oslc.where": false,
"oslc.orderBy": false,
"oslc.prefix": false
}
while (node.left != null && node.right != null) {
while(node.left != null){
stack.push(node)
node = node.left
}
if (node.val === "oslc.select") {
found["oslc.where"] = false
found["oslc.orderBy"] = false
found["oslc.prefix"] = false
found["oslc.select"] = true
var_stack.push("?s")
node = node.pop().right
} else if(node.val === "oslc.where") {
found["oslc.select"] = false
found["oslc.orderBy"] = false
found["oslc.prefix"] = false
found["oslc.where"] = true
node = stack.pop().right
} else if(node.val === "oslc.prefix") {
found["oslc.select"] = false
found["oslc.orderBy"] = false
found["oslc.where"] = false
found["oslc.prefix"] = true
node = stack.pop().right
} else if(node.val === "oslc.orderBy") {
found["oslc.select"] = false
found["oslc.where"] = false
found["oslc.prefix"] = false
found["oslc.orderBy"] = true
node = stack.pop().right
} else if(node.val === "oslc.searchTerms") {
found["oslc.searchTerms"] = true
} else {
if (found["oslc.prefix"]){
sparql_query_prefix+="PREFIX "+node.val+": "
node = stack.pop().right
sparql_query_prefix+=node.val+" "
node = stack.pop().right
}
if (found["oslc.orderBy"]) {
if (node.val.charAt(0) === '-') {
sparql_query_orderBy += "DESC(?"+node.val.substring(1, node.val.length).replace(':','_') + ") "
} else if(node.val.charAt(0) === '+') {
sparql_query_orderBy += "ASC(?"+node.val.substring(1, node.val.length).replace(':','_') + ") "
} else {
sparql_orderBy += "?"+query.substring(index, i).replace(':','_')+" "
}
node = stack.pop().right
}
if (found["oslc.select"]) {
sparql_query_select += "?"+node.val.replace(':','_')+" "
sparql_query_where += var_stack[stack.length-1]+ " " + node.val + " ?" + node.val.replace(':', '_') + " . "
var new_var = node.val.replace(':','_')
node = stack.pop()
/*
if (node.val === '{') {
var_stack.push(new_var)
}
if (node.val === '}') {
if (var_stack[var_stack.length-1] !== "?s") {
var_stack.pop()
} else {
callback("Syntax Error")
}
}
*/
node = node.right
}
if (found["oslc.where"]) {
sparql_query_where += "?s " + node.val + " "
node = stack.pop()
sparql_query_where += node.right.val + " . "
// node = stack.pop().right
node = node.right
// DOES NOT consider filters
}
}
}
uri = sparql_query_prefix+sparql_query_select+sparql_query_where+sparql_query_orderBy + "} }"
var options = {
uri: db+"query?query="+uri,
method: "GET",
headers: {
"Accept": "application/sparql-results+json"
}
}
request(options, function(err, ires, body) {
console.log("REQUEST " + options.uri);
if (err) {
callback(err)
return
}
var triples = []
body = JSON.parse(body)
if (body["results"]["bindings"].length === 0) {
callback(err, ires);
} else {
for (var i = 0; i < JSON.parse(body)["results"]["bindings"].length; i++) {
triples.push({"subject": base, "predicate": rdf.resource, "object": body["results"]["bindings"][i]["uri"]})
}
jsonld.serialize(triples, function(err, result){
ires.body = result
callback(err, ires)
})
}
})
}
}